使用docker-compose部署sentinel
程序员文章站
2024-01-12 14:44:22
...
参考博客
docker-compose部署:
https://www.cnblogs.com/boshen-hzb/p/10471665.html
普通部署:
https://www.cnblogs.com/kevingrace/p/9004460.html
使用docker-compose部署
redis-sentinel # 项目根路径
├── docker-compose.yml # docker-compose文件
└── sentinel # 存放初始化sentinel容器的相关文件
├── Dockerfile # sentinel构建镜像文件
├── sentinel.conf # sentinel配置文件
└── sentinel-entrypoint.sh # sentinel.sh启动脚本
redis-sentinel/docker-compose.yml
master:
image: redis
command: redis-server --port 6379 --requirepass 123456 --masterauth 123456
ports:
- "6379:6379"
slave1:
image: redis
command: redis-server --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456
links:
- master:redis-master
ports:
- "6380:6379"
slave2:
image: redis
command: redis-server --slaveof redis-master 6379 --requirepass 123456 --masterauth 123456
links:
- master:redis-master
ports:
- "6381:6379"
sentinel1:
build: sentinel
environment:
- SENTINEL_DOWN_AFTER=5000
- SENTINEL_FAILOVER=5000
links:
- master:redis-master
- slave1
ports:
- "26379:26379"
sentinel2:
build: sentinel
environment:
- SENTINEL_DOWN_AFTER=5000
- SENTINEL_FAILOVER=5000
links:
- master:redis-master
- slave2
ports:
- "26380:26379"
redis-sentinel/sentinel/Dockerfile
FROM redis
EXPOSE 26379
ADD sentinel.conf /etc/redis/sentinel.conf
RUN chown redis:redis /etc/redis/sentinel.conf
COPY sentinel-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/sentinel-entrypoint.sh
ENTRYPOINT ["sentinel-entrypoint.sh"]
redis-sentinel/sentinel/sentinel.conf
port 26379
protected-mode no
sentinel monitor mymaster redis-master 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 5000
sentinel-entrypoint.sh
#!/bin/sh
sed -i "s/$SENTINEL_QUORUM/$SENTINEL_QUORUM/g" /etc/redis/sentinel.conf
sed -i "s/$SENTINEL_DOWN_AFTER/$SENTINEL_DOWN_AFTER/g" /etc/redis/sentinel.conf
sed -i "s/$SENTINEL_FAILOVER/$SENTINEL_FAILOVER/g" /etc/redis/sentinel.conf
exec docker-entrypoint.sh redis-server /etc/redis/sentinel.conf --sentinel
运行下面的命令
[aaa@qq.com-node4 redis-sentinel]# cd redis-sentinel/ # 进入项目根路径
[aaa@qq.com-node4 redis-sentinel]# docker-compose up --force-recreate # 使用docker-compose启动项目
[aaa@qq.com-node4 redis-sentinel]# docker rm -f $(docker ps -a | grep redis | awk '{print $1}') # 删除所有redis相关的docker容器
集群图解与测试
redis-cli -h 127.0.0.1 -p 26379 连接sentinel查看集群信息
[aaa@qq.com-node4 redis-sentinel]# redis-cli -h 127.0.0.1 -p 26379
[aaa@qq.com-node4 redis-sentinel]# redis-cli -h 127.0.0.1 -p 26380
127.0.0.1:26379> info
# Server
redis_version:5.0.7
redis_mode:sentinel
os:Linux 3.10.0-957.1.3.el7.x86_64 x86_64
tcp_port:26379
executable:/data/redis-server
config_file:/etc/redis/sentinel.conf
# Clients
connected_clients:2
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
# 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.17.0.2:6379,slaves=2,sentinels=2
127.0.0.1:26379>
redis-master上可以写入和读取,但是slave只能读取不能写入
# master上可以写入和读取,但是slave只能读取不能写入
'''打开一个新终端在redis-master写入数据 '''
[aaa@qq.com-node4 redis-sentinel]# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> set test01 master6379
OK
127.0.0.1:6379>
'''打开第二个新终端在redis-slave可以查看master中写入的值但是salve自己不能写数据 '''
[aaa@qq.com-node4 ~]# redis-cli -h 127.0.0.1 -p 6380
127.0.0.1:6380> get test01 # slave可以查看到master写入的数据
"master6379"
127.0.0.1:6380> set test02 redis6380 # salve自己无法写入数据
(error) READONLY You can't write against a read only replica.
停止redis-master模拟鼓掌切换
[aaa@qq.com-node4 redis-sentinel]# docker stop redis-sentinel_master_1 # 停止redis-master模拟故障
redis-sentinel_master_1
[aaa@qq.com-node4 redis-sentinel]# docker ps | grep redis # 确定master已经停止
88d270dad925 redis-sentinel_sentinel2 redis-sentinel_sentinel2_1
307c9c7907e0 redis-sentinel_sentinel1 redis-sentinel_sentinel1_1
2ed5dbfff01d redis redis-sentinel_slave1_1
ca3b568d0b19 redis redis-sentinel_slave2_1
[aaa@qq.com-node4 redis-sentinel]# redis-cli -p 26379 # 连接sentinel,发现已经选举出新的master
127.0.0.1:26379> sentinel masters
1) 1) "name"
2) "mymaster"
3) "ip"
4) "172.17.0.4"
5) "port"
6) "6379"
7) "runid"
8) "af1aa34a2a013c4b776456732ee5424c5590829c"
[aaa@qq.com-node4 redis-sentinel]# docker inspect redis-sentinel_slave2_1 # 我这里查看sentinel_slave2_1的ip为172.17.0.4
[aaa@qq.com-node4 redis-sentinel]# redis-cli -h 127.0.0.1 -p 6381
127.0.0.1:6381> info # 查看角色
127.0.0.1:6381> set name zhangsan # 发现主库可以进行写入啦
OK
127.0.0.1:6381>
使用redis连接sentinel测试reds集群写入
#!/usr/bin/env python
import redis
from redis.sentinel import Sentinel
# 连接哨兵服务器(主机名也可以用域名)
sentinel = Sentinel([('192.168.56.14', 26379), # 连接的是哨兵的
('192.168.56.14', 26380),
],
socket_timeout=2,
)
# 获取主服务器地址
master = sentinel.discover_master('mymaster')
# 获取主服务器进行写入(密码应为redis密码),源码中连接的是docker内部地址网络不通
master = sentinel.master_for('mymaster', socket_timeout=0.5, password='123456', db=15)
w_ret = master.set('foo', 'bar')
# 输出:True
# # 获取从服务器进行读取(默认是round-roubin)
slave = sentinel.slave_for('mymaster', socket_timeout=0.5, password='redis_auth_pass', db=15)
r_ret = slave.get('foo')
print(r_ret)
# # 输出:bar
推荐阅读
-
使用docker-compose部署sentinel
-
Docker 部署kibana7.7.0 使用xpack监控ES集群
-
淘淘商城第117讲——使用maven的tomcat插件来实现Tomcat热部署
-
使用腾讯云容器服务(TKE)是如何实现应用跨可用区高可用部署的? TKE高可用
-
ZooKeeper的部署以及简单使用 springmvc+mybatisdubbo+zookeeperrestful redis分布式缓存spring mvc
-
ZooKeeper的部署以及简单使用 springmvc+mybatisdubbo+zookeeperrestful redis分布式缓存spring mvc
-
ZooKeeper的部署以及简单使用 tomcatjavaapachelinuxjvm
-
Kafka 安装部署及使用(单节点/集群)
-
.Net Core 使用 System.Drawing.Common 部署到CentOS上遇到的问题
-
docker-compose教程之安装使用和快速入门