个人学习笔记,谢绝转载!!!
原文:https://www.cnblogs.com/wshenjin/p/15296600.html
配置三个redis实例:
# cluster cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 15000
启动三个Redis实例:
docker run -tid --network=net_192.168.1 --ip=192.168.1.11 -p 6011:6001 -p 16011:16001 --name redis_cluster_node01 redis-4.0.14 docker run -tid --network=net_192.168.1 --ip=192.168.1.12 -p 6012:6001 -p 16012:16001 --name redis_cluster_node02 redis-4.0.14 docker run -tid --network=net_192.168.1 --ip=192.168.1.13 -p 6013:6001 -p 16013:16001 --name redis_cluster_node03 redis-4.0.14
创建集群:
[root@ ]# redis-trib.rb create 192.168.1.11:6001 192.168.1.12:6001 192.168.1.13:6001 >>> Creating cluster >>> Performing hash slots allocation on 3 nodes... Using 3 masters: 192.168.1.11:6001 192.168.1.12:6001 192.168.1.13:6001 M: 468b51d1918e82ad6ca81b3772e469be1000e23f 192.168.1.11:6001 slots:0-5460 (5461 slots) master M: e91829ab91203f38f37de7114e82ba5883dcff3c 192.168.1.12:6001 slots:5461-10922 (5462 slots) master M: 6dce80e6cc8a8e6863165087581496409f7e73ad 192.168.1.13:6001 slots:10923-16383 (5461 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.11:6001) M: 468b51d1918e82ad6ca81b3772e469be1000e23f 192.168.1.11:6001 slots:0-5460 (5461 slots) master 0 additional replica(s) M: 6dce80e6cc8a8e6863165087581496409f7e73ad 192.168.1.13:6001 slots:10923-16383 (5461 slots) master 0 additional replica(s) M: e91829ab91203f38f37de7114e82ba5883dcff3c 192.168.1.12:6001 slots:5461-10922 (5462 slots) master 0 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
此时的Redis Cluster集群正常的,却无法对外部网络提供业务。
这是因为,Cluster节点需要告诉用户或者是其他节点连接自己的IP和端口,默认情况下,Redis会自动检测自己的IP和从配置中获取绑定的PORT,告诉客户端或者是其他节点。而在Docker环境中,如果使用的不是host网络模式,在容器内部的IP和PORT都是隔离的,那么客户端和其他节点无法通过节点公布的IP和PORT建立连接。
效果如下:
1.1.1.1:6012> CLUSTER NODES 6dce80e6cc8a8e6863165087581496409f7e73ad 192.168.1.13:6001@16001 master - 0 1631788081031 3 connected 10923-16383 468b51d1918e82ad6ca81b3772e469be1000e23f 192.168.1.11:6001@16001 master - 0 1631788082034 1 connected 0-5460 e91829ab91203f38f37de7114e82ba5883dcff3c 192.168.1.12:6001@16001 myself,master - 0 1631788079000 2 connected 5461-10922 1.1.1.1:6012> get mkey02897 -> Redirected to slot [15915] located at 192.168.1.13:6001 Could not connect to Redis at 192.168.1.13:6001: Connection timed out
从Redis4.0将兼容 NAT 和 Docker, 如下配置:
cluster-announce-ip:要宣布的IP地址。 cluster-announce-port:要宣布的数据端口。 cluster-announce-bus-port:要宣布的集群总线端口 ##每个节点都要配置 192.168.1.11:6001> config set cluster-announce-bus-port 16011 OK 192.168.1.11:6001> config set cluster-announce-port 6011 OK 192.168.1.11:6001> config set cluster-announce-ip 1.1.1.1 OK
配置了以后,Redis节点会将配置中的这些IP和PORT告知客户端或其他节点,而这些IP和PORT是通过Docker转发到容器内的临时IP和PORT的。
配置后的效果:
1.1.1.1:6012> CLUSTER NODES 6dce80e6cc8a8e6863165087581496409f7e73ad 1.1.1.1:6013@16013 master - 0 1631788348900 3 connected 10923-16383 468b51d1918e82ad6ca81b3772e469be1000e23f 1.1.1.1:6011@16011 master - 0 1631788349904 1 connected 0-5460 e91829ab91203f38f37de7114e82ba5883dcff3c 1.1.1.1:6001@16001 myself,master - 0 1631788346000 2 connected 5461-10922 1.1.1.1:6012> get mkey02897 -> Redirected to slot [15915] located at 1.1.1.1:6013 "__ac268adff36ba00140264436b7436b749a"
学习参考:https://www.jianshu.com/p/57aac41e26e8