redis集群搭建

redis

redis下载安装
cd tmp
mkdir frank
cd frank
mkdir redis
cd redis
wget http://download.redis.io/releases/redis-4.0.1.tar.gz
tar xzf redis-4.0.1.tar.gz
cd redis-4.0.1
make && make install
redis查看,关闭,卸载
  1. 查看
ps aux|grep redis
  1. 关闭
redis-cli shutdown 
or redis-cli -h 127.0.0.1 -p 6379 shutdown 
or kill -9 10654
  1. 卸载
rm -rf /usr/local/bin/redis*
rm -rf /tmp/frank/redis/redis-4.0.1
启动redis服务
redis-server
启动redis客户端
redis-cli or redis-cli -h 127.0.0.1 -p 6379

ruby

安装需要的Package
# yum -y install gcc

# yum -y install gcc-c++

# yum -y install zlib zlib-devel

# yum -y install readline readline-devel

# yum -y install openssl openssl-devel

最好是 #yum install gcc gcc-c++ openssl* readline* ncurses* zlib* libxml* libjpeg* libpng* libxslt* libtool*
ruby下载安装
cd tmp
mkdir frank
cd frank
mkdir ruby
cd ruby
wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.2.tar.gz
tar xzf ruby-2.4.2.tar.gz
cd ruby-2.4.2
./configure
make && make install
安装redis接口,gem未ruby下的一个工具包
gem install redis

1.问题1

ERROR:  Loading command: install (LoadError)
    cannot load such file -- zlib
ERROR:  While executing gem ... (NoMethodError)
    undefined method `invoke_with_build_args' for nil:NilClass

解决 :

# 进入ruby源码文件夹 
# 安装ruby自身提供的zlib包 
cd ext/zlib
ruby ./extconf.rb
make
make install

搭建redis集群

  1. 环境

redis 采用 redis-3.2.1 版本。

服务器CentOS7.0 两台 (IP:116.196.65.198),(IP:47.52.41.245)

  1. 在两台服务器上,下载安装redis在tmp/frank/redis目录下
cd /tmp
mkdir frank
cd frank
mkdir redis
wget http://download.redis.io/releases/redis-3.2.1.tar.gz
tar -zxvf redis-3.2.1.tar.gz 
cd redis-3.2.1
make && make install
  1. 将 redis安装目录下面的src目录下面的redis-trib.rb 复制到 /usr/local/bin 目录下
cd src
cp redis-trib.rb /usr/local/bin/
  1. 创建 Redis 节点

分别在两台服务器上redis安装目录下创建 redis_cluster 目录;

mkdir redis_cluster

在116.196.65.198服务器上面的redis_cluster目录下面创建名为7000、7001、7002的目录,并将 redis.conf 拷贝到这三个目录中

cd redis_cluster 
mkdir 7000 7001 7002
cd ..
cp redis.conf redis_cluster/7000
cp redis.conf redis_cluster/7001
cp redis.conf redis_cluster/7002 

分别修改这三个配置文件,修改如下内容

port  7000                                        //端口7000,7002,7003    protected-mode no                               //关掉保护模式3.2之后 不然会报Sorry, can't connect to node
#bind 本机ip                                       //默认ip为127.0.0.1,在3.2之后要注释掉 daemonize    yes                               //redis后台运行
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7001,7002
cluster-enabled  yes                           //开启集群  把注释#去掉
cluster-config-file  nodes_7000.conf   //集群的配置  配置文件首次启动自动生成 7000,7001,7002
cluster-node-timeout  15000                //请求超时  默认15秒,可自行设置
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日志

接着在另外一台机器上(47.52.41.245),的操作重复以上三步,只是把目录改为7003、7004、7005,对应的配置文件也按照这个规则修改即可

  1. 启动各个节点
#第一台机器上执行即116.196.65.198
redis-server redis_cluster/7000/redis.conf
redis-server redis_cluster/7001/redis.conf
redis-server redis_cluster/7002/redis.conf
 
#另外一台机器上执行即47.52.41.245
redis-server redis_cluster/7003/redis.conf
redis-server redis_cluster/7004/redis.conf
redis-server redis_cluster/7005/redis.conf 
  1. 检查 redis 启动情况
ps -ef | grep redis

root      2138     1  0 13:57 ?        00:00:01 redis-server *:7003 [cluster]
root      2142     1  0 13:57 ?        00:00:01 redis-server *:7004 [cluster]
root      2146     1  0 13:57 ?        00:00:01 redis-server *:7005 [cluster]
root      2185  2119  0 14:20 pts/0    00:00:00 grep --color=auto redis


netstat -tnlp | grep redis

tcp        0      0 0.0.0.0:17003           0.0.0.0:*               LISTEN      2138/redis-server * 
tcp        0      0 0.0.0.0:17004           0.0.0.0:*               LISTEN      2142/redis-server * 
tcp        0      0 0.0.0.0:17005           0.0.0.0:*               LISTEN      2146/redis-server * 
tcp        0      0 0.0.0.0:7003            0.0.0.0:*               LISTEN      2138/redis-server * 
tcp        0      0 0.0.0.0:7004            0.0.0.0:*               LISTEN      2142/redis-server * 
tcp        0      0 0.0.0.0:7005            0.0.0.0:*               LISTEN      2146/redis-server * 
tcp6       0      0 :::17003                :::*                    LISTEN      2138/redis-server * 
tcp6       0      0 :::17004                :::*                    LISTEN      2142/redis-server * 
tcp6       0      0 :::17005                :::*                    LISTEN      2146/redis-server * 
tcp6       0      0 :::7003                 :::*                    LISTEN      2138/redis-server * 
tcp6       0      0 :::7004                 :::*                    LISTEN      2142/redis-server * 
tcp6       0      0 :::7005                 :::*                    LISTEN      2146/redis-server * 
  1. 创建集群

Redis 官方提供了 redis-trib.rb 这个工具,就在解压目录的 src 目录中,第三步中已将它复制到 /usr/local/bin 目录中,可以直接在命令行中使用了。使用下面这个命令即可完成安装

redis-trib.rb create --replicas 1 116.196.65.198:7000 116.196.65.198:7001 116.196.65.198:7002 47.52.41.245:7003 47.52.41.245:7004 47.52.41.245:7005

其中,前三个 ip:port 为第一台机器的节点,剩下三个为第二台机器。

这个工具是用 ruby 实现的,所以需要安装 ruby。安装命令如下:

yum -y install ruby ruby-devel rubygems rpm-build

gem install redis

或者按上面的流程安装ruby

之后再运行 redis-trib.rb 命令,会出现如下提示:

redis-trib.rb create --replicas 1 116.196.65.198:7000 116.196.65.198:7001 116.196.65.198:7002 47.52.41.245:7003 47.52.41.245:7004 47.52.41.245:7005

###出现下面这些代表成功

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
116.196.65.198:7000
47.52.41.245:7003
116.196.65.198:7001
Adding replica 47.52.41.245:7004 to 116.196.65.198:7000
Adding replica 116.196.65.198:7002 to 47.52.41.245:7003
Adding replica 47.52.41.245:7005 to 116.196.65.198:7001
M: 33d50636b5235de9970de2da75901b642d2b14d2 116.196.65.198:7000
   slots:0-5460 (5461 slots) master
M: 668397aba571ece85532b1eb1fccb42e4e33b1f2 116.196.65.198:7001
   slots:10923-16383 (5461 slots) master
S: 908430b2bf63669898e9eaef79dd6c1b33c8c57a 116.196.65.198:7002
   replicates cc86a24f3896ad7530e2687cf52582912f74b661
M: cc86a24f3896ad7530e2687cf52582912f74b661 47.52.41.245:7003
   slots:5461-10922 (5462 slots) master
S: 3a0b27e2e34e72430ce41bb78f6d207579175306 47.52.41.245:7004
   replicates 33d50636b5235de9970de2da75901b642d2b14d2
S: ca60de73e15c19a9aa45da508d270a5781a76724 47.52.41.245:7005
   replicates 668397aba571ece85532b1eb1fccb42e4e33b1f2
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join......
>>> Performing Cluster Check (using node 116.196.65.198:7000)
M: 33d50636b5235de9970de2da75901b642d2b14d2 116.196.65.198:7000
   slots:0-5460 (5461 slots) master
M: 668397aba571ece85532b1eb1fccb42e4e33b1f2 116.196.65.198:7001
   slots:10923-16383 (5461 slots) master
M: 908430b2bf63669898e9eaef79dd6c1b33c8c57a 116.196.65.198:7002
   slots: (0 slots) master
   replicates cc86a24f3896ad7530e2687cf52582912f74b661
M: cc86a24f3896ad7530e2687cf52582912f74b661 47.52.41.245:7003
   slots:5461-10922 (5462 slots) master
M: 3a0b27e2e34e72430ce41bb78f6d207579175306 47.52.41.245:7004
   slots: (0 slots) master
   replicates 33d50636b5235de9970de2da75901b642d2b14d2
M: ca60de73e15c19a9aa45da508d270a5781a76724 47.52.41.245:7005
   slots: (0 slots) master
   replicates 668397aba571ece85532b1eb1fccb42e4e33b1f2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

  1. 集群验证
    在第一台机器上连接集群的7002端口的节点,在另外一台连接7005节点,连接方式为 redis-cli -h 116.196.65.198 -c -p 7002 ,加参数 -C 可连接到集群,因为上面 redis.conf 将 bind 改为了ip地址,所以 -h 参数不可以省略。
####链接116.196.65.198:7002存入 键jiqun 值chenggong
[root@iZj6c7eeosj2t5vjw8rf4xZ redis-3.2.1]# redis-cli -h 116.196.65.198 -c -p 7002
116.196.65.198:7002> set jiqun chenggong
-> Redirected to slot [8235] located at 47.52.41.245:7003
OK
47.52.41.245:7003> 

####链接47.52.41.245:700取出 键jiqun 的值
[root@jdu4e00u53f7 redis-3.2.1]# redis-cli -h 47.52.41.245 -c -p 7005
47.52.41.245:7005> get jiqun
-> Redirected to slot [8235] located at 47.52.41.245:7003
"chenggong"
47.52.41.245:7003> 
  1. 可能出现问题
  • Sorry, can't connect to node

解决方案:

首先检查服务器端口是否开发,阿里,京东等服务器需要在安全组里面开发
端口,直接在服务器里面用命令开发端口是无效的。其次在redis.config配置文件里面关闭保护模式和注释掉bin

  • [ERR] Node 172.168.63.202:7001 is not empty. Either the nodealready knows other nodes (check with CLUSTER NODES) or contains some
    解决方案:

1)、将需要新增的节点下aof、rdb等本地备份文件删除;

2)、同时将新Node的集群配置文件删除,即:删除你redis.conf里面cluster-config-file所在的文件;

3)、再次添加新节点如果还是报错,则登录新Node,./redis-cli –h x –p对数据库进行清除:

116.196.65.198:7000> flushdb #清空当前数据库

4),重启服务器,或者重启redis

  • Waiting for the cluster to join 一直等待

解决方案:

1、服务器的防火墙是否允许集群总线端口通过

redis集群不仅需要开通redis客户端连接的端口,而且需要开通集群总线端口,集群总线端口为redis客户端连接的端口 + 10000

如上面用到redis端口为6379,7000,7001,7002,7003,7004,7005,则集群总线端口为16379,17000,17001,17002,17003,17004,17005,故,所有服务器的点需要开通redis的客户端连接端口和集群总线端口。如果服务器有安全组安全组也要开通,没有只要开通服务器本身的

##centos7.3服务器
systemctl status firewalld.service #防火墙状态
systemctl start firewalld.service   #防火墙开启
systemctl disable firewalld.service #防火墙不可用

firewall-cmd --zone=public --add-port=80/tcp --permanent #开启端口

命令含义:

--zone #作用域

--add-port=80/tcp  #添加端口,格式为:端口/通讯协议

--permanent  #永久生效,没有此参数重启后失效

重启防火墙

firewall-cmd --reload

你可能感兴趣的:(redis集群搭建)