部署Redis集群
redis集群高可用部署 我们定义6个redis节点 利用三主三从实现redis高可用
# 自定义一个网络
[root@ecs-98457 ~]# docker network create redis --subnet 172.38.0.0/16
08c633236a59151a68272d44be289c01f23e0d74adff1ff018ed936b25818066
# 通过脚本创建6个redis配置
[root@ecs-98457 ~]# 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@ecs-98457 ~]# cd /mydata/
[root@ecs-98457 mydata]# ls
redis
[root@ecs-98457 mydata]# cd redis/
[root@ecs-98457 redis]# ls
node-1 node-2 node-3 node-4 node-5 node-6
# 通过脚本启动6个容器
[root@ecs-98457 redis]# for i in `seq 1 6`; \
do \
docker run -d -p 637${i}:6379 -p 1637${i}:16379 --name redis-${i} \
-v /mydata/redis/node-${i}/data:/data \
-v /mydata/redis/node-${i}/conf/redis.conf:/etc/redis/redis.conf \
--net redis --ip 172.38.0.1${i} redis:6.0 redis-server /etc/redis/redis.conf
done
Unable to find image 'redis:6.0' locally
6.0: Pulling from library/redis
c229119241af: Already exists
bed41eb8190a: Pull complete
5e59eaa723f1: Pull complete
c54685b5cebc: Pull complete
b527893de1ba: Pull complete
2145fb9278bf: Pull complete
Digest: sha256:cf9a2a1e97e3061316132b3ff38513ae7878ef2f340dd38aa57d8c963c45aa6f
Status: Downloaded newer image for redis:6.0
b0c396d00a1068c52addce75c2f3b78dcdf3346b948f79adc52bb06589c4df88
ff4a20b2b93bbb81fbe8643cdeef2b5de935ecd7f4d0af9b31fc77bfc467f1c3
f787b4cd607ceb4789fd4922b83a7463a127cd0137df386c49fd65d40426a95e
65523f02f5a31430116b28258c1554fb0e2315de2d57af9dd9bae2d72f965a55
4c89c6302313d95d9a6d1a6687002c284c8594f725986ce09f59db5618f7d62c
43d29ddda9123d1adf89f48ad70e6dacf654a77b8bd01836b1fba81638903ee8
[root@ecs-98457 redis]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43d29ddda912 redis:6.0 "docker-entrypoint.s…" 6 seconds ago Up 4 seconds 0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp redis-6
4c89c6302313 redis:6.0 "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp redis-5
65523f02f5a3 redis:6.0 "docker-entrypoint.s…" 7 seconds ago Up 5 seconds 0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp redis-4
f787b4cd607c redis:6.0 "docker-entrypoint.s…" 7 seconds ago Up 6 seconds 0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16373->16379/tcp, :::16373->16379/tcp redis-3
ff4a20b2b93b redis:6.0 "docker-entrypoint.s…" 8 seconds ago Up 6 seconds 0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp redis-2
b0c396d00a10 redis:6.0 "docker-entrypoint.s…" 8 seconds ago Up 7 seconds 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@ecs-98457]# docker exec -it redis-1 /bin/sh
# pwd
/data
# ls
appendonly.aof nodes.conf
配置集群
# 配置集群
# 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: 9df4ebeea505a05eafd8c731c969dda0c803062e 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
M: c6bac15b83224dd4b598e47624ea4e58f1f6d99c 172.38.0.12:6379
slots:[5461-10922] (5462 slots) master
M: 6697a9d87e07ac613564d2b0243b40d34ebfbcf3 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
S: ba1a0cca877c6cf84772262e01b8489b92ffeb66 172.38.0.14:6379
replicates 6697a9d87e07ac613564d2b0243b40d34ebfbcf3
S: 1fc0840b571388c02fc01c3f77a3fc7e070a6014 172.38.0.15:6379
replicates 9df4ebeea505a05eafd8c731c969dda0c803062e
S: b715c791fa6adea842e54ed2a037d11c2a2db38f 172.38.0.16:6379
replicates c6bac15b83224dd4b598e47624ea4e58f1f6d99c
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: 9df4ebeea505a05eafd8c731c969dda0c803062e 172.38.0.11:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: b715c791fa6adea842e54ed2a037d11c2a2db38f 172.38.0.16:6379
slots: (0 slots) slave
replicates c6bac15b83224dd4b598e47624ea4e58f1f6d99c
S: 1fc0840b571388c02fc01c3f77a3fc7e070a6014 172.38.0.15:6379
slots: (0 slots) slave
replicates 9df4ebeea505a05eafd8c731c969dda0c803062e
M: 6697a9d87e07ac613564d2b0243b40d34ebfbcf3 172.38.0.13:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: ba1a0cca877c6cf84772262e01b8489b92ffeb66 172.38.0.14:6379
slots: (0 slots) slave
replicates 6697a9d87e07ac613564d2b0243b40d34ebfbcf3
M: c6bac15b83224dd4b598e47624ea4e58f1f6d99c 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.
#
# 连接集群查看信息
# redis-cli -c
# 查看集群信息
127.0.0.1:6379> cluster nodes
c6bac15b83224dd4b598e47624ea4e58f1f6d99c 172.38.0.12:6379@16379 master - 0 1649679820110 2 connected 5461-10922
b715c791fa6adea842e54ed2a037d11c2a2db38f 172.38.0.16:6379@16379 slave c6bac15b83224dd4b598e47624ea4e58f1f6d99c 0 1649679818107 2 connected
ba1a0cca877c6cf84772262e01b8489b92ffeb66 172.38.0.14:6379@16379 slave 6697a9d87e07ac613564d2b0243b40d34ebfbcf3 0 1649679819108 3 connected
6697a9d87e07ac613564d2b0243b40d34ebfbcf3 172.38.0.13:6379@16379 master - 0 1649679819509 3 connected 10923-16383
1fc0840b571388c02fc01c3f77a3fc7e070a6014 172.38.0.15:6379@16379 myself,slave 9df4ebeea505a05eafd8c731c969dda0c803062e 0 1649679819000 1 connected
9df4ebeea505a05eafd8c731c969dda0c803062e 172.38.0.11:6379@16379 master - 0 1649679818507 1 connected 0-5460
测试
127.0.0.1:6379> set cxz 1
-> Redirected to slot [7190] located at 172.38.0.12:6379
OK
172.38.0.12:6379> get cxz
"1"
172.38.0.12:6379>
我们可以看到数据存储到了172.38.0.12:6379
测试停掉172.38.0.12:6379 这台服务 看我们是否可以获取到数据
172.38.0.12:6379>
# exit
[root@ecs-98457 redis]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43d29ddda912 redis:6.0 "docker-entrypoint.s…" 27 minutes ago Up 27 minutes 0.0.0.0:6376->6379/tcp, :::6376->6379/tcp, 0.0.0.0:16376->16379/tcp, :::16376->16379/tcp redis-6
4c89c6302313 redis:6.0 "docker-entrypoint.s…" 27 minutes ago Up 27 minutes 0.0.0.0:6375->6379/tcp, :::6375->6379/tcp, 0.0.0.0:16375->16379/tcp, :::16375->16379/tcp redis-5
65523f02f5a3 redis:6.0 "docker-entrypoint.s…" 27 minutes ago Up 27 minutes 0.0.0.0:6374->6379/tcp, :::6374->6379/tcp, 0.0.0.0:16374->16379/tcp, :::16374->16379/tcp redis-4
f787b4cd607c redis:6.0 "docker-entrypoint.s…" 27 minutes ago Up 27 minutes 0.0.0.0:6373->6379/tcp, :::6373->6379/tcp, 0.0.0.0:16373->16379/tcp, :::16373->16379/tcp redis-3
ff4a20b2b93b redis:6.0 "docker-entrypoint.s…" 27 minutes ago Up 27 minutes 0.0.0.0:6372->6379/tcp, :::6372->6379/tcp, 0.0.0.0:16372->16379/tcp, :::16372->16379/tcp redis-2
b0c396d00a10 redis:6.0 "docker-entrypoint.s…" 27 minutes ago Up 27 minutes 0.0.0.0:6371->6379/tcp, :::6371->6379/tcp, 0.0.0.0:16371->16379/tcp, :::16371->16379/tcp redis-1
482044879e15 tomcat "catalina.sh run" 48 minutes ago Up 48 minutes 0.0.0.0:49156->8080/tcp, :::49156->8080/tcp mynet-tomcat02
43cf1db15f2a tomcat "catalina.sh run" 48 minutes ago Up 48 minutes 0.0.0.0:49155->8080/tcp, :::49155->8080/tcp mynet-tomcat01
6ab8c06deb14 tomcat "catalina.sh run" 21 hours ago Up 21 hours 0.0.0.0:49154->8080/tcp, :::49154->8080/tcp tomcat02
1c2ff81d2e3f tomcat "catalina.sh run" 33 hours ago Up 33 hours 0.0.0.0:49153->8080/tcp, :::49153->8080/tcp tomcat01
[root@ecs-98457 redis]# docker stop ff4a20b2b93b
ff4a20b2b93b
[root@ecs-98457 redis]# docker exec -it redis-1 /bin/sh
# redis-cli -c
127.0.0.1:6379> get cxz
-> Redirected to slot [7190] located at 172.38.0.16:6379
"1"
172.38.0.16:6379>
我们可以看到在172.38.0.16:6379服务上获取到了数据 继而实现了redis的高可用