docker-compose.yml
master: image: redis:4 container_name: redis-cluster_master command: redis-server --port 6379 ports: - "6379" net: "host" slave_1: image: redis:4 container_name: redis-cluster_slave_1 command: redis-server --port 6380 --slaveof 192.168.80.19 6379 ports: - "6380" net: "host" slave_2: image: redis:4 container_name: redis-cluster_slave_2 command: redis-server --port 6381 --slaveof 192.168.80.19 6379 ports: - "6381" net: "host" sentinel_1: build: sentinel container_name: redis-cluster_sentinel_1 environment: - SENTINEL_PORT=26379 - SENTINEL_DOWN_AFTER=5000 - SENTINEL_FAILOVER=5000 ports: - "26379" net: "host" sentinel_2: build: sentinel container_name: redis-cluster_sentinel_2 environment: - SENTINEL_PORT=26380 - SENTINEL_DOWN_AFTER=5000 - SENTINEL_FAILOVER=5000 ports: - "26380" net: "host" sentinel_3: build: sentinel container_name: redis-cluster_sentinel_3 environment: - SENTINEL_PORT=26381 - SENTINEL_DOWN_AFTER=5000 - SENTINEL_FAILOVER=5000 ports: - "26381" net: "host"
Dockerfile
FROM redis:4 ADD sentinel.conf /etc/redis/sentinel.conf RUN chown redis:redis /etc/redis/sentinel.conf ENV SENTINEL_QUORUM 2 ENV SENTINEL_DOWN_AFTER 30000 ENV SENTINEL_FAILOVER 180000 COPY sentinel-entrypoint.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/sentinel-entrypoint.sh ENTRYPOINT ["sentinel-entrypoint.sh"]
sentinel.conf
# Example sentinel.conf can be downloaded from http://download.redis.io/redis-stable/sentinel.conf port $SENTINEL_PORT dir /tmp sentinel monitor mymaster 192.168.80.19 6379 $SENTINEL_QUORUM sentinel down-after-milliseconds mymaster $SENTINEL_DOWN_AFTER sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster $SENTINEL_FAILOVER
sentinel-entrypoint.sh
#!/bin/sh sed -i "s/\$SENTINEL_QUORUM/$SENTINEL_QUORUM/g" /etc/redis/sentinel.conf sed -i "s/\$SENTINEL_DOWN_AFTER/$SENTINEL_DOWN_AFTER/g" /etc/redis/sentinel.conf sed -i "s/\$SENTINEL_FAILOVER/$SENTINEL_FAILOVER/g" /etc/redis/sentinel.conf sed -i "s/\$SENTINEL_PORT/$SENTINEL_PORT/g" /etc/redis/sentinel.conf exec docker-entrypoint.sh redis-server /etc/redis/sentinel.conf --sentinel
只要在redis-cluster目录下,依次执行以下命令即可
注意:192.168.80.19为本机ip
docker-compose build docker-compose up -d
附上springboot的配置
application.yml
#Spring配置 spring redis: #host: 192.168.80.19 #port: 6379 password: database: 0 sentinel: master: mymaster nodes: 192.168.80.19:26379,192.168.80.19:26380,192.168.80.19:26381
原文地址:https://www.cnblogs.com/DurantSimpson/p/11603758.html
时间: 2024-10-12 09:51:02