Redis-Cluster集羣

一.簡介

  1. 爲什麼要使用集羣

爲了在大流量訪問下保證業務的穩定,集羣化是存儲的必然形態
爲了保證在某個服務器崩潰後,仍然能夠提供穩定的服務
單個服務器容易因爲單機內存,併發和流量等問題
未來的發展趨勢肯定是雲計算和大數據的緊密結合
只有分佈式架構才能滿足需求

  1. redis-cluster描述

redis-cluster採用無中心化結構,每個節點保存數據和整個集羣狀態,每個節點和其他所有節點連接,連接任意一個節點,即可連接到集羣
在redis3.0之後,就支持redis-cluster集羣,至少需要3Master+3Slave才能建立集羣
是主僕模式和哨兵模式的結合,優於這兩個模式

  1. 特點

所有的redis節點彼此互聯(PING-PONG機制),內部使用二進制協議優化傳輸速度和寬帶
節點的fail是通過集羣中超過半數的節點檢測失效時纔會生效
客戶端與redis節點直連,不需要中間proxy層,客戶端不需要連接集羣中的所有節點,連接其中一個即可
redis-cluster把所有的物理節點映射到[0-1638]slot上,不一定平均分配,有cluster負責維護
redis集羣會預先分好16384個哈希槽,當需要在redis集羣中放置一個key-value時,redis會對key使用crc16算法算出一個結果,然後把結果對16384求餘數,這樣key就會對應一個哈希槽,redis會根據節點數量大致均勻的將哈希槽映射到不同的節點中

  1. redis-cluster的容錯

master的不可用
使用投票機制,投票過程是集羣中的所有Master參與的,如果有半數以上的Master與檢測的Master節點連接超時,就會認爲該Master節點不可用,就會讓該Master的Slave代替
整個集羣的不可用
當集羣中任意Master不可用,且當前Master沒有可用的Slave,整個集羣進入fail狀態,這時哈希槽的映射也不完整了

二.redis-cluster集羣的搭建

redis3.X和redis4.X的版本還需要下載redis-trib.rb工具,這裏安裝的redis版本爲5.0.7

  1. 創建文件夾redis-cluster
 mkdir redis-cluster
  1. 在文件夾中創建六個文件夾,用於存放配置文件
cd redis-cluster
mkdir 7000 7001 7002 7003 7004 7005

如果是想搭建在不同服務器上的話,這六個文件夾就應該創建到不同服務器上

  1. 拷貝redis.conf
    將安裝redis時,壓縮包解壓的文件夾(redis-5.0.7)中的redis.conf複製到我們剛剛創建的文件夾中,每個文件夾都要一個配置
cp redis.conf /usr/local/redis-cluster/7000
  1. 修改redis.conf
    需要修改每個文件夾下的redis.conf
#關閉保護模式 用於公網訪問
protected-mode no
port 7000
#開啓集羣模式
#需要刪掉#,解開註釋
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
#後臺啓動
daemonize yes
pidfile /var.run/redis_7000.pid
logfile "7000.log"
#需要註釋
#dir /redis/data
#將網絡綁定註釋,外網就可以訪問redis了
#bind 127.0.0.1
#用於連接主節點的密碼
masterauth 123456
#設置redis密碼,各節點密碼最好保持一致
requirepass 123456

可以通過/名稱來查找屬性,修改完7000文件夾下的redis.conf後,複製到其他文件夾中,使用:%s/7000/7001來實現快速修改其他redis.conf

  1. 複製src/redis-server
    要加-r,將壓縮包解壓的文件(redis-5.0.7)中的src複製到redis-cluster
 cp -r src /usr/local/redis-cluster

到這裏,redis-cluster集羣的配置和啓動項就已經配置完了

三.redis-cluster集羣的啓動

  1. 啓動各個節點
 /src/redis-server ./7000/redis.conf
./src/redis-server ./7001/redis.conf
./src/redis-server ./7002/redis.conf
./src/redis-server ./7003/redis.conf
./src/redis-server ./7004/redis.conf
./src/redis-server ./7005/redis.conf
  1. 查看節點是否啓動
 ps -ef | grep -i redis
root      75240      1  0 15:34 ?        00:00:00 ./src/redis-server *:7000 [cluster]
root      75253      1  0 15:36 ?        00:00:00 ./src/redis-server *:7001 [cluster]
root      75258      1  0 15:36 ?        00:00:00 ./src/redis-server *:7002 [cluster]
root      75271      1  0 15:36 ?        00:00:00 ./src/redis-server *:7003 [cluster]
root      75276      1  0 15:36 ?        00:00:00 ./src/redis-server *:7004 [cluster]
root      75282      1  0 15:36 ?        00:00:00 ./src/redis-server *:7005 [cluster]
  1. 創建redis-cluster集羣
./src/redis-cli --cluster create -a 123456 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1

如果是不同服務器的話,修改ip地址和端口即可

出現這條語句,填yes即可,這是詢問是否接受它的配置

Can I set the above configuration? (type 'yes' to accept): yes

出現以下代碼,就是redis-cluster集羣已經創建完成並啓動了

[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

四.redis-cluster集羣的連接

  1. 連接上redis-cluster集羣中的某個節點
    仍然需要藉助src/redis-cli客戶端來連接
./src/redis-cli -h 127.0.0.1 -c -p 7000 -a 123456
  1. 查看集羣狀態
    可以通過這兩條命令來查看集羣的狀態cluster nodes/info replication
**cluster nodes**
d1826fd298ef94e6fe7c71d1c4792dfbb1c1496d 127.0.0.1:7001@17001 master - 0 1582963346000 2 connected 5461-10922
23125cf80e6bd8a2eacce63f333524d83244cb50 127.0.0.1:7000@17000 myself,master - 0 1582963346000 1 connected 0-5460
ac2f2a46fe10651091b73cfd4eb10d2de33c7f82 127.0.0.1:7003@17003 slave d1826fd298ef94e6fe7c71d1c4792dfbb1c1496d 0 1582963346000 4 connected
3265277ca8991eb3b77573d7d959e8cf432d85d1 127.0.0.1:7002@17002 master - 0 1582963347312 3 connected 10923-16383
a17a19a0d51db9ff7c35adf9515284ed0b831964 127.0.0.1:7004@17004 slave 3265277ca8991eb3b77573d7d959e8cf432d85d1 0 1582963346301 5 connected
781bf8a0adfbf007a6b676ec48a59ddde611b766 127.0.0.1:7005@17005 slave 23125cf80e6bd8a2eacce63f333524d83244cb50 0 1582963346000 6 connected
**info replication**
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7005,state=online,offset=1023,lag=1
master_replid:6565417229c062bb60e6e5c576a97bc12c5270c8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1023
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:1023

五.redis-cluster集羣的關閉

  1. 創建一個shutdown.sh文件
vim shutdown.sh

需要注意這裏的src/redis-cli的路徑,一定在該文件能夠讀取到

src/redis-cli -c -h 127.0.0.1 -p 7000 -a 123456 shutdown
src/redis-cli -c -h 127.0.0.1 -p 7001 -a 123456 shutdown
src/redis-cli -c -h 127.0.0.1 -p 7002 -a 123456 shutdown
src/redis-cli -c -h 127.0.0.1 -p 7003 -a 123456 shutdown
src/redis-cli -c -h 127.0.0.1 -p 7004 -a 123456 shutdown
src/redis-cli -c -h 127.0.0.1 -p 7005 -a 123456 shutdown
  1. 修改shutdown.sh權限
chmod u+x shutdown.sh
  1. 執行shutdown.sh關閉集羣
./shutdown.sh
[root@localhost redis-cluster]# ./shutdown.sh
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.

啓動也可以參考關閉這種形式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章