Python操作Redis哨兵+集群(基础配置)
仅供参考
一、配置信息
名称 | 版本 |
---|---|
虚拟机 | centos7 |
主机 | macos10.14.6 |
Redis | redis-4.0.10 |
Python | 3.7.3 |
二、依赖包
名称 | 网站 |
---|---|
redis-py | https://pypi.org/project/redis/ |
redis-py-cluster | https://pypi.org/project/redis-py-cluster/ |
1、Redis主从配置
主要为了实现数据备份与读写分离
- 创建Redis配置文件夹`
mkdir /etc/init.d/redis
- 复制Redis模板配置文件(配置文件一般在/usr/local/redis/下)
cp /usr/local/redis-4.0.10/redis.conf /etc/init.d/redis
- 进入Redis文件夹配置
mv redis.conf master.conf
cp master.conf ./slave.conf
- 配置master.conf
由于我们是由本机连接虚拟机
默认配置 | 修改后配置 |
---|---|
bind 127.0.0.1 | bind 0.0.0.0 |
- 配置slave.conf
默认配置 | 修改后配置 |
---|---|
bind 127.0.0.1 | bind 0.0.0.0 |
port 6379 | port 6378 |
#slaveof | slaveof 6379 |
- 重启Redis
redis-server /etc/init.d/redis/master.conf
redis-server /etc/init.d/redis/slave.conf
- 查看进程
ps aux | grep redis
如果你看到三个进程,那么恭喜你
2、Redis哨兵配置
根据Redis要求,我们最少设置3个哨兵
- 复制哨兵配置文件到redis文件目录下
cp sentinel.conf /etc/init.d/redis/sentinel01.conf
- 修改sentinel01.conf配置文件
默认配置 | 修改后配置 |
---|---|
bind 127.0.0.1 | bind 0.0.0.0 |
port 26379 | port 26380 |
sentinel monitor mymaster 127.0.0.1 6379 2 | sentinel monitor mymaster 172.16.154.177 6379 2 |
172.16.154.177是我的IP,根据自己的IP配置
- 复制两份
cp sentinel01.conf ./sentinel02.conf
cp sentinel01.conf ./sentinel03.conf
修改sentinel 文件中的port即可(26380,26381,26382)
4.启动哨兵
redis-sentinel /etc/init.d/redis/sentinel01.conf
redis-sentinel /etc/init.d/redis/sentinel02.conf
redis-sentinel /etc/init.d/redis/sentinel03.conf
# 我把误删redis-sentinel就用了下面的方法来启动
redis-server /etc/init.d/redis/sentinel01.conf --sentinel
redis-server /etc/init.d/redis/sentinel02.conf --sentinel
redis-server /etc/init.d/redis/sentinel03.conf --sentinel
- 查看进程
ps aux | grep sentinel
3、Python远程连接Redis哨兵
from redis.sentinel import Sentinel
#配置哨兵IP和端口号
sentinels = [
('172.16.154.177', 26380),
('172.16.154.177', 26381),
('172.16.154.177', 26382),
]
# 创建哨兵对象
sentinel = Sentinel(sentinels=sentinels)
# 主数据库别名(根据自己爱好设置的,叫狗蛋也挺好)
service_name = 'mymaster'
# 通过哨兵获取redis主从
redis_master = sentinel.master_for(service_name=service_name)
redis_slave = sentinel.slave_for(service_name=service_name)
我们可以通过redis_master对象进行读写操作
我们可以通过redis_slave对象进行读操作
记得开启自己虚拟机防火墙
firewall-cmd --add-port=26380/tcp --permanent
firewall-cmd --add-port=26381/tcp --permanent
firewall-cmd --add-port=26382/tcp --permanent
firewall-cmd --add-port=6379/tcp --permanent
firewall-cmd --add-port=6378/tcp --permanent
#最后执行重启
firewall-cmd --reload
#或者可以关闭防火墙
systemctl stop firewalld.service
4、Redis集群配置
Redis集群要求至少三主三从, 我没有那么多虚拟机。所以我在centos上创建了6个Redis实例
- 复制Redis配置文件到redis文件目录下
cp /usr/local/redis-4.0.10/redis.conf /etc/init.d/redis/7000.conf
- 修改7000.conf配置文件
默认配置 | 修改后配置 |
---|---|
bind 127.0.0.0 | bind 0.0.0.0 |
port6379 | port 7000 |
cluster-enable no | cluster-enable yes |
cluster-config-file nodes-6379.conf | cluster-config-file nodes-7000.conf |
appendonly no | appendonly yes |
修改好配置文件后复制5份,分别命名为7001.conf,7002.conf,7003.conf,7004.conf,7005.conf
分别修改配置文件中的luster-config-file nodes-7000.conf字段为luster-config-file nodes-7001.conf,luster-config-file nodes-7002.conf,luster-config-file nodes-7003.conf,luster-config-file nodes-7004.conf,luster-config-file nodes-7005.conf
- 启动Redis实例
redis-server /etc/init.d/redis/7000.conf
redis-server /etc/init.d/redis/7001.conf
redis-server /etc/init.d/redis/7002.conf
redis-server /etc/init.d/redis/7003.conf
redis-server /etc/init.d/redis/7004.conf
redis-server /etc/init.d/redis/7005.conf
- 创建集群
我用的是Redis4.0,所以需要用到集群管理工具redis-trib.rb
把这个文件放入/usr/local/bin 目录下
cp redis-trib.rb /usr/local/bin/
安装ruby
https://blog.csdn.net/qq_26440803/article/details/82717244
可以参考这篇博客来进行安装
- 创建集群
redis-trib.rb create --replicas 1 0.0.0.0:7000 0.0.0.0:7001 0.0.0.0:7002 0.0.0.0:7003 0.0.0.0:7004 0.0.0.0:7005
- 访问集群
到这里我们就可以访问集群了
redis-cli -p 7000 -c
5、Python操作Redis集群
from rediscluster import RedisCluster
nodes = [
{'host': '172.16.154.177', 'port': 7000},
{'host': '172.16.154.177', 'port': 7001},
{'host': '172.16.154.177', 'port': 7002},
]
rc = RedisCluster(startup_nodes=nodes, decode_responses=True)
rc.set('name', 'zs')
print(rc.get('name'))
最简单的Redis集群配置就这样了,关于Redis详细配置信息请查阅官方文档