Docker環境搭建Redis4.0 Cluster
阿新 • • 發佈:2021-09-16
個人學習筆記,謝絕轉載!!!
原文: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"