Redis叢集環境搭建
主從配置(基礎)
優點:
Master Server是以非阻塞的方式為Slaves提供服務。是以在Master-Slave同步期間,用戶端仍然可以送出查詢或修改請求。
Slave Server同樣是以非阻塞的方式完成資料同步。在同步期間,如果有用戶端送出查詢請求,Redis則傳回同步之前的資料
支援主從複制,主機會自動将資料同步到從機,可以進行讀寫分離
缺點:
Redis不具備自動容錯和恢複功能,主機從機的當機都會導緻前端部分讀寫請求失敗,需要等待機器重新開機或者手動切換前端的IP才能恢複。
主機當機,當機前有部分資料未能及時同步到從機,切換IP後還會引入資料不一緻的問題,降低了系統的可用性。
Redis較難支援線上擴容,在叢集容量達到上限時線上擴容會變得很複雜。為避免這一問題,運維人員在系統上線時必須確定有足夠的空間,這對資源造成了很大的浪費。
案例使用:
redis版本 redis-5.0.3.tar.gz 下載下傳位址http://download.redis.io/releases/
環境ubuntu14
這裡采用正常的一主兩從的模式
1.下載下傳redis并導入到/usr/local/src/redis
2.在該目錄下執行tar -zxvf redis-5.0.3.tar.gz
3.在解壓後的檔案夾目錄執行make指令(需提前安裝yum install gcc-c++)
4.解壓完成後去src目錄下看是否有redis-server檔案有說明已經安裝成功了
5.複制3配置設定置檔案分别命名redis-6379.conf,redis-6380.conf,redis-6381.conf
redis-6379.conf(在原配置檔案上修改)
#redis運作端口
port 6379
#背景運作
daemonize yes
redis-6380.conf
#redis運作端口
port 6379
#背景運作
daemonize yes
#所述那個主機
slaveof 127.0.0.1 6379
redis-6381.conf
#redis運作端口
port 6379
#背景運作
daemonize yes
#所述那個主機
slaveof 127.0.0.1 6379
然後啟動3個執行個體(這裡按照解壓出來的路徑沒有做分類)
在/usr/local/src/redis/redis-5.0.3下執行
./src/redis-server redis-6379.conf
./src/redis-server redis-6380.conf
./src/redis-server redis-6381.conf
檢視redis運作
ps -ef | grep redis

這樣說明已經成功啟動了3個執行個體,然後進行簡單的測試
這裡在主機上設定一個name從機上可以讀取到但無法寫,這樣最簡單的主從模式就搭建好了
哨兵模式(sentinel)
根據上面的主從配置同樣的這邊配置3個哨兵監督主機
建立哨兵模式的配置檔案sentinel-26379.conf,sentinel-26380.conf,sentinel-26381.conf
#哨兵監聽端口
port 26379
#哨兵工作目錄
dir /usr/local/src/redis/sentinel-26379
#監控的master 2表示有2個哨兵認為出問題
sentinel monitor mymaster 127.0.0.1 6379 2
#master認證資訊
sentinel auth-pass mymaster mldnjava
#設定master不活躍時間
sentinel down-after-milliseconds mymaster 30000
#選舉新的master失敗時間
sentinel failover-timeout mymaster 180000
#隻有1個master
sentinel parallel-syncs mymaster 1
#撤銷redis保護模式
protected-mode no
如果沒有特殊需求我們可以簡化采用預設配置隻需要配置這邊不再背景運作
#哨兵監聽端口
port 26379
#監控的master 2表示有2個哨兵認為出問題
sentinel monitor mymaster 127.0.0.1 6379 2
#哨兵監聽端口
port 26380
#監控的master 2表示有2個哨兵認為出問題
sentinel monitor mymaster 127.0.0.1 6379 2
#哨兵監聽端口
port 26381
#監控的master 2表示有2個哨兵認為出問題
sentinel monitor mymaster 127.0.0.1 6379 2
跟上面一樣啟動執行個體
在/usr/local/src/redis/redis-5.0.3下執行
./src/redis-server redis-6379.conf
./src/redis-server redis-6380.conf
./src/redis-server redis-6381.conf
這樣就能監控到主機和從機的狀态、接下來我們把主機關了
将主機關掉之後
我們可以看到6381端口的從機切換成了主機我們進入看一下這台機器的狀态
他的角色已經變成了主機,這就完成了簡單的哨兵模式的主從自動切換,重新啟動6379的redis發現
發現它已經變成了從機,檢查哨兵配置檔案發現變動了很多,這也是哨兵模式的缺陷。
叢集模式(Cluster)
首先叢集模式不再需要哨兵,是以這裡隻需要修改redis.conf配置檔案即可
這次我們準備8個節點對于端口6379-6386,按如下修改配置檔案
#取消本機ip綁定
#bind 127.0.0.1
#在配置之前一定要取消保護模式
protected-mode no
#監聽端口
port 6379
背景運作
daemonize yes
定義pid儲存路徑
pidfile /usr/local/src/redis
定義日志路徑
logfile "/usr/local/src/redis"
資料儲存目錄
dir /usr/local/src/redis
不要配置密碼
#requirepass
打開RedisCluster叢集
cluster-enabled yes
定義cluster配置的儲存檔案
cluster-config-file nodes-6379.conf
定義節點的逾時時間
cluster-node-timeout 15000
然後分别啟動8個節點
在/usr/local/src/redis/redis-5.0.3下執行
./src/redis-server redis-6379.conf
./src/redis-server redis-6380.conf
./src/redis-server redis-6381.conf
./src/redis-server redis-6382.conf
./src/redis-server redis-6383.conf
./src/redis-server redis-6384.conf
./src/redis-server redis-6385.conf
./src/redis-server redis-6386.conf
因為我這裡使用的是5.0版本不再使用redis-trib.rb工具5.0以下版本可參考https://www.cnblogs.com/jianjianyang/archive/2017/09/03/7467875.html
輸入叢集配置指令
在/usr/local/src/redis/redis-5.0.3下執行
./src/redis-cli --cluster create 101.37.159.107:6379 101.37.159.107:6380 101.37.159.107:6381 101.37.159.107:6382 101.37.159.107:6383 101.37.159.107:6384 101.37.159.107:6385 101.37.159.107:6386 --cluster-replicas 1
運作到這說明你的叢集已經搭建成功了