redis集群之新增节点
2018-01-17 本文已影响0人
Frankeen
操作环境
服务器centos7.3, ip:47.52.41.245,所包含的集群节点有7003,7004,7005
查看所有节点信息
- 没有密码情况下
redis-cli -c -p port cluster nodes
port:端口
- 有密码情况下
redis-cli -c -p port -a password cluster nodes
password:密码
port 是本服务器集群中任意节点的端口
[root@iZj6c7eeosj2t5vjw8rf4xZ /]# redis-cli -c -p 7003 cluster nodes
668397aba571ece85532b1eb1fccb42e4e33b1f2 116.196.65.198:7001 master - 0 1506343504317 2 connected 10923-16383
908430b2bf63669898e9eaef79dd6c1b33c8c57a 116.196.65.198:7002 slave cc86a24f3896ad7530e2687cf52582912f74b661 0 15063435093
33d50636b5235de9970de2da75901b642d2b14d2 116.196.65.198:7000 master - 0 1506343506319 1 connected 0-5460
cc86a24f3896ad7530e2687cf52582912f74b661 172.31.195.1:7003 myself,master - 0 0 4 connected 5461-10922
3a0b27e2e34e72430ce41bb78f6d207579175306 47.52.41.245:7004 slave 33d50636b5235de9970de2da75901b642d2b14d2 0 1506343507275
ca60de73e15c19a9aa45da508d270a5781a76724 47.52.41.245:7005 slave 668397aba571ece85532b1eb1fccb42e4e33b1f2 0 1506343508277
新增节点
- 在先前已经搭建好集群的服务器47.52.41.245上面的/tmp/frank/redis/redis-3.2.1/redis_cluster下面新建一个文件夹7006,然后把7003下redis.config拷贝里面
[root@iZj6c7eeosj2t5vjw8rf4xZ redis_cluster]# cd /tmp/frank/redis/redis-3.2.1/redis_cluster/
[root@iZj6c7eeosj2t5vjw8rf4xZ redis_cluster]# ls
7003 7004 7005
[root@iZj6c7eeosj2t5vjw8rf4xZ redis_cluster]# mkdir 7006
[root@iZj6c7eeosj2t5vjw8rf4xZ redis_cluster]# cp /tmp/frank/redis/redis-3.2.1/redis_cluster/7003/redis.conf 7006
[root@iZj6c7eeosj2t5vjw8rf4xZ redis_cluster]# cd 7006
[root@iZj6c7eeosj2t5vjw8rf4xZ 7006]# ls
redis.conf
- 修改redis.config,修改内容如下
port 7006 //端口
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_7006.pid//pidfile文件对应7006
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_7006.conf//集群的配置 配置文件首次启动自动生成 7006
cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置
appendonly yes //aof日志开启
- 开放服务器和安全组的7006和17006(集群对应的)端口,先启动防火墙,然后开发对应端口,接着重庆防火墙,如果有安全组,安全组同样要开启
[root@iZj6c7eeosj2t5vjw8rf4xZ 7006]# systemctl start firewalld.service
[root@iZj6c7eeosj2t5vjw8rf4xZ 7006]# firewall-cmd --zone=public --add-port=7006/tcp --permanent
success
[root@iZj6c7eeosj2t5vjw8rf4xZ 7006]# firewall-cmd --zone=public --add-port=17006/tcp --permanent
success
[root@iZj6c7eeosj2t5vjw8rf4xZ 7006]# firewall-cmd --reload
success
4.启动节点并查看启动状态
[root@iZj6c7eeosj2t5vjw8rf4xZ 7006]# redis-server redis.conf
[root@iZj6c7eeosj2t5vjw8rf4xZ 7006]# netstat -tnlp | grep redis
tcp 0 0 0.0.0.0:17003 0.0.0.0:* LISTEN 2228/redis-server *
tcp 0 0 0.0.0.0:17004 0.0.0.0:* LISTEN 2224/redis-server *
tcp 0 0 0.0.0.0:17005 0.0.0.0:* LISTEN 2232/redis-server *
tcp 0 0 0.0.0.0:17006 0.0.0.0:* LISTEN 3867/redis-server *
tcp 0 0 0.0.0.0:7003 0.0.0.0:* LISTEN 2228/redis-server *
tcp 0 0 0.0.0.0:7004 0.0.0.0:* LISTEN 2224/redis-server *
tcp 0 0 0.0.0.0:7005 0.0.0.0:* LISTEN 2232/redis-server *
tcp 0 0 0.0.0.0:7006 0.0.0.0:* LISTEN 3867/redis-server *
tcp6 0 0 :::17003 :::* LISTEN 2228/redis-server *
tcp6 0 0 :::17004 :::* LISTEN 2224/redis-server *
tcp6 0 0 :::17005 :::* LISTEN 2232/redis-server *
tcp6 0 0 :::17006 :::* LISTEN 3867/redis-server *
tcp6 0 0 :::7003 :::* LISTEN 2228/redis-server *
tcp6 0 0 :::7004 :::* LISTEN 2224/redis-server *
tcp6 0 0 :::7005 :::* LISTEN 2232/redis-server *
tcp6 0 0 :::7006 :::* LISTEN 3867/redis-server *
- 将新节点加入到集群
执行 redis-trib.rb add-node host:port host1:port1
host:port:为新节点的主机和端口
host1:port1:为集群里面的某个节点主机和端口
[root@iZj6c7eeosj2t5vjw8rf4xZ 7006]# redis-trib.rb add-node 47.52.41.245:7006 47.52.41.245:7003
>>> Adding node 47.52.41.245:7006 to cluster 47.52.41.245:7003
>>> Performing Cluster Check (using node 47.52.41.245:7003)
M: cc86a24f3896ad7530e2687cf52582912f74b661 47.52.41.245:7003
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 668397aba571ece85532b1eb1fccb42e4e33b1f2 116.196.65.198:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 908430b2bf63669898e9eaef79dd6c1b33c8c57a 116.196.65.198:7002
slots: (0 slots) slave
replicates cc86a24f3896ad7530e2687cf52582912f74b661
M: 33d50636b5235de9970de2da75901b642d2b14d2 116.196.65.198:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 3a0b27e2e34e72430ce41bb78f6d207579175306 47.52.41.245:7004
slots: (0 slots) slave
replicates 33d50636b5235de9970de2da75901b642d2b14d2
S: ca60de73e15c19a9aa45da508d270a5781a76724 47.52.41.245:7005
slots: (0 slots) slave
replicates 668397aba571ece85532b1eb1fccb42e4e33b1f2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
[WARNING] Node 116.196.65.198:7001 has slots in importing state (5598,8235).
[WARNING] Node 116.196.65.198:7000 has slots in importing state (5598,8235).
[WARNING] The following slots are open: 5598,8235
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 47.52.41.245:7006 to make it join the cluster.
[OK] New node added correctly.
- 查看新增节点
[root@iZj6c7eeosj2t5vjw8rf4xZ 7006]# redis-cli -c -p 7003 cluster nodes
668397aba571ece85532b1eb1fccb42e4e33b1f2 116.196.65.198:7001 master - 0 1506345812199 2 connected 10923-16383
908430b2bf63669898e9eaef79dd6c1b33c8c57a 116.196.65.198:7002 slave cc86a24f3896ad7530e2687cf52582912f74b661 0 1506345808190 4 connected
33d50636b5235de9970de2da75901b642d2b14d2 116.196.65.198:7000 master - 0 1506345811199 1 connected 0-5460
cc86a24f3896ad7530e2687cf52582912f74b661 172.31.195.1:7003 myself,master - 0 0 4 connected 5461-10922
f38c5163523a5040372f58b95cd184c601fd9388 47.52.41.245:7006 master - 0 1506345807145 0 connected
3a0b27e2e34e72430ce41bb78f6d207579175306 47.52.41.245:7004 slave 33d50636b5235de9970de2da75901b642d2b14d2 0 1506345809150 5 connected
ca60de73e15c19a9aa45da508d270a5781a76724 47.52.41.245:7005 slave 668397aba571ece85532b1eb1fccb42e4e33b1f2 0 1506345809651 6 connected
- 增加了新的节点之后,这个新的节点可以成为主节点或者是从节点
把这个节点变成主节点,使用redis-trib程序,将集群中的某些哈希槽移动到新节点里面, 这个新节点就成为真正的主节点了。
执行下面的命令对集群中的哈希槽进行移动
cd /usr/local/redis3.0/src
./redis-trib.rb reshard 47.52.41.245:7003
执行过程中系统会提示我们要移动多少哈希槽,这里移动1000个,然后我们就输入1000.接着会问你需要指定把这些哈希槽转移到哪个节点上,然后输入我们新增的节点id f38c5163523a5040372f58b95cd184c601fd9388,紧接着会问需要从哪几个节点转移,输入all就行了,由redis集群自己分配,然后再输入yes就OK了。然后查看一下节点状态
[root@iZj6c7eeosj2t5vjw8rf4xZ 7006]# redis-cli -c -p 7003 cluster nodes
668397aba571ece85532b1eb1fccb42e4e33b1f2 116.196.65.198:7001 master - 0 1506349557418 2 connected 10923-16383
908430b2bf63669898e9eaef79dd6c1b33c8c57a 116.196.65.198:7002 slave cc86a24f3896ad7530e2687cf52582912f74b661 0 1506349558418 4 connected
33d50636b5235de9970de2da75901b642d2b14d2 116.196.65.198:7000 master - 0 1506349555415 1 connected 0-5460
cc86a24f3896ad7530e2687cf52582912f74b661 172.31.195.1:7003 myself,master - 0 0 4 connected 5598-10922 [5598->-f38c5163523a5040372f58b95cd184c601fd9388]
f38c5163523a5040372f58b95cd184c601fd9388 47.52.41.245:7006 master - 0 1506349556372 7 connected 5461-5597
3a0b27e2e34e72430ce41bb78f6d207579175306 47.52.41.245:7004 slave 33d50636b5235de9970de2da75901b642d2b14d2 0 1506349553367 5 connected
ca60de73e15c19a9aa45da508d270a5781a76724 47.52.41.245:7005 slave 668397aba571ece85532b1eb1fccb42e4e33b1f2 0 1506349558876 6 connected
已经有了插槽
- 如果想要把新节点变成某个节点的从节点执行如下命令,前提新节点还没有分配哈希槽
redis-cli -c -p 7006 cluster replicate 33d50636b5235de9970de2da75901b642d2b14d2
33d50636b5235de9970de2da75901b642d2b14d2
为某个主节点的id。
通过如下命令查看是否成功
redis-cli -p 7000 cluster nodes | grep slave | grep 33d50636b5235de9970de2da75901b642d2b14d2
7000为主节点的端口,33d50636b5235de9970de2da75901b642d2b14d2为主节点的id
- 可能出现问题
ERR] Nodes don't agree about configuration!
>>> Check for open slots...
[WARNING] Node 116.196.65.198:7000 has slots in importing state (5608).
[WARNING] The following slots are open: 5608
可以通过如下命令解决
#先连接集群
redis-cli -h 116.196.65.198 -c -p 7000
#然后输入
cluster setslot 5608 stable
#或者通过
redis-trib.rb fix 116.196.65.198:7000