docker-redis叢集

@licheng發表於2020-10-20

目錄

介紹

  • 簡化部署

redis

  • redis.sh指令碼,在任意目錄執行都行
#!/bin/bash
mkdir -p /data/redis/700{1..6}/data 
cd /data/redis/
echo "version: '3'

services:
 redis1:
  image: publicisworldwide/redis-cluster
  network_mode: host
  restart: always
  volumes:
   - /data/redis/7001/data:/data
  environment:
   - REDIS_PORT=7001

 redis2:
  image: publicisworldwide/redis-cluster
  network_mode: host
  restart: always
  volumes:
   - /data/redis/7002/data:/data
  environment:
   - REDIS_PORT=7002

 redis3:
  image: publicisworldwide/redis-cluster
  network_mode: host
  restart: always
  volumes:
   - /data/redis/7003/data:/data
  environment:
   - REDIS_PORT=7003

 redis4:
  image: publicisworldwide/redis-cluster
  network_mode: host
  restart: always
  volumes:
   - /data/redis/7004/data:/data
  environment:
   - REDIS_PORT=7004

 redis5:
  image: publicisworldwide/redis-cluster
  network_mode: host
  restart: always
  volumes:
   - /data/redis/7005/data:/data
  environment:
   - REDIS_PORT=7005

 redis6:
  image: publicisworldwide/redis-cluster
  network_mode: host
  restart: always
  volumes:
   - /data/redis/7006/data:/data
  environment:
   - REDIS_PORT=7006" > docker-compose.yml
docker-compose up -d  # 需要安裝docker-compose

docker-compose up -d 執行後會出現,都為up則啟動成功 注意埠是否被佔用

     Name                   Command               State   Ports
---------------------------------------------------------------
redis_redis1_1   /usr/local/bin/entrypoint. ...   Up           
redis_redis2_1   /usr/local/bin/entrypoint. ...   Up           
redis_redis3_1   /usr/local/bin/entrypoint. ...   Up           
redis_redis4_1   /usr/local/bin/entrypoint. ...   Up           
redis_redis5_1   /usr/local/bin/entrypoint. ...   Up           
redis_redis6_1   /usr/local/bin/entrypoint. ...   Up   

redis-cluster叢集配置

上述只是啟動了6個redis容器,並沒有設定叢集,隨便進入一個redis容器,執行下面的命令,IP替換成自己宿主機的IP,雲伺服器換成外網ip

docker exec -ti d3a904cc0d5a bash 
redis-cli --cluster create 192.168.1.100:7001 192.168.1.100:7002 192.168.1.100:7003 192.168.1.100:7004 192.168.1.100:7005 192.168.1.100:7006 --cluster-replicas 1

> 會詢問是否開啟叢集
> 輸入yes即可
> Can I set the above configuration? (type 'yes' to accept): yes