【linux】linux在Docker里面安装redis集群搭建详细步骤

搭建3主3从redis集群

创建六个redis容器

[root@localhost tomcatData]# docker run -d --name redis-node-1 --net host --privileged=true -v /redis/redis-node-1:/data redis --cluster-enabled yes --appendonly yes --port 6379
7f597842ac768d5d25124f560a1fd07488b29c44b2d7bf35560701ad52157aa6
[root@localhost tomcatData]# docker run -d --name redis-node-2 --net host --privileged=true -v /redis/redis-node-2:/data redis --cluster-enabled yes --appendonly yes --port 6380
72dbd4eef385a6dd521b8167619542b7b75d049dc306ef33c4478537f5ee8153
[root@localhost tomcatData]# docker run -d --name redis-node-3 --net host --privileged=true -v /redis/redis-node-3:/data redis --cluster-enabled yes --appendonly yes --port 6381
b385ca96ea0d29ad7d5dda0f1ab69d2d359e6009587d83500f2c586ef656b319
[root@localhost tomcatData]# docker run -d --name redis-node-4 --net host --privileged=true -v /redis/redis-node-4:/data redis --cluster-enabled yes --appendonly yes --port 6382
808b5e0c9ddb7736e0855644b917da05ad0ec307a4b1eb39b967ccb3edff1ed5
[root@localhost tomcatData]# docker run -d --name redis-node-5 --net host --privileged=true -v /redis/redis-node-5:/data redis --cluster-enabled yes --appendonly yes --port 6383
9a7156a41bc4abe6edb0f45354dc1aa1a812e2c8a667ea53036f66690317ba51
[root@localhost tomcatData]# docker run -d --name redis-node-6 --net host --privileged=true -v /redis/redis-node-6:/data redis --cluster-enabled yes --appendonly yes --port 6384
8016db0a503c2563fe06433d347191c84388d39f7d4cefec069e1e2bfe8ca07f

查看docker容器

docker ps -a
CONTAINER ID   IMAGE      COMMAND                  CREATED         STATUS                     PORTS                                       NAMES
54734fb05ac1   redis      "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes                                                           redis-node-6
a9830d1571ff   redis      "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes                                                           redis-node-5
111f4e7b1888   redis      "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes                                                           redis-node-4
304aee9902f3   redis      "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes                                                           redis-node-3
4b58839a75fe   redis      "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes                                                           redis-node-2
615e92e7c167   redis      "docker-entrypoint.s…"   4 minutes ago   Exited (1) 4 minutes ago                                               redis-node-1
04ba18605068   redis      "docker-entrypoint.s…"   13 hours ago    Up 13 hours                0.0.0.0:6379->6379/tcp, :::6379->6379/tcp   myredis
983cab77b3f3   mysql      "docker-entrypoint.s…"   3 days ago      Exited (0) 13 hours ago                                                mysql3131
dbc4fa32de95   registry   "/entrypoint.sh /etc…"   3 days ago      Created                                                                distracted_knuth
96afa0e2bb9c   registry   "/entrypoint.sh /etc…"   3 days ago      Created                                                                xenodochial_babbage

进入节点1,构建6台节点的集群关系

命令格式: --cluster-replicas 1 表示为每个master创建一个slave节点

redis-cli --cluster create 192.168.216.130:6378 192.168.216.130:6380 192.168.216.130:6381 192.168.216.130:6382 192.168.216.130:6383 192.168.216.130:6384 --cluster-replicas 1
root@localhost:/data# redis-cli --cluster create 192.168.216.130:6378 192.168.216.130:6380 192.168.216.130:6381 192.168.216.130:6382 192.168.216.130:6383 192.168.216.130:6384 --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 192.168.216.130:6383 to 192.168.216.130:6378
Adding replica 192.168.216.130:6384 to 192.168.216.130:6380
Adding replica 192.168.216.130:6382 to 192.168.216.130:6381
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: ec8c48e4fa51f203c6e0bc5956fb575b9f7b84ab 192.168.216.130:6378
   slots:[0-5460] (5461 slots) master
M: db7c89c11e63efe5293508f2a7a5cd39316a4efc 192.168.216.130:6380
   slots:[5461-10922] (5462 slots) master
M: affd3d49af6e0a650e0d7990b100f37da0adb9d3 192.168.216.130:6381
   slots:[10923-16383] (5461 slots) master
S: a4687fbc50556343b1f71d2179a57249415ab00a 192.168.216.130:6382
   replicates db7c89c11e63efe5293508f2a7a5cd39316a4efc
S: 3967b0ec029eeb11d7a2549dad21e0697fc23e7a 192.168.216.130:6383
   replicates affd3d49af6e0a650e0d7990b100f37da0adb9d3
S: 9e34bb0683ca49ae6fd680888c1c405261553d81 192.168.216.130:6384
   replicates ec8c48e4fa51f203c6e0bc5956fb575b9f7b84ab
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.216.130:6378)
M: ec8c48e4fa51f203c6e0bc5956fb575b9f7b84ab 192.168.216.130:6378
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: db7c89c11e63efe5293508f2a7a5cd39316a4efc 192.168.216.130:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: a4687fbc50556343b1f71d2179a57249415ab00a 192.168.216.130:6382
   slots: (0 slots) slave
   replicates db7c89c11e63efe5293508f2a7a5cd39316a4efc
S: 3967b0ec029eeb11d7a2549dad21e0697fc23e7a 192.168.216.130:6383
   slots: (0 slots) slave
   replicates affd3d49af6e0a650e0d7990b100f37da0adb9d3
M: affd3d49af6e0a650e0d7990b100f37da0adb9d3 192.168.216.130:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 9e34bb0683ca49ae6fd680888c1c405261553d81 192.168.216.130:6384
   slots: (0 slots) slave
   replicates ec8c48e4fa51f203c6e0bc5956fb575b9f7b84ab
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看主从关系

命令格式:redis-cli --cluster check 【本台redis自己的IP】:【本台redis自己的端口】

 redis-cli --cluster check 192.168.216.130:6378
root@localhost:/data#  redis-cli --cluster check 192.168.216.130:6378
192.168.216.130:6378 (ec8c48e4...) -> 0 keys | 5461 slots | 1 slaves.
192.168.216.130:6380 (db7c89c1...) -> 0 keys | 5462 slots | 1 slaves.
192.168.216.130:6381 (affd3d49...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.216.130:6378)
M: ec8c48e4fa51f203c6e0bc5956fb575b9f7b84ab 192.168.216.130:6378
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: db7c89c11e63efe5293508f2a7a5cd39316a4efc 192.168.216.130:6380
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: a4687fbc50556343b1f71d2179a57249415ab00a 192.168.216.130:6382
   slots: (0 slots) slave
   replicates db7c89c11e63efe5293508f2a7a5cd39316a4efc
S: 3967b0ec029eeb11d7a2549dad21e0697fc23e7a 192.168.216.130:6383
   slots: (0 slots) slave
   replicates affd3d49af6e0a650e0d7990b100f37da0adb9d3
M: affd3d49af6e0a650e0d7990b100f37da0adb9d3 192.168.216.130:6381
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 9e34bb0683ca49ae6fd680888c1c405261553d81 192.168.216.130:6384
   slots: (0 slots) slave
   replicates ec8c48e4fa51f203c6e0bc5956fb575b9f7b84ab
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 数据读写测试

加参数 -c ,防止路由失效

root@localhost:/data# redis-cli -p 6379 -c
127.0.0.1:6379> get name
-> Redirected to slot [5798] located at 192.168.216.130:6380
"zhangsan"
192.168.216.130:6380>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

白鱼塘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值