3

Ubuntu22 Docker Compose安装Redis哨兵

 2 years ago
source link: https://syxdevcode.github.io/2022/08/24/Ubuntu22%20Docker%20Compose%E5%AE%89%E8%A3%85Redis%E5%93%A8%E5%85%B5/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
root@pony:/lims/redis# tree
.
├── conf
│ ├── master.conf
│ ├── sentinel-1.conf
│ ├── sentinel-2.conf
│ ├── sentinel-3.conf
│ ├── slave1.conf
│ └── slave2.conf
├── docker-compose.yml
├── master
│ └── data
├── slave1
│ └── data
└── slave2
└── data

注意:新建的目录需要使用 chmod 命令授权。

移除 redis(可选)

apt-get autoremove --purge redis-server

master.conf 配置如下:

# 使用守护进程模式,如果为 yes,docker 则一直处于重试状态
daemonize no
# 非保护模式,可以外网访问
#protected-mode no
timeout 300
databases 16
rdbcompression yes
# 学习开发,使用最大日志级别,能够看到最多的日志信息
loglevel debug
#bind 127.0.0.1
#bind 10.10.0.106
port 6379

##暂且禁用指令重命名
##rename-command
##开启AOF,禁用snapshot
appendonly yes
# masteruser "PonyLimsCopy_Slave"
masterauth PN4Hxgcm.

requirepass PN4Hxgcm.

maxclients 10000
maxmemory 2000mb
logfile /data/redis.log

replica-announce-ip 10.10.0.106
replica-announce-port 36379

#cluster-announce-ip 10.10.0.106
#cluster-announce-port 36379
#cluster-announce-bus-port 46379

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

slave1.conf 配置如下:

# 使用守护进程模式,如果为 yes,docker 则一直处于重试状态
daemonize no
# 非保护模式,可以外网访问
# protected-mode no
timeout 300
databases 16
rdbcompression yes
# 学习开发,使用最大日志级别,能够看到最多的日志信息
loglevel debug
#bind 127.0.0.1
port 6379

##暂且禁用指令重命名
##rename-command
##开启AOF,禁用snapshot
appendonly yes
replicaof redis-master 6379
# replicaof 10.10.0.106 36379
# masteruser PonyLimsCopy_Slave
masterauth PN4Hxgcm.
requirepass PN4Hxgcm.

maxclients 10000
maxmemory 2000mb
logfile /data/redis.log

replica-announce-ip 10.10.0.106
replica-announce-port 36380

#cluster-announce-ip 10.10.0.106
#cluster-announce-port 36379
#cluster-announce-bus-port 46379

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

slave1.conf 配置如下:

注意 replica-announce-port 端口的区别。

# 使用守护进程模式,如果为 yes,docker 则一直处于重试状态
daemonize no
# 非保护模式,可以外网访问
protected-mode no
timeout 300
databases 16
rdbcompression yes
# 学习开发,使用最大日志级别,能够看到最多的日志信息
loglevel debug
# bind 127.0.0.1
port 6379

##暂且禁用指令重命名
##rename-command
##开启AOF,禁用snapshot
appendonly yes
replicaof redis-master 6379
# replicaof 10.10.0.106 36379
# masteruser PonyLimsCopy_Slave
masterauth PN4Hxgcm.
requirepass PN4Hxgcm.

maxclients 10000
maxmemory 2000mb
logfile /data/redis.log

replica-announce-ip 10.10.0.106
replica-announce-port 36381

#cluster-announce-ip 10.10.0.106
#cluster-announce-port 36379
#cluster-announce-bus-port 46379

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

sentinel(哨兵)配置

sentinel-1.conf 配置如下:

port 26379
dir "/tmp"
sentinel announce-ip 10.10.0.106
sentinel announce-port 26379
sentinel monitor limsmaster 10.10.0.106 36379 2
requirepass PN4Hxgcm.
sentinel auth-pass limsmaster PN4Hxgcm.
sentinel down-after-milliseconds limsmaster 60000
sentinel failover-timeout limsmaster 180000
sentinel parallel-syncs limsmaster 2

sentinel-2.conf 配置如下:

port 26379
dir "/tmp"
sentinel announce-ip 10.10.0.106
sentinel announce-port 26380
sentinel monitor limsmaster 10.10.0.106 36379 2
requirepass PN4Hxgcm.
sentinel auth-pass limsmaster PN4Hxgcm.
sentinel down-after-milliseconds limsmaster 60000
sentinel failover-timeout limsmaster 180000
sentinel parallel-syncs limsmaster 2

sentinel-3.conf 配置如下:

port 26379
dir "/tmp"
sentinel announce-ip 10.10.0.106
sentinel announce-port 26381
sentinel monitor limsmaster 10.10.0.106 36379 2
requirepass PN4Hxgcm.
sentinel auth-pass limsmaster PN4Hxgcm.
sentinel down-after-milliseconds limsmaster 60000
sentinel failover-timeout limsmaster 180000
sentinel parallel-syncs limsmaster 2

docker-compose配置

新建 docker-compose.yml 文件。

version: '4.1'

services:
redis-master:
image: redis:7.0.4
container_name: redis-master
restart: always
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- /lims/redis/conf/master.conf:/usr/local/etc/redis/redis.conf
- /lims/redis/master/data:/data
ports:
- 36379:6379
environment:
- TZ=Asia/Shanghai # 时区配置亚洲上海
redis-slave1:
image: redis:7.0.4
container_name: redis-slave1
restart: always
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- /lims/redis/conf/slave1.conf:/usr/local/etc/redis/redis.conf
- /lims/redis/slave1/data:/data
ports:
- 36380:6379
environment:
- TZ=Asia/Shanghai # 时区配置亚洲上海
redis-slave2:
image: redis:7.0.4
container_name: redis-slave2
restart: always
command: redis-server /usr/local/etc/redis/redis.conf
volumes:
- /lims/redis/conf/slave2.conf:/usr/local/etc/redis/redis.conf
- /lims/redis/slave2/data:/data
ports:
- 36381:6379
environment:
- TZ=Asia/Shanghai # 时区配置亚洲上海
redis-sentinel-1:
image: redis:7.0.4
container_name: redis-sentinel-1
restart: always
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /lims/redis/conf/sentinel-1.conf:/usr/local/etc/redis/sentinel.conf
ports:
- 26379:26379
environment:
- TZ=Asia/Shanghai # 时区配置亚洲上海
depends_on:
- "redis-master"
- "redis-slave1"
- "redis-slave2"
redis-sentinel-2:
image: redis:7.0.4
container_name: redis-sentinel-2
restart: always
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /lims/redis/conf/sentinel-2.conf:/usr/local/etc/redis/sentinel.conf
ports:
- 26380:26379
environment:
- TZ=Asia/Shanghai # 时区配置亚洲上海
depends_on:
- "redis-master"
- "redis-slave1"
- "redis-slave2"
redis-sentinel-3:
image: redis:7.0.4
container_name: redis-sentinel-3
restart: always
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- /lims/redis/conf/sentinel-3.conf:/usr/local/etc/redis/sentinel.conf
ports:
- 26381:26379
environment:
- TZ=Asia/Shanghai # 时区配置亚洲上海
depends_on:
- "redis-master"
- "redis-slave1"
- "redis-slave2"

docker-compose命令

# 启动
docker compose up -d

# 移除
docker-compose down -v

# 查看日志
docker logs redis-master

# 停止
docker-compose stop

# 启动
docker-compose start

# 重启
docker-compose restart

# 暂停
docker-compose pause redis-master

# 取消暂停
docker-compose unpause redis-master
docker exec -it redis-master redis-cli -a PN4Hxgcm.

docker exec redis-sentinel-1 redis-cli -h 10.10.0.106 -p 26379 -a PN4Hxgcm. SENTINEL get-master-addr-by-name limsmaster
docker exec redis-sentinel-2 redis-cli -h 10.10.0.106 -p 26379 -a PN4Hxgcm. SENTINEL get-master-addr-by-name limsmaster
docker exec redis-sentinel-3 redis-cli -h 10.10.0.106 -p 26379 -a PN4Hxgcm. SENTINEL get-master-addr-by-name limsmaster

docker exec redis-sentinel-1 redis-cli -h 10.10.0.106 -p 26379 -a PN4Hxgcm. sentinel masters
docker exec redis-sentinel-2 redis-cli -h 10.10.0.106 -p 26380 -a PN4Hxgcm. sentinel masters
docker exec redis-sentinel-3 redis-cli -h 10.10.0.106 -p 26381 -a PN4Hxgcm. sentinel masters

docker exec redis-sentinel-1 redis-cli -h 10.10.0.106 -p 26379 -a PN4Hxgcm. info sentinel
docker exec redis-sentinel-2 redis-cli -h 10.10.0.106 -p 26380 -a PN4Hxgcm. info sentinel
docker exec redis-sentinel-3 redis-cli -h 10.10.0.106 -p 26381 -a PN4Hxgcm. info sentinel

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-master
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-slave1
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis-slave2

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK