使用docker容器创建cluster模式Redis集群

创建docker容器Redis自己的网路

[root@localhost home]# docker network create --subnet 192.3.0.0/16 --gateway 192.3.0.1 redis

生成Redis集群需要使用的配置文件

#创建redis集群配置文件存放目录
[root@localhost redis]# mkdir -p /home/redis
#进入这个目录
##我这偷懒了使用了一个shell脚本生成了6个集群的配置文件
#创建 redis-cluter.sh 文件输入编写生文件的脚本
for port in $(seq 1 6)
do
mkdir -p /home/redis/node-${port}/conf
touch /home/redis/node-${port}/conf/redis.conf
cat << EOF >> /home/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 192.3.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done

##然后保存 退出,然后设置脚本可以执行权限
[root@localhost redis]# chmod 777 redis-cluter.sh
#执行脚本生成文件
[root@localhost redis]# ./redis-cluter.sh 
[root@localhost redis]# ll
总用量 4
drwxr-xr-x. 4 root root  30 8月   5 16:29 node-1
drwxr-xr-x. 4 root root  30 8月   5 16:29 node-2
drwxr-xr-x. 4 root root  30 8月   5 16:29 node-3
drwxr-xr-x. 4 root root  30 8月   5 16:29 node-4
drwxr-xr-x. 4 root root  30 8月   5 16:29 node-5
drwxr-xr-x. 4 root root  30 8月   5 16:29 node-6
-rwxr-xr-x. 1 root root 387 8月   5 16:28 redis-cluter.sh

使用生成的配置文件挂载到启动的docker容器上

#想创建6个redis 3主3从的模式
#同样我使用了批量脚本生成的
========================脚本内容=====================
for port in $(seq 1 6)
do
docker run  -p 637${port}:6397 -p 1637${port}:16379 --name redis-${port} -v /home/redis/node-${port}/data:/data\
 -v /home/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf\
 -d --net redis --ip 192.3.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
done
===================脚本内容end=====================
#去执行 启动的时候使用-v参数去挂载我们的配置文件到容器
[root@localhost redis]# for port in $(seq 1 6)
> do
> docker run  -p 637${port}:6397 -p 1637${port}:16379 --name redis-${port} -v /home/redis/node-${port}/data:/data\
>  -v /home/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf\
>  -d --net redis --ip 192.3.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
> 
> done
#容器创建成功
[root@localhost redis]# docker ps
CONTAINER ID        IMAGE                    COMMAND                  CREATED             STATUS              PORTS                                                        NAMES
be1f6a282860        redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   11 minutes ago      Up 11 minutes       6379/tcp, 0.0.0.0:6376->6397/tcp, 0.0.0.0:16376->16379/tcp   redis-6
7eb4b979162f        redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   11 minutes ago      Up 11 minutes       6379/tcp, 0.0.0.0:6375->6397/tcp, 0.0.0.0:16375->16379/tcp   redis-5
15e7f867324c        redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   11 minutes ago      Up 11 minutes       6379/tcp, 0.0.0.0:6374->6397/tcp, 0.0.0.0:16374->16379/tcp   redis-4
51341ff453c3        redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   11 minutes ago      Up 11 minutes       6379/tcp, 0.0.0.0:6373->6397/tcp, 0.0.0.0:16373->16379/tcp   redis-3
d6988b362c41        redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   11 minutes ago      Up 11 minutes       6379/tcp, 0.0.0.0:6372->6397/tcp, 0.0.0.0:16372->16379/tcp   redis-2
56aa26eed4fe        redis:5.0.9-alpine3.11   "docker-entrypoint.s…"   11 minutes ago      Up 11 minutes       6379/tcp, 0.0.0.0:6371->6397/tcp, 0.0.0.0:16371->16379/tcp   redis-1

进入某台容器设置Redis集群

[root@localhost redis]# docker exec -it redis-1 /bin/sh
#这里已经进入到了容器内部
/data # 

# 设置集群配置
/data # redis-cli --cluster create 192.3.0.11:6379 192.3.0.12:6379 192.3.0.13:6379 192.3.0.14:6379 192.3.0.15:6379 192.3.0.16:6379 --cl
uster-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 192.3.0.15:6379 to 192.3.0.11:6379
Adding replica 192.3.0.16:6379 to 192.3.0.12:6379
Adding replica 192.3.0.14:6379 to 192.3.0.13:6379
M: 0c1b389144f5cbb4e49837c570ad5989e778e64f 192.3.0.11:6379
   slots:[0-5460] (5461 slots) master
M: 1295d9b8523b22f9bbbb80156aa5bdc0f7d2588c 192.3.0.12:6379
   slots:[5461-10922] (5462 slots) master
M: e7ad9743ac37334ca1626c2bfcb48743be7afa02 192.3.0.13:6379
   slots:[10923-16383] (5461 slots) master
S: c0ddab92ca1cffdfa949005ced53445f805ecc0e 192.3.0.14:6379
   replicates e7ad9743ac37334ca1626c2bfcb48743be7afa02
S: f9b6854c6c1daddca566d6eac4993f054444c62b 192.3.0.15:6379
   replicates 0c1b389144f5cbb4e49837c570ad5989e778e64f
S: 7c3e3617c8fcdfca6bbff85b628feabe1a62abba 192.3.0.16:6379
   replicates 1295d9b8523b22f9bbbb80156aa5bdc0f7d2588c
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.3.0.11:6379)
M: 0c1b389144f5cbb4e49837c570ad5989e778e64f 192.3.0.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: c0ddab92ca1cffdfa949005ced53445f805ecc0e 192.3.0.14:6379
   slots: (0 slots) slave
   replicates e7ad9743ac37334ca1626c2bfcb48743be7afa02
S: f9b6854c6c1daddca566d6eac4993f054444c62b 192.3.0.15:6379
   slots: (0 slots) slave
   replicates 0c1b389144f5cbb4e49837c570ad5989e778e64f
S: 7c3e3617c8fcdfca6bbff85b628feabe1a62abba 192.3.0.16:6379
   slots: (0 slots) slave
   replicates 1295d9b8523b22f9bbbb80156aa5bdc0f7d2588c
M: e7ad9743ac37334ca1626c2bfcb48743be7afa02 192.3.0.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 1295d9b8523b22f9bbbb80156aa5bdc0f7d2588c 192.3.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.
#这里显示已经成功

集群测试

#注意这里使用 -c 集群模式连接
/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:55
cluster_stats_messages_pong_sent:58
cluster_stats_messages_sent:113
cluster_stats_messages_ping_received:53
cluster_stats_messages_pong_received:55
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:113
#这里看到他将key设置到了 192.3.0.13这台机器上了
127.0.0.1:6379> set key zl
-> Redirected to slot [12539] located at 192.3.0.13:6379
OK
192.3.0.13:6379> get key
"zl"

#查看nodes
192.3.0.13:6379> CLUSTER nodes
0c1b389144f5cbb4e49837c570ad5989e778e64f 192.3.0.11:6379@16379 master - 0 1596617212761 1 connected 0-5460
1295d9b8523b22f9bbbb80156aa5bdc0f7d2588c 192.3.0.12:6379@16379 master - 0 1596617213566 2 connected 5461-10922
e7ad9743ac37334ca1626c2bfcb48743be7afa02 192.3.0.13:6379@16379 myself,master - 0 1596617213000 3 connected 10923-16383
7c3e3617c8fcdfca6bbff85b628feabe1a62abba 192.3.0.16:6379@16379 slave 1295d9b8523b22f9bbbb80156aa5bdc0f7d2588c 0 1596617213767 6 connected
c0ddab92ca1cffdfa949005ced53445f805ecc0e 192.3.0.14:6379@16379 slave e7ad9743ac37334ca1626c2bfcb48743be7afa02 0 1596617212562 4 connected
f9b6854c6c1daddca566d6eac4993f054444c62b 192.3.0.15:6379@16379 slave 0c1b389144f5cbb4e49837c570ad5989e778e64f 0 1596617213000 5 connected

##查看可以看到 13对应的从节点是14 
##测试我们将13停掉

[root@localhost redis]# docker stop redis-3
redis-3
#然后在其他节点去 查询节点信息 发现13已经停止 并且14已经成了主节点
127.0.0.1:6379> CLUSTER nodes
c0ddab92ca1cffdfa949005ced53445f805ecc0e 192.3.0.14:6379@16379 master - 0 1596617640886 8 connected 10923-16383
f9b6854c6c1daddca566d6eac4993f054444c62b 192.3.0.15:6379@16379 slave 0c1b389144f5cbb4e49837c570ad5989e778e64f 0 1596617640000 5 connected
7c3e3617c8fcdfca6bbff85b628feabe1a62abba 192.3.0.16:6379@16379 slave 1295d9b8523b22f9bbbb80156aa5bdc0f7d2588c 0 1596617640383 6 connected
0c1b389144f5cbb4e49837c570ad5989e778e64f 192.3.0.11:6379@16379 myself,master - 0 1596617639000 1 connected 0-5460
e7ad9743ac37334ca1626c2bfcb48743be7afa02 192.3.0.13:6379@16379 master,fail - 1596617611227 1596617610716 3 connected
1295d9b8523b22f9bbbb80156aa5bdc0f7d2588c 192.3.0.12:6379@16379 master - 0 1596617641389 2 connected 5461-10922
##获取数据 发现他是从14节点获取的数据
127.0.0.1:6379> get key
-> Redirected to slot [12539] located at 192.3.0.14:6379
"zl"

你可能感兴趣的:(docker,linux,docker,centos,服务器,redis)