使用docker-compose快速搭建redis主从集群和sentinel集群
网上有很多文章,但是都不够简单,包含很多额外的操作,不够细致,我这里整理下。
1、请提前安装好docker和docker-compose,并pull redis镜像文件。
2、准备好redis配置文件和sentinel配置文件,各三份,对应3个redis-server和3个sentinel-server的配置。
3、为redis-sentinel集群创建一个文件夹,存放redis和sentinel配置,以及集群运行产生的日志和数据
4、最后为redis-sentinel集群编写一个docker-compose配置文件,里面配置好redis-sentinel集群的网络,以及配置文件挂载路径、数据目录挂载路径。
5、docker-compose 启动整个集群即可,无需其他操作。
首先展示下集群配置和数据目录创建完毕后的目录结构:
[root@dev redis-sentinel]# find
.
./redis
./redis/conf
./redis/conf/redis-server1.conf
./redis/conf/redis-server2.conf
./redis/conf/redis-server3.conf
./redis/data
./redis/data/s1
./redis/data/s2
./redis/data/s3
./sentinel
./sentinel/conf
./sentinel/conf/sentinel1.conf
./sentinel/conf/sentinel2.conf
./sentinel/conf/sentinel3.conf
./sentinel/data
./docker-compose.yaml
一、redis配置文件
3个redis-server配置略有不同,首先声明的ip不同,然后是slave会多一些复制的配置。
- redis-server1.conf 配置内容,去掉了注释和空行
# grep -v "^#" redis-server1.conf|grep -v "^$"
loglevel debug
logfile "/data/redis-6379.log"
save 3600 1
save 300 100
save 60 10000
stop-writes-on-bgsave-error no
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
rdb-del-sync-files no
dir /data/
requirepass MyRedisPassword123456 # 密码记得更改
appendonly yes
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
masterauth MyRedisPassword123456 # 密码记得更改
replica-announce-ip 172.19.0.2 # 注意,这个ip是在docker-compose配置中定义的
replica-announce-port 6379
然后这里是3个配置文件通过vim -d展示的差异部分:
+ +-- 12 lines: ############################ BASE ###############################---------|+ +-- 12 lines: ############################ BASE ###############################---------|+ +-- 12 lines: ############################ BASE ###############################---------
# output for logging but daemonize, logs will be sent to /dev/null | # output for logging but daemonize, logs will be sent to /dev/null | # output for logging but daemonize, logs will be sent to /dev/null
logfile "/data/redis-6379.log" | logfile "/data/redis-6380.log" | logfile "/data/redis-6381.log"
| |
+ +--256 lines: Save the DB to disk.------------------------------------------------------|+ +--256 lines: Save the DB to disk.------------------------------------------------------|+ +--256 lines: Save the DB to disk.------------------------------------------------------
# replicaof <masterip> <masterport> | # replicaof <masterip> <masterport> | # replicaof <masterip> <masterport>
#replicaof 10.28.0.203 6379 | replicaof 172.19.0.2 6379 | replicaof 172.19.0.2 6379
| |
+ +-- 46 lines: If the master is password protected (using the "requirepass" configuration|+ +-- 46 lines: If the master is password protected (using the "requirepass" configuration|+ +-- 46 lines: If the master is password protected (using the "requirepass" configuration
# | # | #
replica-announce-ip 172.19.0.2 | replica-announce-ip 172.19.0.3 | replica-announce-ip 172.19.0.4
replica-announce-port 6379 | replica-announce-port 6379 | replica-announce-port 6379
----------------------------------------------------------------------------------------| slaveof 172.19.0.2 6379 | slaveof 172.19.0.2 6379
二、 sentinel 配置
1、这里展示下sentienl-server1.conf去掉注释和空行的配置内容:
grep -v "^#" sentinel1.conf | grep -v "^$"
protected-mode no
port 26379
daemonize no
pidfile /var/run/redis-sentinel.pid
logfile /data/sentinel-1.log
sentinel announce-ip 172.19.0.5
sentinel announce-port 26379
dir /tmp
sentinel monitor mymaster 172.19.0.2 6379 2
sentinel auth-pass mymaster xxxxx
sentinel down-after-milliseconds mymaster 30000
acllog-max-len 128
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
SENTINEL resolve-hostnames no
SENTINEL announce-hostnames no
2、 然后对比了下3个sentinel配置差异部分:
+ +-- 34 lines: Example sentinel.conf-----------------------------------------------------|+ +-- 34 lines: Example sentinel.conf-----------------------------------------------------|+ +-- 34 lines: Example sentinel.conf-----------------------------------------------------
# output for logging but daemonize, logs will be sent to /dev/null | # output for logging but daemonize, logs will be sent to /dev/null | # output for logging but daemonize, logs will be sent to /dev/null
logfile /data/sentinel-1.log | logfile /data/sentinel-2.log | logfile /data/sentinel-3.log
| |
+ +-- 20 lines: sentinel announce-ip <ip>-------------------------------------------------|+ +-- 20 lines: sentinel announce-ip <ip>-------------------------------------------------|+ +-- 20 lines: sentinel announce-ip <ip>-------------------------------------------------
# sentinel announce-ip 1.2.3.4 | # sentinel announce-ip 1.2.3.4 | # sentinel announce-ip 1.2.3.4
sentinel announce-ip 172.19.0.5 | sentinel announce-ip 172.19.0.6 | sentinel announce-ip 172.19.0.7
sentinel announce-port 26379 | sentinel announce-port 26379 | sentinel announce-port 26379
+ +--283 lines: dir <working-directory>---------------------------------------------------|+ +--283 lines: dir <working-directory>---------------------------------------------------|+ +--283 lines: dir <working-directory>---------------------------------------------------
三、docker-compose.yaml 配置:
version: '3'
services:
# redis-masater
redis-server-1:
image: redis
container_name: redis-server-1
restart: always
networks:
redis_sentinel_network:
ipv4_address: 172.19.0.2
ports:
- 6379:6379
environment:
TZ: "Asia/Shanghai"
volumes:
# configuration and data folder
- ./redis/conf/redis-server1.conf:/usr/local/etc/redis/redis.conf
- ./redis/data/s1/:/data:Z
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
# redis slave 1
redis-server-2:
image: redis
container_name: redis-server-2
restart: always
networks:
redis_sentinel_network:
ipv4_address: 172.19.0.3
ports:
- 6380:6379
depends_on:
- redis-server-1
environment:
TZ: "Asia/Shanghai"
volumes:
- ./redis/conf/redis-server2.conf:/usr/local/etc/redis/redis.conf
- ./redis/data/s2/:/data:Z
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
# redis slave 2
redis-server-3:
image: redis
container_name: redis-server-3
restart: always
networks:
redis_sentinel_network:
ipv4_address: 172.19.0.4
ports:
- 6381:6379
depends_on:
- redis-server-1
environment:
TZ: "Asia/Shanghai"
volumes:
- ./redis/conf/redis-server3.conf:/usr/local/etc/redis/redis.conf
- ./redis/data/s3/:/data:Z
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
# sentinel 1
redis-sentinel-1:
image: redis
container_name: redis-sentinel-1
restart: always
networks:
redis_sentinel_network:
ipv4_address: 172.19.0.5
environment:
TZ: "Asia/Shanghai"
ports:
- 26379:26379
depends_on:
- redis-server-1
volumes:
- ./sentinel/conf/sentinel1.conf:/usr/local/etc/redis/sentinel.conf
- ./sentinel/data:/data:Z
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
# sentinel 2
redis-sentinel-2:
image: redis
container_name: redis-sentinel-2
restart: always
networks:
redis_sentinel_network:
ipv4_address: 172.19.0.6
environment:
TZ: "Asia/Shanghai"
ports:
- 26380:26379
depends_on:
- redis-server-1
volumes:
- ./sentinel/conf/sentinel2.conf:/usr/local/etc/redis/sentinel.conf
- ./sentinel/data:/data:Z
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
# sentinel 3
redis-sentinel-3:
image: redis
container_name: redis-sentinel-3
restart: always
networks:
redis_sentinel_network:
ipv4_address: 172.19.0.7
environment:
TZ: "Asia/Shanghai"
ports:
- 26381:26379
depends_on:
- redis-server-1
volumes:
- ./sentinel/conf/sentinel3.conf:/usr/local/etc/redis/sentinel.conf
- ./sentinel/data:/data:Z
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
networks:
redis_sentinel_network:
ipam:
config:
- subnet: 172.19.0.0/16
准备好如上文件后,将文件scp到对应目录即可。
四、启动验证集群
# history
2504 2023-25-08/18/23 12:25:05 docker-compose up -d
2505 2023-25-08/18/23 12:25:14 docker-compose ls
2506 2023-25-08/18/23 12:25:25 docker-compose top
2507 2023-25-08/18/23 12:25:35 docker container ls -a
随便找几个redis或者sentinel验证下
# redis-cli -h dev -p 6379 -a "这里保密"
dev:6379> INFO REPLICATION
# Replication
role:master
connected_slaves:2
slave0:ip=172.19.0.4,port=6379,state=online,offset=414005,lag=1
slave1:ip=172.19.0.3,port=6379,state=online,offset=414005,lag=0
master_failover_state:no-failover
master_replid:b0ab4ec1445ff9a3ea1db3f0263336efd3504041
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:414140
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:414140
# redis-cli -h dev -p 6380 -a "xxxxx"
INFO REPLICATION
# Replication
role:slave
master_host:172.19.0.2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_read_repl_offset:424200
slave_repl_offset:424200
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:b0ab4ec1445ff9a3ea1db3f0263336efd3504041
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:424200
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:834
repl_backlog_histlen:423367
验证下sentinel
sentinel master mymaster
sentinel slaves mymaster
完毕!
浙公网安备 33010602011771号