溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何用docker部署redis cluster的方法

發(fā)布時(shí)間:2020-09-27 20:20:02 來(lái)源:腳本之家 閱讀:246 作者:小新是也 欄目:服務(wù)器

前言

由于本人是個(gè)docker控,不喜歡安裝各種環(huán)境,而且安裝redis-trib也有點(diǎn)繁瑣,索性用docker來(lái)做redis cluster。

本文用的是偽集群,真正的集群放到不同的機(jī)器即可。端口是7001-7006。

工作目錄: /data/redis

創(chuàng)建文件夾

首先創(chuàng)建一堆對(duì)應(yīng)端口的文件夾,下面是腳本

create.sh

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

添加執(zhí)行權(quán)限并執(zhí)行

chmod 777 create.sh
./create.sh

部署redis

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

創(chuàng)建docker-compose.yml

用publicisworldwide/redis-cluster鏡像的原因是人家已經(jīng)把配置文件寫好了,配置文件沒有掛載是懶,別學(xué)我

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

啟動(dòng)所有redis

docker-compose up -d

如果報(bào)錯(cuò),那就是版本問(wèn)題。

查看docker版本

docker -v

在文檔中可查看docker版本支持的docker-compose.yml版本,為了方便大家查看,我復(fù)制出來(lái)了。不過(guò)一般來(lái)說(shuō),docker升級(jí)比較快,功能迭代也很快,最好還是用最新版本。

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

運(yùn)行以下命令(inem0o/redis-trib沒有pull會(huì)自動(dòng)pull)

注意:加上-it,不然后續(xù)的確認(rèn)沒法繼續(xù)

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

會(huì)出現(xiàn)

>>> 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.

完成。

遇到的問(wèn)題

創(chuàng)建集群的時(shí)候會(huì)遇到Waiting for the cluster to join....................一直沒有成功

答:網(wǎng)絡(luò)模式改成host,docker run 的方式加上--net host,docker-compose方式加上network_mode: host。

我原本是端口映射 7001:7000+17001:17000,但是不行,不知道什么原因。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI