09: redis集群之sentinel
1.1 实验说明
1、环境说明
1. 前提:本试验环境已经提前安装了docker和docker-compose
2. 说明:本次部署是单机伪集群,想要部署真正的集群,需要将秒个主件拆分到各个机器上去部署,只修改ip地址
2、redis三种集群
1. 主从(主从同步原理)
2. sentinel(哨兵模式)
3. codis
详细内容请参考:https://www.cnblogs.com/xiaonq/p/11635195.html
1.2 使用docker-compose部署sentinel
1、参考博客
docker-compose部署:https://www.cnblogs.com/boshen-hzb/p/10471665.html
普通部署:https://www.cnblogs.com/kevingrace/p/9004460.html
2、使用docker-compose部署
redis-sentinel # 项目根路径
├── docker-compose.yml # docker-compose文件
└── sentinel # 存放初始化sentinel容器的相关文件
├── Dockerfile # sentinel构建镜像文件
├── sentinel.conf # sentinel配置文件
└── sentinel-entrypoint.sh # sentinel.sh启动脚本
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"
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"]
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
#!/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
[root@linux-node4 redis-sentinel]# cd redis-sentinel/ # 进入项目根路径
[root@linux-node4 redis-sentinel]# docker-compose up --force-recreate # 使用docker-compose启动项目
[root@linux-node4 redis-sentinel]# docker rm -f $(docker ps -a | grep redis | awk '{print $1}') # 删除所有redis相关的docker容器
3、集群图解 & 测试
[root@linux-node4 redis-sentinel]# redis-cli -h 127.0.0.1 -p 26379 [root@linux-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>
# master上可以写入和读取,但是slave只能读取不能写入 '''打开一个新终端在redis-master写入数据 ''' [root@linux-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自己不能写数据 ''' [root@linux-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.
[root@linux-node4 redis-sentinel]# docker stop redis-sentinel_master_1 # 停止redis-master模拟故障 redis-sentinel_master_1 [root@linux-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 [root@linux-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" [root@linux-node4 redis-sentinel]# docker inspect redis-sentinel_slave2_1 # 我这里查看sentinel_slave2_1的ip为172.17.0.4 [root@linux-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>
#!/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
作者:学无止境
出处:https://www.cnblogs.com/xiaonq
生活不只是眼前的苟且,还有诗和远方。