Redis cluster集群实战

集群准备

  • Redis-5.0.8
  • 3台服务器: 192.168.100.200,192.168.100.201,192.168.100.202

集群开始

① 下载redis并解压,移动至/usr/local/redis目录

$ wget http://download.redis.io/releases/redis-5.0.8.tar.gz
$ tar -zxvf redis-5.0.8.tar.gz
$ mv redis-5.0.8 /usr/local/redis

② 安装redis

$ cd /usr/local/redis
$ make

③ 创建目录cluster,将配置文件复制到cluster目录,并修改以下参数

$ cp redis.conf cluster/redis-8000.conf
$ vi redis-8000.conf

bind 192.168.100.200
protected-mode no
port 8000
daemonize yes
pidfile /var/run/redis_8000.pid
logfile "/home/redis/logs/redis-8000.log"
dbfilename dump-8000.rdb
masterauth 123456
requirepass 123456
appendonly yes
appendfilename "appendonly-8000.aof"
cluster-enabled yes
cluster-config-file nodes-8000.conf
cluster-node-timeout 15000
cluster-replica-validity-factor 0
cluster-migration-barrier 1
cluster-require-full-coverage no

④ 复制一份配置,并修改端口

$ cp redis-8000.conf redis-8001.conf

#将redis-8001.conf文件中的所有"8000"的字符替换成"8001"
$ sed -i "s/8000/8001/g" redis-8001.conf

⑤ 另外两台机器同样的操作,192.168.100.201配置8002、8003,192.168.100.202配置8004、8005(附上远程复制的命令,其它操作按上面那样修改即可,注意:bind 的值分别修改成当前服务器的ip地址)

$ scp -r redis-8000.conf root@192.168.100.201:/usr/local/redis/cluster/
# 回车输入密码即可将redis-8000.conf复制到192.168.100.201这台主机的/usr/local/redis/cluster/这个目录
# 使用sed命令快速替换端口

⑥ 分别使用这6个配置文件启动redis, 这里只演示一台机器上的redis启动,其它两台按这样操作即可

$ cd /usr/local/redis
$ ./src/redis-server cluster/redis-8000.conf
$ ./src/redis-server cluster/redis-8001.conf
$ ps -ef|grep redis
root      85872      1  0 7月08 ?       00:05:28 ./src/redis-server 192.168.100.70:8002 [cluster]
root      85883      1  0 7月08 ?       00:05:27 ./src/redis-server 192.168.100.70:8003 [cluster]

⑦ 开始配置集群

$ ./usr/local/redis/src/redis-cli --cluster create 192.168.100.200:8000 192.168.100.200:8001 192.168.100.201:8002 192.168.100.201:8003 192.168.100.202:8004 192.168.100.202:8005  --cluster-replicas 1 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.100.201:8003 to 192.168.100.200:8000
Adding replica 192.168.100.202:8005 to 192.168.100.201:8002
Adding replica 192.168.100.200:8001 to 192.168.100.202:8004
M: d75eec16033d159910fec9480be295ec112fc6a0 192.168.100.200:8000
   slots:[0-5460] (5461 slots) master
S: 1d5db0cf2a68d4beeb73167a95d94e053a3e2387 192.168.100.200:8001
   replicates c419454dce169beb4babfe666f59ff2dcfc140fe
M: 38697b58d34bacab4cab716335fae41d16b43bae 192.168.100.201:8002
   slots:[5461-10922] (5462 slots) master
S: d8f7545836b0f6c9008441153a163c0bebf45346 192.168.100.201:8003
   replicates d75eec16033d159910fec9480be295ec112fc6a0
M: c419454dce169beb4babfe666f59ff2dcfc140fe 192.168.100.202:8004
   slots:[10923-16383] (5461 slots) master
S: 3c11fd85d94364f65ced59b7ecb4f735bf29d79f 192.168.100.202:8005
   replicates 38697b58d34bacab4cab716335fae41d16b43bae
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 192.168.100.200:8000)
M: d75eec16033d159910fec9480be295ec112fc6a0 192.168.100.200:8000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 3c11fd85d94364f65ced59b7ecb4f735bf29d79f 192.168.100.202:8005
   slots: (0 slots) slave
   replicates 38697b58d34bacab4cab716335fae41d16b43bae
M: 38697b58d34bacab4cab716335fae41d16b43bae 192.168.100.201:8002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 1d5db0cf2a68d4beeb73167a95d94e053a3e2387 192.168.100.200:8001
   slots: (0 slots) slave
   replicates c419454dce169beb4babfe666f59ff2dcfc140fe
M: c419454dce169beb4babfe666f59ff2dcfc140fe 192.168.100.202:8004
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: d8f7545836b0f6c9008441153a163c0bebf45346 192.168.100.201:8003
   slots: (0 slots) slave
   replicates d75eec16033d159910fec9480be295ec112fc6a0
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

⑧ 新增Master节点,在192.168.100.202这台机上再复制一份redis配置,端口为8006

$ cp /usr/local/redis/cluster/redis-8005.conf /usr/local/redis/cluster/redis-8006.conf
$ sed -i "s/8005/8006/g" redis-8006.conf
$ cd /usr/local/redis
$ ./src/redis-server cluster/redis-8006.conf

# 将新节点加入集群--cluster add-node <新节点ip:port> <集群中已存在任一节点ip:port>
$ ./src/redis-cli --cluster add-node 192.168.100.202:8006 192.168.100.202:8005
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.100.202:8006 to cluster 192.168.100.202:8005
>>> Performing Cluster Check (using node 192.168.100.202:8005)
S: 3c11fd85d94364f65ced59b7ecb4f735bf29d79f 192.168.100.202:8005
   slots: (0 slots) slave
   replicates 38697b58d34bacab4cab716335fae41d16b43bae
M: d75eec16033d159910fec9480be295ec112fc6a0 192.168.100.200:8000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 1d5db0cf2a68d4beeb73167a95d94e053a3e2387 192.168.100.200:8001
   slots: (0 slots) slave
   replicates c419454dce169beb4babfe666f59ff2dcfc140fe
S: d8f7545836b0f6c9008441153a163c0bebf45346 192.168.100.201:8003
   slots: (0 slots) slave
   replicates d75eec16033d159910fec9480be295ec112fc6a0
M: c419454dce169beb4babfe666f59ff2dcfc140fe 192.168.100.202:8004
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 38697b58d34bacab4cab716335fae41d16b43bae 192.168.100.201:8002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 192.168.100.202:8006 to make it join the cluster.
[OK] New node added correctly.

⑨ 随后我们登录集群,查看集群的节点信息

$ ./src/redis-cli -c -h 192.168.100.202 -p 8004 -a 123456
192.168.100.200:8000>cluster nodes
3c11fd85d94364f65ced59b7ecb4f735bf29d79f 192.168.100.202:8005@18005 slave 38697b58d34bacab4cab716335fae41d16b43bae 0 1594439680933 6 connected
d75eec16033d159910fec9480be295ec112fc6a0 192.168.100.200:8000@18000 myself,master - 0 1594439679000 1 connected 0-5460
5b8bd456bf3163c52f24b27ece810cc73e1c1a2e 192.168.100.202:8006@18006 master - 0 1594439679931 0 connected
38697b58d34bacab4cab716335fae41d16b43bae 192.168.100.201:8002@18002 master - 0 1594439676000 3 connected 5461-10922
1d5db0cf2a68d4beeb73167a95d94e053a3e2387 192.168.100.200:8001@18001 slave c419454dce169beb4babfe666f59ff2dcfc140fe 0 1594439678000 5 connected
c419454dce169beb4babfe666f59ff2dcfc140fe 192.168.100.202:8004@18004 master - 0 1594439678924 5 connected 10923-16383
d8f7545836b0f6c9008441153a163c0bebf45346 192.168.100.201:8003@18003 slave d75eec16033d159910fec9480be295ec112fc6a0 0 1594439677917 4 connected
# 我们可以看到192.168.100.202:8006这个节点已经被顺利加入了集群中,但是还没有给它分配slot,这样我们添加的master节点实际上是不参与集群的工作的

⑩ 分配slot

$ ./src/redis-cli --cluster reshard 192.168.100.202:8004 --cluster-from d75eec16033d159910fec9480be295ec112fc6a0,38697b58d34bacab4cab716335fae41d16b43bae,c419454dce169beb4babfe666f59ff2dcfc140fe --cluster-to 5b8bd456bf3163c52f24b27ece810cc73e1c1a2e --cluster-slots 1024 -a 123456
...
    Moving slot 11261 from c419454dce169beb4babfe666f59ff2dcfc140fe
    Moving slot 11262 from c419454dce169beb4babfe666f59ff2dcfc140fe
    Moving slot 11263 from c419454dce169beb4babfe666f59ff2dcfc140fe
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 5461 from 192.168.100.201:8002 to 192.168.100.202:8006: 
Moving slot 5462 from 192.168.100.201:8002 to 192.168.100.202:8006: 
Moving slot 5463 from 192.168.100.201:8002 to 192.168.100.202:8006: 
Moving slot 5464 from 192.168.100.201:8002 to 192.168.100.202:8006: 
Moving slot 5465 from 192.168.100.201:8002 to 192.168.100.202:8006: 
Moving slot 5466 from 192.168.100.201:8002 to 192.168.100.202:8006: 
Moving slot 5467 from 192.168.100.201:8002 to 192.168.100.202:8006: 
...

--cluster-from:表示slot目前所在的节点的node ID,多个ID用逗号分隔
--cluster-to:表示需要新分配节点的node ID(貌似每次只能分配一个)
--cluster-slots:分配的slot数量

$ ./src/redis-cli -c -h 192.168.100.202 -p 8004 -a 123456
192.168.100.202:8004> cluster nodes
3c11fd85d94364f65ced59b7ecb4f735bf29d79f 192.168.100.202:8005@18005 slave 38697b58d34bacab4cab716335fae41d16b43bae 0 1594455325990 6 connected
38697b58d34bacab4cab716335fae41d16b43bae 192.168.100.201:8002@18002 master - 0 1594455324984 3 connected 5803-10922
d75eec16033d159910fec9480be295ec112fc6a0 192.168.100.200:8000@18000 master - 0 1594455326000 1 connected 341-5460
d8f7545836b0f6c9008441153a163c0bebf45346 192.168.100.201:8003@18003 slave d75eec16033d159910fec9480be295ec112fc6a0 0 1594455325000 4 connected
5b8bd456bf3163c52f24b27ece810cc73e1c1a2e 192.168.100.202:8006@18006 master - 0 1594455326994 7 connected 0-340 5461-5802 10923-11263
c419454dce169beb4babfe666f59ff2dcfc140fe 192.168.100.202:8004@18004 myself,master - 0 1594455322000 5 connected 11264-16383
1d5db0cf2a68d4beeb73167a95d94e053a3e2387 192.168.100.200:8001@18001 slave c419454dce169beb4babfe666f59ff2dcfc140fe 0 1594455326000 5 connected
# 我们重新查看节点情况,可以看到我们新加入的节点已经被分配到了slot: 0-340 5461-5802 10923-11263

⑪ 删除节点,首先查看master是否有slave节点,如果有,要先删除该节点

# 删除节点 del-node后面跟着slave节点的ip:port和node ID
redis-cli --cluster del-node IP:PORT nodeID

⑫ 将slot原路退回

$ ./src/redis-cli --cluster reshard 192.168.100.202:8006 --cluster-from 5b8bd456bf3163c52f24b27ece810cc73e1c1a2e --cluster-to d75eec16033d159910fec9480be295ec112fc6a0 --cluster-slots 341 --cluster-yes -a 123456
$ ./src/redis-cli --cluster reshard 192.168.100.202:8006 --cluster-from 5b8bd456bf3163c52f24b27ece810cc73e1c1a2e --cluster-to 38697b58d34bacab4cab716335fae41d16b43bae --cluster-slots 342 --cluster-yes -a 123456
$ ./src/redis-cli --cluster reshard 192.168.100.202:8006 --cluster-from 5b8bd456bf3163c52f24b27ece810cc73e1c1a2e --cluster-to c419454dce169beb4babfe666f59ff2dcfc140fe --cluster-slots 341 --cluster-yes -a 123456

⑬ 将节点从集群中删除

$ ./src/redis-cli --cluster del-node 192.168.100.202:8006 5b8bd456bf3163c52f24b27ece810cc73e1c1a2e
>>> Removing node 5b8bd456bf3163c52f24b27ece810cc73e1c1a2e from cluster 192.168.100.202:8006
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

Redis开机自启配置

  • 创建文件
$ touch /etc/init.d/redis
$ vim redis
#复制下面的脚本放入redis文件中,按实际情况修改路径和端口号
#!/bin/sh
# chkconfig: 2345 80 90
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.
<200b>
REDISPORT1=8002
REDISPORT2=8003
EXEC=/usr/local/redis/src/redis-server
CLIEXEC=/usr/local/redis/src/redis-cli
<200b>
PIDFILE=/var/run/redis_${REDISPORT1}.pid
<200b>
CONF1="/usr/local/redis/cluster/redis-${REDISPORT1}.conf"
CONF2="/usr/local/redis/cluster/redis-${REDISPORT2}.conf"
<200b>
case "$1" in
    start)
        if [ -f $PIDFILE ]
        then
                echo "$PIDFILE exists, process is already running or crashed"
        else
                echo "Starting Redis cluster server..."
                $EXEC $CONF1 &
                $EXEC $CONF2 &
                echo "启动成功..."
        fi
        ;;
    stop)
        if [ ! -f $PIDFILE ]
        then
                echo "$PIDFILE does not exist, process is not running"
        else
                PID=$(cat $PIDFILE)
                echo "Stopping ..."
                $CLIEXEC -p $REDISPORT1 shutdown
                $CLIEXEC -p $REDISPORT2 shutdown
                while [ -x /proc/${PID} ]
                do
                    echo "Waiting for Redis cluster to shutdown ..."
                    sleep 1
                done
                echo "Redis cluster stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument"
        ;;
esac

  • 添加服务、启动服务
$ chkconfig --add /etc/init.d/redis
$ service redis start
#关闭服务 service redis stop
  • 开机自启
$ chkconfig redis on
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值