docker redis 1主2从+3哨兵

2021-05-11  本文已影响0人  街头民工
# 为了方便,所有密码可以都设置相同的
requirepass yourPassword
# 为了方便,所有密码可以都设置相同的            
masterauth masterPassword           
#关闭保护模式
protected-mode no
#开启后,Redis会把每次写入的数据在接收后都写入 appendonly.aof 文件,每次启动时Redis都会先把这个文件的数据读入内存里
appendonly yes
docker run --name redis1 -v /data/redis-data/node1/redis.conf:/usr/local/etc/redis/redis.conf -d -p 6379:6379 -p 26379:26379  redis redis-server /usr/local/etc/redis/redis.conf
docker inspect 容器id
image.png
# 为了方便,所有密码可以都设置相同的
requirepass yourPassword      
# 为了方便,所有密码可以都设置相同的      
masterauth masterPassword           
#关闭保护模式
protected-mode no
#主容器获得
replicaof 172.18.0.2 6379
appendonly yes
docker run --name redis2 -v /data/redis-data/node2/redis.conf:/usr/local/etc/redis/redis.conf -d -p 6380:6379 -p 26380:26380  redis redis-server /usr/local/etc/redis/redis.conf
docker run --name redis3 -v /data/redis-data/node3/redis.conf:/usr/local/etc/redis/redis.conf -d -p 6381:6379 -p 26381:26381  redis redis-server /usr/local/etc/redis/redis.conf
docker exec -it redis1 bash
redis-cli
info Replication
image.png
#第二台
docker exec -it redis2 bash
redis-cli
SLAVEOF 172.18.0.2 6379
image.png
#第三台
docker exec -it redis3 bash
redis-cli
SLAVEOF 172.18.0.2 6379
image.png
docker exec -it redis1 bash
redis-cli
info Replication
image.png

sentinel 配置[redis哨兵]

#node1/sentinel.conf  [port:指定哨兵监听端口,dir:#sentinel工作目录,logfile:#日志文件,sentinel monitor nameL#启动哨兵(名称:name) 监听master,daemonize:#后台运行]
port 26379 
dir "/data"
logfile "26379.log"
sentinel monitor nameh 172.18.0.2 6379 2
sentinel auth-pass nameh ****开始redis.conf配置的密码
sentinel failover-timeout nameh 180000
daemonize yes
#node2/sentinel.conf  [port:指定哨兵监听端口,dir:#sentinel工作目录,logfile:#日志文件,sentinel monitor nameL#启动哨兵(名称:name) 监听master,daemonize:#后台运行]
port 26380
dir "/data"
logfile "26380.log"
sentinel monitor nameh 172.18.0.2 6379 2
sentinel auth-pass nameh ****开始redis.conf配置的密码
sentinel failover-timeout nameh 180000
daemonize yes
#node3/sentinel.conf  [port:指定哨兵监听端口,dir:#sentinel工作目录,logfile:#日志文件,sentinel monitor nameL#启动哨兵(名称:name) 监听master,daemonize:#后台运行]
port 26381
dir "/data"
logfile "26381.log"
sentinel monitor nameh 172.18.0.2 6379 2
sentinel auth-pass nameh ****开始redis.conf配置的密码
sentinel failover-timeout nameh 180000
daemonize yes
docker cp /data/redis-data/node1/sentinel.conf 容器ID:/data
docker cp /data/redis-data/node2/sentinel.conf 容器ID:/data
docker cp /data/redis-data/node3/sentinel.conf 容器ID:/data
apt-get update

apt-get install procps

ps -ef
image.png

PHP 链接哨兵

参考文档:https://segmentfault.com/a/1190000011185598

image.png
#前提是php安装了redis扩展
$redis = new Redis();  
//连接sentinel服务 host为ip,port为端口
$redis->connect("host", "port");
//获取主库列表及其状态信息
$result = $redis->rawCommand('SENTINEL', 'masters');
print_r($result);

//根据所配置的主库redis名称获取对应的信息
//master_name应该由运维告知(也可以由上一步的信息中获取)
$result1 = $redis->rawCommand('SENTINEL', 'master', $master_name);
print_r($result1);

//根据所配置的主库redis名称获取其对应从库列表及其信息
$result2 = $redis->rawCommand('SENTINEL', 'slaves', $master_name);
print_r($result2);

//获取特定名称的redis主库地址
$result3 = $redis->rawCommand('SENTINEL', 'get-master-addr-by-name', $master_name);
print_r($result3);

上一篇 下一篇

猜你喜欢

热点阅读