Ubuntu22 Docker Compose安装Redis哨兵

目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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(可选)

1
2
apt-get autoremove --purge redis-server
apt-get autoremove --purge redis-sentinel

配置文件

主从配置

loglevel 安装默认的设置为 verbose:

  • 1)debug:会打印出很多信息,适用于开发和测试阶段
  • 2)verbose(冗长的):包含很多不太有用的信息,但比debug要清晰一些
  • 3)notice:适用于生产模式
  • 4)warning : 警告信息

master.conf 配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 使用守护进程模式,如果为 yes,docker 则一直处于重试状态
daemonize no
# 非保护模式,可以外网访问
#protected-mode no
timeout 300
databases 16
rdbcompression yes
# 学习开发,使用最大日志级别,能够看到最多的日志信息
loglevel notice
#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 no

repl-diskless-sync no
repl-diskless-sync-delay 5

repl-diskless-load disabled
repl-disable-tcp-nodelay no

slave1.conf 配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 使用守护进程模式,如果为 yes,docker 则一直处于重试状态
daemonize no
# 非保护模式,可以外网访问
# protected-mode no
timeout 300
databases 16
rdbcompression yes
# 学习开发,使用最大日志级别,能够看到最多的日志信息
loglevel notice
#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 no

repl-diskless-sync no
repl-diskless-sync-delay 5

repl-diskless-load disabled
repl-disable-tcp-nodelay no

slave2.conf 配置如下:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 使用守护进程模式,如果为 yes,docker 则一直处于重试状态
daemonize no
# 非保护模式,可以外网访问
protected-mode no
timeout 300
databases 16
rdbcompression yes
# 学习开发,使用最大日志级别,能够看到最多的日志信息
loglevel notice
# 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 no

repl-diskless-sync no
repl-diskless-sync-delay 5

repl-diskless-load disabled
repl-disable-tcp-nodelay no

sentinel(哨兵)配置

docker 部署内部端口可以是一样的,只改变对外映射的端口号即可。

sentinel-1.conf 配置如下:

1
2
3
4
5
6
7
8
9
10
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 Pd4Ervty3w6NmSB2Fm.
sentinel auth-pass limsmaster PN4Hxgcm.
sentinel down-after-milliseconds limsmaster 30000
sentinel failover-timeout limsmaster 180000
sentinel parallel-syncs limsmaster 2

sentinel-2.conf 配置如下:

1
2
3
4
5
6
7
8
9
10
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 Pd4Ervty3w6NmSB2Fm.
sentinel auth-pass limsmaster PN4Hxgcm.
sentinel down-after-milliseconds limsmaster 30000
sentinel failover-timeout limsmaster 180000
sentinel parallel-syncs limsmaster 2

sentinel-3.conf 配置如下:

1
2
3
4
5
6
7
8
9
10
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 Pd4Ervty3w6NmSB2Fm.
sentinel auth-pass limsmaster PN4Hxgcm.
sentinel down-after-milliseconds limsmaster 30000
sentinel failover-timeout limsmaster 180000
sentinel parallel-syncs limsmaster 2

docker-compose 配置

创建外部网络

如果没有外部网络,需要运行以下命令创建:

1
2
3
4
5
# 查看网络列表
docker network ls

# 创建网络
docker network create --driver bridge --subnet 10.10.13.0/24 --gateway 10.10.13.1 docker_compose_net

新建 docker-compose.yml 文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
version: "3.1"

networks:
default:
external:
name: docker_compose_net

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

docker-compose 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 启动
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

相关命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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