Loading

基于Docker的Redis哨兵模式部署(支持离线部署)

 

部署架构

架构图

 

一、物料准备

1、Docker安装包:https://download.docker.com/linux/static/stable/x86_64/docker-29.7.2.tgz

2、Docker Compose可执行文件:https://github.com/docker/compose/releases/download/v5.4.0/docker-compose-linux-x86_64

3、Redis镜像:redis:8.10.0

 

二、安装Docker、Docker Compose

1、Docker:

tar xf docker-29.7.2.tgz

mv docker/* /usr/bin/

rm -rf docker docker-29.7.2.tgz

cat > /etc/systemd/system/docker.service << EOF

[Unit]

Description=Docker Application Container Engine

Documentation=https://docs.docker.com

After=network-online.target firewalld.service containerd.service time-set.target

Wants=network-online.target containerd.service

 

[Service]

Type=notify

# the default is not to use systemd for cgroups because the delegate issues still

# exists and systemd currently does not support the cgroup feature set required

# for containers run by docker

ExecStart=/usr/bin/dockerd

ExecReload=/bin/kill -s HUP $MAINPID

TimeoutStartSec=0

RestartSec=2

Restart=always

 

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.

# Both the old, and new location are accepted by systemd 229 and up, so using the old location

# to make them work for either version of systemd.

StartLimitBurst=3

 

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.

# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make

# this option work for either version of systemd.

StartLimitInterval=60s

 

# Having non-zero Limit*s causes performance problems due to accounting overhead

# in the kernel. We recommend using cgroups to do container-local accounting.

LimitNPROC=infinity

LimitCORE=infinity

# Older systemd versions default to a LimitNOFILE of 1024:1024, which is insufficient for many

# applications including dockerd itself and will be inherited. Raise the hard limit, while

# preserving the soft limit for select(2).

LimitNOFILE=1024:524288

 

# Comment TasksMax if your systemd version does not support it.

# Only systemd 226 and above support this option.

TasksMax=infinity

 

# set delegate yes so that systemd does not reset the cgroups of docker containers

Delegate=yes

 

# kill only the docker process, not all processes in the cgroup

KillMode=process

OOMScoreAdjust=-500

 

[Install]

WantedBy=multi-user.target

 

EOF

设置docker存储位置

cat > /etc/docker/daemon.json << EOF

{

  "data-root": "/data/docker",

  "log-driver": "json-file",

  "log-opts": {

    "max-size": "100m",

    "max-file": "5"

  }

}

EOF

systemctl daemon-reload

systemctl start docker

systemctl enable docker

2、Docker Compose:

mv docker-compose-linux-x86_64 /usr/bin/docker-compose

chmod +x /usr/bin/docker-compose

 

四、部署Redis

所有节点文件保持一致,只需要修改docker-compose.yml中的REDIS_ROLE值。

redis.conf

bind 0.0.0.0
port 6379
protected-mode yes
dir /data
appendonly yes
appendfsync everysec
maxmemory 8gb
maxmemory-policy allkeys-lru
save 900 1
save 300 10
save 60 10000
requirepass Password123@redis
masterauth Password123@redis
replica-read-only yes
repl-diskless-sync yes
repl-diskless-sync-delay 5
tcp-keepalive 60
timeout 0
loglevel notice

 

sentinel.conf

port 26379
bind 0.0.0.0
protected-mode yes
dir /data
sentinel monitor mymaster 192.168.164.168 6379 2
sentinel down-after-milliseconds mymaster 10000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
sentinel auth-pass mymaster Password123@redis
logfile ""
daemonize no
protected-mode yes

 

docker-compose.yml

只需修改REDIS_ROLE,主节点REDIS_ROLE设置为master,其他节点设置为replica。

services:

  redis:
    image: redis:8.10.0
    container_name: redis
    restart: unless-stopped
    network_mode: host
    environment:
      REDIS_ROLE: master
      REDIS_MASTER_IP: 192.168.164.168
    volumes:
      - /data/redis/redis.conf:/usr/local/etc/redis/redis.conf:ro
      - /data/redis/redis_data:/data
    command:
      - /bin/sh
      - -c
      - |
          if [ "$${REDIS_ROLE}" = "master" ]; then
            exec redis-server /usr/local/etc/redis/redis.conf
          else
            exec redis-server /usr/local/etc/redis/redis.conf \
              --replicaof "$${REDIS_MASTER_IP}" 6379
          fi

  sentinel:
    image: redis:8.10.0
    container_name: redis-sentinel
    restart: unless-stopped
    network_mode: host
    volumes:
      - /data/redis/sentinel.conf:/usr/local/etc/redis/sentinel.conf
      - /data/redis/sentinel_data:/data
    command:
      - redis-server
      - /usr/local/etc/redis/sentinel.conf
      - --sentinel

 

启动(168、169、170按顺序启动

docker-compose up -d

 

posted @ 2026-08-18 18:14  hackyo  阅读(1)  评论(0)    收藏  举报