最近站长阅读了一篇WordPress速度优化教程,从中了解到redis可以搭建集群系统。
由于站长一直使用的redis免费版的插件,经常会遇到redis突然崩溃的问题。
一直对于redis崩溃给用户带来的影响没有很好的解决办法。
从这篇文章中站长获取到一些相关的信息,然后通过AI给出方案和搭建。
经过一上午的实验终于完成了搭建。
然后wp-config.php 只需要填写一个IP和端口连接HAProxy 就可以自动实现负载均衡和容灾自动迁移。

这个是可以正常运行的容器一共7个容器。
下面给出编排脚本 docker-compose.yml
version: '3.8'
services:
redis-master:
image: redis:7-alpine
container_name: redis-master
mem_limit: 128m
environment:
- REDIS_PASSWORD=redis密码
command:
- sh
- -c
- |
exec redis-server --port 6380 \
--requirepass "$$REDIS_PASSWORD" \
--masterauth "$$REDIS_PASSWORD" \
--maxmemory 128mb \
--maxmemory-policy allkeys-lru \
--appendonly no \
--save "" \
--activedefrag yes \
--hz 100
networks:
- redis-net
restart: always
healthcheck:
test: ["CMD-SHELL", "redis-cli --no-auth-warning -a $$REDIS_PASSWORD ping"]
interval: 5s
timeout: 3s
retries: 3
redis-slave1:
image: redis:7-alpine
container_name: redis-slave1
mem_limit: 128m
environment:
- REDIS_PASSWORD=redis密码
command:
- sh
- -c
- |
exec redis-server --port 6381 \
--requirepass "$$REDIS_PASSWORD" \
--masterauth "$$REDIS_PASSWORD" \
--replicaof redis-master 6380 \
--maxmemory 128mb \
--maxmemory-policy allkeys-lru \
--appendonly no \
--save "" \
--activedefrag yes \
--hz 100
networks:
- redis-net
restart: always
depends_on:
- redis-master
redis-slave2:
image: redis:7-alpine
container_name: redis-slave2
mem_limit: 128m
environment:
- REDIS_PASSWORD=redis密码
command:
- sh
- -c
- |
exec redis-server --port 6382 \
--requirepass "$$REDIS_PASSWORD" \
--masterauth "$$REDIS_PASSWORD" \
--replicaof redis-master 6380 \
--maxmemory 128mb \
--maxmemory-policy allkeys-lru \
--appendonly no \
--save "" \
--activedefrag yes \
--hz 100
networks:
- redis-net
restart: always
depends_on:
- redis-master
sentinel1:
image: redis:7-alpine
container_name: sentinel1
mem_limit: 64m
user: root
environment:
- REDIS_PASSWORD=redis密码
command:
- sh
- -c
- |
mkdir -p /data/conf
echo "port 26379" > /data/conf/sentinel.conf
echo "bind 0.0.0.0" >> /data/conf/sentinel.conf
echo "protected-mode no" >> /data/conf/sentinel.conf
echo "sentinel resolve-hostnames yes" >> /data/conf/sentinel.conf
echo "sentinel announce-hostnames yes" >> /data/conf/sentinel.conf
echo "sentinel monitor mymaster redis-master 6380 2" >> /data/conf/sentinel.conf
echo "sentinel down-after-milliseconds mymaster 5000" >> /data/conf/sentinel.conf
echo "sentinel parallel-syncs mymaster 1" >> /data/conf/sentinel.conf
echo "sentinel failover-timeout mymaster 15000" >> /data/conf/sentinel.conf
echo "sentinel auth-pass mymaster $$REDIS_PASSWORD" >> /data/conf/sentinel.conf
exec redis-server /data/conf/sentinel.conf --sentinel
ports:
- "26379:26379"
networks:
- redis-net
restart: always
depends_on:
redis-master:
condition: service_healthy
sentinel2:
image: redis:7-alpine
container_name: sentinel2
mem_limit: 64m
user: root
environment:
- REDIS_PASSWORD=redis密码
command:
- sh
- -c
- |
mkdir -p /data/conf
echo "port 26379" > /data/conf/sentinel.conf
echo "bind 0.0.0.0" >> /data/conf/sentinel.conf
echo "protected-mode no" >> /data/conf/sentinel.conf
echo "sentinel resolve-hostnames yes" >> /data/conf/sentinel.conf
echo "sentinel announce-hostnames yes" >> /data/conf/sentinel.conf
echo "sentinel monitor mymaster redis-master 6380 2" >> /data/conf/sentinel.conf
echo "sentinel down-after-milliseconds mymaster 5000" >> /data/conf/sentinel.conf
echo "sentinel parallel-syncs mymaster 1" >> /data/conf/sentinel.conf
echo "sentinel failover-timeout mymaster 15000" >> /data/conf/sentinel.conf
echo "sentinel auth-pass mymaster $$REDIS_PASSWORD" >> /data/conf/sentinel.conf
exec redis-server /data/conf/sentinel.conf --sentinel
ports:
- "26380:26379"
networks:
- redis-net
restart: always
depends_on:
redis-master:
condition: service_healthy
sentinel3:
image: redis:7-alpine
container_name: sentinel3
mem_limit: 64m
user: root
environment:
- REDIS_PASSWORD=redis密码
command:
- sh
- -c
- |
mkdir -p /data/conf
echo "port 26379" > /data/conf/sentinel.conf
echo "bind 0.0.0.0" >> /data/conf/sentinel.conf
echo "protected-mode no" >> /data/conf/sentinel.conf
echo "sentinel resolve-hostnames yes" >> /data/conf/sentinel.conf
echo "sentinel announce-hostnames yes" >> /data/conf/sentinel.conf
echo "sentinel monitor mymaster redis-master 6380 2" >> /data/conf/sentinel.conf
echo "sentinel down-after-milliseconds mymaster 5000" >> /data/conf/sentinel.conf
echo "sentinel parallel-syncs mymaster 1" >> /data/conf/sentinel.conf
echo "sentinel failover-timeout mymaster 15000" >> /data/conf/sentinel.conf
echo "sentinel auth-pass mymaster $$REDIS_PASSWORD" >> /data/conf/sentinel.conf
exec redis-server /data/conf/sentinel.conf --sentinel
ports:
- "26381:26379"
networks:
- redis-net
restart: always
depends_on:
redis-master:
condition: service_healthy
haproxy:
image: haproxy:alpine
container_name: redis-haproxy
mem_limit: 64m
user: root
environment:
- REDIS_PASSWORD=redis密码
command:
- sh
- -c
- |
echo "global" > /tmp/haproxy.cfg
echo " log stdout format raw local0" >> /tmp/haproxy.cfg
echo " maxconn 4096" >> /tmp/haproxy.cfg
echo "defaults" >> /tmp/haproxy.cfg
echo " mode tcp" >> /tmp/haproxy.cfg
echo " log global" >> /tmp/haproxy.cfg
echo " option tcplog" >> /tmp/haproxy.cfg
echo " timeout connect 5s" >> /tmp/haproxy.cfg
echo " timeout client 30s" >> /tmp/haproxy.cfg
echo " timeout server 30s" >> /tmp/haproxy.cfg
echo "frontend redis_frontend" >> /tmp/haproxy.cfg
echo " bind *:6390" >> /tmp/haproxy.cfg
echo " default_backend redis_backend" >> /tmp/haproxy.cfg
echo "backend redis_backend" >> /tmp/haproxy.cfg
echo " mode tcp" >> /tmp/haproxy.cfg
echo " balance roundrobin" >> /tmp/haproxy.cfg
echo " option tcp-check" >> /tmp/haproxy.cfg
echo " tcp-check send \"AUTH $$REDIS_PASSWORD\r\n\"" >> /tmp/haproxy.cfg
echo " tcp-check expect string \"+OK\"" >> /tmp/haproxy.cfg
echo " tcp-check send \"INFO replication\r\n\"" >> /tmp/haproxy.cfg
echo " tcp-check expect string \"role:master\"" >> /tmp/haproxy.cfg
echo " server redis-master redis-master:6380 check inter 2s rise 2 fall 3" >> /tmp/haproxy.cfg
echo " server redis-slave1 redis-slave1:6381 check inter 2s rise 2 fall 3 backup" >> /tmp/haproxy.cfg
echo " server redis-slave2 redis-slave2:6382 check inter 2s rise 2 fall 3 backup" >> /tmp/haproxy.cfg
exec haproxy -f /tmp/haproxy.cfg
ports:
- "6390:6390"
networks:
- redis-net
restart: always
depends_on:
- redis-master
- redis-slave1
- redis-slave2
- sentinel1
- sentinel2
- sentinel3
networks:
redis-net:
driver: bridge
name: redis-sentinel-net
然后脚本的配置大概是限制大小是128M,这个根据自身的服务器内存大小进行修改也可以让AI去修改。
以下关键参数信息
满了就按最近最少使用淘汰(allkeys-lru)
关闭持久化(纯缓存用,不要 RDB/AOF,快了不止一点)
开启主动碎片整理和异步删除,防止内存膨胀。
如果您想通过简单的【宝塔docker】进行【容器编排】实现redis集群,欢迎参考本文章。
欢迎关注 www.7chaowan.com
请在下载后24小时内删除,切勿商用。使用者需自行承担相应法律责任,发布者概不负责。







暂无评论内容