Redis 容器化部署一主兩從三哨兵模式
reids主從配置完成
- 鏡像下載下傳
[root@CCLOUD01 ~]# docker images|grep redis
redis 5.0.7 7eed8df88d3b 8 months ago 98.2MB
- 用docker啟動redis執行個體
docker run -d --name redis-master --restart=always -p 6380:6379 redis:5.0.7 --requirepass 1qaz@WSX --masterauth 1qaz@WSX
docker run -d --name redis-slave1 --restart=always -p 6381:6379 redis:5.0.7 --requirepass 1qaz@WSX --masterauth 1qaz@WSX
docker run -d --name redis-slave2 --restart=always -p 6382:6379 redis:5.0.7 --requirepass 1qaz@WSX --masterauth 1qaz@WSX
-d 以守護程序模式運作
-p 将容器的6380端口映射到主控端的6379端口
--requirepass 設定redis密碼
--masterauth 設定連接配接主服務的密碼,需要和requirepass設定一樣
- 配置redis主從叢集
docker inspect redis-master|egrep -w "IPAddress" # master: 172.17.0.7
docker inspect redis-slave1|egrep -w "IPAddress" # redis-slave1: 172.17.0.8
docker inspect redis-slave2|egrep -w "IPAddress" # redis-slave2: 172.17.0.9
- 進入redis容器配置
[root@CCLOUD01 ~]# docker exec -it redis-master /bin/bash
root@a59d956a146f:/data# redis-cli -a 1qaz@WSX
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:master # 會發現三個都是master
connected_slaves:0
master_replid:8c12c1482f8217057e6b4e0655325fe6bdd43bb1
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
127.0.0.1:6379>
- 進入
redis-slave1
進行配置
redis-slave2
[root@CCLOUD01 ~]# docker exec -it redis-slave1 /bin/bash
root@74ee44150667:/data# redis-cli -a 1qaz@WSX
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> SLAVEOF 172.20.31.130 6380 # 配置master節點主機映射後的的IP Port
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:172.20.31.130
master_port:6380
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_repl_offset:84
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:3a41a54a2c2784b7dd553651aa942e4aa3319723
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:84
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:84
** 配置redis sentinel哨兵**
- 建立配置檔案
1. mkdir /redis_data/redis && /redis_data/redis
2. vim sentinel1.conf
sentinel monitor mymaster 172.20.31.130 6380 2
daemonize yes
sentinel auth-pass mymaster 1qaz@WSX
sentinel down-after-milliseconds mymaster 10000
logfile "/data/log.txt"
port 26379
#sentinel monior mymaster 172.20.31.130 6380 2 監聽名為mymaster(名字随便起)的主服務 ip為172.18.0.2 端口為6379
#daemonize yes 以守護程序方式運作
#sentinel auth-pass mymaster <password> 驗證主redis密碼
#sentinel down-after-milliseconds mymaster 10000 超過10秒沒有響應認為下線
#sentinel failover-timeout mymaster 60000 60秒逾時
#logfile "/data/log.txt" 日志輸出位置
3. cp sentinel1.conf sentinel2.conf
cp sentinel1.conf sentinel3.conf
- 啟動單個
容器
redis-sentinal
docker run -it --name redis_sentinel1 --restart=always -p 26380:26379 -v /redis_data/redis/sentinel1.conf:/data/sentinel.conf -d redis:5.0.7
docker run -it --name redis_sentinel2 --restart=always -p 26381:26379 -v /redis_data/redis/sentinel2.conf:/data/sentinel.conf -d redis:5.0.7
docker run -it --name redis_sentinel3 --restart=always -p 26382:26379 -v /redis_data/redis/sentinel3.conf:/data/sentinel.conf -d redis:5.0.7
- 分别進入3個sentinel容器中啟動sentinel
docker exec -it redis_sentinel1 /bin/bash
redis-sentinel sentinel.conf
docker exec -it redis_sentinel2 /bin/bash
redis-sentinel sentinel.conf
docker exec -it redis_sentinel3 /bin/bash
redis-sentinel sentinel.conf
- 驗證檢視
[root@CCLOUD01 ~]# redis-cli -p 26381 -a 1qaz@WSX
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:26381> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=172.20.31.130:6380,slaves=1,sentinels=3
127.0.0.1:26381>