首頁 > 軟體

docker搭建redis主從哨兵叢集的實現步驟

2022-07-10 14:02:04

本文以docker-compose 搭建高可用Redis 主從、哨兵叢集為例子

關於redis主從,哨兵叢集原理參見:Redis 單機安裝/ 哨兵模式叢集安裝

1:獲取redis映象

docker pull redis:6.2.7

2:建立redis主從+哨兵docker-compose檔案

cd /opt/docker/redis
vi docker-compose.yml

docker-compose.yml的內容如下 

version: '3'
services:
  master:
    image: redis:6.2.7       ## 映象
    container_name: redis-master
    command: redis-server /etc/redis/redis.conf --requirepass 123456 --masterauth 123456
    volumes:
    - /opt/docker/redis/data/redis_data1:/data
    - /opt/docker/redis/conf/redis1.conf:/etc/redis/redis.conf
    network_mode: "host"
  slave1:
    image: redis:6.2.7                ## 映象
    container_name: redis-slave-1
    volumes:
    - /opt/docker/redis/data/redis_data2:/data
    - /opt/docker/redis/conf/redis2.conf:/etc/redis/redis.conf
    command: redis-server /etc/redis/redis.conf --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456 
    depends_on:
    - master
    network_mode: "host"
  slave2:
    image: redis:6.2.7                ## 映象
    container_name: redis-slave-2
    volumes:
    - /opt/docker/redis/data/redis_data3:/data
    - /opt/docker/redis/conf/redis3.conf:/etc/redis/redis.conf
    command: redis-server /etc/redis/redis.conf --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456
    depends_on:
    - master
    network_mode: "host"
  sentinel1:
    image: redis:6.2.7       ## 映象
    container_name: redis-sentinel-1
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - /opt/docker/redis/conf/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
    network_mode: "host"
    depends_on:
    - master
    - slave1
    - slave2
  sentinel2:
    image: redis:6.2.7                ## 映象
    container_name: redis-sentinel-2          
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - /opt/docker/redis/conf/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
    network_mode: "host"
    depends_on:
    - master
    - slave1
    - slave2
  sentinel3:
    image: redis:6.2.7                ## 映象
    container_name: redis-sentinel-3          
    command: redis-sentinel /usr/local/etc/redis/sentinel.conf
    volumes:
    - /opt/docker/redis/conf/sentinel3.conf:/usr/local/etc/redis/sentinel.conf
    network_mode: "host"
    depends_on:
    - master
    - slave1
    - slave2

3:redis設定和哨兵設定說明

 檢視組態檔的目錄樹

cd /opt/docker/redis
tree ./

結構如下

redis1.conf,redis2.conf,redis3.conf設定如下

#redis1.conf
bind 0.0.0.0
port 6379
protected-mode no
slave-read-only no
 
#redis2.conf
bind 0.0.0.0
port 6380
protected-mode no
slave-read-only no
 
#redis3.conf
bind 0.0.0.0
port 6381
protected-mode no
slave-read-only no

 sentinel1.conf,sentinel1.conf,sentinel1.conf設定:

#sentinel1.conf
port 26379
dir /tmp
sentinel monitor mymaster 192.168.18.131 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
 
#sentinel2.conf
port 26380
dir /tmp
sentinel monitor mymaster 192.168.18.131 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
 
#sentinel3.conf
port 26381
dir /tmp
sentinel monitor mymaster 192.168.18.131 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

說明:

【sentinel monitor mymaster 192.168.18.131 6379 2】192.168.18.131為伺服器的IP地址,6379為redis master的埠號
【sentinel auth-pass mymaster 123456 】設定主節點的密碼  
【sentinel down-after-milliseconds mymaster 30000】表示在一段時間範圍內sentinel向master傳送的心跳PING沒有回覆則認為master不可用了。 
【sentinel parallel-syncs mymaster 1】的parallel-syncs表示設定在故障轉移之後,同時可以重新設定使用新master的slave的數量。數位越低,更多的時間將會用故障轉移完成,但是如果slaves設定為服務舊資料,你可能不希望所有的slave同時重新同步master。因為主從複製對於slave是非阻塞的,當停止從master載入批次資料時有一個片刻延遲。通過設定選項為1,確信每次只有一個slave是不可到達的。
【sentinel failover-timeout mymaster 10000  】表示10秒內mymaster還沒活過來,則認為master宕機了。 

redis_data1, redis_data2,redis_data3為空資料夾,用於存放redis資料檔案

4:啟動docker-compose

docker-compose up
#或者,後臺啟動
docker-compose up -d

5:檢視啟動情況

6:進入主節點檢視叢集情況

docker exec -it 主節點容器id或者容器名稱 bash
redis-cli -p 6379
info replication

 到此這篇關於docker搭建redis主從哨兵叢集的實現步驟的文章就介紹到這了,更多相關docker redis主從哨兵叢集內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com