基於Docker redis3.2.4 叢集 Cluster
環境搭建步驟
一 準備
Docker環境(centos7 + docker1.12.1)
Redis 3.2.4 wget http://download.redis.io/releases/redis-3.2.4.tar.gz
因為cluster模式最少三個主資料庫才能正常執行,所以先做三主三從
172.17.0.3:7000 172.17.0.4:7001 172.17.0.5:7002 172.17.0.6:7003 172.17.0.7:7004 172.17.0.7:7005
另外:搭建完成後,為了驗證增加節點的操作,增加如下節點
172.17.0.8:7006 172.17.0.9:7007
遺留問題:
1 cluster setslot 分配slot後,通過cluster slots命令在分配的這臺機器看可以看到分配正確,但是在其它節點檢視時還是舊的分配情況
2 客戶端程式碼如何連線叢集
3 節點宕機後測試(已測試,對應的從節點會接管主節點,主節點故障修復後啟動變成這個新的主節點的從資料庫)
二 基礎映象製作
Dockerfile如下:
FROM centos:6.7 MAINTAINER loomz [email protected] ENV REFRESHED_AT 2017-03-08 ENV REDIS_HOME /opt/redis/redis_default ADD redis-3.2.4.tar.gz /opt/redis/ RUN ln -s /opt/redis/redis-3.2.4 $REDIS_HOME RUN yum -y install gcc WORKDIR $REDIS_HOME RUN make && make install EXPOSE 6379 ENTRYPOINT [ "/usr/local/bin/redis-server", "/etc/redis/redis.conf" ]
三 容器啟動
配置檔案內容:redis.conf
port 6379
bind 172.17.0.5
daemonize no
pidfile "/var/run/redis.pid"
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
appendonly yes
容器啟動指令碼start-7000.sh:
#!/bin/bash docker run -d -p 7000:6379 --restart always -h redis-7000 --name redis-7000 -v /home/loomz/dockerfiles/redis_3.2_cluster/redis_cluster/7000:/etc/redis/:rw redis:v3.2.4
start-7001.sh start-7002.sh start-7003.sh start-7004.sh start-7005.sh 依此類推
啟動所有容器後,節點並未形成叢集,docker logs redis-7000,內容顯示:
No cluster configuration found, I'm 0435ad36e3c8a12b4850e0d472cc07d5b3854b11
四 建立叢集
1 建立叢集需要用的ruby指令碼,先安裝ruby 和 rubygems,進入其中一臺容器建立
docker exec -it redis-7000 /bin/bash
yum install -y ruby rubygems
ruby -v
2 建立叢集節點
在redis編譯原始碼包的src目錄,將指令碼拷貝到/usr/local/bin目錄
cp redis-trib.rb /usr/local/bin
建立節點命令:
redis-trib.rb create --replicas 1 172.17.0.5 6379 172.17.0.6 6379 172.17.0.7 6379 172.17.0.8 6379 172.17.0.9 6379 172.17.0.10 6379
輸入命令後會展示叢集分配情況,輸入yes確認,最終節點分配以及slot分配情況如下:
S: 4c98b473b20ca9487b6cd042015269aaedb550dc 172.17.0.5:6379
slots: (0 slots) slave
replicates c469f66da77cd61fe8aef25e553e5a388e5b32bb
S: cd289a8c8d618fd8f8918bbae59264e3a5a27403 172.17.0.6:6379
slots: (0 slots) slave
replicates fa0a9e0fbc24ddb871c09ca7534bc2f170c77fc8
M: c469f66da77cd61fe8aef25e553e5a388e5b32bb 172.17.0.8:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: fa0a9e0fbc24ddb871c09ca7534bc2f170c77fc8 172.17.0.9:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: c0ab7e8c1934b51dc03767c8866bc4dcc9ace93d 172.17.0.10:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 0435ad36e3c8a12b4850e0d472cc07d5b3854b11 172.17.0.7:6379
slots: (0 slots) slave
replicates c0ab7e8c1934b51dc03767c8866bc4dcc9ace93d
3 也可以登入其中一臺客戶端驗證
redis-cli > cluster nodes
cd289a8c8d618fd8f8918bbae59264e3a5a27403 172.17.0.6:6379 slave fa0a9e0fbc24ddb871c09ca7534bc2f170c77fc8 0 1489315757449 5 connected
c469f66da77cd61fe8aef25e553e5a388e5b32bb 172.17.0.8:6379 master - 0 1489315758452 4 connected 10923-16383
fa0a9e0fbc24ddb871c09ca7534bc2f170c77fc8 172.17.0.9:6379 master - 0 1489315760456 5 connected 5461-10922
c0ab7e8c1934b51dc03767c8866bc4dcc9ace93d 172.17.0.10:6379 master - 0 1489315761456 6 connected 0-5460
4c98b473b20ca9487b6cd042015269aaedb550dc 172.17.0.5:6379 myself,slave c469f66da77cd61fe8aef25e553e5a388e5b32bb 0 0 1 connected
0435ad36e3c8a12b4850e0d472cc07d5b3854b11 172.17.0.7:6379 slave c0ab7e8c1934b51dc03767c8866bc4dcc9ace93d 0 1489315759454 6 connected
五 增加節點
命令:cluster meet 叢集中任意節點和埠
redis-cli > cluster meet 172.17.0.10 6379
redis-cli > cluster nodes
cd289a8c8d618fd8f8918bbae59264e3a5a27403 172.17.0.6:6379 slave fa0a9e0fbc24ddb871c09ca7534bc2f170c77fc8 0 1489319127890 5 connected
7ff58bf488f4d6a1062c106f018ae41e28164b28 172.17.0.11:6379 myself,master - 0 0 0 connected
c0ab7e8c1934b51dc03767c8866bc4dcc9ace93d 172.17.0.10:6379 master - 0 1489319128893 6 connected 0-5460
0435ad36e3c8a12b4850e0d472cc07d5b3854b11 172.17.0.7:6379 slave c0ab7e8c1934b51dc03767c8866bc4dcc9ace93d 0 1489319126888 6 connected
c469f66da77cd61fe8aef25e553e5a388e5b32bb 172.17.0.8:6379 master - 0 1489319124884 4 connected 10923-16383
4c98b473b20ca9487b6cd042015269aaedb550dc 172.17.0.5:6379 slave c469f66da77cd61fe8aef25e553e5a388e5b32bb 0 1489319125084 4 connected
fa0a9e0fbc24ddb871c09ca7534bc2f170c77fc8 172.17.0.9:6379 master - 0 1489319125886 5 connected 5461-10922
增加的節點172.17.0.11 預設變成master但是沒有分配到slot
redis-cli > cluster slots
1) 1) (integer) 0
2) (integer) 5460
3) 1) "172.17.0.10"
2) (integer) 6379
3) "c0ab7e8c1934b51dc03767c8866bc4dcc9ace93d"
4) 1) "172.17.0.7"
2) (integer) 6379
3) "0435ad36e3c8a12b4850e0d472cc07d5b3854b11"
2) 1) (integer) 10923
2) (integer) 16383
3) 1) "172.17.0.8"
2) (integer) 6379
3) "c469f66da77cd61fe8aef25e553e5a388e5b32bb"
4) 1) "172.17.0.5"
2) (integer) 6379
3) "4c98b473b20ca9487b6cd042015269aaedb550dc"
3) 1) (integer) 5461
2) (integer) 10922
3) 1) "172.17.0.9"
2) (integer) 6379
3) "fa0a9e0fbc24ddb871c09ca7534bc2f170c77fc8"
4) 1) "172.17.0.6"
2) (integer) 6379
3) "cd289a8c8d618fd8f8918bbae59264e3a5a27403"
可以使用以下兩種方式,這裡不再細說:
/usr/local/bin/redis-trib.rb reshard 命令重新分配slot,支援資料遷移
cluster setslot 5460 node 7ff58bf488f4d6a1062c106f018ae41e28164b28 資料遷移需要手動
增加節點設定為某個主節點的從資料庫
cluster nodes
CLUSTER REPLICATE <node_id> 將當前節點設定為 node_id 指定的節點的從節點。
客戶端連線程式碼
public static void main(String[] args) {
JedisPoolConfig poolconfig = new JedisPoolConfig();
poolconfig.setMaxIdle(30);
poolconfig.setMaxTotal(1000);
poolconfig.setMaxWaitMillis(3000);
Set<HostAndPort> nodes = new HashSet<HostAndPort>();
HostAndPort hostAndPort = new HostAndPort("172.17.0.1", 7000);
HostAndPort hostAndPort1 = new HostAndPort("172.17.0.1", 7001);
HostAndPort hostAndPort2 = new HostAndPort("172.17.0.1", 7002);
HostAndPort hostAndPort3 = new HostAndPort("172.17.0.1", 7003);
HostAndPort hostAndPort4 = new HostAndPort("172.17.0.1", 7004);
HostAndPort hostAndPort5 = new HostAndPort("172.17.0.1", 7005);
HostAndPort hostAndPort6 = new HostAndPort("172.17.0.1", 7006);
HostAndPort hostAndPort7 = new HostAndPort("172.17.0.1", 7007);
nodes.add(hostAndPort);
nodes.add(hostAndPort1);
nodes.add(hostAndPort2);
nodes.add(hostAndPort3);
nodes.add(hostAndPort4);
nodes.add(hostAndPort5);
nodes.add(hostAndPort6);
nodes.add(hostAndPort7);
JedisCluster jedisCluster = new JedisCluster(nodes, poolconfig);
String string = jedisCluster.get("aa");
System.out.println(string);
}
六 叢集相關命令
cluster叢集相關命令,更多redis相關命令見文件:http://redis.readthedocs.org/en/latest
叢集
CLUSTER INFO 列印叢集的資訊
CLUSTER NODES 列出叢集當前已知的所有節點(node),以及這些節點的相關資訊。
節點
CLUSTER MEET <ip> <port> 將 ip 和 port 所指定的節點新增到叢集當中,讓它成為叢集的一份子。
CLUSTER FORGET <node_id> 從叢集中移除 node_id 指定的節點。
CLUSTER REPLICATE <node_id> 將當前節點設定為 node_id 指定的節點的從節點。
CLUSTER SAVECONFIG 將節點的配置檔案儲存到硬盤裡面。
槽(slot)
CLUSTER ADDSLOTS <slot> [slot ...] 將一個或多個槽(slot)指派(assign)給當前節點。
CLUSTER DELSLOTS <slot> [slot ...] 移除一個或多個槽對當前節點的指派。
CLUSTER FLUSHSLOTS 移除指派給當前節點的所有槽,讓當前節點變成一個沒有指派任何槽的節點。
CLUSTER SETSLOT <slot> NODE <node_id> 將槽 slot 指派給 node_id 指定的節點,如果槽已經指派給另一個節點,那麼先讓另一個節點刪除該槽>,然後再進行指派。
CLUSTER SETSLOT <slot> MIGRATING <node_id> 將本節點的槽 slot 遷移到 node_id 指定的節點中。
CLUSTER SETSLOT <slot> IMPORTING <node_id> 從 node_id 指定的節點中匯入槽 slot 到本節點。
CLUSTER SETSLOT <slot> STABLE 取消對槽 slot 的匯入(import)或者遷移(migrate)。
鍵
CLUSTER KEYSLOT <key> 計算鍵 key 應該被放置在哪個槽上。
CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的鍵值對數量。
CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 個 slot 槽中的鍵。
其它:
疑問1資訊
172.17.0.8:6379> cluster slots
1) 1) (integer) 0
2) (integer) 5459
3) 1) "172.17.0.10"
2) (integer) 6379
3) "c0ab7e8c1934b51dc03767c8866bc4dcc9ace93d"
4) 1) "172.17.0.7"
2) (integer) 6379
3) "0435ad36e3c8a12b4850e0d472cc07d5b3854b11"
2) 1) (integer) 10923
2) (integer) 16383
3) 1) "172.17.0.5"
2) (integer) 6379
3) "4c98b473b20ca9487b6cd042015269aaedb550dc"
4) 1) "172.17.0.8"
2) (integer) 6379
3) "c469f66da77cd61fe8aef25e553e5a388e5b32bb"
3) 1) (integer) 5460
2) (integer) 5461
3) 1) "172.17.0.11"
2) (integer) 6379
3) "7ff58bf488f4d6a1062c106f018ae41e28164b28"
4) 1) "172.17.0.12"
2) (integer) 6379
3) "a36dbb0895a24d82f8cc6bb2c7af1adb6432a7da"
4) 1) (integer) 5462
2) (integer) 10922
3) 1) "172.17.0.9"
2) (integer) 6379
3) "fa0a9e0fbc24ddb871c09ca7534bc2f170c77fc8"
4) 1) "172.17.0.6"
2) (integer) 6379
3) "cd289a8c8d618fd8f8918bbae59264e3a5a27403"
172.17.0.8:6379> exit
[[email protected] redis-3.2.4]# redis-cli -h 172.17.0.9
172.17.0.9:6379>
172.17.0.9:6379> cluster slots
1) 1) (integer) 5461
2) (integer) 5461
3) 1) "172.17.0.11"
2) (integer) 6379
3) "7ff58bf488f4d6a1062c106f018ae41e28164b28"
4) 1) "172.17.0.12"
2) (integer) 6379
3) "a36dbb0895a24d82f8cc6bb2c7af1adb6432a7da"
2) 1) (integer) 5462
2) (integer) 10922
3) 1) "172.17.0.9"
2) (integer) 6379
3) "fa0a9e0fbc24ddb871c09ca7534bc2f170c77fc8"
4) 1) "172.17.0.6"
2) (integer) 6379
3) "cd289a8c8d618fd8f8918bbae59264e3a5a27403"
3) 1) (integer) 10923
2) (integer) 16383
3) 1) "172.17.0.5"
2) (integer) 6379
3) "4c98b473b20ca9487b6cd042015269aaedb550dc"
4) 1) "172.17.0.8"
2) (integer) 6379
3) "c469f66da77cd61fe8aef25e553e5a388e5b32bb"
4) 1) (integer) 0
2) (integer) 5460
3) 1) "172.17.0.10"
2) (integer) 6379
3) "c0ab7e8c1934b51dc03767c8866bc4dcc9ace93d"
4) 1) "172.17.0.7"
2) (integer) 6379
3) "0435ad36e3c8a12b4850e0d472cc07d5b3854b11"