centos7 docker安装 redis Cluster 集群 (三主三从)

一  准备

 创建文件 redis-cluster.tmpl,内容  ,192.168.253.130 是我centos7的 ip

port ${PORT}
protected-mode no
requirepass 123456
masterauth 123456
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.253.130
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes

二 执行命令

执行下面的命令  创建命令

for port in `seq 8001 8006`; do \
base=7999 \
&& ip=$[port-base] \
&& mkdir -p ./redis-${port}/conf \
&& PORT=${port} TEMP=${ip} envsubst < ./redis-cluster.tmpl > ./redis-${port}/conf/redis.conf \
&& mkdir -p ./redis-${port}/data; \
done
for port in `seq 8001 8006`; do \
 docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
 -v /root/redis/redis-${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
 -v /root/redis/redis-${port}/data:/data \
 --restart always --name redis-${port} --net redis-net \
 --sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; \
 done

如图所示

[root@localhost-1 redis]# for port in `seq 8001 8006`; do \
>  docker run -d -ti -p ${port}:${port} -p 1${port}:1${port} \
>  -v /root/redis/redis-${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
>  -v /root/redis/redis-${port}/data:/data \
>  --restart always --name redis-${port} --net redis-net \
>  --sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; \
>  done
0f94d9eef399b96d5e778901652923c8c81e260c9c3caa54b1fcc3d61d49bd0a
40c6727e7423a1ca432fe0c6845c9d62f88fbeb9818542cb1b234bbfe42226af
7d2bed46567e7c5d63933c0960c8f1074949140c8a2813cee6756f662c311745
2b32a9b43dbd3da09ce97761b6d1058852152583cedd62cb94cbb02d9ce28d45
a4082498ad7a85e97921d655d5b0372d68975b4c36be54adde31f34cbb39a08b
066fd6161732afe8c014b634f168db9cfa205743aa6fb5a88cbbcaa628430c87
[root@localhost-1 redis]# 

使用 tree 查询生成结果 

 

 进入其中一个容器   docker exec -it redis-8001 bash

执行 命令  redis-cli --cluster create 192.168.253.130:8001 192.168.253.130:8002 192.168.253.130:8003 192.168.253.130:8004 192.168.253.130:8005 192.168.253.130:8006 --cluster-replicas 1 -a 123456

[root@localhost-1 ~]# docker exec -it redis-8001 bash
root@0f94d9eef399:/data# redis-cli --cluster create 192.168.253.130:8001 192.168.253.130:8002 192.168.253.130:8003 192.168.253.130:8004 192.168.253.130:8005 192.168.253.130:8006 --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.253.130:8005 to 192.168.253.130:8001
Adding replica 192.168.253.130:8006 to 192.168.253.130:8002
Adding replica 192.168.253.130:8004 to 192.168.253.130:8003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: ff253cf3d3f92f0d81d6d1113ef3de8ff5aacc93 192.168.253.130:8001
   slots:[0-5460] (5461 slots) master
M: a8e0fd61e68580e22584e1d46f398fdc8d221e63 192.168.253.130:8002
   slots:[5461-10922] (5462 slots) master
M: 416d3bf9ecc7d6b4154912e4cb4ce2cbaa745a2a 192.168.253.130:8003
   slots:[10923-16383] (5461 slots) master
S: 8d5411db5e23c1e70a02884d0525c24746453f19 192.168.253.130:8004
   replicates 416d3bf9ecc7d6b4154912e4cb4ce2cbaa745a2a
S: 4614e5d22ba1aacc00a352ab8175db696ebc4b29 192.168.253.130:8005
   replicates ff253cf3d3f92f0d81d6d1113ef3de8ff5aacc93
S: 6d55d0795c44637dd982ad8ade67037a94ebe291 192.168.253.130:8006
   replicates a8e0fd61e68580e22584e1d46f398fdc8d221e63
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.253.130:8001)
M: ff253cf3d3f92f0d81d6d1113ef3de8ff5aacc93 192.168.253.130:8001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 4614e5d22ba1aacc00a352ab8175db696ebc4b29 192.168.253.130:8005
   slots: (0 slots) slave
   replicates ff253cf3d3f92f0d81d6d1113ef3de8ff5aacc93
M: 416d3bf9ecc7d6b4154912e4cb4ce2cbaa745a2a 192.168.253.130:8003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 8d5411db5e23c1e70a02884d0525c24746453f19 192.168.253.130:8004
   slots: (0 slots) slave
   replicates 416d3bf9ecc7d6b4154912e4cb4ce2cbaa745a2a
M: a8e0fd61e68580e22584e1d46f398fdc8d221e63 192.168.253.130:8002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 6d55d0795c44637dd982ad8ade67037a94ebe291 192.168.253.130:8006
   slots: (0 slots) slave
   replicates a8e0fd61e68580e22584e1d46f398fdc8d221e63
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@0f94d9eef399:/data#

三  验证结果 

然后执行 redis-cli -p 8001  ,进入后 输入密码  auth 123456 ,查询信息 info Replication

进入其中任意一个master ,docker exec -it redis-8003 bash

redis-cli -c -h 192.168.253.130 -p 8002 -a 123456

一定要有 -c 参数

[root@localhost-1 redis]# docker exec -it redis-8003 bash
root@0f94d9eef399:/data# redis-cli -c -h 192.168.253.130 -p 8002 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.253.130:8002>

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黄泉路好走

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值