如何用docker部署redis cluster

前言

因爲本人是個docker控,不喜歡安裝各類環境,並且安裝redis-trib也有點繁瑣,索性用docker來作redis cluster。
本文用的是僞集羣,真正的集羣放到不一樣的機器便可。端口是7001-7006。
工做目錄:/data/redisnode

建立文件夾

首先建立一堆對應端口的文件夾,下面是腳本
create.shredis

for i in `seq 7001 7006`
do
 mkdir -p ${i}/data
done

添加執行權限並執行docker

chmod 777 create.sh
./create.sh

部署redis

本人不喜歡docker run,因此用了docker-compose。官方文檔 https://docs.docker.com/compose/overview/網絡

建立docker-compose.yml

publicisworldwide/redis-cluster鏡像的緣由是人家已經把配置文件寫好了,配置文件沒有掛載是懶,別學我ide

docker-compose.ymlspa

version: '3.4'

x-image:
 &default-image
 publicisworldwide/redis-cluster
x-restart:
 &default-restart
 always
x-netmode:
 &default-netmode
 host

services:
 redis1:
  image: *default-image
  network_mode: *default-netmode
  restart: *default-restart
  volumes:
  - /data/redis/7001/data:/data
  environment:
  - REDIS_PORT=7001

 redis2:
  image: *default-image
  network_mode: *default-netmode
  restart: *default-restart
  volumes:
  - /data/redis/7002/data:/data
  environment:
  - REDIS_PORT=7002

 redis3:
  image: *default-image
  network_mode: *default-netmode
  restart: *default-restart
  volumes:
  - /data/redis/7003/data:/data
  environment:
  - REDIS_PORT=7003

 redis4:
  image: *default-image
  network_mode: *default-netmode
  restart: *default-restart
  volumes:
  - /data/redis/7004/data:/data
  environment:
  - REDIS_PORT=7004

 redis5:
  image: *default-image
  network_mode: *default-netmode
  restart: *default-restart
  volumes:
  - /data/redis/7005/data:/data
  environment:
  - REDIS_PORT=7005

 redis6:
  image: *default-image
  network_mode: *default-netmode
  restart: *default-restart
  volumes:
  - /data/redis/7006/data:/data
  environment:
  - REDIS_PORT=7006

啓動全部redis

docker-compose up -d

若是報錯,那就是版本問題。
查看docker版本3d

docker -v

文檔中可查看docker版本支持的docker-compose.yml版本,爲了方便你們查看,我複製出來了。不過通常來講,docker升級比較快,功能迭代也很快,最好仍是用最新版本。rest

Compose file format Docker Engine release
3.4 17.09.0+
3.3 17.06.0+
3.2 17.04.0+
3.1 1.13.1+
3.0 1.13.0+
2.3 17.06.0+
2.2 1.13.0+
2.1 1.12.0+
2.0 1.10.0+
1.0 1.9.1.+

部署cluster

運行如下命令(inem0o/redis-trib沒有pull會自動pull)
注意:加上-it,否則後續的確認無法繼續code

docker run --rm -it --net host inem0o/redis-trib create --replicas 1 192.168.30.70:7001 192.168.30.70:7002 192.168.30.70:7003 192.168.30.70:7004 192.168.30.70:7005 192.168.30.70:7006

會出現orm

>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.30.70:7001
192.168.30.70:7002
192.168.30.70:7003
Adding replica 192.168.30.70:7004 to 192.168.30.70:7001
Adding replica 192.168.30.70:7005 to 192.168.30.70:7002
Adding replica 192.168.30.70:7006 to 192.168.30.70:7003
M: 5a7bd7698b1fe55beb44faac051d66c8a03fd1b1 192.168.30.70:7001
   slots:0-5460 (5461 slots) master
M: bb8fda08e1dcd39e937443f81b5458e80f52d804 192.168.30.70:7002
   slots:5461-10922 (5462 slots) master
M: d907530ee9f6356e0e61a6c7f4d0cc1b22da1189 192.168.30.70:7003
   slots:10923-16383 (5461 slots) master
S: 52eee69afa751d71c84d5436d14b0e16a37536fa 192.168.30.70:7004
   replicates 5a7bd7698b1fe55beb44faac051d66c8a03fd1b1
S: 701ed2fbb3df9fc63b083818620f5c020d05e323 192.168.30.70:7005
   replicates bb8fda08e1dcd39e937443f81b5458e80f52d804
S: a3548a9dffa225f05786ea2289db65f5f1c623be 192.168.30.70:7006
   replicates d907530ee9f6356e0e61a6c7f4d0cc1b22da1189
Can I set the above configuration? (type 'yes' to accept):

輸入yes

Waiting for the cluster to join.....
>>> Performing Cluster Check (using node 192.168.30.70:7001)
M: 5a7bd7698b1fe55beb44faac051d66c8a03fd1b1 192.168.30.70:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: d907530ee9f6356e0e61a6c7f4d0cc1b22da1189 192.168.30.70:7003@17003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: a3548a9dffa225f05786ea2289db65f5f1c623be 192.168.30.70:7006@17006
   slots: (0 slots) slave
   replicates d907530ee9f6356e0e61a6c7f4d0cc1b22da1189
S: 701ed2fbb3df9fc63b083818620f5c020d05e323 192.168.30.70:7005@17005
   slots: (0 slots) slave
   replicates bb8fda08e1dcd39e937443f81b5458e80f52d804
S: 52eee69afa751d71c84d5436d14b0e16a37536fa 192.168.30.70:7004@17004
   slots: (0 slots) slave
   replicates 5a7bd7698b1fe55beb44faac051d66c8a03fd1b1
M: bb8fda08e1dcd39e937443f81b5458e80f52d804 192.168.30.70:7002@17002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

完成。

遇到的問題

  • 建立集羣的時候會遇到Waiting for the cluster to join....................一直沒有成功
    :網絡模式改爲host,docker run 的方式加上--net host,docker-compose方式加上network_mode: host
    我本來是端口映射 7001:7000+17001:17000,可是不行,不知道什麼緣由。
相關文章
相關標籤/搜索