Redis在3.0版本以後開始支持集群,經過中間幾個版本的不斷更新優化,最新的版本集群功能已經非常完善。本文簡單介紹一下Redis集群搭建的過程和配置方法,redis版本是5.0.4,操作系統是中標麒麟(和Centos內核基本一致)。 1、Redis集群原理 Redis 集群是一個提供在多個Redi ...
Redis在3.0版本以後開始支持集群,經過中間幾個版本的不斷更新優化,最新的版本集群功能已經非常完善。本文簡單介紹一下Redis集群搭建的過程和配置方法,redis版本是5.0.4,操作系統是中標麒麟(和Centos內核基本一致)。
1、Redis集群原理
Redis 集群是一個提供在多個Redis間節點間共用數據的程式集,集群節點共同構建了一個去中心化的網路,集群中的每個節點擁有平等的身份,節點各自保存各自的數據和集群狀態。節點之間採用Gossip協議進行通信,保證了節點狀態的信息同步。
Redis 集群數據通過分區來進行管理,每個節點保存集群數據的一個子集。數據的分配採用一種叫做哈希槽(hash slot)
的方式來分配,和傳統的一致性哈希不太相同。Redis 集群有16384個哈希槽,每個key通過CRC16校驗後對16384取模來決定放置哪個槽。
為了使在部分節點失敗或者大部分節點無法通信的情況下集群仍然可用,集群使用了主從複製模型。讀取數據時,根據一致性哈希演算法到對應的 master 節點獲取數據,如果master 掛掉之後,會啟動一個對應的 salve 節點來充當 master 。
2、環境準備
這裡準備在一臺PC上搭建一個3主3從的redis集群。
在/opt/目錄下新建一個文件夾rediscluster,用來存放集群節點目錄。
然後分別新建server10、server11、server20、server21、server30、server31 6個文件夾準備6個redis節點,這些節點分別使用6379、6380、6381、6382、6383、6384埠,以server10為例配置如下:
port 6379
daemonize yes
pidfile /var/run/redis_6379.pid
cluster-enabled yes
cluster-node-timeout 15000
cluster-config-file nodes-6379.conf
其他節點只需修改埠和文件名,依次按此進行配置即可,配置完成後啟動這些節點。
[root@localhost rediscluster]# ./server10/redis-server ./server10/redis.conf & [root@localhost rediscluster]# ./server11/redis-server ./server11/redis.conf & [root@localhost rediscluster]# ./server20/redis-server ./server20/redis.conf & [root@localhost rediscluster]# ./server21/redis-server ./server21/redis.conf & [root@localhost rediscluster]# ./server30/redis-server ./server30/redis.conf & [root@localhost rediscluster]# ./server31/redis-server ./server31/redis.conf &
查看啟動狀態:
[root@localhost rediscluster]# ps -ef|grep redis
root 11842 1 0 15:03 ? 00:00:12 ./server10/redis-server 127.0.0.1:6379 [cluster]
root 11950 1 0 15:03 ? 00:00:13 ./server11/redis-server 127.0.0.1:6380 [cluster]
root 12074 1 0 15:04 ? 00:00:13 ./server20/redis-server 127.0.0.1:6381 [cluster]
root 12181 1 0 15:04 ? 00:00:12 ./server21/redis-server 127.0.0.1:6382 [cluster]
root 12297 1 0 15:04 ? 00:00:12 ./server30/redis-server 127.0.0.1:6383 [cluster]
root 12404 1 0 15:04 ? 00:00:12 ./server31/redis-server 127.0.0.1:6384 [cluster]
3、集群配置
非常簡單:redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster -replicas 1
其中-replicas 1表示每個主節點1個從節點
[root@localhost rediscluster]# ./server10/redis-cli --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1 >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 127.0.0.1:6383 to 127.0.0.1:6379 Adding replica 127.0.0.1:6384 to 127.0.0.1:6380 Adding replica 127.0.0.1:6382 to 127.0.0.1:6381 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379 slots:[0-5460] (5461 slots) master M: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380 slots:[5461-10922] (5462 slots) master M: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381 slots:[10923-16383] (5461 slots) master S: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382 replicates 63e20c75984e493892265ddd2a441c81bcdc575c S: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383 replicates d9a79ed6204e558b2fcee78ea05218b4de006acd S: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384 replicates efa84a74525749b8ea20585074dda81b852e9c29 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 127.0.0.1:6379) M: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382 slots: (0 slots) slave replicates 63e20c75984e493892265ddd2a441c81bcdc575c S: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384 slots: (0 slots) slave replicates efa84a74525749b8ea20585074dda81b852e9c29 M: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383 slots: (0 slots) slave replicates d9a79ed6204e558b2fcee78ea05218b4de006acd [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
創建完成,主從節點分配如下:
Adding replica 127.0.0.1:6383 to 127.0.0.1:6379 Adding replica 127.0.0.1:6384 to 127.0.0.1:6380 Adding replica 127.0.0.1:6382 to 127.0.0.1:6381
4、集群測試
通過6379客戶端連接後進行測試,發現轉向了6381:
[root@localhost rediscluster]# ./server10/redis-cli -h 127.0.0.1 -c -p 6379 127.0.0.1:6379> set foo bar -> Redirected to slot [12182] located at 127.0.0.1:6381 OK 127.0.0.1:6381> get foo "bar"
在6381上連接測試:
[root@localhost rediscluster]# ./server10/redis-cli -h 127.0.0.1 -c -p 6381 127.0.0.1:6381> get foo "bar"
結果相同,說明集群配置正常。
5、集群節點擴容
在rediscluster目錄下在新增兩個目錄server40和server41,新增2個redis節點配置6385和6386兩個埠。將6385作為新增的master節點,6386作為從節點,然後啟動節點:
[root@localhost server41]# ps -ef|grep redis root 11842 1 0 15:03 ? 00:00:18 ./server10/redis-server 127.0.0.1:6379 [cluster] root 11950 1 0 15:03 ? 00:00:19 ./server11/redis-server 127.0.0.1:6380 [cluster] root 12074 1 0 15:04 ? 00:00:18 ./server20/redis-server 127.0.0.1:6381 [cluster] root 12181 1 0 15:04 ? 00:00:18 ./server21/redis-server 127.0.0.1:6382 [cluster] root 12297 1 0 15:04 ? 00:00:17 ./server30/redis-server 127.0.0.1:6383 [cluster] root 12404 1 0 15:04 ? 00:00:18 ./server31/redis-server 127.0.0.1:6384 [cluster] root 30563 1 0 18:01 ? 00:00:00 ./redis-server 127.0.0.1:6385 [cluster] root 30582 1 0 18:02 ? 00:00:00 ./redis-server 127.0.0.1:6386 [cluster]
添加主節點:
[root@localhost server41]# ./redis-cli --cluster add-node 127.0.0.1:6385 127.0.0.1:6379 >>> Adding node 127.0.0.1:6385 to cluster 127.0.0.1:6379 >>> Performing Cluster Check (using node 127.0.0.1:6379) M: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382 slots: (0 slots) slave replicates 63e20c75984e493892265ddd2a441c81bcdc575c S: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384 slots: (0 slots) slave replicates efa84a74525749b8ea20585074dda81b852e9c29 M: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383 slots: (0 slots) slave replicates d9a79ed6204e558b2fcee78ea05218b4de006acd [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 127.0.0.1:6385 to make it join the cluster. [OK] New node added correctly.
查看節點列表:
[root@localhost server41]# ./redis-cli 127.0.0.1:6379> cluster nodes 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 127.0.0.1:6385@16385 master - 0 1555064037664 0 connected efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379@16379 myself,master - 0 1555064036000 1 connected 0-5460 d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381@16381 master - 0 1555064038666 3 connected 10923-16383 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382@16382 slave 63e20c75984e493892265ddd2a441c81bcdc575c 0 1555064035000 4 connected ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384@16384 slave efa84a74525749b8ea20585074dda81b852e9c29 0 1555064037000 6 connected 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380@16380 master - 0 1555064037000 2 connected 5461-10922 fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383@16383 slave d9a79ed6204e558b2fcee78ea05218b4de006acd 0 1555064037000 5 connected
添加從節點:
[root@localhost server41]# ./redis-cli --cluster add-node 127.0.0.1:6386 127.0.0.1:6379 --cluster-slave --cluster-master-id 22e8a8e97d6f7cc7d627e577a986384d4d181a4f >>> Adding node 127.0.0.1:6386 to cluster 127.0.0.1:6379 >>> Performing Cluster Check (using node 127.0.0.1:6379) M: efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) M: 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 127.0.0.1:6385 slots: (0 slots) master M: d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382 slots: (0 slots) slave replicates 63e20c75984e493892265ddd2a441c81bcdc575c S: ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384 slots: (0 slots) slave replicates efa84a74525749b8ea20585074dda81b852e9c29 M: 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383 slots: (0 slots) slave replicates d9a79ed6204e558b2fcee78ea05218b4de006acd [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 127.0.0.1:6386 to make it join the cluster. Waiting for the cluster to join >>> Configure node as replica of 127.0.0.1:6385. [OK] New node added correctly.
添加成功後,為新節點分配數據:
[root@localhost server41]# ./redis-cli --cluster reshard 127.0.0.1:6385 How many slots do you want to move (from 1 to 16384)? 1000 What is the receiving node ID? 22e8a8e97d6f7cc7d627e577a986384d4d181a4f Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1: all
這樣就新增完畢了,可以通過cluster nodes命令查看一下新增後的slot分佈
127.0.0.1:6379> cluster nodes 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 127.0.0.1:6385@16385 master - 0 1555064706000 7 connected 0-332 5461-5794 10923-11255 efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379@16379 myself,master - 0 1555064707000 1 connected 333-5460 d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381@16381 master - 0 1555064705000 3 connected 11256-16383 7c24e205301b38caa1ff3cd8b270a1ceb7249a2e 127.0.0.1:6386@16386 slave 22e8a8e97d6f7cc7d627e577a986384d4d181a4f 0 1555064705000 7 connected 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382@16382 slave 63e20c75984e493892265ddd2a441c81bcdc575c 0 1555064707000 4 connected ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384@16384 slave efa84a74525749b8ea20585074dda81b852e9c29 0 1555064707236 6 connected 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380@16380 master - 0 1555064706000 2 connected 5795-10922 fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383@16383 slave d9a79ed6204e558b2fcee78ea05218b4de006acd 0 1555064708238 5 connected
6、集群節點縮減
縮減節點時先縮減從節點:
[root@localhost server41]# ./redis-cli --cluster del-node 127.0.0.1:6386 7c24e205301b38caa1ff3cd8b270a1ceb7249a2e >>> Removing node 7c24e205301b38caa1ff3cd8b270a1ceb7249a2e from cluster 127.0.0.1:6386 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node.
然後進行主節點slot轉移:
[root@localhost server41]# ./redis-cli --cluster reshard 127.0.0.1:6385 How many slots do you want to move (from 1 to 16384)? 1000 What is the receiving node ID? efa84a74525749b8ea20585074dda81b852e9c29 //要移到的節點 Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1: 22e8a8e97d6f7cc7d627e577a986384d4d181a4f //要刪除的主節點 Source node #2: done
最後在縮減主節點
[root@localhost server41]# ./redis-cli --cluster del-node 127.0.0.1:6385 22e8a8e97d6f7cc7d627e577a986384d4d181a4f >>> Removing node 22e8a8e97d6f7cc7d627e577a986384d4d181a4f from cluster 127.0.0.1:6385 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node. [root@localhost server41]# ./redis-cli 127.0.0.1:6379> cluster nodes efa84a74525749b8ea20585074dda81b852e9c29 127.0.0.1:6379@16379 myself,master - 0 1555066937000 8 connected 0-6250 10923-11710 d9a79ed6204e558b2fcee78ea05218b4de006acd 127.0.0.1:6381@16381 master - 0 1555066938698 3 connected 11711-16383 0469ec03b43e27dc2b7b4eb24de34e10969e3adf 127.0.0.1:6382@16382 slave 63e20c75984e493892265ddd2a441c81bcdc575c 0 1555066939700 4 connected ddebc3ca467d15c7d25125e4e16bcc5576a13699 127.0.0.1:6384@16384 slave efa84a74525749b8ea20585074dda81b852e9c29 0 1555066936693 8 connected 63e20c75984e493892265ddd2a441c81bcdc575c 127.0.0.1:6380@16380 master - 0 1555066937696 2 connected 6251-10922 fd8ea61503e7c9b6e950894c0da41aed3ee19e7e 127.0.0.1:6383@16383 slave d9a79ed6204e558b2fcee78ea05218b4de006acd 0 1555066937000 5 connected
縮減完成!