Centos 7搭建Redis羣集

博文目錄
1、Redis羣集原理
一、架構細節
二、Redis選舉
2、搭建Redis羣集
一、安裝第一臺Redis並修改配置文件
二、安裝第二臺Redis並修改配置文件
三、使用腳本建立羣集
四、測試羣集node

1、Redis羣集原理

關於Redis非關係型數據庫工做原理詳述請參考博文:聊一聊Centos 7中的Redis(非關係型數據庫)redis

Redis Cluster是一個無中心的結構,每一個節點都保存數據和整個羣集的狀態。每一個節點都會保存其餘節點的信息,知道其餘節點鎖負責的槽,而且會與其餘節點定時發送心跳信息,可以及時感知羣集中異常的節點。以下圖所示:
Centos 7搭建Redis羣集數據庫

當客戶端向羣集中任一節點發送與數據庫鍵有關的命令時,接受命令的節點會計算出命令要處理的數據庫鍵屬於哪一個槽,並檢查這個槽是否指派給了本身。若是鍵所在的槽正好指派給了當前節點,那麼節點直接執行這個命令;若是鍵所在的槽並無指派給當前節點,那麼節點會向客戶端返回一個MOVED錯誤,指引客戶端轉向(redirect)正確的節點,並再次發送以前想要執行的命令。vim

羣集角色有Master和Slave。Master之間分配slots,一共有16384個slot。Slave向它指定的Master同步數據,實現備份。當其中一個Master沒法提供服務時,該Master的Slave將提高爲Master。以保證羣集鍵slot的完整性。當其中的某一個Master和它的Slave都失效,就會致使slot不完整,羣集失效,這是就須要人工去處理了。centos

羣集搭建好後,羣集中的每一個節點都會按期地想其餘節點發送PING消息,若是接收PING消息的節點沒有在規定的時間內返回PONG消息,那麼發送PING消息的節點就會將其標記爲疑似下線(PFAIL)。各個節點會經過互相發送消息的方式來交換羣集中各個節點的狀態信息。若是在一個羣集裏面,半數以上的主節點都將某個節點 X 報告爲疑似下線,那麼這個主節點 X 將被標記爲已下線(FAIL),同時會向羣集廣播一條關於主節點 X 的FAIL消息,全部收到這條FAIL消息的節點都會當即將主節點 X 標記爲已下線。ruby

當須要減小或增長羣集中的機器時,咱們須要將已經指派給某個節點(源節點)的槽改成指派給另外一個節點(目標節點),而且將相關槽所屬的鍵值對從源節點移動到目標節點。服務器

Redis羣集的從新分片操做是由Redis的羣集管理軟件redis-trib負責執行的,不支持自動的分片,並且須要本身計算從哪些節點上遷移多少Slot。在從新分片的過程當中,羣集不須要下線,而且源節點和目標節點均可以繼續處理命令請求。網絡

一、架構細節

  • 全部的redis節點彼此互聯(PING-PONG機制),內部使用二進制協議優化傳輸速度和帶寬;架構

  • 節點的失效(fail)在羣集中超過半數的主(master)節點檢測失效才失效;tcp

  • 客戶端與redis節點直連,不須要中間代理(proxy)層,客戶端不須要鏈接羣集全部節點,鏈接羣集中任何一個可用節點便可;

  • redis-cluster把全部的物理節點映射到【0-16383】slot上,cluster 負責維護node<->slot<->key;

二、Redis選舉

以下圖所示:選舉過程是羣集中全部master參與,若是半數以上master節點與當前master節點通訊超時(cluster-node-timeout),認爲當前master節點掛掉。
Centos 7搭建Redis羣集
如下兩種狀況爲整個羣集不可用(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用於羣集總線,即便用二進制協議的節點到節點通訊通道。節點使用羣集總線進行故障檢測、配置更新、故障轉移受權等。若是開啓了防火牆,須要開放這兩個端口。

2、搭建Redis羣集

環境描述:

此案例的環境採用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文件上傳到服務器;

廢話少說,大點幹早點散,開始幹活...

一、安裝第一臺Redis並修改配置文件

[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

二、安裝第二臺Redis並修改配置文件

[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

———————— 本文至此結束,感謝閱讀 ————————

相關文章
相關標籤/搜索