Redis 1 主 2 从 3 哨兵(Docker-compose部署)(单机模式)

资源清单

主机 IP
redis节点 10.0.0.1
软件 版本
docker 20.10.12
docker-compose 1.23.1
redis镜像 6.2.6

一、Docker 安装

1. 使用国内 yum

# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2. 卸载旧版本的 docker

## 如果主机上已经有docker存在且不是想要安装的版本,需要先进行卸载。
# yum remove -y docker \
              docker-client \
              docker-client-latest \
              docker-common \
              docker-latest \
              docker-latest-logrotate \
              docker-logrotate \
              docker-selinux \
              docker-engine-selinux \
              docker-engine \
              container*

3. 安装 Docker20.10 版本

# yum -y install docker-ce-20.10.12-3.el7 docker-ce-cli-20.10.12-3.el7 vim

4. 设置镜像加速

# mkdir /etc/docker
# vi /etc/docker/daemon.json

{
  "registry-mirrors": ["https://xxxxxxxxx.mirror.aliyuncs.com"]
}

5. 启动 docker

# systemctl start docker
# systemctl enable docker
# systemctl status docker

二、Docker-compose 安装

1. Docker-compose 安装

## github.com 可能访问超时,可以使用下面的获取下载下来后上传服务器即可
# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# curl -k "https://dl.cactifans.com/zabbix_docker/docker-compose" -o /usr/bin/docker-compose

# chmod a+x /usr/bin/docker-compose

2. 查看 docker-compose 版本

# docker-compose version

三、Redis 1主2从3哨兵安装

1. 创建一个自定义网桥

后续 docker-compose 文件中会使用 redisNet 网桥

docker-compose.yml 已提供动态创建 redisNet 网桥

# docker network create --driver bridge --subnet 172.20.0.0/24 --gateway 172.20.0.1 redisNet

2. 详细的 docker-compose 文件信息

version: '3'

services:
  master:
    image: redis:6.2.6
    container_name: redis-master
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /data/redis/master/conf:/etc/redis
      - /data/redis/master/data:/data
      - /data/redis/master/log:/log
    ports:
      - 6379:6379
    networks:
      redisNet:
        ipv4_address: 172.20.0.10

  slave1:
    image: redis:6.2.6
    container_name: redis-slave1
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /data/redis/slave1/conf:/etc/redis
      - /data/redis/slave1/data:/data
      - /data/redis/slave1/log:/log
    ports:
      - 6380:6379
    networks:
      redisNet:
        ipv4_address: 172.20.0.11

  slave2:
    image: redis:6.2.6
    container_name: redis-slave2
    command: /bin/bash -c "redis-server /etc/redis/redis.conf"
    volumes:
      - /data/redis/slave2/conf:/etc/redis
      - /data/redis/slave2/data:/data
      - /data/redis/slave2/log:/log
    ports:
      - 6381:6379
    networks:
      redisNet:
        ipv4_address: 172.20.0.12

  sentinel1:
    image: redis:6.2.6
    depends_on:
      - master
      - slave1
      - slave2
    container_name: redis-sentinel1
    command: /bin/bash -c "redis-sentinel /etc/redis/redis-sentinel.conf"
    volumes:
      - /data/redis/sentinel/sentinel1/conf:/etc/redis
      - /data/redis/sentinel/sentinel1/data:/data
      - /data/redis/sentinel/sentinel1/log:/log
    ports:
      - 26379:26379
    networks:
      redisNet:
        ipv4_address: 172.20.0.20

  sentinel2:
    image: redis:6.2.6
    depends_on:
      - master
      - slave1
      - slave2
    container_name: redis-sentinel2
    command: /bin/bash -c "redis-sentinel /etc/redis/redis-sentinel.conf"
    volumes:
      - /data/redis/sentinel/sentinel2/conf:/etc/redis
      - /data/redis/sentinel/sentinel2/data:/data
      - /data/redis/sentinel/sentinel2/log:/log
    ports:
      - 26380:26379
    networks:
      redisNet:
        ipv4_address: 172.20.0.21

  sentinel3:
    image: redis:6.2.6
    depends_on:
      - master
      - slave1
      - slave2
    container_name: redis-sentinel3
    command: /bin/bash -c "redis-sentinel /etc/redis/redis-sentinel.conf"
    volumes:
      - /data/redis/sentinel/sentinel3/conf:/etc/redis
      - /data/redis/sentinel/sentinel3/data:/data
      - /data/redis/sentinel/sentinel3/log:/log
    ports:
      - 26381:26379
    networks:
      redisNet:
        ipv4_address: 172.20.0.22

networks:
  redisNet:
    driver: bridge # 创建一个docker 的桥接网络
    ipam:
      driver: default
      config:
        - subnet: 172.20.0.0/24
    
# networks . external: true

networks:
  redisNet:
    external: true

# docker-compose 会找到一个名为 redisNet 的已存在的 network,并将 service 的容器连接到 redisNet network。

3. 安装服务

先启动服务后,自动生成目录结构,在上传配置文件

# mkdir /data/redis/ -pv
# docker-compose up -d

4. redis-master 配置

# cd /data/redis
# vim master/conf/redis.conf 

bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize no
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/log/redis.log"
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir ./
masterauth edocyun@test
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-diskless-load disabled
repl-disable-tcp-nodelay no
replica-priority 100
acllog-max-len 128
requirepass edocyun@test
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes
jemalloc-bg-thread yes

5. slave1 | slave2 配置

# cd /data/redis
# vim slave2/conf/redis.conf  [ slave1 | slave2 ] 
## 在master配置的基础上新增如下配置即可

slaveof 172.20.0.10 6379

6. sentinel1 | sentinel2 | sentinel3 配置

# cd /data/redis
# vim sentinel/sentinel1/conf/redis-sentinel.conf  [ sentinel1 | sentinel2 | sentinel3 ] 

port 26379
daemonize no
logfile "/log/sentinel.log"
sentinel monitor mymaster 172.20.0.10 6379 2

7. 重启服务

# docker-compose down
# docker-compose up -d
posted @ 2022-04-28 16:19  evescn  阅读(261)  评论(0编辑  收藏  举报