redis集群部署方案

本文介绍了两种Redis集群的部署方案:一种是在Portainer中直接安装,另一种是通过上传docker-entrypoint.sh脚本和redis配置文件来实现。每种方案都需要修改后端服务的配置文件,取消主机和端口设置,启用集群配置。对于第二种方案,还涉及到多个redis-node.conf文件的配置,特别是修改port值以区分不同节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方案一:在portainer上进行安装

#如果集群故障了,则所有节点重启一次即可  如果单独重启了启动节点  则把启动节点数改为0  不然会一直在重启
version: '3.8'

#网络模式配置  redis集群固定要用host模式
x-common-ports: &common-ports 
      protocol: tcp
      mode: host
      
#日志大小设置
x-logging: &default-logging
  options:
    max-size: "3g"
    max-file: "1"
  driver: json-file
      
#公共参数
x-common-setting: &common-setting 
    image: master:5000/redis:5.0.7
    logging: *default-logging
    restart: always
    networks:
      - default

#集群启动节点 
x-common-startup-setting: &common-startup-setting 
    << : *common-setting
    deploy:
      restart_policy:
        condition: on-failure 
    
#redis工作节点的资源配置
x-common-worker-deploy-resources: &common-worker-deploy-resources 
      resources:
        limits:
          memory: 1030M
        reservations:
          memory: 1030M
        
#redis工作节点的基础配置
x-common-worker-setting: &common-worker-setting 
    << : *common-setting
    entrypoint:
      - "redis-server"
      - "--bind 0.0.0.0" 
      - "--cluster-enabled"
      - "yes"
      - "--cluster-config-file"
      - "nodes.conf"
      - "--cluster-node-timeout"
      - "5000"
      - "--requirepass"
      - "37621040"
      - "--masterauth"
      - "37621040"
      - "--maxmemory"
      - "1024MB"
      - "--maxmemory-policy"
      - "volatile-lru"
      - "--appendonly"
      - "yes"      
      
services:       
  redis-startup:
    << : *common-startup-setting
    command:   
      - "/bin/bash"
      - "-c"      
      #主从模式 至少6个工作节点
      - "redis-cli -h 192.168.35.210 -p 26379 -a 37621040 --cluster create 192.168.35.210:26379 192.168.35.211:26380 192.168.35.212:26381 192.168.35.213:26382 192.168.35.214:26383 192.168.35.215:26384 --cluster-replicas 1 --cluster-yes;"   
      - "redis-cli  -h 192.168.35.210 -p 26379 -a 37621040 -c --cluster info 192.168.35.210:26379;redis-server;"
    depends_on:
      - redis1
      - redis2
      - redis3
      - redis4
      - redis5
      - redis6
      
  redis1:
    << : *common-worker-setting
    #host模式 启动集群以及后续连接的时候都要指定的ip  所以固定在某台主机上
    deploy: 
      << : *common-worker-deploy-resources
      placement:
        constraints: [node.Hostname == p1]
    #host模式 端口不能冲突 指定端口
    command:   
      - "--port"
      - "26379"
    ports:   
        - target: 26379
          published: 26379
          << : *common-ports
          
  redis2:
    << : *common-worker-setting
    #host模式 启动集群以及后续连接的时候都要指定的ip  所以固定在某台主机上
    deploy: 
      << : *common-worker-deploy-resources
      placement:
        constraints: [node.Hostname == p2]
    #host模式 端口不能冲突 指定端口
    command:   
      - "--port"
      - "26380"
    ports:   
        - target: 26380
          published: 26380
          << : *common-ports
          
  redis3:
    << : *common-worker-setting
    #host模式 启动集群以及后续连接的时候都要指定的ip  所以固定在某台主机上
    deploy: 
      << : *common-worker-deploy-resources
      placement:
        constraints: [node.Hostname == p3]
    #host模式 端口不能冲突 指定端口
    command:   
      - "--port"
      - "26381"
    ports:   
        - target: 26381
          published: 26381
          << : *common-ports
          
  redis4:
    << : *common-worker-setting
    #host模式 启动集群以及后续连接的时候都要指定的ip  所以固定在某台主机上
    deploy: 
      << : *common-worker-deploy-resources
      placement:
        constraints: [node.Hostname == p4]
    #host模式 端口不能冲突 指定端口
    command:   
      - "--port"
      - "26382"
    ports:   
        - target: 26382
          published: 26382
          << : *common-ports
          
  redis5:
    << : *common-worker-setting
    #host模式 启动集群以及后续连接的时候都要指定的ip  所以固定在某台主机上
    deploy: 
      << : *common-worker-deploy-resources
      placement:
        constraints: [node.Hostname == p5]
    #host模式 端口不能冲突 指定端口
    command:   
      - "--port"
      - "26383"
    ports:   
        - target: 26383
          published: 26383
          << : *common-ports
          
  redis6:
    << : *common-worker-setting
    #host模式 启动集群以及后续连接的时候都要指定的ip  所以固定在某台主机上
    deploy: 
      << : *common-worker-deploy-resources
      placement:
        constraints: [node.Hostname == p6]
    #host模式 端口不能冲突 指定端口
    command:   
      - "--port"
      - "26384"
    ports:   
        - target: 26384
          published: 26384
          << : *common-ports
networks:
  portainer_agent_network:
    external: true
  default:
    external:
      name: host

后端服务的配置文件redis添加配置,注释掉host,port配置,添加cluster配置!

方案二:在portaainer上进行安装,并且需要在服务器上上传docker-entrypoint.sh脚本,redis-node1-6.conf文件

version: '3.8'    
services: 
  redis-0:
    image: master:5000/redis:5.0.7    
    restart: always
    networks:
      - default
    entrypoint : ["/bin/bash", "-c", "/usr/local/bin/docker-entrypoint.sh"]
    volumes:
      - "/data/share/common/redis/docker-entrypoint.sh:/usr/local/bin/docker-entrypoint.sh"         
    deploy:
      restart_policy:
        condition: on-failure       
      placement:
        constraints: [node.hostname == p1] 
    depends_on:
      - redis-1
      - redis-2
      - redis-3
      - redis-4
      - redis-5
      - redis-6
      
  redis-1:
    image: master:5000/redis:5.0.7
    container_name: redis-1
    hostname: redis1
    restart: always
    networks:
      - default
    entrypoint: ["redis-server","/etc/redis.conf"]
    volumes:
      - "/data/share/common/redis/redis-node1.conf:/etc/redis.conf"      
    ports:
      - target: 26379
        published: 26379
        protocol: tcp
        mode: host       
    deploy:
      placement:
        constraints: [node.hostname == p1] 
    
  redis-2:
    image: master:5000/redis:5.0.7
    container_name: redis-2
    hostname: redis2
    restart: always
    networks:
      - default
    entrypoint: ["redis-server","/etc/redis.conf"]
    volumes:
      - "/data/share/common/redis/redis-node2.conf:/etc/redis.conf"
    ports:
      - target: 26380
        published: 26380
        protocol: tcp
        mode: host      
    deploy:
      placement:
        constraints: [node.hostname == p2] 
        
  redis-3:
    image: master:5000/redis:5.0.7
    container_name: redis-3
    hostname: redis3
    restart: always
    networks:
      - default
    entrypoint: ["redis-server","/etc/redis.conf"]
    volumes:
      - "/data/share/common/redis/redis-node3.conf:/etc/redis.conf"
    ports:
      - target: 26381
        published: 26381
        protocol: tcp
        mode: host       
    deploy:
      placement:
        constraints: [node.hostname == p3] 
        
  redis-4:
    image: master:5000/redis:5.0.7
    container_name: redis-4
    hostname: redis4
    restart: always
    networks:
      - default
    entrypoint: ["redis-server","/etc/redis.conf"]
    volumes:
      - "/data/share/common/redis/redis-node4.conf:/etc/redis.conf"
    ports:
      - target: 26382
        published: 26382
        protocol: tcp
        mode: host       
    deploy:
      placement:
        constraints: [node.hostname == p4] 
        
  redis-5:
    image: master:5000/redis:5.0.7
    container_name: redis-5
    hostname: redis5
    restart: always
    networks:
      - default
    entrypoint: ["redis-server","/etc/redis.conf"]
    volumes:
      - "/data/share/common/redis/redis-node5.conf:/etc/redis.conf"
    ports:
      - target: 26383
        published: 26383
        protocol: tcp
        mode: host       
    deploy:
      placement:
        constraints: [node.hostname == p5] 
  redis-6:
    image: master:5000/redis:5.0.7
    container_name: redis-6
    hostname: redis6
    restart: always
    networks:
      - default
    entrypoint: ["redis-server","/etc/redis.conf"]
    volumes:
      - "/data/share/common/redis/redis-node6.conf:/etc/redis.conf"
    ports:
      - target: 26384
        published: 26384
        protocol: tcp
        mode: host 
     
    deploy:
      placement:
        constraints: [node.hostname == p6] 
  
networks:
  default:
    external:
      name: host

docker-entrypoint.sh脚本如下:

#!/bin/sh
set -e 
redis-cli -h 127.0.0.1 -p 26379 -a 37621040 --cluster create 192.168.35.210:26379 192.168.35.211:26380 192.168.35.212:26381 192.168.35.213:26382 192.168.35.214:26383 192.168.35.215:26384 --cluster-replicas 1 --cluster-yes 

redis-cli  -h 127.0.0.1 -p 26379 -a 37621040 -c --cluster info 192.168.35.210:26379

redis-server /etc/redis.conf

 redis-node1.conf文件如下

port 26379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
requirepass 37621040

redis-node2.conf.........redis-node6.conf的文件和redis-node1.conf一样,只需修改里面的port值!!!

 后端服务的配置文件redis添加配置,注释掉host,port配置,添加cluster配置!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值