docker搭建redis集群
一、编写脚步建立配置redis的配置文件
1、建立配置文件脚本
vim redis. sh
for port in $( seq 1 6 ) ; \
do \
mkdir - p / volumn/ redis/ node- ${ port} / conf
touch / volumn/ redis/ node- ${ port} / conf/ redis. conf
cat << EOF > / volumn/ redis/ node- ${ port} / conf/ redis. conf
port 6379
bind 0.0 .0 .0
cluster- enabled yes
cluster- config- file nodes. conf
cluster- node- timeout 5000
cluster- announce- ip 172.38 .0 .1 ${ port}
cluster- announce- port 6379
cluster- announce- bus- port 16379
appendonly yes
EOF
done
2、运行脚本
sh redis. sh
二、启动六个redis服务
1、创建redis的网关
docker network create redis - - subnet 172.38 .0 .0 / 16
2、启动redis服务
docker run - p 39301 : 6379 - p 39302 : 16379 - - name redis- 1 \
- v / volumn/ redis/ node- 1 / data: / data \
- v / volumn/ redis/ node- 1 / conf/ redis. conf: / etc/ redis/ redis. conf \
- d - - net redis - - ip 172.38 .0 .11 redis: 5.0 .9 - alpine3. 11 redis- server / etc/ redis/ redis. conf
docker run - p 39303 : 6379 - p 39304 : 16379 - - name redis- 2 \
- v / volumn/ redis/ node- 2 / data: / data \
- v / volumn/ redis/ node- 2 / conf/ redis. conf: / etc/ redis/ redis. conf \
- d - - net redis - - ip 172.38 .0 .12 redis: 5.0 .9 - alpine3. 11 redis- server / etc/ redis/ redis. conf
docker run - p 39305 : 6379 - p 39306 : 16379 - - name redis- 3 \
- v / volumn/ redis/ node- 3 / data: / data \
- v / volumn/ redis/ node- 3 / conf/ redis. conf: / etc/ redis/ redis. conf \
- d - - net redis - - ip 172.38 .0 .13 redis: 5.0 .9 - alpine3. 11 redis- server / etc/ redis/ redis. conf
docker run - p 39307 : 6379 - p 39308 : 16379 - - name redis- 4 \
- v / volumn/ redis/ node- 4 / data: / data \
- v / volumn/ redis/ node- 4 / conf/ redis. conf: / etc/ redis/ redis. conf \
- d - - net redis - - ip 172.38 .0 .14 redis: 5.0 .9 - alpine3. 11 redis- server / etc/ redis/ redis. conf
docker run - p 39309 : 6379 - p 39310 : 16379 - - name redis- 5 \
- v / volumn/ redis/ node- 5 / data: / data \
- v / volumn/ redis/ node- 5 / conf/ redis. conf: / etc/ redis/ redis. conf \
- d - - net redis - - ip 172.38 .0 .15 redis: 5.0 .9 - alpine3. 11 redis- server / etc/ redis/ redis. conf
docker run - p 39311 : 6379 - p 39312 : 16379 - - name redis- 6 \
- v / volumn/ redis/ node- 6 / data: / data \
- v / volumn/ redis/ node- 6 / conf/ redis. conf: / etc/ redis/ redis. conf \
- d - - net redis - - ip 172.38 .0 .16 redis: 5.0 .9 - alpine3. 11 redis- server / etc/ redis/ redis. conf
三、创建集群
1、进入容器
docker exec - it redis- 1 / bin / sh
2、创建集群
redis- cli - - cluster create 172.38 .0 .11 : 6379 172.38 .0 .12 : 6379 172.38 .0 .13 : 6379 172.38 .0 .14 : 6379 172.38 .0 .15 : 6379 172.38 .0 .16 : 6379 - - cluster- replicas 1
3、查看集群信息
redis- cli - c cluster info cluste nodes