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>