Redis——docker部署redis-32G集群-4节点-单副本

三台宿主机器 192.168.1.20 192.168.1.21 192.168.1.22

首先英勇地进入一个坑

docker run -d --name redisnode1 \
              -p 6301:6379 \
              -p 16301:16379 \
              -v /root/data/soft/redis/redisnode1:/data \
              -m 1G --memory-swap 1G \
              redis:6.2.3 \
              --cluster-enabled yes \
              --cluster-config-file cluster.conf
  docker exec -it redisnode1 \
         redis-cli --cluster create \
         192.168.1.20:6301 \
         192.168.1.21:6301 \
         192.168.1.22:6301 \
         192.168.1.22:6302 \
         --cluster-replicas 0
[root@bogon redis]#   docker exec -it redisnode1 \
>          redis-cli --cluster create \
>          192.168.1.20:6301 \
>          192.168.1.21:6301 \
>          192.168.1.22:6301 \
>          192.168.1.22:6302 \
>          --cluster-replicas 0
>>> Performing hash slots allocation on 4 nodes...
Master[0] -> Slots 0 - 4095
Master[1] -> Slots 4096 - 8191
Master[2] -> Slots 8192 - 12287
Master[3] -> Slots 12288 - 16383
M: 62d99c4b0638ce358f4697b86614ca679697dba6 192.168.1.20:6301
   slots:[0-4095] (4096 slots) master
M: edae88dce6f71e59146d1f7bf748fedf52f76e2f 192.168.1.21:6301
   slots:[4096-8191] (4096 slots) master
M: 73f8b40df6688e025e767c1be054768b6a658528 192.168.1.22:6301
   slots:[8192-12287] (4096 slots) master
M: 23d762dd13c532bd7842753ff63a0ae1abc01e27 192.168.1.22:6302
   slots:[12288-16383] (4096 slots) master
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
.................................................................
.................................................................
...........................................................
.

长时间的等待“Waiting for the cluster to join”,无法完成集群创建。

 

一顿繁乱地尝试、百度。。。发现。。。redis官网建议docker方式部署redis集群用  --net host 方式!!

Redis——docker部署redis-32G集群-4节点-单副本

 

 

 

重新整理docker命令

拉取镜像
docker pull redis:6.2.3
单节点启动
docker run -d --name redis30000 -p 30000:6379 -m 1G --memory-swap 1G redis:6.2.3

1、集群节点部署,host模式

docker run -d --name redisnode1 \
--net host \
-v /root/data/soft/redis/redisnode1:/data \
-m 1G --memory-swap 1G \
redis:6.2.3 \
--port 6301 \
--cluster-enabled yes \
--cluster-config-file cluster.conf

docker run -d --name redisnode2 \
--net host \
-v /root/data/soft/redis/redisnode2:/data \
-m 1G --memory-swap 1G \
redis:6.2.3 \
--port 6302 \
--cluster-enabled yes \
--cluster-config-file cluster.conf

2、三台机器 192.168.1.20 192.168.1.21 192.168.1.22
各启一个 redisnode1 ,22机机器多启动一个redisnode2,共组成4节点单实例集群

docker exec -it redisnode1 \
redis-cli --cluster create \
192.168.1.20:6301 \
192.168.1.21:6301 \
192.168.1.22:6301 \
192.168.1.22:6302 \
--cluster-replicas 0

3、测试

docker exec -it redisnode1 redis-cli -h 192.168.1.20 -p 6301 cluster info
docker exec -it redisnode1 redis-cli -h 192.168.1.20 -p 6301 get "name"

4、其它

docker stop redisnode1 redisnode2
docker rm redisnode1 redisnode2
rm -rfv redisnode*
ll

 

还是不行,A宿主(92.168.1.20)容器中的redis-cli无法访问 B宿主(92.168.1.20)容器的redis,

访问自己容器的没问题,

这不行啊,这集群还是无法创建。

[root@bogon redis]# docker exec -it redisnode1 redis-cli -h 192.168.1.21 -p 6301 cluster nodes
9b614232b65ace657925fdc3bbb8a73fa2bc6701 :6301@16301 myself,master - 0 0 0 connected
[root@bogon redis]# 
[root@bogon redis]# 
[root@bogon redis]# 
[root@bogon redis]# docker exec -it redisnode1 redis-cli -h 192.168.1.21 -p 6301 cluster nodes
Could not connect to Redis at 192.168.1.21:6301: No route to host
[root@bogon redis]# 

 

又是一顿操作,发现是firewall问题

重新配置防火墙如下

[root@bogon redis]# cat /etc/firewalld/zones/public.xml

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust 
the other computers on networks to not harm your computer.
Only selected incoming connections are accepted.
</description> <service name="ssh"/> <service name="dhcpv6-client"/>
<rule family="ipv4"> <source address="172.17.0.0/16"/> <accept/> </rule>
<rule family="ipv4"> <source address="192.168.1.0/24"/> <port protocol="tcp" port="6301-20000"/> <accept/> </rule> </zone>

执行重新加载防火墙配置

[root@bogon redis]# firewall-cmd --reload
success
[root@bogon redis]# 
[root@bogon redis]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
    rule family="ipv4" source address="172.17.0.0/16" accept
    rule family="ipv4" source address="192.168.1.0/24" port port="6301-20000" protocol="tcp" accept
[root@bogon redis]# 

 

再次创建redis集群,成功。

[root@bogon redis]#   docker exec -it redisnode1 \
>          redis-cli --cluster create \
>          192.168.1.20:6301 \
>          192.168.1.21:6301 \
>          192.168.1.22:6301 \
>          192.168.1.22:6302 \
>          --cluster-replicas 0
>>> Performing hash slots allocation on 4 nodes...
Master[0] -> Slots 0 - 4095
Master[1] -> Slots 4096 - 8191
Master[2] -> Slots 8192 - 12287
Master[3] -> Slots 12288 - 16383
M: 26628db2d0e62996f61a41e9fa0d2f80cf7e6d41 192.168.1.20:6301
   slots:[0-4095] (4096 slots) master
M: 1ca8d228aedb534d765054293c7e19861a042931 192.168.1.21:6301
   slots:[4096-8191] (4096 slots) master
M: d717ce80fa79287259ae833ecf6b32596302453c 192.168.1.22:6301
   slots:[8192-12287] (4096 slots) master
M: 08602462b3fd85f4744f7fc18cd009ac82289dfd 192.168.1.22:6302
   slots:[12288-16383] (4096 slots) master
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.1.20:6301)
M: 26628db2d0e62996f61a41e9fa0d2f80cf7e6d41 192.168.1.20:6301
   slots:[0-4095] (4096 slots) master
M: 08602462b3fd85f4744f7fc18cd009ac82289dfd 192.168.1.22:6302
   slots:[12288-16383] (4096 slots) master
M: d717ce80fa79287259ae833ecf6b32596302453c 192.168.1.22:6301
   slots:[8192-12287] (4096 slots) master
M: 1ca8d228aedb534d765054293c7e19861a042931 192.168.1.21:6301
   slots:[4096-8191] (4096 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@bogon redis]# 

 

测试集群

记得加  -c  表求集群方式访问。

  458  docker exec -it redisnode1 redis-cli -h 192.168.1.21 -p 6301 set "name" "zhangsan"
  459  docker exec -it redisnode1 redis-cli -h 192.168.1.21 -p 6301 get "name"
  460  docker exec -it redisnode1 redis-cli -h 192.168.1.22 -p 6301 get "name"
  461  docker exec -it redisnode1 redis-cli -h 192.168.1.20 -p 6301 get "name"
  462  docker exec -it redisnode1 redis-cli -h 192.168.1.20 -p 6301 -c get "name"
  463  docker exec -it redisnode1 redis-cli -h 192.168.1.22 -p 6301 -c get "name"
  464  docker exec -it redisnode1 redis-cli -h 192.168.1.22 -p 6302 -c get "name"

 

 

后记

redis集群总线:

redis集群总线端口为redis客户端端口加上10000,比如说你的redis 6379端口为客户端通讯端口,那么16379端口为集群总线端口
我搭建的redis集群中端口号是从 7001 ~ 7006的,其中 7001、7003、7005 为主节点,7002、7004、7006为从节点;那么redis集群中总线端口为17001、17003、17005,如图所示:

Redis——docker部署redis-32G集群-4节点-单副本

 

 

还有一个需要注意的问题是,iptables或者firewall需要添加对应集群总线端口,如果你把这两个都关了的话可以忽略以下内容:

iptables和firewall的配置参考博客 :

https://blog.****.net/XIANZHIXIANZHIXIAN/article/details/78918772

添加redis集群总线端口和每个redis实例的通信端口即可

建议把每个节点的总线端口都配置进防火墙,主从切换时也不会出现节点不通的问题。

 

但是尝试不用docker的--net host模式,并把16379的总线端口也映射出来,仍然无法创建集群。

最终选择用--net host模式。

 

上一篇:LInux系统下搭建redis集群


下一篇:Python 类 __slots__ & 私有变量