Docker 快速搭建主從 + 哨兵監控

有我在發表於2020-05-10

執行命令

docker pull redis

啟動三臺機器,6379容器作為主節點,其餘作為從節點

機器IP 角色
172.17.0.2 Master 6379
172.17.0.3 Slave1 6380
172.17.0.4 Slave2 6381

目錄結構

├── conf
│   ├── redis_6379.conf
│   ├── redis_6380.conf
│   ├── redis_6381.conf
│   ├── sentinel_26379.conf
│   ├── sentinel_26380.conf
│   └── sentinel_26381.conf
├── data_6379
│   ├── appendonly.aof
│   └── dump.rdb
├── data_6380
│   └── dump.rdb
├── data_6381
│   └── dump.rdb
└── scripts
    ├── run.sh
    └── sentinel.sh

redis_.conf 配置檔案

下載配置檔案,具體配置另行調整即可

https://raw.githubusercontent.com/antirez/redis/5.0/redis.conf

sentinel_.conf 配置檔案

port 26379
dir "/etc/redis"
sentinel monitor mymaster 172.17.0.2 6379 1
sentinel down-after-milliseconds mymaster 60000
sentinel failover-timeout mymaster 180000

run.sh 啟動指令碼檔案

#!/usr/bin/env bash
set -e

# 指令碼當前目錄
cPath=$(cd $(dirname "$0") || exit; pwd)

# 根目錄
dirPath=$(dirname "$cPath")

# 獲取埠
port="$1"
if [[ ! "$port" ]]; then
  port=6379
fi

# 建立資料目錄
mkdir -p "$dirPath"/data_"$port"

# 刪除已啟動服務
containerId=$(docker ps -a | grep "redis_$port" | awk -F' ' '{print $1}')
if [[ "$containerId" ]]; then
    echo "正在刪除服務:${containerId}"
    docker rm -f ${containerId} > /dev/null
fi

# 啟動服務
docker run -itd --privileged=true -p "$port":6379 --name redis_"$port" \
-v="$dirPath"/conf/redis_"$port".conf:/etc/redis/redis.conf \
-v="$dirPath"/data_"$port":/data \
redis \
redis-server /etc/redis/redis.conf

sentinel.sh 啟動指令碼檔案

#!/usr/bin/env bash
set -e

# 指令碼當前目錄
cPath=$(cd $(dirname "$0") || exit; pwd)

# 根目錄
dirPath=$(dirname "$cPath")

# 設定埠
port=$1
if [[ ! "$port" ]]; then
  port=26379
fi

# 刪除已啟動服務
containerId=$(docker ps -a | grep "sentinel_$port" | awk -F' ' '{print $1}')
if [[ "$containerId" ]]; then
    echo "正在刪除服務:${containerId}"
    docker rm -f ${containerId} > /dev/null
fi

# 啟動服務
docker run -itd --privileged=true -p "$port":26379 --name "sentinel_$port" \
-v="${dirPath}/conf/sentinel_${port}.conf":/etc/redis/sentinel.conf \
-w=/etc/redis/ \
redis \
redis-server /etc/redis/sentinel.conf --sentinel

注意

啟動 sentinel 服務如果如下錯誤

1:X 10 May 2020 08:10:48.234 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:X 10 May 2020 08:10:48.235 # Sentinel config file /etc/redis/sentinel.conf is not writable: Permission denied. Exiting...

解決方案

  • 在docker run 中增加 ==-w=/etc/redis/==,具體見 sentinel.sh 指令碼
  • 修改目錄許可權
docker run -it --rm --privileged=true -v=/root/docker-config/redis/conf/:/etc/redis/ redis chown -R redis /etc/redis

主要語句:==chown -R redis /etc/redis==

檢視修改後的許可權

root@DESKTOP-Q13EI52:~/docker-config/redis# docker run -it --rm --privileged=true -v=/root/docker-config/redis/conf/:/etc/redis/ redis ls -l /etc/redis/
total 204
-rw-r--r-- 1 redis root 61793 May 10 07:41 redis_6379.conf
-rw-r--r-- 1 redis root 61864 May 10 12:52 redis_6380.conf
-rw-r--r-- 1 redis root 61890 May 10 12:52 redis_6381.conf
-rw-r--r-- 1 redis root   459 May 10 12:52 sentinel_26379.conf
-rw-r--r-- 1 redis root   161 May 10 07:56 sentinel_26380.conf
-rw-r--r-- 1 redis root   161 May 10 10:16 sentinel_26381.conf

啟動主服務

sh scripts/run.sh 6379

啟動從服務

sh scripts/run.sh 6380
sh scripts/run.sh 6381

進入從服務

docker exec -it redis_6380 bash
docker exec -it redis_6381 bash

# 進入容器後執行命令
redis-cli

# 設定為從服務
slaveof 172.17.0.2 6379

效果圖

sh scripts/sentinel.sh

效果圖

  • 目前機器服務結構如下
root@DESKTOP-Q13EI52:~/docker-config/redis# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                                NAMES
e518580ce8eb        redis               "docker-entrypoint.s…"   About a minute ago   Up About a minute   6379/tcp, 0.0.0.0:26379->26379/tcp   sentinel_26379
e2ef253d3513        redis               "docker-entrypoint.s…"   2 hours ago          Up 2 hours          0.0.0.0:6381->6379/tcp               redis_6381
15fdc81eb530        redis               "docker-entrypoint.s…"   2 hours ago          Up 2 hours          0.0.0.0:6380->6379/tcp               redis_6380
77f4fdc84cb1        redis               "docker-entrypoint.s…"   2 hours ago          Up 2 hours          0.0.0.0:6379->6379/tcp               redis_6379

執行命令:docker stop redis_6379

1:X 10 May 2020 12:52:15.634 # +sdown master mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:15.634 # +odown master mymaster 172.17.0.2 6379 #quorum 1/1
1:X 10 May 2020 12:52:15.634 # +new-epoch 1
1:X 10 May 2020 12:52:15.634 # +try-failover master mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:15.649 # +vote-for-leader 6905a137af7baedbdfce4978d1d9126fdaad4faf 1
1:X 10 May 2020 12:52:15.649 # +elected-leader master mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:15.649 # +failover-state-select-slave master mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:15.725 # +selected-slave slave 172.17.0.3:6379 172.17.0.3 6379 @ mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:15.725 * +failover-state-send-slaveof-noone slave 172.17.0.3:6379 172.17.0.3 6379 @ mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:15.781 * +failover-state-wait-promotion slave 172.17.0.3:6379 172.17.0.3 6379 @ mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:16.798 # +promoted-slave slave 172.17.0.3:6379 172.17.0.3 6379 @ mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:16.798 # +failover-state-reconf-slaves master mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:16.845 * +slave-reconf-sent slave 172.17.0.4:6379 172.17.0.4 6379 @ mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:17.837 * +slave-reconf-inprog slave 172.17.0.4:6379 172.17.0.4 6379 @ mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:17.837 * +slave-reconf-done slave 172.17.0.4:6379 172.17.0.4 6379 @ mymaster 172.17.0.2 6379
1:X 10 May 2020 12:52:17.937 # +failover-end master mymaster 172.17.0.2 6379

# 生成執行故障轉移
1:X 10 May 2020 12:52:17.937 # +switch-master mymaster 172.17.0.2 6379 172.17.0.3 6379

1:X 10 May 2020 12:52:17.938 * +slave slave 172.17.0.4:6379 172.17.0.4 6379 @ mymaster 172.17.0.3 6379
1:X 10 May 2020 12:52:17.938 * +slave slave 172.17.0.2:6379 172.17.0.2 6379 @ mymaster 172.17.0.3 6379

1:X 10 May 2020 12:53:18.010 # +sdown slave 172.17.0.2:6379 172.17.0.2 6379 @ mymaster 172.17.0.3 6379

注意

已經掉線的172.17.0.2:6379 主節點重新上線後將自動切換為從服務

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章