docker-compose搭建3主3从redis集群

Z先森 2021-06-29 PM 236℃ 0条

新建目录,并建临时文件

mkdir /opt/redis-cluster && cd /opt/redis-cluster
cat > redis-cluster.tmpl <<EOF
port ${PORT}
requirepass 1234
masterauth 1234
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip 192.168.50.111
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
EOF

创建每个节点所需目录和配置文件

for port in `seq 6371 6376`; \
do \
mkdir -p ${port}/conf   && \
PORT=${port} envsubst < redis-cluster.tmpl > ${port}/conf/redis.conf   && \
mkdir -p ${port}/data;\
done

编写docker-compose文件

cat > docker-compose.yaml <<EOF
version: '3'

# 定义服务,可以多个
services:
  redis-6371: # 服务名称
    image: redis # 创建容器时所需的镜像
    container_name: redis-6371 # 容器名称
    restart: always # 容器总是重新启动
    network_mode: "host" # host 网络模式
    volumes: # 数据卷,目录挂载
      - /opt/redis-cluster/6371/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /opt/redis-cluster/6371/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf # 覆盖容器启动后默认执行的命令

  redis-6372:
    image: redis
    container_name: redis-6372
    restart: always
    network_mode: "host"
    volumes:
      - /opt/redis-cluster/6372/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /opt/redis-cluster/6372/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf

  redis-6373:
    image: redis
    container_name: redis-6373
    restart: always
    network_mode: "host"
    volumes:
      - /opt/redis-cluster/6373/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /opt/redis-cluster/6373/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf

  redis-6374:
    image: redis
    container_name: redis-6374
    restart: always
    network_mode: "host"
    volumes:
      - /opt/redis-cluster/6374/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /opt/redis-cluster/6374/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf

  redis-6375:
    image: redis
    container_name: redis-6375
    restart: always
    network_mode: "host"
    volumes:
      - /opt/redis-cluster/6375/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /opt/redis-cluster/6375/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf

  redis-6376:
    image: redis
    container_name: redis-6376
    restart: always
    network_mode: "host"
    volumes:
      - /opt/redis-cluster/6376/conf/redis.conf:/usr/local/etc/redis/redis.conf
      - /opt/redis-cluster/6376/data:/data
    command: redis-server /usr/local/etc/redis/redis.conf
EOF

构造

docker-compose up -d

创建集群

docker exec -it redis-6371 bash
redis-cli -a 1234 --cluster create 192.168.50.111:6371 192.168.50.111:6372 192.168.50.111:6373 192.168.50.111:6374 192.168.50.111:6375 192.168.50.111:6376 --cluster-replicas 1
#检查集群状态
redis-cli -a 1234 --cluster check 192.168.50.111:6371

标签: none

如无特殊说明,本博所有文章均为博主原创。

评论啦~