參考
https://www.jianshu.com/p/339549138e07
mkdir -p /data/redis-cluster && cd /data/redis-cluster
編輯 master結點配置檔案 redis-master.conf
port 6379 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes daemonize no protected-mode no # requirepass 123456 # masterauth 123456 pidfile /var/run/redis.pid cluster-announce-ip 192.168.3.201
cluster-config-file: 叢集配置檔案(自動建立)
cluster-node-timeout: 叢集超時時間(毫秒)
cluster-announce-ip: 叢集節點 IP,如果想要叢集可以供外網訪問,這裡直接填宿主機的IP
port 6380 cluster-enabled no appendonly yes daemonize no protected-mode no pidfile /var/run/redis.pid slaveof 192.168.3.202 6379
slaveof 192.168.3.203 7001: 直接指定成為對應master的從節點
docker run -d --privileged=true --restart always -p 6379:6379 -p 16379:16379 \ -v /data/redis-cluster/master-1/:/etc/redis/ \ -v /data/redis-cluster/master-1/:/data \ --name redis-master-1 redis:7.0 redis-server /etc/redis/redis-master.conf --appendonly yes
docker run -d --privileged=true --restart always -p 6380:6380 \ -v /data/redis-cluster/slave-2/:/etc/redis/ \ -v /data/redis-cluster/slave-2/:/data \ --name redis-slave-2 redis:7.0 redis-server /etc/redis/redis-slave.conf --appendonly yes
docker run -d --privileged=true --restart always -p 6379:6379 -p 16379:16379 \ -v /data/redis-cluster/master-2/:/etc/redis/ \ -v /data/redis-cluster/master-2/:/data \ --name redis-master-2 redis:7.0 redis-server /etc/redis/redis-master.conf --appendonly yes docker run -d --privileged=true --restart always -p 6380:6380 \ -v /data/redis-cluster/slave-3/:/etc/redis/ \ -v /data/redis-cluster/slave-3/:/data \ --name redis-slave-3 redis:7.0 redis-server /etc/redis/redis-slave.conf --appendonly yes
docker run -d --privileged=true --restart always -p 6379:6379 -p 16379:16379 \ -v /data/redis-cluster/master-3/:/etc/redis/ \ -v /data/redis-cluster/master-3/:/data \ --name redis-master-3 redis:7.0 redis-server /etc/redis/redis-master.conf --appendonly yes docker run -d --privileged=true --restart always -p 6380:6380 \ -v /data/redis-cluster/slave-1/:/etc/redis/ \ -v /data/redis-cluster/slave-1/:/data \ --name redis-slave-1 redis:7.0 redis-server /etc/redis/redis-slave.conf --appendonly yes
注意3個主結點的容器,需要同時對映6379埠和16379埠。每個Redis Cluster節點都需要開啟兩個TCP連線。用於服務客戶端的普通Redis TCP埠(如6379),以及資料埠加上10000得到的埠(例如16379)。第二個高階口用於叢集匯流排,是使用二進位制協議的節點到節點通訊通道。節點使用叢集匯流排進行故障檢測、配置更新、故障轉移授權等。
此時僅僅建立了三對主從節點,但是三個主節點(7001, 7002, 7003)是孤立的,並且還未分配slots。
docker exec -it redis-master-1 /bin/bash
redis-cli --cluster create 192.168.3.201:6379 192.168.3.202:6379 192.168.3.203:6379 --cluster-replicas 0 redis-cli -a 123456 --cluster create 192.168.2.201:6379 192.168.2.202:6379 192.168.2.203:6379 --cluster-replicas 0
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.100.101:7001) M: f936070976e483985e9fa0ac904268e952e4bae3 192.168.100.101:7001 slots:[0-5460] (5461 slots) master M: 9e6435cc51704ef543e60c186fec4e38277c3a88 192.168.2.240:7003 slots:[10923-16383] (5461 slots) master M: e4f2be7cb9321058e84bc482db0c59b9d1213faa 192.168.2.240:7002 slots:[5461-10922] (5462 slots) master [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
redis-cli -h 192.168.100.101 -p 7001 -c
有密碼加 -a 密碼
192.168.100.101:7001> set k1 v1
如果計算出來這個 key的 slot不在當前結點,則會提示:
-> Redirected to slot [12706] located at 192.168.242.93:7003 OK
表示透過計算 slot分配到 192.168.242.93:7003結點去了。
我們透過普通模式連線到主結點 192.168.242.93:7003和從結點 192.168.100.106:6379,都能查到這個 k1