编写脚本
批量创建目录文件,编写配置文件
[root@localhost ~]# cat redis.sh
#/bin/bash
for port in $(seq 1 6); do
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >>/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
赋予权限并执行
[root@localhost ~]# chmod 777 redis.sh
[root@localhost ~]# ./redis.sh
#验证
[root@localhost ~]# cd /mydata/redis/
[root@localhost redis]# ll
total 0
drwxr-xr-x. 3 root root 18 Mar 8 19:43 node-1
drwxr-xr-x. 3 root root 18 Mar 8 19:43 node-2
drwxr-xr-x. 3 root root 18 Mar 8 19:43 node-3
drwxr-xr-x. 3 root root 18 Mar 8 19:43 node-4
drwxr-xr-x. 3 root root 18 Mar 8 19:43 node-5
drwxr-xr-x. 3 root root 18 Mar 8 19:43 node-6
[root@localhost redis]# cd node-1/conf/
[root@localhost conf]# cat redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.11
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
创建6个redis节点
[root@localhost conf]# docker run -p 6371:6379 -p 16371:16379 --name redis-1 -v /mydata/redis/node-1/data:/data -v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
Unable to find image 'redis:5.0.9-alpine3.11' locally
5.0.9-alpine3.11: Pulling from library/redis
cbdbe7a5bc2a: Pull complete
dc0373118a0d: Pull complete
cfd369fe6256: Pull complete
3e45770272d9: Pull complete
558de8ea3153: Pull complete
a2c652551612: Pull complete
Digest: sha256:83a3af36d5e57f2901b4783c313720e5fa3ecf0424ba86ad9775e06a9a5e35d0
Status: Downloaded newer image for redis:5.0.9-alpine3.11
4aa9af4a6fc623ac78c593d89e1e28877753c46f8c4ea4822c57760a457634f5
[root@localhost conf]# docker run -p 6372:6379 -p 16372:16379 --name redis-2 -v /mydata/redis/node-2/data:/data -v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
8929cc4f724aa131433e151b8bfdb1eedf92fac369eb33386f666be0774248c8
[root@localhost conf]# docker run -p 6373:6379 -p 16373:16379 --name redis-3 -v /mydata/redis/node-3/data:/data -v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
ab12911b3219a41cb4bc312ec0670341a70fcafd122af7e6d929b6eaa3a16668
[root@localhost conf]# docker run -p 6374:6379 -p 16374:16379 --name redis-4 -v /mydata/redis/node-4/data:/data -v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
d1b7a902129666731868004ee2c37d0c53e38eadd800da3d6aa15e399d90d517
[root@localhost conf]# docker run -p 6375:6379 -p 16375:16379 --name redis-5 -v /mydata/redis/node-5/data:/data -v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
21e026df6569c13e2c0b662918d4e185363f666d74ddf1bd0bcce7ef1afd3f01
[root@localhost conf]# docker run -p 6376:6379 -p 16376:16379 --name redis-6 -v /mydata/redis/node-6/data:/data -v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf -d --net redis --ip 172.38.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
1ed447b3c1493a54e2e98f561d530b6692436e0f2ba94b5233fcdd13f280ef46
[root@localhost conf]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ed447b3c149 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 2 seconds ago Up 2 seconds 0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp redis-6
21e026df6569 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 21 seconds ago Up 21 seconds 0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp redis-5
d1b7a9021296 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 44 seconds ago Up 43 seconds 0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp redis-4
ab12911b3219 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16373->16379/tcp, :::16373->16379/tcp redis-3
8929cc4f724a redis:5.0.9-alpine3.11 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp redis-2
4aa9af4a6fc6 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp redis-1
进入redis-1节点,创建集群
[root@localhost conf]# docker exec -it redis-1 /bin/sh
/data # ls
appendonly.aof nodes.conf
#创建集群
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.
38.0.16:6379 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.38.0.15:6379 to 172.38.0.11:6379
Adding replica 172.38.0.16:6379 to 172.38.0.12:6379
Adding replica 172.38.0.14:6379 to 172.38.0.13:6379
M: df7140c32a3ff89276d01318aebee214540e6207 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: 3c2077ccf83262ec029ad6edbb10eb8411a0fba1 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: 22966010cbe3258f73b6eef88ed56ae43b607a96 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: 3ddbe00fbb5d18f461a1aba8f5ffe922242d04aa 172.38.0.14:6379
replicates 22966010cbe3258f73b6eef88ed56ae43b607a96
S: 71fbe3da42723fe63dfdabe98f8864f74b24ce49 172.38.0.15:6379
replicates df7140c32a3ff89276d01318aebee214540e6207
S: 032ab2483f4a1cd8b1da07925ebcdc0083f4f1c0 172.38.0.16:6379
replicates 3c2077ccf83262ec029ad6edbb10eb8411a0fba1
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 172.38.0.11:6379)
M: df7140c32a3ff89276d01318aebee214540e6207 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 22966010cbe3258f73b6eef88ed56ae43b607a96 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 032ab2483f4a1cd8b1da07925ebcdc0083f4f1c0 172.38.0.16:6379
slots: (0 slots) slave
replicates 3c2077ccf83262ec029ad6edbb10eb8411a0fba1
S: 3ddbe00fbb5d18f461a1aba8f5ffe922242d04aa 172.38.0.14:6379
slots: (0 slots) slave
replicates 22966010cbe3258f73b6eef88ed56ae43b607a96
S: 71fbe3da42723fe63dfdabe98f8864f74b24ce49 172.38.0.15:6379
slots: (0 slots) slave
replicates df7140c32a3ff89276d01318aebee214540e6207
M: 3c2077ccf83262ec029ad6edbb10eb8411a0fba1 172.38.0.12:6379
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.
连接集群
/data # redis-cli -c
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:170
cluster_stats_messages_pong_sent:177
cluster_stats_messages_sent:347
cluster_stats_messages_ping_received:172
cluster_stats_messages_pong_received:170
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:347
127.0.0.1:6379>
查看集群信息
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:170
cluster_stats_messages_pong_sent:177
cluster_stats_messages_sent:347
cluster_stats_messages_ping_received:172
cluster_stats_messages_pong_received:170
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:347
查看节点信息,三主三从
127.0.0.1:6379> cluster nodes
22966010cbe3258f73b6eef88ed56ae43b607a96 172.38.0.13:6379@16379 master - 0 1709960670173 3 connected 10923-16383
032ab2483f4a1cd8b1da07925ebcdc0083f4f1c0 172.38.0.16:6379@16379 slave 3c2077ccf83262ec029ad6edbb10eb8411a0fba1 0 1709960671000 6 connected
3ddbe00fbb5d18f461a1aba8f5ffe922242d04aa 172.38.0.14:6379@16379 slave 22966010cbe3258f73b6eef88ed56ae43b607a96 0 1709960669171 4 connected
71fbe3da42723fe63dfdabe98f8864f74b24ce49 172.38.0.15:6379@16379 slave df7140c32a3ff89276d01318aebee214540e6207 0 1709960671178 5 connected
df7140c32a3ff89276d01318aebee214540e6207 172.38.0.11:6379@16379 myself,master - 0 1709960668000 1 connected 0-5460
3c2077ccf83262ec029ad6edbb10eb8411a0fba1 172.38.0.12:6379@16379 master - 0 1709960670000 2 connected 5461-10922
高可用测试
set a b两个值
127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 172.38.0.13:6379
OK
#发现当前处理的是172.38.0.13:6379,是第三个节点master
停止redis-3容器运行
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ed447b3c149 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 12 minutes ago Up 12 minutes 0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp redis-6
21e026df6569 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 12 minutes ago Up 12 minutes 0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp redis-5
d1b7a9021296 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp redis-4
ab12911b3219 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16373->16379/tcp, :::16373->16379/tcp redis-3
8929cc4f724a redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp redis-2
4aa9af4a6fc6 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp redis-1
[root@localhost ~]# docker stop redis-3
redis-3
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1ed447b3c149 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp redis-6
21e026df6569 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 13 minutes ago Up 13 minutes 0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp redis-5
d1b7a9021296 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp redis-4
8929cc4f724a redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 14 minutes ago Up 14 minutes 0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp redis-2
4aa9af4a6fc6 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 15 minutes ago Up 15 minutes 0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp redis-1
get a获取信息
发现是get到a了,虽然redis3已经停止运行了,但redis4上获取到了
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.38.0.14:6379
"b"
172.38.0.14:6379> cluster nodes
高可用实现
redis-3节点停掉之后,redis-4节点变成master节点