ad2020314
游戏联盟分享平台-全自助-免费游戏分享-七玩网
独家出售24-70级附魔端授权、完美六职业、农场BOSS挑战、自定义加密RFS、支持称号图片,同时兼容64位与32位系统。-七玩网
蓝易云香港服务器特惠:29元/月尊享1核1G内存10Mbps CN2线路,大带宽首选,品质推荐,信赖之选!-七玩网
AI DIGEST
AI 摘要
LIVE

站长因免费版Redis插件经常崩溃,从一篇WordPress速度优化教程中获得灵感,决定搭建Redis集群系统。他通过AI给出了解决方案并完成搭建,环境采用一主二从三哨兵加HAProxy的模式,共运行7个容器。WordPress的wp-config.php只需填写HAProxy的IP和端口,即可自动实现负载均衡与故障迁移。脚本配置中,每个Redis容器内存限制设为128MB,并可根据服务器调整;淘汰策略采用allkeys-lru,关闭持久化以提高纯缓存性能。同时开启了主动碎片整理和异步删除,防止内存膨胀。如果希望用宝塔Docker进行容器编排实现Redis集群,可以参考该文章。

【站长技术分享】如何给您的【WordPress】使用上redis集群系统!!!

最近站长阅读了一篇WordPress速度优化教程,从中了解到redis可以搭建集群系统。

由于站长一直使用的redis免费版的插件,经常会遇到redis突然崩溃的问题。

一直对于redis崩溃给用户带来的影响没有很好的解决办法。

从这篇文章中站长获取到一些相关的信息,然后通过AI给出方案和搭建。

经过一上午的实验终于完成了搭建。

具体环境是 一主+二从+三哨兵+HAProxy的模式。

然后wp-config.php 只需要填写一个IP和端口连接HAProxy 就可以自动实现负载均衡和容灾自动迁移。

【站长技术分享】如何给您的【WordPress】使用上redis集群系统!!!

这个是可以正常运行的容器一共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


© 版权声明
THE END
文章不错?点个赞呗
点赞10分享
评论 抢沙发

请登录后发表评论

    暂无评论内容