您好,登錄后才能下訂單哦!
博文目錄
一、Redis群集原理
1、架構細節
2、Redis選舉
二、搭建Redis群集
1、安裝第一臺Redis并修改配置文件
2、安裝第二臺Redis并修改配置文件
3、使用腳本創建群集
4、測試群集
關于Redis非關系型數據庫工作原理詳述請參考博文:聊一聊Centos 7中的Redis(非關系型數據庫)
Redis Cluster是一個無中心的結構,每個節點都保存數據和整個群集的狀態。每個節點都會保存其他節點的信息,知道其他節點鎖負責的槽,并且會與其他節點定時發送心跳信息,能夠及時感知群集中異常的節點。如下圖所示:
當客戶端向群集中任一節點發送與數據庫鍵有關的命令時,接受命令的節點會計算出命令要處理的數據庫鍵屬于哪個槽,并檢查這個槽是否指派給了自己。如果鍵所在的槽正好指派給了當前節點,那么節點直接執行這個命令;如果鍵所在的槽并沒有指派給當前節點,那么節點會向客戶端返回一個MOVED錯誤,指引客戶端轉向(redirect)正確的節點,并再次發送之前想要執行的命令。
群集角色有Master和Slave。Master之間分配slots,一共有16384個slot。Slave向它指定的Master同步數據,實現備份。當其中一個Master無法提供服務時,該Master的Slave將提升為Master。以保證群集鍵slot的完整性。當其中的某一個Master和它的Slave都失效,就會導致slot不完整,群集失效,這是就需要人工去處理了。
群集搭建好后,群集中的每個節點都會定期地想其他節點發送PING消息,如果接收PING消息的節點沒有在規定的時間內返回PONG消息,那么發送PING消息的節點就會將其標記為疑似下線(PFAIL)。各個節點會通過互相發送消息的方式來交換群集中各個節點的狀態信息。如果在一個群集里面,半數以上的主節點都將某個節點 X 報告為疑似下線,那么這個主節點 X 將被標記為已下線(FAIL),同時會向群集廣播一條關于主節點 X 的FAIL消息,所有收到這條FAIL消息的節點都會立即將主節點 X 標記為已下線。
當需要減少或增加群集中的機器時,我們需要將已經指派給某個節點(源節點)的槽改為指派給另一個節點(目標節點),并且將相關槽所屬的鍵值對從源節點移動到目標節點。
Redis群集的重新分片操作是由Redis的群集管理軟件redis-trib負責執行的,不支持自動的分片,而且需要自己計算從哪些節點上遷移多少Slot。在重新分片的過程中,群集不需要下線,并且源節點和目標節點都可以繼續處理命令請求。
所有的redis節點彼此互聯(PING-PONG機制),內部使用二進制協議優化傳輸速度和帶寬;
節點的失效(fail)在群集中超過半數的主(master)節點檢測失效才失效;
客戶端與redis節點直連,不需要中間代理(proxy)層,客戶端不需要連接群集所有節點,連接群集中任何一個可用節點即可;
- redis-cluster把所有的物理節點映射到【0-16383】slot上,cluster 負責維護node<->slot<->key;
如下圖所示:選舉過程是群集中所有master參與,如果半數以上master節點與當前master節點通信超時(cluster-node-timeout),認為當前master節點掛掉。
以下兩種情況為整個群集不可用(cluster_state:fail),當群集不可用時,所有對群集的操作都不可用,收到((error) CLUSTERDOWN The cluster is down)錯誤。
如果群集任意master掛掉,且當前master沒有slave,則群集進入fail狀態,也可以理解為群集的slot映射[0-16383]不完整時進入的fail狀態;
- 如果群集中出現半數的master掛掉,無論是否有slave,群集都進入fail狀態;默認情況下,每個群集的節點都使用兩個TCP端口,一個是6379,一個16379;6379服務用于客戶端的連接,16379用于群集總線,即使用二進制協議的節點到節點通信通道。節點使用群集總線進行故障檢測、配置更新、故障轉移授權等。如果開啟了防火墻,需要開放這兩個端口。
環境描述:
此案例的環境采用6臺Centos 7服務器搭建Redis群集,其中3臺為master,3臺為salve;
6臺服務器的IP地址為192.168.100.10/24——192.168.100.60/24;
自行準備所需源碼包即工具,也可以訪問:https://pan.baidu.com/s/126siXcoxrqBWmp9SWhEVmQ
提取碼:a45i ;
自行配置網絡及防火墻,放行TCP的6379和16379這兩個端口,我這里直接關閉了防火墻;
自行通過rz命令將redis壓縮包和redis的gem文件上傳到服務器;
廢話少說,大點干早點散,開始干活...
[root@centos01 ~]# ls <!--查看redis壓縮包和redis的gem文件是否上傳成功-->
anaconda-ks.cfg initial-setup-ks.cfg redis-3.2.0.gem redis-3.2.9.tar.gz
[root@centos01 ~]# tar zxvf redis-3.2.9.tar.gz -C /usr/src/ <!--redis解壓縮到/usr/src/目錄-->
[root@centos01 ~]# mv /usr/src/redis-3.2.9/ /usr/src/redis/
<!--將redis所有配置文件剪切到/usr/src/redis/目錄 -->
[root@centos01 ~]# cd /usr/src/redis/ <!--進入redis目錄->
[root@centos01 redis]# ls
00-RELEASENOTES COPYING Makefile redis.conf runtest-sentinel tests
BUGS deps MANIFESTO runtest sentinel.conf utils
CONTRIBUTING INSTALL README.md runtest-cluster src
[root@centos01 redis]# make && make install <!--編輯及安裝redis-->
[root@centos01 redis]# cd utils/ <!--進入utils目錄-->
[root@centos01 utils]# ./install_server.sh <!--初始化redis-->
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379] <!--回車鍵即可-->
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] <!--回車鍵即可-->
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] <!--回車鍵即可-->
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] <!--回車鍵即可-->
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server] <!--回車鍵即可-->
Selected config:
Port : 6379 <!--端口號,默認允許6379-->
Config file : /etc/redis/6379.conf <!--redis主配置文件-->
Log file : /var/log/redis_6379.log <!--redis日志文件-->
Data dir : /var/lib/redis/6379 <!--設置數據目錄-->
Executable : /usr/local/bin/redis-server <!--執行命令-->
Cli Executable : /usr/local/bin/redis-cli <!--客戶端命令-->
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@centos01 ~]# vim /etc/redis/6379.conf <!--修改redis主配置文件啟動群集功能-->
62 bind 192.168.100.10 <!--監聽的主機地址-->
85 port 6379 <!--監聽端口號-->
129 daemonize yes <!--啟動守護進程-->
164 logfile /var/log/redis_6379.log <!--指定日志文件-->
722 cluster-enabled yes <!--啟動群集-->
730 cluster-config-file nodes-6379.conf <!--群集配置文件-->
736 cluster-node-timeout 15000
813 cluster-require-full-coverage no
[root@centos01 ~]# /etc/init.d/redis_6379 start <!--啟動redis服務-->
Starting Redis server...
[root@centos01 ~]# netstat -anptu | grep redis
<!--查看6379和16379端口是否已經正常啟動-->
tcp 0 0 192.168.100.10:6379 0.0.0.0:* LISTEN 4662/redis-server 1
tcp 0 0 192.168.100.10:16379 0.0.0.0:* LISTEN 4662/redis-server 1
[root@centos01 ~]# yum -y install ruby rubygems <!--安裝群集所需依賴程序-->
[root@centos01 ~]# gem install redis --version 3.2.0 <!--安裝gem工具-->
<!--j接下來將redis壓縮包遠程復制到100.20、30、40、50、60服務器的根目錄-->
[root@centos01 ~]# scp redis-3.2.9.tar.gz root@192.168.100.20:/root
<!--將redis壓縮包復制到100.20服務器的根目錄-->
The authenticity of host '192.168.100.20 (192.168.100.20)' can't be established.
ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.
ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
Are you sure you want to continue connecting (yes/no)? yes <!--輸入yes-->
Warning: Permanently added '192.168.100.20' (ECDSA) to the list of known hosts.
root@192.168.100.20's password: <!--輸入密碼-->
redis-3.2.9.tar.gz 100% 1511KB 44.5MB/s 00:00
[root@centos01 ~]# scp redis-3.2.9.tar.gz root@192.168.100.30:/root
<!--將redis壓縮包復制到100.30服務器的根目錄-->
The authenticity of host '192.168.100.30 (192.168.100.30)' can't be established.
ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.
ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
Are you sure you want to continue connecting (yes/no)? yes <!--輸入yes-->
Warning: Permanently added '192.168.100.30' (ECDSA) to the list of known hosts.
root@192.168.100.30's password: <!--輸入密碼-->
redis-3.2.9.tar.gz 100% 1511KB 48.7MB/s 00:00
[root@centos01 ~]# scp redis-3.2.9.tar.gz root@192.168.100.40:/root
<!--將redis壓縮包復制到100.40服務器的根目錄-->
The authenticity of host '192.168.100.40 (192.168.100.40)' can't be established.
ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.
ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
Are you sure you want to continue connecting (yes/no)? yes <!--輸入yes-->
Warning: Permanently added '192.168.100.40' (ECDSA) to the list of known hosts.
root@192.168.100.40's password: <!--輸入密碼-->
redis-3.2.9.tar.gz 100% 1511KB 72.2MB/s 00:00
[root@centos01 ~]# scp redis-3.2.9.tar.gz root@192.168.100.50:/root
<!--將redis壓縮包復制到100.50服務器的根目錄-->
The authenticity of host '192.168.100.50 (192.168.100.50)' can't be established.
ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.
ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
Are you sure you want to continue connecting (yes/no)? yes <!--輸入yes-->
Warning: Permanently added '192.168.100.50' (ECDSA) to the list of known hosts.
root@192.168.100.50's password: <!--輸入密碼-->
redis-3.2.9.tar.gz 100% 1511KB 47.3MB/s 00:00
[root@centos01 ~]# scp redis-3.2.9.tar.gz root@192.168.100.60:/root
<!--將redis壓縮包復制到100.60服務器的根目錄-->
The authenticity of host '192.168.100.60 (192.168.100.60)' can't be established.
ECDSA key fingerprint is SHA256:PUueT9fU9QbsyNB5NC5hbSXzaWxxQavBxXmfoknXl4I.
ECDSA key fingerprint is MD5:6d:f7:95:0e:51:1a:d8:9e:7b:b6:3f:58:51:51:4b:3b.
Are you sure you want to continue connecting (yes/no)? yes <!--輸入yes-->
Warning: Permanently added '192.168.100.60' (ECDSA) to the list of known hosts.
root@192.168.100.60's password: <!--輸入密碼-->
redis-3.2.9.tar.gz 100% 1511KB 3.5MB/s 00:00
[root@centos02 ~]# yum -y install ruby rubygems <!--安裝redis群集所需依賴程序-->
[root@centos02 ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg redis-3.2.9.tar.gz
[root@centos02 ~]# tar zxvf redis-3.2.9.tar.gz <!--解壓縮redis程序包-->
[root@centos02 ~]# mv redis-3.2.9 /usr/src/redis/ <!--剪切到/usr/src/redis/目錄-->
[root@centos02 ~]# cd /usr/src/redis/ <!--進入redis目錄-->
[root@centos02 redis]# make && make install <!--編輯及安裝redis-->
[root@centos02 redis]# cd utils/ <!--進入utils目錄-->
[root@centos02 utils]# ./install_server.sh <!--初始化redis-->
Welcome to the redis service installer
This script will help you easily set up a running redis server
Please select the redis port for this instance: [6379]
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/bin/redis-server
Cli Executable : /usr/local/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
<!--在第一臺redis服務器上將redis主配置文件遠程復制到剩下五臺服務器上-->
[root@centos01 ~]# scp /etc/redis/6379.conf root@192.168.100.20:/etc/redis/
<!--將第一臺redis服務器上的主配置文件遠程復制到第二臺redis服務器的/etc/redis/目錄下-->
root@192.168.100.20's password: <!--輸入密碼-->
6379.conf 100% 46KB 37.4MB/s 00:00
[root@centos01 ~]# scp /etc/redis/6379.conf root@192.168.100.30:/etc/redis/
<!--將第一臺redis服務器上的主配置文件遠程復制到第三臺redis服務器的/etc/redis/目錄下-->
root@192.168.100.30's password: <!--輸入密碼-->
6379.conf 100% 46KB 27.9MB/s 00:00
[root@centos01 ~]# scp /etc/redis/6379.conf root@192.168.100.40:/etc/redis/
<!--將第一臺redis服務器上的主配置文件遠程復制到第四臺redis服務器的/etc/redis/目錄下-->
root@192.168.100.40's password: <!--輸入密碼-->
6379.conf 100% 46KB 13.5MB/s 00:00
[root@centos01 ~]# scp /etc/redis/6379.conf root@192.168.100.50:/etc/redis/
<!--將第一臺redis服務器上的主配置文件遠程復制到第五臺redis服務器的/etc/redis/目錄下-->
root@192.168.100.50's password: <!--輸入密碼-->
6379.conf 100% 46KB 35.4MB/s 00:00
[root@centos01 ~]# scp /etc/redis/6379.conf root@192.168.100.60:/etc/redis/
<!--將第一臺redis服務器上的主配置文件遠程復制到第六臺redis服務器的/etc/redis/目錄下-->
root@192.168.100.60's password: <!--輸入密碼-->
6379.conf 100% 46KB 44.7MB/s 00:00
[root@centos02 ~]# vim /etc/redis/6379.conf
<!--第二臺redis服務器修改主配置文件監聽的IP地址-->
bind 192.168.100.20 <!--將IP地址改為本服務器自身的IP-->
[root@centos02 ~]# redis-server /etc/redis/6379.conf <!--啟動redis服務-->
[root@centos02 ~]# netstat -anptu | grep redis <!--監聽redis服務是否正常啟動-->
tcp 0 0 192.168.100.20:6379 0.0.0.0:* LISTEN 6224/redis-server 1
tcp 0 0 192.168.100.20:16379 0.0.0.0:* LISTEN 6224/redis-server 1
剩下四臺服務器的操作步驟按照第二臺的操作步驟做同樣的配置即可,修改主配置文件監聽的IP地址設置成服務器自己的IP地址即可
創建群集要用到ruby的一個腳本,在創建群集之前,需要先安裝ruby的運行環境和ruby的Redis客戶端,該操作在其中一臺服務器進行即可。gem命令是提前下載的redis-3.2.0 gem軟件包提供的,直接上傳服務器即可使用。
[root@centos01 ~]# /usr/src/redis/src/redis-trib.rb create --replicas 1
192.168.100.10:6379 192.168.100.20:6379
192.192.168.100.30:6379 192.168.100.40:6379
192. 192.168.100.50:6379 192.168.100.60:6379
<!--在第一臺redis服務器上使用腳本創建群集-->
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.100.10:6379
192.168.100.20:6379
192.168.100.30:6379
Adding replica 192.168.100.40:6379 to 192.168.100.10:6379
Adding replica 192.168.100.50:6379 to 192.168.100.20:6379
Adding replica 192.168.100.60:6379 to 192.168.100.30:6379
M: 53a7082afe52d1216a447bd505f1828e8c04c7b6 192.168.100.10:6379
slots:0-5460 (5461 slots) master
M: 7023c518c9bde44e137db167abcaf3ef6302ef5c 192.168.100.20:6379
slots:5461-10922 (5462 slots) master
M: 82196443876dd7a7dba2cbda237064577e6996e5 192.168.100.30:6379
slots:10923-16383 (5461 slots) master
S: b86a7228dc45da696a9e95f6593cf28e9d350643 192.168.100.40:6379
replicates 53a7082afe52d1216a447bd505f1828e8c04c7b6
S: 2ef78b8d7e4174c7cb8ff6c9c7834e8e0e97e6fc 192.168.100.50:6379
replicates 7023c518c9bde44e137db167abcaf3ef6302ef5c
S: 161c231d36b342103ff1524d027e131e66da06ef 192.168.100.60:6379
replicates 82196443876dd7a7dba2cbda237064577e6996e5
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
Waiting for the cluster to join.....
>>> Performing Cluster Check (using node 192.168.100.10:6379)
M: 53a7082afe52d1216a447bd505f1828e8c04c7b6 192.168.100.10:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 7023c518c9bde44e137db167abcaf3ef6302ef5c 192.168.100.20:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: b86a7228dc45da696a9e95f6593cf28e9d350643 192.168.100.40:6379
slots: (0 slots) slave
replicates 53a7082afe52d1216a447bd505f1828e8c04c7b6
M: 82196443876dd7a7dba2cbda237064577e6996e5 192.168.100.30:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 161c231d36b342103ff1524d027e131e66da06ef 192.168.100.60:6379
slots: (0 slots) slave
replicates 82196443876dd7a7dba2cbda237064577e6996e5
S: 2ef78b8d7e4174c7cb8ff6c9c7834e8e0e97e6fc 192.168.100.50:6379
slots: (0 slots) slave
replicates 7023c518c9bde44e137db167abcaf3ef6302ef5c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@centos01 ~]# cd /usr/src/redis/src/
[root@centos01 src]# ./redis-trib.rb check 192.168.100.10:6379
<!--查看群集狀態
(可以清楚的看到哪臺主機是master、哪臺主機是salve)-->
>>> Performing Cluster Check (using node 192.168.100.10:6379)
M: 53a7082afe52d1216a447bd505f1828e8c04c7b6 192.168.100.10:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 7023c518c9bde44e137db167abcaf3ef6302ef5c 192.168.100.20:6379
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: b86a7228dc45da696a9e95f6593cf28e9d350643 192.168.100.40:6379
slots: (0 slots) slave
replicates 53a7082afe52d1216a447bd505f1828e8c04c7b6
M: 82196443876dd7a7dba2cbda237064577e6996e5 192.168.100.30:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 161c231d36b342103ff1524d027e131e66da06ef 192.168.100.60:6379
slots: (0 slots) slave
replicates 82196443876dd7a7dba2cbda237064577e6996e5
S: 2ef78b8d7e4174c7cb8ff6c9c7834e8e0e97e6fc 192.168.100.50:6379
slots: (0 slots) slave
replicates 7023c518c9bde44e137db167abcaf3ef6302ef5c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
<!--登錄redis群集,設置鍵值測試。這里需要跟“-c”參數來激活群集模式-->
[root@centos01 ~]# redis-cli -h 192.168.100.10 -p 6379 -c <!--登錄到群集-->
192.168.100.10:6379> set centos 7.4 <!--插入數據-->
OK
192.168.100.10:6379> get centos <!--查看-->
"7.4"
192.168.100.10:6379> quit <!--退出群集-->
[root@centos01 ~]# redis-cli -h 192.168.100.40 -p 6379 -c
192.168.100.40:6379> get centos
<!--查看在100.10上插入的數據,同樣可以看到;數據自動同步其redis服務器-->
-> Redirected to slot [467] located at 192.168.100.10:6379
"7.4"
192.168.100.10:6379> quit
[root@centos01 ~]# redis-cli -h 192.168.100.60 -p 6379 -c
192.168.100.60:6379> get centos
-> Redirected to slot [467] located at 192.168.100.10:6379
"7.4"
192.168.100.10:6379> quit
———————— 本文至此結束,感謝閱讀 ————————
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。