下载 redis7.x

部署swarm redis-cluster集群

  • redis.conf
port 7001
maxclients 10000
requirepass 123456 #带密码
maxmemory 1GB

appendonly yes
cluster-enabled yes
cluster-config-file nodes7001.conf
protected-mode no
masterauth 123456

cluster-node-timeout 15000
cluster-announce-ip redis7001 
cluster-announce-port 7001
cluster-announce-bus-port 17001
#daemonize yes  #不能加这个否则无法启动

deploy.yml (部署代码)

version: "3.9"
services:
  redis7001:
    image: redis:7.0.5-alpine
    user: root 
    volumes: 
      - "/data/redis/7001:/data:rw"
    ports:
      - "7001:7001"
    command: ['redis-server','/home/redis.conf']    
    environment:
      - TZ=Asia/Shanghai
    deploy:
      mode: replicated
      replicas: 1
      placement:
        constraints:
          - node.hostname == node1
          #- node.role == manager
    networks:
    - net1116
networks:
  net1116:
    name: net-redis-cluster 
    #网络要先创建 命令  docker network create --driver overlay net-redis-cluster
    external: true
  • 部署6个服务,启动6个redis 端口依次(7001-7006,17001-17006)
    docker stack deploy -c /data/redis/redis7001.yml rds

redis-cli 初始化集群

#进入第一台redis sh
docker exec -it rds_redis7001.1.vwplbnib1pfpgrb0862m2z2wy sh
#使用集群内主机名创建集群
redis-cli -h redis7001 -p 7001 -a 123456 --cluster create redis7001:7001 redis7002:7002 redis7003:7003 redis7004:7004 redis7005:7005 redis7006:7006 --cluster-replicas 1 --cluster-yes

注:

—cluster-replicas 1 一主一从模式最少6台redis节点才可以
—cluster-replicas 0 只需要3台机子即可,不创建从
—cluster-yes

查看集群信息

docker exec -it rds_redis7001.1.vwplbnib1pfpgrb0862m2z2wy sh
redis-cli -h redis7001 -p 7001 -a 123456
info replication #检测集群状态
#查看信息
cluster info 
cluster nodes 查看节点

测试集群


进入第一台redis:
docker exec -it rds_redis7001.1.vwplbnib1pfpgrb0862m2z2wy sh
redis-cli -h redis7001 -p 7001 -a 123456
set user "zhshan"

进入另一台服务器 
get user
set node "7003"

进入再另一台服务器 
get user
get node