环境
系统:Centos6.6
软件:redis 3.0.3
背景
因为2个机器搭建的redis集群一旦1台机器损坏,集群依然不可用,为了解决该问题,决定添加一个机器,组成3台机器的高可用环境。
集群节点:
把原来的5.17 5.18 的7002端口去掉,新增5.16的7000 7001
迁移前主机 端口 迁移后主机 端口
10.112.5.17 7000 10.112.5.16 7000
10.112.5.17 7001 10.112.5.16 7001
10.112.5.17 7002 10.112.5.17 7000
10.112.5.18 7000 10.112.5.17 7001
10.112.5.18 7001 10.112.5.18 7000
10.112.5.18 7002 10.112.5.18 7001
2个机器的原始状态:
[worker@LFTz-Zkp02 ~]$ redis-cli -p 7000 -c cluster nodes|awk '{print $1,$2,$3,$4,$NF}'| sort -k2
231b544622793b0b69e88a286ac8b46d211c8172 10.112.5.17:7000 myself,master - 5461-10922
936482ede8b9e8b94eeb7d947e51ef635e5173df 10.112.5.17:7001 slave c3312b79d40e6b90eb856c3e52d0fae21ba217e7 connected
2304aalsdkjfalsjdflkajsfj23rklwj10932je9 10.112.5.17:7002 slave 231b544622793b0b69e88a286ac8b46d211c8172 connected
c3312b79d40e6b90eb856c3e52d0fae21ba217e7 10.112.5.18:7000 master - 0-5460
fb13b382748edee5e329db61af6ccbe5a426dade 10.112.5.18:7001 slave e442abf8ec244a697a203c67092fc184b45220bb connected
e442abf8ec244a697a203c67092fc184b45220bb 10.112.5.18:7002 master - 10923-16383
因为需要摘除7002节点,所以找到7002节点
[worker@LFTz-Zkp02 ~]$ redis-cli -p 7000 -c cluster nodes|awk '{print $1,$2,$3,$4,$NF}'| sort -k2|grep :7002
2304aalsdkjfalsjdflkajsfj23rklwj10932je9 10.112.5.17:7002 slave 231b544622793b0b69e88a286ac8b46d211c8172 connected
e442abf8ec244a697a203c67092fc184b45220bb 10.112.5.18:7002 master - 10923-16383
从上面的节点信息可以看到
10.112.5.17:7002是slave节点,可以直接摘除,
10.112.5.18:7002是master节点,需要先变成从节点,再进行摘除:
现在先把10.112.5.18:7002变成从节点
1 先kill掉10.112.5.18:7002的进程,让salve这个节点下线,让他的slave节点变成主
ps -ef|grep 7002 |awk '{print $2}'|kill
2.查看节点状态
redis-cli -c -p 7000 cluster nodes
发现10.112.5.18:7002 已经下线,并且10.112.5.18:7001 已经变成了主节点
3.启动10.112.5.18:7002 ,让他也变成从节点
redis-server 7002/7002.conf
redis-cli -c -p 7000 cluster nodes
查看节点状态后,10.112.5.18:7002已经变成从节点
此时2个节点已经变成从节点,可以直接执行摘除操作。
为了安全考虑,先把2个新的节点添加到集群,再执行节点摘除操作。
[worker@LFTz-Zkp02 ~]$ redis-cli -p 7000 -c cluster nodes|awk '{print $1,$2,$3,$4,$NF}'| sort -k2
231b544622793b0b69e88a286ac8b46d211c8172 10.112.5.17:7000 myself,master - 5461-10922
936482ede8b9e8b94eeb7d947e51ef635e5173df 10.112.5.17:7001 slave c3312b79d40e6b90eb856c3e52d0fae21ba217e7 connected
2304aalsdkjfalsjdflkajsfj23rklwj10932je9 10.112.5.17:7002 slave 231b544622793b0b69e88a286ac8b46d211c8172 connected
c3312b79d40e6b90eb856c3e52d0fae21ba217e7 10.112.5.18:7000 master - 0-5460
fb13b382748edee5e329db61af6ccbe5a426dade 10.112.5.18:7001 master - 10923-16383
e442abf8ec244a697a203c67092fc184b45220bb 10.112.5.18:7002 slave fb13b382748edee5e329db61af6ccbe5a426dade connected
新的节点是为了替换2个7002端口的从节点,所以找到2个7002端口对应的2个主节点:
231b544622793b0b69e88a286ac8b46d211c8172 10.112.5.17:7000 myself,master - 5461-10922
fb13b382748edee5e329db61af6ccbe5a426dade 10.112.5.18:7001 master - 10923-16383
执行添加节点操作:
命令用法:redis-trib.rb add-node newip:newport IP:port
redis-trib.rb add-node --slave --master-id 231b544622793b0b69e88a286ac8b46d211c8172 10.112.5.16:7000 10.112.5.17:7000
redis-trib.rb add-node --slave --master-id fb13b382748edee5e329db61af6ccbe5a426dade 10.112.5.16:7001 10.112.5.17:7000
添加2个新的从节点后,查看状态,一共8个节点
[worker@LFTz-Zkp02 ~]$ redis-cli -p 7000 -c cluster nodes|awk '{print $1,$2,$3,$4,$NF}'| sort -k2
e442abf8ec244a697a203c67092fc184b45220bb 10.112.5.16:7001 slave fb13b382748edee5e329db61af6ccbe5a426dade connected
341a85b5ca42f8bb7baf707579025fa352c822b6 10.112.5.16:7000 slave 231b544622793b0b69e88a286ac8b46d211c8172 connected
231b544622793b0b69e88a286ac8b46d211c8172 10.112.5.17:7000 myself,master - 5461-10922
936482ede8b9e8b94eeb7d947e51ef635e5173df 10.112.5.17:7001 slave c3312b79d40e6b90eb856c3e52d0fae21ba217e7 connected
2304aalsdkjfalsjdflkajsfj23rklwj10932je9 10.112.5.17:7002 slave 231b544622793b0b69e88a286ac8b46d211c8172 connected
c3312b79d40e6b90eb856c3e52d0fae21ba217e7 10.112.5.18:7000 master - 0-5460
fb13b382748edee5e329db61af6ccbe5a426dade 10.112.5.18:7001 master - 10923-16383
e442abf8ec244a697a203c67092fc184b45220bb 10.112.5.18:7002 slave fb13b382748edee5e329db61af6ccbe5a426dade connected
删除2个7002端口的几点:
redis-trib.rb del-node 10.112.5.17:7001 e442abf8ec244a697a203c67092fc184b45220bb
redis-trib.rb del-node 10.112.5.17:7001 fb13b382748edee5e329db61af6ccbe5a426dade
迁移后的节点状态:
[worker@LFTz-Zkp02 ~]$ redis-cli -p 7000 -c cluster nodes|awk '{print $1,$2,$3,$4,$NF}'
e442abf8ec244a697a203c67092fc184b45220bb 10.112.5.16:7001 slave fb13b382748edee5e329db61af6ccbe5a426dade connected
936482ede8b9e8b94eeb7d947e51ef635e5173df 10.112.5.17:7001 slave c3312b79d40e6b90eb856c3e52d0fae21ba217e7 connected
231b544622793b0b69e88a286ac8b46d211c8172 10.112.5.17:7000 myself,master - 5461-10922
341a85b5ca42f8bb7baf707579025fa352c822b6 10.112.5.16:7000 slave 231b544622793b0b69e88a286ac8b46d211c8172 connected
c3312b79d40e6b90eb856c3e52d0fae21ba217e7 10.112.5.18:7000 master - 0-5460
fb13b382748edee5e329db61af6ccbe5a426dade 10.112.5.18:7001 master - 10923-16383