1. 程式人生 > >Docker Compose搭建Redis一主二從三哨兵高可用叢集

Docker Compose搭建Redis一主二從三哨兵高可用叢集

一、Docker Compose介紹

https://docs.docker.com/compose/

Docker官方的網站是這樣介紹Docker Compose的:

Compose是用於定義和執行多容器Docker應用程式的工具。通過Compose,您可以使用YAML檔案來配置應用程式的服務。然後,使用一個命令,就可以從配置中建立並啟動所有服務。

這裡Docker Compose給我的感受就是便捷、快速。只需編寫一個docker-compose.yml檔案,然後通過命令docker-compose up -d,這裡就可以搭建多個服務起來,非常適合搭建叢集環境。

 

二、安裝Docker Compose工具

通過命令安裝Compose工具,安裝Compose的前提是安裝了Docker環境,如何安裝和快速使用Docker,可以翻看我之前的那篇部落格--Docker快速上手之搭建SpringBoot專案,這裡不做贅述了。

sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose

給安裝指令碼新增執行許可權

sudo chmod +x /usr/local/bin/docker-compose

這裡可以使用命令檢視是否安裝成功

docker-compose -v

到這裡Compose工具就安裝好了

 

三、使用Compose搭建Redis主從伺服器

主從複製:主節點負責寫資料,從節點負責讀資料,主節點定期把資料同步到從節點保證資料的一致性。

從效能方面,redis複製功能增加了讀的效能,理論上說,每增加一倍的伺服器,整個系統的讀能力就增加一倍。

 

選擇好路徑,通過命令建立資料夾redis,然後進入資料夾建立docker-compose.yml,或者在本地建立,然後通過Xftp工具上傳至伺服器。docker-compose.yml寫入如下內容。

version: '2'
services:
  master:
    image: redis    ## 映象
    container_name: redis-master    ##容器別名
    command: redis-server --requirepass 123456    ##redis密碼
    ports:
    - "6379:6379"    ##暴露埠
    networks:
    - sentinel-master
  slave1:
    image: redis
    container_name: redis-slave-1
    ports:
    - "6380:6379"
    command: redis-server --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456 
    depends_on:
    - master
    networks:
    - sentinel-master
  slave2:
    image: redis
    container_name: redis-slave-2
    ports:
    - "6381:6379"
    command: redis-server --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456
    depends_on:
    - master
    networks:
    - sentinel-master

啟動redis叢集

docker-compose up -d

docker ps檢視執行的例項,這裡我們需要用到主節點的ip地址,注意不是電腦的ip哦,是節點的ip地址。

docker inspect 主節點容器id

這裡先記著這個ip地址,後面使用到哨兵需要繫結這個主節點的ip。

 

四、哨兵sentinel模式搭建

上面我們已經搭建好了主從模式,當主伺服器宕機後,需要手動把一臺伺服器切換成主伺服器,這裡需要人工干預,費事費力,還會造成一段時間內服務不可用。這不是一種推薦的方式,更多時候,我們優先考慮哨兵模式。 哨兵是redis高可用的解決方案,由一個或多個Sentinel例項組成的Sentinel系統可以監視任意多個主伺服器以及這些主伺服器屬下的所有從伺服器,它能夠在被監視的主伺服器下線時,自動將該主伺服器屬下的某個優先的從伺服器升級為新的主伺服器,由這個主伺服器代替已下線的主伺服器繼續處理命令請求。   哨兵的功能: 1.監控:哨兵會不斷地監控檢測主節點和從節點是否運作正常。 2.自動故障轉移:當主節點不能正常工作時,哨兵會開始自動故障轉移操作,它會將失效主節點的其中一個從節點升級為新的主節點,並讓其他從節點改為複製新的主節點。 3.通知:哨兵可以將故障轉移的結果傳送給客戶端。 4.配置提供者:客戶端在初始化時,通過連線哨兵來獲得當前Redis服務的主節點地址。   這裡建立sentinel資料夾,然後同樣編寫docker-compose.yml檔案
version: '2'
services:
  sentinel1:
    image: redis       ## 映象
    container_name: redis-sentinel-1
    ports:
    - "26379:26379"
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - "./sentinel.conf:/usr/local/etc/redis/sentinel.conf"
  sentinel2:
    image: redis                ## 映象
    container_name: redis-sentinel-2
    ports:
    - "26380:26379"           
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - "./sentinel2.conf:/usr/local/etc/redis/sentinel.conf"
  sentinel3:
    image: redis                ## 映象
    container_name: redis-sentinel-3
    ports:
    - "26381:26379"           
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf
networks:
  default:
    external:
      name: redis_sentinel-master

繼續在此目錄編寫檔案,編寫sentinel.conf檔案

port 26379
dir /tmp
#172.18.0.3填寫自己的主節點ip
sentinel monitor mymaster 172.18.0.3 6379 2
sentinel auth-pass mymaster 123456 
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 10000  
sentinel deny-scripts-reconfig yes

對命令的解釋

第三行表示Redis監控一個叫做mymaster的執行在172.18.0.3:6379的master,投票達到2則表示master以及掛掉了。
第四行設定主節點的密碼。
第五行表示在一段時間範圍內sentinel向master傳送的心跳PING沒有回覆則認為master不可用了。
第六行的parallel-syncs表示設定在故障轉移之後,同時可以重新配置使用新master的slave的數量。數字越低,更多的時間將會用故障轉移完成,但是如果slaves配置為服務舊資料,你可能不希望所有的slave同時重新同步master。因為主從複製對於slave是非阻塞的,當停止從master載入批量資料時有一個片刻延遲。通過設定選項為1,確信每次只有一個slave是不可到達的。
第七行表示10秒內mymaster還沒活過來,則認為master宕機了。

  建立好檔案後,複製好三份
cp sentinel.conf sentinel1.conf
cp sentinel.conf sentinel2.conf
cp sentinel.conf sentinel3.conf

啟動redis哨兵模式

docker-compose up -d

 

五、故障轉移測試

上面既然說到了哨兵可以自動轉移故障,也就是當主節點宕機的時候,它們能通過選舉制度,在從節點中選出一個節點來代替主節點。

我們先來看看主節點此時的資訊,這裡a288c55db497是我主節點容器的ip,這裡先是進入容器,再進入redis客戶端,再輸入密碼,然後使用info檢視節點資訊。

這裡可以看到這個節點的身份是master也就是主節點,然後有兩個連線著的從節點

 

現在我們停掉這個節點,看看哨兵是否可以實現故障轉移

docker stop a288c55db497

然後選擇其中一個從節點進入客戶端,輸入info檢視資訊

這裡發現其中一臺從節點已經變成主節點了,而且連線的從節點,也變成了一個。

 

瞭解Docker Compose可參考:https://www.jianshu.com/p/658911a8cff3

瞭解Redis主從哨兵可參考:https://www.cnblogs.com/leeSmall/p/8398401.html