Docker-Redis集群实践

1、删除所有的容器

[root@jzyue ~]# docker rm -f $(docker ps -aq)

2、创建redis网络

[root@jzyue ~]# docker network create redis --subnet 172.16.0.0/16
[root@jzyue ~]# docker network inspect redis

3、创建redis配置和目录

[root@jzyue ~]# 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.16.0.1${port}
> cluster-announce-port 6379
> cluster-announce-bus-port 16379
> appendonly yes
> EOF
> done

4、启动redis

#启动第一个redis容器
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.16.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf   #指定IP地址和redis版本并启动redis

#启动第二个redis容器
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.16.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf 

#启动第三个redis容器
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.16.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf 

#启动第四个redis容器
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.16.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf

#启动第五个redis容器
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.16.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf 

#启动第六个redis容器
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.16.0.16 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf 

#查看创建的容器
[root@jzyue ~]# docker ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS              PORTS                                              NAMES
469f2ade3a98   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   7 minutes ago    Up 7 minutes        0.0.0.0:6374->6379/tcp, 0.0.0.0:16374->16379/tcp   redis-4
50b12b9db083   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   18 minutes ago   Up About a minute   0.0.0.0:6376->6379/tcp, 0.0.0.0:16376->16379/tcp   redis-6
0799fd3753f3   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   19 minutes ago   Up About a minute   0.0.0.0:6375->6379/tcp, 0.0.0.0:16375->16379/tcp   redis-5
8295e507116f   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   26 minutes ago   Up About a minute   0.0.0.0:6373->6379/tcp, 0.0.0.0:16373->16379/tcp   redis-3
ac5cc7c7975e   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   27 minutes ago   Up About a minute   0.0.0.0:6372->6379/tcp, 0.0.0.0:16372->16379/tcp   redis-2
951cc1971783   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   31 minutes ago   Up About a minute   0.0.0.0:6371->6379/tcp, 0.0.0.0:16371->16379/tcp   redis-1

可以参考启动代码,免去一步步操作。

5、创建redis集群

/data # redis-cli --cluster create 172.16.0.11:6379 172.16.0.12:6379 172.16.0.13:6379 172
.16.0.14:6379 172.16.0.15:6379 172.16.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.16.0.15:6379 to 172.16.0.11:6379
Adding replica 172.16.0.16:6379 to 172.16.0.12:6379
Adding replica 172.16.0.14:6379 to 172.16.0.13:6379
M: 1efc267cc9f4c6e658af30e66937bf63417d2b6a 172.16.0.11:6379
   slots:[0-5460] (5461 slots) master
M: 7c6fdaf9bbb401d6270be53f8e8392a267208071 172.16.0.12:6379
   slots:[5461-10922] (5462 slots) master
M: e0b6265cc80543f9fb968dc9afc11ea179fba089 172.16.0.13:6379
   slots:[10923-16383] (5461 slots) master
S: f45b456e9999b36639baba1ebcec9168c07436a6 172.16.0.14:6379
   replicates e0b6265cc80543f9fb968dc9afc11ea179fba089
S: 191e06581a8c9b8d52d6791788be77f0fb2bde26 172.16.0.15:6379
   replicates 1efc267cc9f4c6e658af30e66937bf63417d2b6a
S: 9498e06c7593aa3de7bc7258f8f824451344840f 172.16.0.16:6379
   replicates 7c6fdaf9bbb401d6270be53f8e8392a267208071
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.16.0.11:6379)
M: 1efc267cc9f4c6e658af30e66937bf63417d2b6a 172.16.0.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 191e06581a8c9b8d52d6791788be77f0fb2bde26 172.16.0.15:6379
   slots: (0 slots) slave
   replicates 1efc267cc9f4c6e658af30e66937bf63417d2b6a
M: 7c6fdaf9bbb401d6270be53f8e8392a267208071 172.16.0.12:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: e0b6265cc80543f9fb968dc9afc11ea179fba089 172.16.0.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: f45b456e9999b36639baba1ebcec9168c07436a6 172.16.0.14:6379
   slots: (0 slots) slave
   replicates e0b6265cc80543f9fb968dc9afc11ea179fba089
S: 9498e06c7593aa3de7bc7258f8f824451344840f 172.16.0.16:6379
   slots: (0 slots) slave
   replicates 7c6fdaf9bbb401d6270be53f8e8392a267208071
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

6、验证集群高可用

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:159
cluster_stats_messages_pong_sent:161
cluster_stats_messages_sent:320
cluster_stats_messages_ping_received:156
cluster_stats_messages_pong_received:159
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:320
127.0.0.1:6379> cluster nodes  #查看集群节点信息
191e06581a8c9b8d52d6791788be77f0fb2bde26 172.16.0.15:6379@16379 slave 1efc267cc9f4c6e658a                                                                                   f30e66937bf63417d2b6a 0 1608305265000 5 connected
7c6fdaf9bbb401d6270be53f8e8392a267208071 172.16.0.12:6379@16379 master - 0 1608305266768                                                                                    2 connected 5461-10922
1efc267cc9f4c6e658af30e66937bf63417d2b6a 172.16.0.11:6379@16379 myself,master - 0 1608305                                                                                   266000 1 connected 0-5460
e0b6265cc80543f9fb968dc9afc11ea179fba089 172.16.0.13:6379@16379 master - 0 1608305265759                                                                                    3 connected 10923-16383
f45b456e9999b36639baba1ebcec9168c07436a6 172.16.0.14:6379@16379 slave e0b6265cc80543f9fb9                                                                                   68dc9afc11ea179fba089 0 1608305265556 4 connected
9498e06c7593aa3de7bc7258f8f824451344840f 172.16.0.16:6379@16379 slave 7c6fdaf9bbb401d6270                                                                                   be53f8e8392a267208071 0 1608305265556 6 connected
127.0.0.1:6379> set a b  #设定一个值,默认保存在13这个容器中
-> Redirected to slot [15495] located at 172.16.0.13:6379
OK
#手动将13这个容器停止
[root@jzyue ~]# docker ps  #查看redis所有容器
CONTAINER ID   IMAGE                    COMMAND                  CREATED          STATUS          PORTS                                              NAMES
469f2ade3a98   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   17 minutes ago   Up 17 minutes   0.0.0.0:6374->6379/tcp, 0.0.0.0:16374->16379/tcp   redis-4
50b12b9db083   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   28 minutes ago   Up 11 minutes   0.0.0.0:6376->6379/tcp, 0.0.0.0:16376->16379/tcp   redis-6
0799fd3753f3   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   29 minutes ago   Up 11 minutes   0.0.0.0:6375->6379/tcp, 0.0.0.0:16375->16379/tcp   redis-5
8295e507116f   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   36 minutes ago   Up 11 minutes   0.0.0.0:6373->6379/tcp, 0.0.0.0:16373->16379/tcp   redis-3
ac5cc7c7975e   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   37 minutes ago   Up 11 minutes   0.0.0.0:6372->6379/tcp, 0.0.0.0:16372->16379/tcp   redis-2
951cc1971783   redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   41 minutes ago   Up 11 minutes   0.0.0.0:6371->6379/tcp, 0.0.0.0:16371->16379/tcp   redis-1
[root@jzyue ~]# docker stop 8295e507116f  #停止redis容器

#从新登陆redis,可以看出13已经停止,14接替主节点13的任务
/data # redis-cli -c
127.0.0.1:6379> get a  #再次get a
-> Redirected to slot [15495] located at 172.16.0.14:6379
"b"
172.16.0.14:6379> cluster nodes
e0b6265cc80543f9fb968dc9afc11ea179fba089 172.16.0.13:6379@16379 master,fail - 1608305470066 1608305469153 3 connected
f45b456e9999b36639baba1ebcec9168c07436a6 172.16.0.14:6379@16379 myself,master - 0 1608305569000 7 connected 10923-16383
7c6fdaf9bbb401d6270be53f8e8392a267208071 172.16.0.12:6379@16379 master - 0 1608305570174 2 connected 5461-10922
9498e06c7593aa3de7bc7258f8f824451344840f 172.16.0.16:6379@16379 slave 7c6fdaf9bbb401d6270be53f8e8392a267208071 0 1608305568000 6 connected
191e06581a8c9b8d52d6791788be77f0fb2bde26 172.16.0.15:6379@16379 slave 1efc267cc9f4c6e658af30e66937bf63417d2b6a 0 1608305569168 5 connected
1efc267cc9f4c6e658af30e66937bf63417d2b6a 172.16.0.11:6379@16379 master - 0 1608305569168 1 connected 0-5460


 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值