前言 由於本人是個docker控,不喜歡安裝各種環境,而且安裝redis trib也有點繁瑣,索性用docker來做redis cluster。 本文用的是偽集群,真正的集群放到不同的機器即可。埠是7001 7006。 工作目錄: 創建文件夾 首先創建一堆對應埠的文件夾,下麵是腳本 create ...
前言
由於本人是個docker控,不喜歡安裝各種環境,而且安裝redis-trib也有點繁瑣,索性用docker來做redis cluster。
本文用的是偽集群,真正的集群放到不同的機器即可。埠是7001-7006。
工作目錄:/data/redis
創建文件夾
首先創建一堆對應埠的文件夾,下麵是腳本
create.sh
for i in `seq 7001 7006`
do
mkdir -p ${i}/data
done
添加執行許可權並執行
chmod 777 create.sh
./create.sh
部署redis
本人不喜歡docker run,所以用了docker-compose
。官方文檔 https://docs.docker.com/compose/overview/
創建docker-compose.yml
用publicisworldwide/redis-cluster
鏡像的原因是人家已經把配置文件寫好了,配置文件沒有掛載是懶,別學我
docker-compose.yml
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版本
docker -v
在文檔中可查看docker版本支持的docker-compose.yml版本,為了方便大家查看,我複製出來了。不過一般來說,docker升級比較快,功能迭代也很快,最好還是用最新版本。
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,不然後續的確認沒法繼續
docker run --rm -it 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
會出現
>>> 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
,但是不行,不知道什麼原因。