1. 程式人生 > >docker搭建redis一主二從哨兵模式叢集

docker搭建redis一主二從哨兵模式叢集

1、IP規劃

master 192.168.0.11:6379

slave1 192.168.0.12:6379

slave2 192.168.0.13:6379

sentinel1 192.168.0.14:26379

sentinel2 192.168.0.15:26379

sentinel3 192.168.0.16:26379

2、目錄規劃

KumasMBA:~ kumas$ ls -l /Users/kumas/Applications/Docker/redis/cluster4sentinels/

total 0

drwxr-xr-x 2 kumas staff 68 8 9 15:28 redis01

drwxr-xr-x 2 kumas staff 68 8 9 15:29 redis02

drwxr-xr-x 2 kumas staff 68 8 9 15:29 redis03

drwxr-xr-x 2 kumas staff 68 8 9 15:28 sentinel01

drwxr-xr-x 2 kumas staff 68 8 9 15:28 sentinel02

drwxr-xr-x 2 kumas staff 68 8 9 15:28 sentinel03

3、啟動master

3.1 啟動一個master(注意:測試使用,沒有expose對外的埠,需要則另加-p 指定)

docker run -d  --name redis01 --network devnet --ip 192.168.0.11  -v /Users/kumas/Applications/Docker/redis/cluster4sentinels/redis01:/data redis:3.2-alpine  --appendonly yes --port 6379

3.2 啟動兩個slave

docker run -d  --name redis02 --network devnet --ip 192.168.0.12   -v /Users/kumas/Applications/Docker/redis/cluster4sentinels/redis02:/data redis:3.2-alpine  --appendonly yes --port 6379  --slave-read-only yes --slaveof 192.168.0.11 6379

docker run -d --name redis03 --network devnet --ip 192.168.0.13  -v /Users/kumas/Applications/Docker/redis/cluster4sentinels/redis03:/data redis:3.2-alpine  --appendonly yes --port 6379  --slave-read-only yes --slaveof 192.168.0.11 6379

4、啟動sentinel

docker run -d --network devnet --ip 192.168.0.14 --name sentinel01 -v /Users/kumas/Applications/Docker/redis/cluster4sentinels/sentinel01:/data redis:3.2-alpine /data/sentinel.conf --sentinel

docker run -d --network devnet --ip 192.168.0.15 --name sentinel02 -v /Users/kumas/Applications/Docker/redis/cluster4sentinels/sentinel02:/data redis:3.2-alpine /data/sentinel.conf --sentinel

docker run -d --network devnet --ip 192.168.0.16 --name sentinel03 -v /Users/kumas/Applications/Docker/redis/cluster4sentinels/sentinel03:/data redis:3.2-alpine /data/sentinel.conf --sentinel
KumasMBA:~ kumas$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
f0af17a64201        redis:3.2-alpine    "docker-entrypoint..."   3 seconds ago       Up 2 seconds        6379/tcp                 sentinel03
41fa659c1e48        redis:3.2-alpine    "docker-entrypoint..."   11 seconds ago      Up 10 seconds       6379/tcp                 sentinel02
7ce382ea3912        redis:3.2-alpine    "docker-entrypoint..."   3 minutes ago       Up 2 minutes        6379/tcp                 sentinel01
f20793fa6676        redis:3.2-alpine    "docker-entrypoint..."   8 minutes ago       Up 8 minutes        6379/tcp                 redis03
550954a24cbb        redis:3.2-alpine    "docker-entrypoint..."   9 minutes ago       Up 9 minutes        6379/tcp                 redis02
01d1493ef2bf        redis:3.2-alpine    "docker-entrypoint..."   11 minutes ago      Up 11 minutes       0.0.0.0:6379->6379/tcp   redis01

ok,可以測試了。

5、附:sentinel.conf樣例

port 26379
dir "/data"
logfile "/data/sentinel-01.log"
#bind 192.168.0.14
sentinel myid 203fb658bc42b4d3f5b77968b4f048e4d1df9889
sentinel monitor mymaster 192.168.0.11 6379 2
sentinel failover-timeout mymaster 60000
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
# Generated by CONFIG REWRITE
sentinel known-slave mymaster 192.168.0.12 6379
sentinel known-slave mymaster 192.168.0.13 6379
sentinel known-sentinel mymaster 192.168.0.16 26379 677facf0a38105a12cfeb8118a32195c92629a88
sentinel known-sentinel mymaster 192.168.0.15 26379 862a852ba0052b74297cc09f9c73b9b6bab129cc
sentinel current-epoch 0