docker-compose搭建redis集群

一、先制作redis配置模板

命名:vi /data/software/redis-cluster/redis-conf.tpl

port ${PORT}
requirepass b1234
masterauth b1234
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.94.6
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes

二、采用docker-compose创建redis容器

第一步:docker pull redis

第二步:vi /data/software/redis-cluster/createRedis.sh

for port in `seq 4381 4389`; do \
  mkdir -p ./${port}/conf \
  && PORT=${port} envsubst < ./redis-conf.tpl > ./${port}/conf/redis.conf \
  && mkdir -p ./${port}/data; \
done

cd /data/software/redis-cluster && chmod +x createRedis.sh && sh createRedis.sh

三、创建redis集群环境

version: "3.8"
services:
   redis-4381:
        image: redis
        container_name: redis-4381
        restart: always
        network_mode: "host"
        volumes:
          - /data/software/redis-cluster/4381/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /data/software/redis-cluster/4381/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf
   redis-4382:
        image: redis
        container_name: redis-4382
        restart: always
        network_mode: "host"
        volumes:
          - /data/software/redis-cluster/4382/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /data/software/redis-cluster/4382/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf
   redis-4383:
        image: redis
        container_name: redis-4383
        restart: always
        network_mode: "host"
        volumes:
          - /data/software/redis-cluster/4383/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /data/software/redis-cluster/4383/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf
   redis-4384:
        image: redis
        container_name: redis-4384
        restart: always
        network_mode: "host"
        volumes:
          - /data/software/redis-cluster/4384/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /data/software/redis-cluster/4384/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf
   redis-4385:
        image: redis
        container_name: redis-4385
        restart: always
        network_mode: "host"
        volumes:
          - /data/software/redis-cluster/4385/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /data/software/redis-cluster/4385/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf
   redis-4386:
        image: redis
        container_name: redis-4386
        restart: always
        network_mode: "host"
        volumes:
          - /data/software/redis-cluster/4386/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /data/software/redis-cluster/4386/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf
   redis-4387:
        image: redis
        container_name: redis-4387
        restart: always
        network_mode: "host"
        volumes:
          - /data/software/redis-cluster/4387/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /data/software/redis-cluster/4387/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf
   redis-4388:
        image: redis
        container_name: redis-4388
        restart: always
        network_mode: "host"
        volumes:
          - /data/software/redis-cluster/4388/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /data/software/redis-cluster/4388/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf
   redis-4389:
        image: redis
        container_name: redis-4389
        restart: always
        network_mode: "host"
        volumes:
          - /data/software/redis-cluster/4389/conf/redis.conf:/usr/local/etc/redis/redis.conf
          - /data/software/redis-cluster/4389/data:/data
        command: redis-server /usr/local/etc/redis/redis.conf

计划搭建三主六从集群环境,先指定三台master,正常生产环境应位于不同物理机,否则会影响选举,假如2个主节点存在于一个物理机上的情况。

redis-cli --cluster create 192.168.94.6:4381 192.168.94.6:4382 192.168.94.6:4383 --cluster-replicas 0 -a b1234

增加slave节点,生产环境中(master与2slave)三台物理机要错开。

四、增加slave节点

docker exec -it redis-4381 bash

redis-cli -a [password] --cluster add-node [old_host:old_port] -a [password] --cluster-slave [old_id]

redis-cli -a b1234 --cluster add-node 192.168.94.6:4384 192.168.94.6:4381 -a b1234 --cluster-slave 9505b1c4ce14a660166fbc972f5de8d02da98dc5

你可能感兴趣的:(环境搭建,redis,docker,缓存)