CentOS 7下 部署Redis-cluster集羣

redis集羣是一個無中心的分佈式redis存儲架構,能夠在多個節點之間進行數據共享,解決了redis高可用、可擴展等問題,redis集羣提供瞭如下兩個好處:
1)將數據自動切分(split)到多個節點
2)當集羣中的某一個節點故障時,redis還能夠繼續處理客戶端的請求 node

一個 Redis 集羣包含 16384 個哈希槽(hash slot),數據庫中的每一個數據都屬於這16384個哈希槽中的一個  集羣使用公式 CRC16(key) % 16384 來計算鍵 key 屬於哪一個槽 集羣中的每個節點負責處理一部分哈希槽
集羣中的主從複製
集羣中的每一個節點都有1個至N個複製品,其中一個爲主節點,其他的爲從節點,若是主節點下線了,集羣就會把這個主節點的一個從節點設置爲新的主節點,繼續工做 這樣集羣就不會由於一個主節點的下線而沒法正常工做linux

Redis Cluster集羣功能推出已經有一段時間了 在單機版的Redis中,每一個Master之間是沒有任何通訊的,因此通常在Jedis客戶端或者Codis這樣的代理中作Pre-sharding 按照CAP理論來講,單機版的Redis屬於保證CP(Consistency & Partition-Tolerancy)而犧牲A(Availability),也就說Redis可以保證全部用戶看到相同的數據(一致性,由於Redis不自動冗餘數據)和網絡通訊出問題時,暫時隔離開的子系統能繼續運行(分區容忍性,由於Master之間沒有直接關係,不須要通訊),可是不保證某些結點故障時,全部請求都能被響應(可用性,某個Master結點掛了的話,那麼它上面分片的數據就沒法訪問了) c++

有了Cluster功能後,Redis從一個單純的NoSQL內存數據庫變成了分佈式NoSQL數據庫,CAP模型也從CP變成了AP 也就是說,經過自動分片和冗餘數據,Redis具備了真正的分佈式能力,某個結點掛了的話,由於數據在其餘結點上有備份,因此其餘結點頂上來就能夠繼續提供服務,保證了Availability 然而,也正由於這一點,Redis沒法保證曾經的強一致性了 這也是CAP理論要求的,三者只能取其二 git

Redis Cluster 是Redis的集羣實現,內置數據自動分片機制,集羣內部將全部的key映射到16384個Slot中,集羣中的每一個Redis Instance負責其中的一部分的Slot的讀寫 集羣客戶端鏈接集羣中任一Redis Instance便可發送命令,當Redis Instance收到本身不負責的Slot的請求時,會將負責請求Key所在Slot的Redis Instance地址返回給客戶端,客戶端收到後自動將原請求從新發往這個地址,對外部透明 一個Key到底屬於哪一個Slot由crc16(key) % 16384 決定 在Redis Cluster裏對於負載均衡和HA相關都已經支持的至關完善了 redis

負載均衡(Load Balance):集羣的Redis Instance之間能夠遷移數據,以Slot爲單位,但不是自動的,須要外部命令觸發
集羣成員管理:集羣的節點(Redis Instance)和節點之間兩兩按期交換集羣內節點信息而且更新,從發送節點的角度看,這些信息包括:集羣內有哪些節點,IP和PORT是什麼,節點名字是什麼,節點的狀態(好比OK,PFAIL,FAIL,後面詳述)是什麼,包括節點角色(master 或者 slave)等
關於可用性,集羣由N組主從Redis Instance組成 算法

主能夠沒有從,可是沒有從 意味着主宕機後主負責的Slot讀寫服務不可用 數據庫

一個主能夠有多個從,主宕機時,某個從會被提高爲主,具體哪一個從被提高爲主,協議相似於Raft,參見這裏 如何檢測主宕機?Redis Cluster採用quorum+心跳的機制 從節點的角度看,節點會按期給其餘全部的節點發送Ping,cluster-node-timeout(可配置,秒級)時間內沒有收到對方的回覆,則單方面認爲對端節點宕機,將該節點標爲PFAIL狀態 經過節點之間交換信息收集到quorum個節點都認爲這個節點爲PFAIL,則將該節點標記爲FAIL,而且將其發送給其餘全部節點,其餘全部節點收到後當即認爲該節點宕機 從這裏能夠看出,主宕機後,至少cluster-node-timeout時間內該主所負責的Slot的讀寫服務不可用 vim

 

redis cluster集羣是爲了下降單節點或單純主從redis的壓力,主主節點之間是不存在同步關係的,各主從之間的數據存在同步關係 有多少主節點,就會把16384個哈希槽(hash slot)平均分配到這些主節點上當往redis裏寫入數據時,會根據哈希算法算出這個數的哈希槽,決定它放到哪個主節點上,而後這個主節點的從節點去自動同步 在客戶端隨便鏈接一個主節點便可,主節點之間會進行內部跳轉!當取對應數據時,各節點之間會自動跳轉到所取數據所在的主節點上!ruby

1)redis cluster節點分配
假設現有有三個主節點分別是:A、 B、C ,它們能夠是一臺機器上的三個端口,也能夠是三臺不一樣的服務器 那麼,採用哈希槽 (hash slot)的方式
來分配16384個slot 的話,它們三個節點分別承擔的slot 區間是:
節點A         覆蓋0-5460;
節點B         覆蓋5461-10922;
節點C        覆蓋10923-16383.

獲取數據:
若是存入一個值,按照redis cluster哈希槽的算法: CRC16('key')%16384 = 6782 那麼就會把這個key 的存儲分配到 B 上了 一樣,當我鏈接
(A,B,C)任何一個節點想獲取'key'這個key時,也會這樣的算法,而後內部跳轉到B節點上獲取數據 

新增一個主節點:
新增一個節點D,redis cluster的這種作法是從各個節點的前面各拿取一部分slot到D上,我會在接下來的實踐中實驗 大體就會變成這樣:
節點A         覆蓋1365-5460
節點B        覆蓋6827-10922
節點C       覆蓋12288-16383
節點D       覆蓋0-1364,5461-6826,10923-12287bash

一樣刪除一個節點也是相似,移動完成後就能夠刪除這個節點了

2)Redis Cluster主從模式
redis cluster 爲了保證數據的高可用性,加入了主從模式,一個主節點對應一個或多個從節點,主節點提供數據存取,從節點則是從主節點拉取數據
備份,當這個主節點掛掉後,就會有這個從節點選取一個來充當主節點,從而保證集羣不會掛掉

上面那個例子裏, 集羣有A、B、C三個主節點, 若是這3個節點都沒有加入從節點,若是B掛掉了,咱們就沒法訪問整個集羣了 A和C的slot也沒法訪問
因此在集羣創建的時候,必定要爲每一個主節點都添加了從節點, 好比像這樣, 集羣包含主節點A、B、C, 以及從節點A一、B一、C1, 那麼即便B掛掉系統也
能夠繼續正確工做 B1節點替代了B節點,因此Redis集羣將會選擇B1節點做爲新的主節點,集羣將會繼續正確地提供服務 當B從新開啓後,它就會變成B1的從節點

不過須要注意,若是節點B和B1同時掛了,Redis集羣就沒法繼續正確地提供服務了

因爲最小的redis集羣須要3個主節點(即Redis Cluster集羣至少須要3個master節點,也就是說至少須要6個節點才能構建Redis cluster集羣),一臺機器可運行多個redis實例(通常使用兩臺機器,每臺啓動3個redis實例,即三個主節點,三個從節點) 不少案例使用單臺服務器開6個端口,操做差很少,只是配置基本相對簡單點,多臺服務器更接近生產環境 【當集羣最開始建立好後,要記住各節點的主從關係(或是建立的時候指定主從關係);如果其中一臺機器重啓,重啓後,需從新將其加入到redis cluster集羣中;這就須要將這臺機器上的各節點以前的從節點變爲主節點(客戶端執行slaveof no one),而後再根據新的主節點添加這臺機器的各節點到集羣中,添加後變爲從節點】

環境 : CentOS7 

redis01 : 192.168.94.11 端口 : 7000、700一、7002

redis02 : 192.168.94.22 端口 : 700三、700四、7005

redis03 : 192.168.94.33 端口 : 700六、700七、7008

關閉SElinux和防火牆

安裝redis

[root@redis01 ~]# yum install -y gcc g++ make gcc-c++ kernel-devel automake autoconf libtool make wget tcl vim ruby rubygems unzip git
[root@redis01 ~]#  wget http://download.redis.io/releases/redis-4.0.1.tar.gz
[root@redis01 ~]# tar xf redis-4.0.1.tar.gz -C /usr/local/src/
[root@redis01 ~]# cd /usr/local/src/redis-4.0.1/
[root@redis01 redis-4.0.1]# make && make test && make PREFIX=/usr/local/redis install
[root@redis01 redis-4.0.1]# mkdir /usr/local/redis/conf
[root@redis01 redis-4.0.1]# cp *.conf /usr/local/redis/conf/
[root@redis01 redis-4.0.1]# cp /usr/local/src/redis-4.0.1/src/redis-trib.rb /usr/local/redis/bin/
[root@redis01 redis-4.0.1]# ln -s /usr/local/redis/bin/* /usr/local/bin/
# 進行操做系統基礎調優設置
[root@redis01 redis-4.0.1]# echo "* - nofile 10240" >> /etc/security/limits.conf 
[root@redis01 redis-4.0.1]# echo "net.core.somaxconn = 10240" >> /etc/sysctl.conf 
[root@redis01 redis-4.0.1]# echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf 
[root@redis01 redis-4.0.1]# sysctl -p
net.core.somaxconn = 10240
vm.overcommit_memory = 1
[root@redis01 redis-4.0.1]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@redis01 redis-4.0.1]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
[root@redis01 redis-4.0.1]# echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
[root@redis01 redis-4.0.1]# echo 'echo never > /sys/kernel/mm/transparent_hugepage/defrag' >> /etc/rc.local
[root@redis01 redis-4.0.1]# su -l

建立節點

# 建立集羣節點目錄
[root@redis01 ~]# mkdir /data/redis/redis-cluster -p
[root@redis01 redis-cluster]# cd /data/redis/redis-cluster/
[root@redis01 redis-cluster]# mkdir 7000 7001 7002
# 修改節點配置文件,爲了測試方便, 只作初步的簡單配置 [root@redis01 redis-cluster]# for i in 0 1 2; do echo -e "port 700$i\n\ bind `hostname -I`\n\ daemonize yes\n\ pidfile /var/run/redis_700$i.pid\n\ cluster-enabled yes\n\ cluster-config-file nodes_700$i.conf\n\ cluster-node-timeout 10100" > 700$i/redis.conf; done
cluster-enabled yes
bind 0.0.0.0
port 7000
pidfile /data/redis-cluster/7000/redis.pid
logfile "/data/redis-cluster/7000/redis.log"
dir /data/redis-cluster/7000/
tcp-backlog 1024
timeout 0
tcp-keepalive 0
daemonize yes
loglevel notice
databases 16
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite yes
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-entries 512
list-max-ziplist-value 64
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
詳細配置(僅供參考)
[root@redis01 redis-cluster]# tree 
.
├── 7000
│   └── redis.conf
├── 7001
│   └── redis.conf
└── 7002
    └── redis.conf

3 directories, 3 files

其餘節點作以上相同操做便可 , 只把目錄名按照端口號區別開便可

而後啓動集羣

# 第一個節點上啓動3個實例
for i in 0 1 2;
> do redis-server /data/redis/redis-cluster/700$i/redis.conf 
> done
68820:C 08 Oct 22:21:14.965 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
68820:C 08 Oct 22:21:14.965 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=68820, just started
68820:C 08 Oct 22:21:14.965 # Configuration loaded
68822:C 08 Oct 22:21:14.972 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
68822:C 08 Oct 22:21:14.972 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=68822, just started
68822:C 08 Oct 22:21:14.972 # Configuration loaded
68824:C 08 Oct 22:21:14.981 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
68824:C 08 Oct 22:21:14.981 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=68824, just started
68824:C 08 Oct 22:21:14.981 # Configuration loaded
# 第二個節點上啓動3個實例
[root@redis02 redis-cluster]# for i in 3 4 5;
> do redis-server /data/redis/redis-cluster/700$i/redis.conf 
> done
41766:C 08 Oct 22:21:42.369 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
41766:C 08 Oct 22:21:42.369 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=41766, just started
41766:C 08 Oct 22:21:42.369 # Configuration loaded
41768:C 08 Oct 22:21:42.375 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
41768:C 08 Oct 22:21:42.375 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=41768, just started
41768:C 08 Oct 22:21:42.375 # Configuration loaded
41770:C 08 Oct 22:21:42.380 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
41770:C 08 Oct 22:21:42.380 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=41770, just started
41770:C 08 Oct 22:21:42.380 # Configuration loaded
# 第三個節點上啓動3個實例
[root@redis03 redis-cluster]# for i in 6 7 8;
> do redis-server /data/redis/redis-cluster/700$i/redis.conf 
> done
41854:C 08 Oct 22:21:51.935 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
41854:C 08 Oct 22:21:51.935 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=41854, just started
41854:C 08 Oct 22:21:51.935 # Configuration loaded
41856:C 08 Oct 22:21:51.939 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
41856:C 08 Oct 22:21:51.939 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=41856, just started
41856:C 08 Oct 22:21:51.939 # Configuration loaded
41858:C 08 Oct 22:21:51.945 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
41858:C 08 Oct 22:21:51.945 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=41858, just started
41858:C 08 Oct 22:21:51.945 # Configuration loaded

檢查各Redis節點的啓動狀態

[root@redis01 redis-cluster]# netstat -antup|grep redis
tcp        0      0 192.168.94.11:17000     0.0.0.0:*               LISTEN      68821/redis-server  
tcp        0      0 192.168.94.11:17001     0.0.0.0:*               LISTEN      68823/redis-server  
tcp        0      0 192.168.94.11:17002     0.0.0.0:*               LISTEN      68828/redis-server  
tcp        0      0 192.168.94.11:7000      0.0.0.0:*               LISTEN      68821/redis-server  
tcp        0      0 192.168.94.11:7001      0.0.0.0:*               LISTEN      68823/redis-server  
tcp        0      0 192.168.94.11:7002      0.0.0.0:*               LISTEN      68828/redis-server 

安裝ruby

[root@redis01 redis-cluster]# yum -y install rh-ruby23
[root@redis01 redis-cluster]# ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
[root@redis01 redis-cluster]# scl enable rh-ruby23 bash
[root@redis01 redis-cluster]# ruby -v
ruby 2.3.6p384 (2017-12-14 revision 61254) [x86_64-linux]

安裝ruby的redis擴展

[root@redis01 redis-cluster]# gem install redis
Fetching: redis-4.0.2.gem (100%)
Successfully installed redis-4.0.2
Parsing documentation for redis-4.0.2
Installing ri documentation for redis-4.0.2
Done installing documentation for redis after 0 seconds
1 gem installed

使用redis自帶的集羣管理工具建立集羣

# 在任意一臺上運行便可,不要在每臺機器上都運行
[root@redis01 redis-cluster]# redis-trib.rb create --replicas 1 192.168.94.11:7000 192.168.94.11:7001 192.168.94.11:7002 192.168.94.22:7003 192.168.94.22:7004 192.168.94.22:7005 192.168.94.33:7006 192.168.94.33:7007 192.168.94.33:7008
>>> Creating cluster
>>> Performing hash slots allocation on 9 nodes...
Using 4 masters:
192.168.94.11:7000
192.168.94.22:7003
192.168.94.33:7006
192.168.94.11:7001
Adding replica 192.168.94.22:7004 to 192.168.94.11:7000
Adding replica 192.168.94.33:7007 to 192.168.94.22:7003
Adding replica 192.168.94.11:7002 to 192.168.94.33:7006
Adding replica 192.168.94.22:7005 to 192.168.94.11:7001
Adding replica 192.168.94.33:7008 to 192.168.94.11:7000
M: d8138e15d9cb69962abb7457125a7965b735a658 192.168.94.11:7000
   slots:0-4095 (4096 slots) master
M: 72f44da597f73c8d23c6d3fb60fa95489eb0168d 192.168.94.11:7001
   slots:12288-16383 (4096 slots) master
S: 711af78396c3ba966f751beef745ae10ff053a3f 192.168.94.11:7002
   replicates af2e1c600ed2511155a6cad61cabeb095affff86
M: 691428ae7c1728a76520b8f9d46fa6d83930f191 192.168.94.22:7003
   slots:4096-8191 (4096 slots) master
S: e752d865374c8ca4149737d81f19ac95cc9f6a44 192.168.94.22:7004
   replicates d8138e15d9cb69962abb7457125a7965b735a658
S: ba4ce74ba7b84bde9ea60cd8bf4c5996f1ca01e5 192.168.94.22:7005
   replicates 72f44da597f73c8d23c6d3fb60fa95489eb0168d
M: af2e1c600ed2511155a6cad61cabeb095affff86 192.168.94.33:7006
   slots:8192-12287 (4096 slots) master
S: b1e201b2acdc0a0740a764978436fa3946325a11 192.168.94.33:7007
   replicates 691428ae7c1728a76520b8f9d46fa6d83930f191
S: 6913f44486938c49eed6efa6f18c782df73e6456 192.168.94.33:7008
   replicates d8138e15d9cb69962abb7457125a7965b735a658
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.94.11:7000)
M: d8138e15d9cb69962abb7457125a7965b735a658 192.168.94.11:7000
   slots:0-4095 (4096 slots) master
   2 additional replica(s)
S: b1e201b2acdc0a0740a764978436fa3946325a11 192.168.94.33:7007
   slots: (0 slots) slave
   replicates 691428ae7c1728a76520b8f9d46fa6d83930f191
S: e752d865374c8ca4149737d81f19ac95cc9f6a44 192.168.94.22:7004
   slots: (0 slots) slave
   replicates d8138e15d9cb69962abb7457125a7965b735a658
M: 691428ae7c1728a76520b8f9d46fa6d83930f191 192.168.94.22:7003
   slots:4096-8191 (4096 slots) master
   1 additional replica(s)
M: af2e1c600ed2511155a6cad61cabeb095affff86 192.168.94.33:7006
   slots:8192-12287 (4096 slots) master
   1 additional replica(s)
M: 72f44da597f73c8d23c6d3fb60fa95489eb0168d 192.168.94.11:7001
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: 711af78396c3ba966f751beef745ae10ff053a3f 192.168.94.11:7002
   slots: (0 slots) slave
   replicates af2e1c600ed2511155a6cad61cabeb095affff86
S: ba4ce74ba7b84bde9ea60cd8bf4c5996f1ca01e5 192.168.94.22:7005
   slots: (0 slots) slave
   replicates 72f44da597f73c8d23c6d3fb60fa95489eb0168d
S: 6913f44486938c49eed6efa6f18c782df73e6456 192.168.94.33:7008
   slots: (0 slots) slave
   replicates d8138e15d9cb69962abb7457125a7965b735a658
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# 三個服務器啓動了9個實例,配置成4主5從,其中一個是一主兩從,其餘都是一主一從

集羣驗證

選項 -c 可鏈接集羣, 由於配置文件中bind改爲了ip因此必需要用 -h 選項,-p選項後接端口號

# 在redis01上的7000端口的實例上set一個key
[root@redis01 redis-cluster]# redis-cli -h 192.168.94.11 -c -p 7000
192.168.94.11:7000> set name weihan
-> Redirected to slot [5798] located at 192.168.94.22:7003
OK
192.168.94.22:7003> get name
"weihan"
# 由上可見 , redis01 set name 後,將數據重定向到了redis02的7003端口的實例上
# 在redis03的7008實例上get一個key
[root@redis01 redis-cluster]# redis-cli -h 192.168.94.33 -c -p 7008
192.168.94.33:7008> get name
-> Redirected to slot [5798] located at 192.168.94.22:7003
"weihan"
192.168.94.22:7003>
# get name 仍是會重定向到7003這個實例上 , 這說明redis cluster集羣已是可用的了

檢查集羣狀態(4個master會分配到4個服務器上, 並且每一個服務器上必須有一個master,建立集羣的時候能夠指定master,我建立的時候沒指定,是默認分配)

[root@redis01 redis-cluster]# redis-trib.rb check 192.168.94.11:7000
>>> Performing Cluster Check (using node 192.168.94.11:7000)
M: d8138e15d9cb69962abb7457125a7965b735a658 192.168.94.11:7000
   slots:0-4095 (4096 slots) master
   2 additional replica(s)
S: b1e201b2acdc0a0740a764978436fa3946325a11 192.168.94.33:7007
   slots: (0 slots) slave
   replicates 691428ae7c1728a76520b8f9d46fa6d83930f191
S: e752d865374c8ca4149737d81f19ac95cc9f6a44 192.168.94.22:7004
   slots: (0 slots) slave
   replicates d8138e15d9cb69962abb7457125a7965b735a658
M: 691428ae7c1728a76520b8f9d46fa6d83930f191 192.168.94.22:7003
   slots:4096-8191 (4096 slots) master
   1 additional replica(s)
M: af2e1c600ed2511155a6cad61cabeb095affff86 192.168.94.33:7006
   slots:8192-12287 (4096 slots) master
   1 additional replica(s)
M: 72f44da597f73c8d23c6d3fb60fa95489eb0168d 192.168.94.11:7001
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: 711af78396c3ba966f751beef745ae10ff053a3f 192.168.94.11:7002
   slots: (0 slots) slave
   replicates af2e1c600ed2511155a6cad61cabeb095affff86
S: ba4ce74ba7b84bde9ea60cd8bf4c5996f1ca01e5 192.168.94.22:7005
   slots: (0 slots) slave
   replicates 72f44da597f73c8d23c6d3fb60fa95489eb0168d
S: 6913f44486938c49eed6efa6f18c782df73e6456 192.168.94.33:7008
   slots: (0 slots) slave
   replicates d8138e15d9cb69962abb7457125a7965b735a658
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

列出集羣節點

[root@redis01 redis-cluster]# redis-cli -h 192.168.94.11 -c -p 7000
192.168.94.11:7000> CLUSTER NODES
b1e201b2acdc0a0740a764978436fa3946325a11 192.168.94.33:7007@17007 slave 691428ae7c1728a76520b8f9d46fa6d83930f191 0 1539016061737 8 connected
e752d865374c8ca4149737d81f19ac95cc9f6a44 192.168.94.22:7004@17004 slave d8138e15d9cb69962abb7457125a7965b735a658 0 1539016062000 5 connected
691428ae7c1728a76520b8f9d46fa6d83930f191 192.168.94.22:7003@17003 master - 0 1539016064655 4 connected 4096-8191
af2e1c600ed2511155a6cad61cabeb095affff86 192.168.94.33:7006@17006 master - 0 1539016062000 7 connected 8192-12287
72f44da597f73c8d23c6d3fb60fa95489eb0168d 192.168.94.11:7001@17001 master - 0 1539016063648 2 connected 12288-16383
711af78396c3ba966f751beef745ae10ff053a3f 192.168.94.11:7002@17002 slave af2e1c600ed2511155a6cad61cabeb095affff86 0 1539016063000 7 connected
d8138e15d9cb69962abb7457125a7965b735a658 192.168.94.11:7000@17000 myself,master - 0 1539016063000 1 connected 0-4095
ba4ce74ba7b84bde9ea60cd8bf4c5996f1ca01e5 192.168.94.22:7005@17005 slave 72f44da597f73c8d23c6d3fb60fa95489eb0168d 0 1539016063000 6 connected
6913f44486938c49eed6efa6f18c782df73e6456 192.168.94.33:7008@17008 slave d8138e15d9cb69962abb7457125a7965b735a658 0 1539016063000 9 connected

打印集羣信息

192.168.94.11:7000> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:9
cluster_size:4
cluster_current_epoch:9
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1240
cluster_stats_messages_pong_sent:1251
cluster_stats_messages_sent:2491
cluster_stats_messages_ping_received:1243
cluster_stats_messages_pong_received:1240
cluster_stats_messages_meet_received:8
cluster_stats_messages_received:2491

指定redis cluster中的master和slave 能夠用下面的方法

# 添加主庫
[root@redis01 redis-cluster]# redis-trib.rb create 192.168.94.11:7000 192.168.94.22:7003 192.168.94.33:7006
# 添加從庫
[root@redis01 redis-cluster]# redis-trib.rb add-node --slave 192.168.94.22:7001 192.168.94.11:7000    # 把192.168.94.11的7001 做爲從庫添加到192.168.94.11的7000
相關文章
相關標籤/搜索