本文记录使用 docker-compose 快速搭建一个 Redis Sentinel 测试环境。其中 Redis 集群为 1 个 master 节点,2 个 slave 节点,Sentinel 为 3 个节点。
主机环境
系统 centos 7.5,ip ,docker 20.10.12,docker-compose v2.2.2,redis latest。
1)在 /usr/local/etc/redis 目录下,新建 docker-compose.yml
version: '3' services: master: image: redis container_name: redis-master command: redis-server --requirepass qwe!@#123 --masterauth qwe!@#123 ports: - 6379:6379 slave1: image: redis container_name: redis-slave-1 ports: - 6380:6380 command: redis-server --slaveof redis-master 6379 --requirepass qwe!@#123 --masterauth qwe!@#123 slave2: image: redis container_name: redis-slave-2 ports: - 6381:6381 command: redis-server --slaveof redis-master 6379 --requirepass qwe!@#123 --masterauth qwe!@#123
--requirepass:指定redis登陆的密码。
2)启动 redis 集群
docker-compose up -d
3)查看 redis-master 节点的 docker-ip 和 network name
docker inspect redis-master
4)搭建 sentinel 集群
4.1 在 /usr/local/etc/redis/sentinel 目录下,新建 docker-compose.yml
version: '3' services: sentinel1: image: redis container_name: redis-sentinel-1 ports: - 26379:26379 command: redis-sentinel /usr/local/etc/redis/sentinel.conf volumes: - ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf sentinel2: image: redis container_name: redis-sentinel-2 ports: - 26380:26380 command: redis-sentinel /usr/local/etc/redis/sentinel.conf volumes: - ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf sentinel3: image: redis container_name: redis-sentinel-3 ports: - 26381:26381 command: redis-sentinel /usr/local/etc/redis/sentinel.conf volumes: - ./sentinel3.conf:/usr/local/etc/redis/sentinel.conf networks: default: external: name: redis_default
4.2 新建 sentinel1.conf
port 26379 dir /tmp sentinel monitor mymaster 172.18.0.3 6379 2 sentinel auth-pass mymaster qwe!@#123 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
复制到 sentinel2.conf, sentinel3.conf 中,端口分别修改为 26380 和 26381
port 26380 dir /tmp sentinel monitor mymaster 172.18.0.3 6379 2 sentinel auth-pass mymaster qwe!@#123 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yesView Code
port 26381 dir /tmp sentinel monitor mymaster 172.18.0.3 6379 2 sentinel auth-pass mymaster qwe!@#123 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yesView Code
4.3 在该目录启动 sentinel 集群
docker-compose up -d
sentinel 启动之后,查看日志:
docker logs -f redis-sentinel-1
如下: