您好,登錄后才能下訂單哦!
這篇文章主要介紹Redis集群的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Redis集群詳解
Redis有三種集群模式,分別是:
* 主從模式 * Sentinel模式 * Cluster模式
三種集群模式各有特點,關于Redis介紹可以參考這里:NoSQL(二)——Redis
Redis官網:https://redis.io/ ,最新版本5.0.4
主從模式
主從模式介紹
主從模式是三種模式中最簡單的,在主從復制中,數據庫分為兩類:主數據庫(master)和從數據庫(slave)。
其中主從復制有如下特點:
* 主數據庫可以進行讀寫操作,當讀寫操作導致數據變化時會自動將數據同步給從數據庫 * 從數據庫一般都是只讀的,并且接收主數據庫同步過來的數據 * 一個master可以擁有多個slave,但是一個slave只能對應一個master * slave掛了不影響其他slave的讀和master的讀和寫,重新啟動后會將數據從master同步過來 * master掛了以后,不影響slave的讀,但redis不再提供寫服務,master重啟后redis將重新對外提供寫服務 * master掛了以后,不會在slave節點中重新選一個master
工作機制:
當slave啟動后,主動向master發送SYNC命令。master接收到SYNC命令后在后臺保存快照(RDB持久化)和緩存保存快照這段時間的命令,然后將保存的快照文件和緩存的命令發送給slave。slave接收到快照文件和命令后加載快照文件和緩存的執行命令。
復制初始化后,master每次接收到的寫命令都會同步發送給slave,保證主從數據一致性。
安全設置:
當master節點設置密碼后,
客戶端訪問master需要密碼 啟動slave需要密碼,在配置文件中配置即可 客戶端訪問slave不需要密碼
缺點:
從上面可以看出,master節點在主從模式中唯一,若master掛掉,則redis無法對外提供寫服務。
環境準備:
master節點 192.168.30.128 slave節點 192.168.30.129 slave節點 192.168.30.130
全部下載安裝:
# cd /software# wget http://download.redis.io/releases/redis-5.0.4.tar.gz# tar zxf redis-5.0.4.tar.gz && mv redis-5.0.4/ /usr/local/redis# cd /usr/local/redis && make && make install# echo $?0
全部配置成服務:
服務文件
# vim /usr/lib/systemd/system/redis.service[Unit]Description=Redis persistent key-value database After=network.target After=network-online.target Wants=network-online.target[Service]ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis.conf --supervised systemd ExecStop=/usr/libexec/redis-shutdown Type=notify User=redis Group=redis RuntimeDirectory=redis RuntimeDirectoryMode=0755[Install]WantedBy=multi-user.target
shutdown腳本
# vim /usr/libexec/redis-shutdown#!/bin/bash## Wrapper to close properly redis and sentineltest x"$REDIS_DEBUG" != x && set -x REDIS_CLI=/usr/local/bin/redis-cli# Retrieve service nameSERVICE_NAME="$1"if [ -z "$SERVICE_NAME" ]; then SERVICE_NAME=redisfi# Get the proper config file based on service nameCONFIG_FILE="/usr/local/redis/$SERVICE_NAME.conf"# Use awk to retrieve host, port from config fileHOST=`awk '/^[[:blank:]]*bind/ { print $2 }' $CONFIG_FILE | tail -n1`PORT=`awk '/^[[:blank:]]*port/ { print $2 }' $CONFIG_FILE | tail -n1`PASS=`awk '/^[[:blank:]]*requirepass/ { print $2 }' $CONFIG_FILE | tail -n1`SOCK=`awk '/^[[:blank:]]*unixsocket\s/ { print $2 }' $CONFIG_FILE | tail -n1`# Just in case, use default host, portHOST=${HOST:-127.0.0.1}if [ "$SERVICE_NAME" = redis ]; then PORT=${PORT:-6379}else PORT=${PORT:-26739}fi# Setup additional parameters# e.g password-protected redis instances[ -z "$PASS" ] || ADDITIONAL_PARAMS="-a $PASS"# shutdown the service properlyif [ -e "$SOCK" ] ; then $REDIS_CLI -s $SOCK $ADDITIONAL_PARAMS shutdownelse $REDIS_CLI -h $HOST -p $PORT $ADDITIONAL_PARAMS shutdownfi
# chmod +x /usr/libexec/redis-shutdown# useradd -s /sbin/nologin redis# chown -R redis:redis /usr/local/redis# chown -R redis:redis /data/redis# yum install -y bash-completion && source /etc/profile #命令補全# systemctl daemon-reload# systemctl enable redis
修改配置:
192.168.30.128
# mkdir -p /data/redis# vim /usr/local/redis/redis.confbind 192.168.30.128 #監聽ip,多個ip用空格分隔daemonize yes #允許后臺啟動logfile "/usr/local/redis/redis.log" #日志路徑dir /data/redis #數據庫備份文件存放目錄masterauth 123456 #slave連接master密碼,master可省略requirepass 123456 #設置master連接密碼,slave可省略appendonly yes #在/data/redis/目錄生成appendonly.aof文件,將每一次寫操作請求都追加到appendonly.aof 文件中# echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf# sysctl -p
192.168.30.129
# mkdir -p /data/redis# vim /usr/local/redis/redis.confbind 192.168.30.129 daemonize yeslogfile "/usr/local/redis/redis.log"dir /data/redis replicaof 192.168.30.128 6379 masterauth 123456 requirepass 123456 appendonly yes# echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf# sysctl -p
192.168.30.130
# mkdir -p /data/redis# vim /usr/local/redis/redis.confbind 192.168.30.130 daemonize yeslogfile "/usr/local/redis/redis.log"dir /data/redis replicaof 192.168.30.128 6379 masterauth 123456 requirepass 123456 appendonly yes# echo 'vm.overcommit_memory=1' >> /etc/sysctl.conf# sysctl -p
全部啟動redis:
# systemctl start redis
查看集群狀態:
# redis-cli -h 192.168.30.128 -a 123456Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.30.128:6379> info replication# Replicationrole:master connected_slaves:2 slave0:ip=192.168.30.129,port=6379,state=online,offset=168,lag=1 slave1:ip=192.168.30.130,port=6379,state=online,offset=168,lag=1 master_replid:fb4941e02d5032ad74c6e2383211fc58963dbe90 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:168 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:168
# redis-cli -h 192.168.30.129 -a 123456 info replicationWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.# Replicationrole:slave master_host:192.168.30.128 master_port:6379 master_link_status:up master_last_io_seconds_ago:1 master_sync_in_progress:0 slave_repl_offset:196 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:fb4941e02d5032ad74c6e2383211fc58963dbe90 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:196 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:196
數據演示:
192.168.30.128:6379> keys *(empty list or set)192.168.30.128:6379> set key1 100 OK 192.168.30.128:6379> set key2 lzx OK 192.168.30.128:6379> keys * 1) "key1"2) "key2"
# redis-cli -h 192.168.30.129 -a 123456Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.30.129:6379> keys * 1) "key2"2) "key1"192.168.30.129:6379> CONFIG GET dir1) "dir"2) "/data/redis"192.168.30.129:6379> CONFIG GET dbfilename 1) "dbfilename"2) "dump.rdb"192.168.30.129:6379> get key1"100"192.168.30.129:6379> get key2"lzx"192.168.30.129:6379> set key3 aaa(error) READONLY You can't write against a read only replica.
# redis-cli -h 192.168.30.130 -a 123456Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.30.130:6379> keys * 1) "key2"2) "key1"192.168.30.130:6379> CONFIG GET dir1) "dir"2) "/data/redis"192.168.30.130:6379> CONFIG GET dbfilename 1) "dbfilename"2) "dump.rdb"192.168.30.130:6379> get key1"100"192.168.30.130:6379> get key2"lzx"192.168.30.130:6379> set key3 aaa(error) READONLY You can't write against a read only replica.
可以看到,在master節點寫入的數據,很快就同步到slave節點上,而且在slave節點上無法寫入數據。
主從模式的弊端就是不具備高可用性,當master掛掉以后,Redis將不能再對外提供寫入操作,因此sentinel應運而生。
sentinel中文含義為哨兵,顧名思義,它的作用就是監控redis集群的運行狀況,特點如下:
* sentinel模式是建立在主從模式的基礎上,如果只有一個Redis節點,sentinel就沒有任何意義 * 當master掛了以后,sentinel會在slave中選擇一個做為master,并修改它們的配置文件,其他slave的配置文件也會被修改,比如slaveof屬性會指向新的master * 當master重新啟動后,它將不再是master而是做為slave接收新的master的同步數據 * sentinel因為也是一個進程有掛掉的可能,所以sentinel也會啟動多個形成一個sentinel集群 * 多sentinel配置的時候,sentinel之間也會自動監控 * 當主從模式配置密碼時,sentinel也會同步將配置信息修改到配置文件中,不需要擔心 * 一個sentinel或sentinel集群可以管理多個主從Redis,多個sentinel也可以監控同一個redis * sentinel最好不要和Redis部署在同一臺機器,不然Redis的服務器掛了以后,sentinel也掛了
工作機制:
* 每個sentinel以每秒鐘一次的頻率向它所知的master,slave以及其他sentinel實例發送一個 PING 命令 * 如果一個實例距離最后一次有效回復 PING 命令的時間超過 down-after-milliseconds 選項所指定的值, 則這個實例會被sentinel標記為主觀下線。 * 如果一個master被標記為主觀下線,則正在監視這個master的所有sentinel要以每秒一次的頻率確認master的確進入了主觀下線狀態 * 當有足夠數量的sentinel(大于等于配置文件指定的值)在指定的時間范圍內確認master的確進入了主觀下線狀態, 則master會被標記為客觀下線 * 在一般情況下, 每個sentinel會以每 10 秒一次的頻率向它已知的所有master,slave發送 INFO 命令 * 當master被sentinel標記為客觀下線時,sentinel向下線的master的所有slave發送 INFO 命令的頻率會從 10 秒一次改為 1 秒一次 * 若沒有足夠數量的sentinel同意master已經下線,master的客觀下線狀態就會被移除; 若master重新向sentinel的 PING 命令返回有效回復,master的主觀下線狀態就會被移除
當使用sentinel模式的時候,客戶端就不要直接連接Redis,而是連接sentinel的ip和port,由sentinel來提供具體的可提供服務的Redis實現,這樣當master節點掛掉以后,sentinel就會感知并將新的master節點提供給使用者。
環境準備:
master節點 192.168.30.128 sentinel端口:26379 slave節點 192.168.30.129 sentinel端口:26379 slave節點 192.168.30.130 sentinel端口:26379
修改配置:
前面已經下載安裝了redis,這里省略,直接修改sentinel配置文件。
192.168.30.128
# vim /usr/local/redis/sentinel.confdaemonize yeslogfile "/usr/local/redis/sentinel.log"dir "/usr/local/redis/sentinel" #sentinel工作目錄sentinel monitor mymaster 192.168.30.128 6379 2 #判斷master失效至少需要2個sentinel同意,建議設置為n/2+1,n為sentinel個數sentinel auth-pass mymaster 123456 sentinel down-after-milliseconds mymaster 30000 #判斷master主觀下線時間,默認30s
這里需要注意,sentinel auth-pass mymaster 123456
需要配置在sentinel monitor mymaster 192.168.30.128 6379 2
下面,否則啟動報錯:
# /usr/local/bin/redis-sentinel /usr/local/redis/sentinel.conf*** FATAL CONFIG FILE ERROR *** Reading the configuration file, at line 104>>> 'sentinel auth-pass mymaster 123456'No such master with specified name.
全部啟動sentinel:
# mkdir /usr/local/redis/sentinel && chown -R redis:redis /usr/local/redis# /usr/local/bin/redis-sentinel /usr/local/redis/sentinel.conf
任一主機查看日志:
# tail -f /usr/local/redis/sentinel.log21574:X 09 May 2019 15:32:04.298 # Sentinel ID is 30c417116a8edbab09708037366c4a7471beb77021574:X 09 May 2019 15:32:04.298 # +monitor master mymaster 192.168.30.128 6379 quorum 221574:X 09 May 2019 15:32:04.299 * +slave slave 192.168.30.129:6379 192.168.30.129 6379 @ mymaster 192.168.30.128 6379 21574:X 09 May 2019 15:32:04.300 * +slave slave 192.168.30.130:6379 192.168.30.130 6379 @ mymaster 192.168.30.128 6379 21574:X 09 May 2019 15:32:16.347 * +sentinel sentinel 79b8d61626afd4d059fb5a6a63393e9a1374e78f 192.168.30.129 26379 @ mymaster 192.168.30.128 6379 21574:X 09 May 2019 15:32:31.584 * +sentinel sentinel d7b429dcba792103ef0d80827dd0910bd9284d21 192.168.30.130 26379 @ mymaster 192.168.30.128 6379
Sentinel模式下的幾個事件:
· +reset-master :主服務器已被重置。 · +slave :一個新的從服務器已經被 Sentinel 識別并關聯。 · +failover-state-reconf-slaves :故障轉移狀態切換到了 reconf-slaves 狀態。 · +failover-detected :另一個 Sentinel 開始了一次故障轉移操作,或者一個從服務器轉換成了主服務器。 · +slave-reconf-sent :領頭(leader)的 Sentinel 向實例發送了 [SLAVEOF](/commands/slaveof.html) 命令,為實例設置新的主服務器。 · +slave-reconf-inprog :實例正在將自己設置為指定主服務器的從服務器,但相應的同步過程仍未完成。 · +slave-reconf-done :從服務器已經成功完成對新主服務器的同步。 · -dup-sentinel :對給定主服務器進行監視的一個或多個 Sentinel 已經因為重復出現而被移除 —— 當 Sentinel 實例重啟的時候,就會出現這種情況。 · +sentinel :一個監視給定主服務器的新 Sentinel 已經被識別并添加。 · +sdown :給定的實例現在處于主觀下線狀態。 · -sdown :給定的實例已經不再處于主觀下線狀態。 · +odown :給定的實例現在處于客觀下線狀態。 · -odown :給定的實例已經不再處于客觀下線狀態。 · +new-epoch :當前的紀元(epoch)已經被更新。 · +try-failover :一個新的故障遷移操作正在執行中,等待被大多數 Sentinel 選中(waiting to be elected by the majority)。 · +elected-leader :贏得指定紀元的選舉,可以進行故障遷移操作了。 · +failover-state-select-slave :故障轉移操作現在處于 select-slave 狀態 —— Sentinel 正在尋找可以升級為主服務器的從服務器。 · no-good-slave :Sentinel 操作未能找到適合進行升級的從服務器。Sentinel 會在一段時間之后再次嘗試尋找合適的從服務器來進行升級,又或者直接放棄執行故障轉移操作。 · selected-slave :Sentinel 順利找到適合進行升級的從服務器。 · failover-state-send-slaveof-noone :Sentinel 正在將指定的從服務器升級為主服務器,等待升級功能完成。 · failover-end-for-timeout :故障轉移因為超時而中止,不過最終所有從服務器都會開始復制新的主服務器(slaves will eventually be configured to replicate with the new master anyway)。 · failover-end :故障轉移操作順利完成。所有從服務器都開始復制新的主服務器了。 · +switch-master :配置變更,主服務器的 IP 和地址已經改變。 這是絕大多數外部用戶都關心的信息。 · +tilt :進入 tilt 模式。 · -tilt :退出 tilt 模式。
master宕機演示:
192.168.30.128
# systemctl stop redis# tail -f /usr/local/redis/sentinel.log22428:X 09 May 2019 15:51:29.287 # +sdown master mymaster 192.168.30.128 637922428:X 09 May 2019 15:51:29.371 # +odown master mymaster 192.168.30.128 6379 #quorum 2/222428:X 09 May 2019 15:51:29.371 # +new-epoch 122428:X 09 May 2019 15:51:29.371 # +try-failover master mymaster 192.168.30.128 637922428:X 09 May 2019 15:51:29.385 # +vote-for-leader 30c417116a8edbab09708037366c4a7471beb770 122428:X 09 May 2019 15:51:29.403 # d7b429dcba792103ef0d80827dd0910bd9284d21 voted for 30c417116a8edbab09708037366c4a7471beb770 122428:X 09 May 2019 15:51:29.408 # 79b8d61626afd4d059fb5a6a63393e9a1374e78f voted for 30c417116a8edbab09708037366c4a7471beb770 122428:X 09 May 2019 15:51:29.451 # +elected-leader master mymaster 192.168.30.128 637922428:X 09 May 2019 15:51:29.451 # +failover-state-select-slave master mymaster 192.168.30.128 637922428:X 09 May 2019 15:51:29.528 # +selected-slave slave 192.168.30.129:6379 192.168.30.129 6379 @ mymaster 192.168.30.128 637922428:X 09 May 2019 15:51:29.528 * +failover-state-send-slaveof-noone slave 192.168.30.129:6379 192.168.30.129 6379 @ mymaster 192.168.30.128 6379 22428:X 09 May 2019 15:51:29.594 * +failover-state-wait-promotion slave 192.168.30.129:6379 192.168.30.129 6379 @ mymaster 192.168.30.128 6379 22428:X 09 May 2019 15:51:30.190 # +promoted-slave slave 192.168.30.129:6379 192.168.30.129 6379 @ mymaster 192.168.30.128 637922428:X 09 May 2019 15:51:30.190 # +failover-state-reconf-slaves master mymaster 192.168.30.128 637922428:X 09 May 2019 15:51:30.258 * +slave-reconf-sent slave 192.168.30.130:6379 192.168.30.130 6379 @ mymaster 192.168.30.128 6379 22428:X 09 May 2019 15:51:30.511 # -odown master mymaster 192.168.30.128 637922428:X 09 May 2019 15:51:31.233 * +slave-reconf-inprog slave 192.168.30.130:6379 192.168.30.130 6379 @ mymaster 192.168.30.128 6379 22428:X 09 May 2019 15:51:31.233 * +slave-reconf-done slave 192.168.30.130:6379 192.168.30.130 6379 @ mymaster 192.168.30.128 6379 22428:X 09 May 2019 15:51:31.297 # +failover-end master mymaster 192.168.30.128 637922428:X 09 May 2019 15:51:31.297 # +switch-master mymaster 192.168.30.128 6379 192.168.30.129 637922428:X 09 May 2019 15:51:31.298 * +slave slave 192.168.30.130:6379 192.168.30.130 6379 @ mymaster 192.168.30.129 6379 22428:X 09 May 2019 15:51:31.298 * +slave slave 192.168.30.128:6379 192.168.30.128 6379 @ mymaster 192.168.30.129 6379 22428:X 09 May 2019 15:52:31.307 # +sdown slave 192.168.30.128:6379 192.168.30.128 6379 @ mymaster 192.168.30.129 6379
從日志中可以看到,master已經從192.168.30.128轉移到192.168.30.129上
192.168.30.129上查看集群信息
# /usr/local/bin/redis-cli -h 192.168.30.129 -p 6379 -a 123456Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.30.129:6379> info replication# Replicationrole:master connected_slaves:1 slave0:ip=192.168.30.130,port=6379,state=online,offset=291039,lag=1 master_replid:757aff269236ed2707ba584a86a40716c1c76d74 master_replid2:47a862fc0ff20362be29096ecdcca6d432070ee9 master_repl_offset:291182 second_repl_offset:248123 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:291182 192.168.30.129:6379> set key4 linux OK
當前集群中只有一個slave——192.168.30.130,master是192.168.30.129,且192.168.30.129具有寫權限。
192.168.30.130上查看redis的配置文件也可以看到replicaof 192.168.30.129 6379
,這是sentinel在選舉master是做的修改。
重新把192.168.30.128上進程啟動
# systemctl start redis# tail -f /usr/local/redis/sentinel.log22428:X 09 May 2019 15:51:31.297 # +switch-master mymaster 192.168.30.128 6379 192.168.30.129 637922428:X 09 May 2019 15:51:31.298 * +slave slave 192.168.30.130:6379 192.168.30.130 6379 @ mymaster 192.168.30.129 6379 22428:X 09 May 2019 15:51:31.298 * +slave slave 192.168.30.128:6379 192.168.30.128 6379 @ mymaster 192.168.30.129 6379 22428:X 09 May 2019 15:52:31.307 # +sdown slave 192.168.30.128:6379 192.168.30.128 6379 @ mymaster 192.168.30.129 637922428:X 09 May 2019 16:01:24.872 # -sdown slave 192.168.30.128:6379 192.168.30.128 6379 @ mymaster 192.168.30.129 6379
查看集群信息
# /usr/local/bin/redis-cli -h 192.168.30.128 -p 6379 -a 123456Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.30.128:6379> info replication# Replicationrole:slave master_host:192.168.30.129 master_port:6379 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:514774 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:757aff269236ed2707ba584a86a40716c1c76d74 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:514774 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:376528 repl_backlog_histlen:138247 192.168.30.128:6379> get key4"linux"192.168.30.128:6379> set key5(error) ERR wrong number of arguments for 'set' command
即使192.168.30.128重新啟動redis服務,也是作為slave加入redis集群,192.168.30.129仍然是master。
sentinel模式基本可以滿足一般生產的需求,具備高可用性。但是當數據量過大到一臺服務器存放不下的情況時,主從模式或sentinel模式就不能滿足需求了,這個時候需要對存儲的數據進行分片,將數據存儲到多個Redis實例中。cluster模式的出現就是為了解決單機Redis容量有限的問題,將Redis的數據根據一定的規則分配到多臺機器。
cluster可以說是sentinel和主從模式的結合體,通過cluster可以實現主從和master重選功能,所以如果配置兩個副本三個分片的話,就需要六個Redis實例。因為Redis的數據是根據一定規則分配到cluster的不同機器的,當數據量過大時,可以新增機器進行擴容。
使用集群,只需要將redis配置文件中的cluster-enable
配置打開即可。每個集群中至少需要三個主數據庫才能正常運行,新增節點非常方便。
cluster集群特點:
* 多個redis節點網絡互聯,數據共享 * 所有的節點都是一主一從(也可以是一主多從),其中從不提供服務,僅作為備用 * 不支持同時處理多個key(如MSET/MGET),因為redis需要把key均勻分布在各個節點上, 并發量很高的情況下同時創建key-value會降低性能并導致不可預測的行為 * 支持在線增加、刪除節點 * 客戶端可以連接任何一個主節點進行讀寫
環境準備:
三臺機器,分別開啟兩個redis服務(端口) 192.168.30.128 端口:7001,7002 192.168.30.129 端口:7003,7004 192.168.30.130 端口:7005,7006
修改配置文件:
192.168.30.128
# mkdir /usr/local/redis/cluster# cp /usr/local/redis/redis.conf /usr/local/redis/cluster/redis_7001.conf# cp /usr/local/redis/redis.conf /usr/local/redis/cluster/redis_7002.conf# chown -R redis:redis /usr/local/redis# mkdir -p /data/redis/cluster/{redis_7001,redis_7002} && chown -R redis:redis /data/redis
# vim /usr/local/redis/cluster/redis_7001.confbind 192.168.30.128 port 7001 daemonize yespidfile "/var/run/redis_7001.pid"logfile "/usr/local/redis/cluster/redis_7001.log"dir "/data/redis/cluster/redis_7001"#replicaof 192.168.30.129 6379masterauth 123456 requirepass 123456 appendonly yescluster-enabled yescluster-config-file nodes_7001.conf cluster-node-timeout 15000
# vim /usr/local/redis/cluster/redis_7002.confbind 192.168.30.128 port 7002 daemonize yespidfile "/var/run/redis_7002.pid"logfile "/usr/local/redis/cluster/redis_7002.log"dir "/data/redis/cluster/redis_7002"#replicaof 192.168.30.129 6379masterauth "123456"requirepass "123456"appendonly yescluster-enabled yescluster-config-file nodes_7002.conf cluster-node-timeout 15000
其它兩臺機器配置與192.168.30.128一致,此處省略
啟動redis服務:
# redis-server /usr/local/redis/cluster/redis_7001.conf# tail -f /usr/local/redis/cluster/redis_7001.log# redis-server /usr/local/redis/cluster/redis_7002.conf# tail -f /usr/local/redis/cluster/redis_7002.log
其它兩臺機器啟動與192.168.30.128一致,此處省略
安裝ruby并創建集群(低版本):
如果redis版本比較低,則需要安裝ruby。任選一臺機器安裝ruby即可
# yum -y groupinstall "Development Tools"# yum install -y gdbm-devel libdb4-devel libffi-devel libyaml libyaml-devel ncurses-devel openssl-devel readline-devel tcl-devel# mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}# wget http://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz -P ~/rpmbuild/SOURCES# wget http://raw.githubusercontent.com/tjinjin/automate-ruby-rpm/master/ruby22x.spec -P ~/rpmbuild/SPECS# rpmbuild -bb ~/rpmbuild/SPECS/ruby22x.spec# rpm -ivh ~/rpmbuild/RPMS/x86_64/ruby-2.2.3-1.el7.x86_64.rpm# gem install redis #目的是安裝這個,用于配置集群
# cp /usr/local/redis/src/redis-trib.rb /usr/bin/# redis-trib.rb create --replicas 1 192.168.30.128:7001 192.168.30.128:7002 192.168.30.129:7003 192.168.30.129:7004 192.168.30.130:7005 192.168.30.130:7006
創建集群:
我這里是redis5.0.4,所以不需要安裝ruby,直接創建集群即可
# redis-cli -a 123456 --cluster create 192.168.30.128:7001 192.168.30.128:7002 192.168.30.129:7003 192.168.30.129:7004 192.168.30.130:7005 192.168.30.130:7006 --cluster-replicas 1Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 Adding replica 192.168.30.129:7004 to 192.168.30.128:7001 Adding replica 192.168.30.130:7006 to 192.168.30.129:7003 Adding replica 192.168.30.128:7002 to 192.168.30.130:7005 M: 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001 slots:[0-5460] (5461 slots) master S: b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002 replicates 6788453ee9a8d7f72b1d45a9093838efd0e501f1 M: 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003 slots:[5461-10922] (5462 slots) master S: b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004 replicates 80c80a3f3e33872c047a8328ad579b9bea001ad8 M: 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005 slots:[10923-16383] (5461 slots) master S: 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006 replicates 4d74ec66e898bf09006dac86d4928f9fad81f373 Can I set the above configuration? (type 'yes' to accept): yes #輸入yes,接受上面配置>>> Nodes configuration updated>>> Assign a different config epoch to each node>>> Sending CLUSTER MEET messages to join the cluster
可以看到,
192.168.30.128:7001是master,它的slave是192.168.30.129:7004; 192.168.30.129:7003是master,它的slave是192.168.30.130:7006; 192.168.30.130:7005是master,它的slave是192.168.30.128:7002
自動生成nodes.conf文件:
# ls /data/redis/cluster/redis_7001/appendonly.aof dump.rdb nodes-7001.conf# vim /data/redis/cluster/redis_7001/nodes-7001.conf 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557454406312 5 connected 10923-16383 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557454407000 6 connected b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557454408371 5 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 myself,master - 0 1557454406000 1 connected 0-5460 b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557454407366 4 connected 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557454407000 3 connected 5461-10922 vars currentEpoch 6 lastVoteEpoch 0
登錄集群:
# redis-cli -c -h 192.168.30.128 -p 7001 -a 123456 # -c,使用集群方式登錄
查看集群信息:
192.168.30.128:7001> CLUSTER INFO #集群狀態cluster_state:ok cluster_slots_assigned:16384 cluster_slots_ok:16384 cluster_slots_pfail:0 cluster_slots_fail:0 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:6 cluster_my_epoch:1 cluster_stats_messages_ping_sent:580 cluster_stats_messages_pong_sent:551 cluster_stats_messages_sent:1131 cluster_stats_messages_ping_received:546 cluster_stats_messages_pong_received:580 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:1131
列出節點信息:
192.168.30.128:7001> CLUSTER NODES #列出節點信息6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557455176000 5 connected 10923-16383 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557455174000 6 connected b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557455175000 5 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 myself,master - 0 1557455175000 1 connected 0-5460 b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557455174989 4 connected 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557455175995 3 connected 5461-10922
這里與nodes.conf文件內容相同
寫入數據:
192.168.30.128:7001> set key111 aaa -> Redirected to slot [13680] located at 192.168.30.130:7005 #說明數據到了192.168.30.130:7005上OK 192.168.30.130:7005> set key222 bbb -> Redirected to slot [2320] located at 192.168.30.128:7001 #說明數據到了192.168.30.128:7001上OK 192.168.30.128:7001> set key333 ccc -> Redirected to slot [7472] located at 192.168.30.129:7003 #說明數據到了192.168.30.129:7003上OK 192.168.30.129:7003> get key111 -> Redirected to slot [13680] located at 192.168.30.130:7005"aaa"192.168.30.130:7005> get key333 -> Redirected to slot [7472] located at 192.168.30.129:7003"ccc"192.168.30.129:7003>
可以看出redis cluster集群是去中心化的,每個節點都是平等的,連接哪個節點都可以獲取和設置數據。
當然,平等指的是master節點,因為slave節點根本不提供服務,只是作為對應master節點的一個備份。
增加節點:
192.168.30.129上增加一節點:
# cp /usr/local/redis/cluster/redis_7003.conf /usr/local/redis/cluster/redis_7007.conf# vim /usr/local/redis/cluster/redis_7007.confbind 192.168.30.129 port 7007 daemonize yespidfile "/var/run/redis_7007.pid"logfile "/usr/local/redis/cluster/redis_7007.log"dir "/data/redis/cluster/redis_7007"#replicaof 192.168.30.129 6379masterauth "123456"requirepass "123456"appendonly yescluster-enabled yescluster-config-file nodes_7007.conf cluster-node-timeout 15000# mkdir /data/redis/cluster/redis_7007# chown -R redis:redis /usr/local/redis && chown -R redis:redis /data/redis# redis-server /usr/local/redis/cluster/redis_7007.conf
192.168.30.130上增加一節點:
# cp /usr/local/redis/cluster/redis_7005.conf /usr/local/redis/cluster/redis_7008.conf# vim /usr/local/redis/cluster/redis_7007.confbind 192.168.30.130 port 7008 daemonize yespidfile "/var/run/redis_7008.pid"logfile "/usr/local/redis/cluster/redis_7008.log"dir "/data/redis/cluster/redis_7008"#replicaof 192.168.30.130 6379masterauth "123456"requirepass "123456"appendonly yescluster-enabled yescluster-config-file nodes_7008.conf cluster-node-timeout 15000# mkdir /data/redis/cluster/redis_7008# chown -R redis:redis /usr/local/redis && chown -R redis:redis /data/redis# redis-server /usr/local/redis/cluster/redis_7008.conf
集群中增加節點:
192.168.30.129:7003> CLUSTER MEET 192.168.30.129 7007 OK 192.168.30.129:7003> CLUSTER NODES 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 myself,master - 0 1557457361000 3 connected 5461-10922 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 master - 0 1557457364746 1 connected 0-5460 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557457362000 6 connected b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557457363000 4 connected b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557457362000 5 connected e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557457362729 0 connected 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557457363739 5 connected 10923-16383
192.168.30.129:7003> CLUSTER MEET 192.168.30.130 7008 OK 192.168.30.129:7003> CLUSTER NODES 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 myself,master - 0 1557457489000 3 connected 5461-10922 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 master - 0 1557457489000 1 connected 0-5460 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557457489000 6 connected b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557457488000 4 connected b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557457489472 5 connected 1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:7008@17008 master - 0 1557457489259 0 connected e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557457489000 0 connected 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557457490475 5 connected 10923-16383
可以看到,新增的節點都是以master身份加入集群的
更換節點身份:
將新增的192.168.30.130:7008節點身份改為192.168.30.129:7007的slave
# redis-cli -c -h 192.168.30.130 -p 7008 -a 123456 cluster replicate e51ab166bc0f33026887bcf8eba0dff3d5b0bf14
cluster replicate
后面跟node_id,更改對應節點身份。也可以登入集群更改
# redis-cli -c -h 192.168.30.130 -p 7008 -a 123456192.168.30.130:7008> CLUSTER REPLICATE e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 OK 192.168.30.130:7008> CLUSTER NODES 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557458316881 3 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 master - 0 1557458314864 1 connected 0-5460 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557458316000 3 connected 5461-10922 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557458315872 5 connected 10923-16383 b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557458317890 5 connected 1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:7008@17008 myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557458315000 7 connected b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557458315000 1 connected e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557458314000 0 connected
查看相應的nodes.conf文件,可以發現有更改,它記錄當前集群的節點信息
# cat /data/redis/cluster/redis_7001/nodes-7001.conf1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:7008@17008 slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557458236169 7 connected 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557458235000 5 connected 10923-16383 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557458234103 6 connected b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557458235129 5 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 myself,master - 0 1557458234000 1 connected 0-5460 b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557458236000 4 connected e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557458236000 0 connected 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557458233089 3 connected 5461-10922 vars currentEpoch 7 lastVoteEpoch 0
刪除節點:
192.168.30.130:7008> CLUSTER FORGET 1a1c7f02fce87530bd5abdfc98df1cffce4f1767(error) ERR I tried hard but I can't forget myself... #無法刪除登錄節點 192.168.30.130:7008> CLUSTER FORGET e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 (error) ERR Can't forget my master! #不能刪除自己的master節點192.168.30.130:7008> CLUSTER FORGET 6788453ee9a8d7f72b1d45a9093838efd0e501f1 OK #可以刪除其它的master節點192.168.30.130:7008> CLUSTER NODES 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557458887328 3 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 master - 0 1557458887000 1 connected 0-5460 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557458886000 3 connected 5461-10922 b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave - 0 1557458888351 5 connected 1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:7008@17008 myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557458885000 7 connected b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557458883289 1 connected e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557458885310 0 connected 192.168.30.130:7008> CLUSTER FORGET b4d3eb411a7355d4767c6c23b4df69fa183ef8bc OK #可以刪除其它的slave節點192.168.30.130:7008> CLUSTER NODES 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557459031397 3 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 master - 0 1557459032407 1 connected 0-5460 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557459035434 3 connected 5461-10922 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557459034000 5 connected 10923-16383 1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:7008@17008 myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557459032000 7 connected b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557459034000 1 connected e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557459034427 0 connected
保存配置:
192.168.30.130:7008> CLUSTER SAVECONFIG #將節點配置信息保存到硬盤OK # cat /data/redis/cluster/redis_7001/nodes-7001.conf1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:7008@17008 slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557458236169 7 connected 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557458235000 5 connected 10923-16383 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557458234103 6 connected b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557458235129 5 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 myself,master - 0 1557458234000 1 connected 0-5460 b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557458236000 4 connected e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557458236000 0 connected 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557458233089 3 connected 5461-10922 vars currentEpoch 7 lastVoteEpoch 0# redis-cli -c -h 192.168.30.130 -p 7008 -a 123456Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.30.130:7008> CLUSTER NODES 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557459500741 3 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 master - 0 1557459500000 1 connected 0-5460 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557459501000 3 connected 5461-10922 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557459500000 5 connected 10923-16383 b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557459499737 5 connected 1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:7008@17008 myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557459499000 7 connected b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 slave 80c80a3f3e33872c047a8328ad579b9bea001ad8 0 1557459501750 1 connected e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557459498000 0 connected
可以看到,之前刪除的節點又恢復了,這是因為對應的配置文件沒有刪除,執行CLUSTER SAVECONFIG
恢復。
模擬master節點掛掉:
192.168.30.128
# netstat -lntp |grep 7001tcp 0 0 192.168.30.128:17001 0.0.0.0:* LISTEN 6701/redis-server 1 tcp 0 0 192.168.30.128:7001 0.0.0.0:* LISTEN 6701/redis-server 1 # kill 6701
192.168.30.130:7008> CLUSTER NODES 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557461178000 3 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 master,fail - 1557460950483 1557460947145 1 disconnected 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557461174922 3 connected 5461-10922 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557461181003 5 connected 10923-16383 b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557461179993 5 connected 1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:7008@17008 myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557461176000 7 connected b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 master - 0 1557461178981 8 connected 0-5460 e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557461179000 0 connected
對應7001的一行可以看到,master fail,狀態為disconnected;而對應7004的一行,slave已經變成master。
重新啟動7001節點:
# redis-server /usr/local/redis/cluster/redis_7001.conf192.168.30.130:7008> CLUSTER NODES 277daeb8660d5273b7c3e05c263f861ed5f17b92 192.168.30.130:7006@17006 slave 4d74ec66e898bf09006dac86d4928f9fad81f373 0 1557461307000 3 connected 80c80a3f3e33872c047a8328ad579b9bea001ad8 192.168.30.128:7001@17001 slave b6331cbc986794237c83ed2d5c30777c1551546e 0 1557461305441 8 connected 4d74ec66e898bf09006dac86d4928f9fad81f373 192.168.30.129:7003@17003 master - 0 1557461307962 3 connected 5461-10922 6788453ee9a8d7f72b1d45a9093838efd0e501f1 192.168.30.130:7005@17005 master - 0 1557461304935 5 connected 10923-16383 b4d3eb411a7355d4767c6c23b4df69fa183ef8bc 192.168.30.128:7002@17002 slave 6788453ee9a8d7f72b1d45a9093838efd0e501f1 0 1557461306000 5 connected 1a1c7f02fce87530bd5abdfc98df1cffce4f1767 192.168.30.130:7008@17008 myself,slave e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 0 1557461305000 7 connected b6331cbc986794237c83ed2d5c30777c1551546e 192.168.30.129:7004@17004 master - 0 1557461308972 8 connected 0-5460 e51ab166bc0f33026887bcf8eba0dff3d5b0bf14 192.168.30.129:7007@17007 master - 0 1557461307000 0 connected
可以看到,7001節點啟動后為slave節點,并且是7004的slave節點。即master節點如果掛掉,它的slave節點變為新master節點繼續對外提供服務,而原來的master節點如果重啟,則變為新master節點的slave節點。
另外,如果這里是拿7007節點做測試的話,會發現7008節點并不會切換,這是因為7007節點上根本沒數據。集群數據被分為三份,采用哈希槽 (hash slot)的方式來分配16384個slot的話,它們三個節點分別承擔的slot 區間是:
節點7004覆蓋0-5460 節點7003覆蓋5461-10922 節點7005覆蓋10923-16383
以上是“Redis集群的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。