1、集群组件安装
EPEL源安装ruby支持 yum install ruby rubygems -y 使用国内源 gem sources --add https://gems.ruby-china.org/ --remove https://rubygems.org/ 如果无法使用,可以使用aliyun gem sources -a http://mirrors.aliyun.com/rubygems/ gem sources --remove http://rubygems.org/ gem sources -l gem install redis -v 3.3.3
2、集群节点准备
规划: 端口:7000-7005 路径: /application/data/7000 /application/data/7001 /application/data/7002 /application/data/7003 /application/data/7004 /application/data/7005 日志路径: /var/log/redis7000.log /var/log/redis7001.log /var/log/redis7002.log /var/log/redis7003.log /var/log/redis7004.log /var/log/redis7005.log pid文件 /application/data/7000/redis.pid /application/data/7001/redis.pid /application/data/7002/redis.pid /application/data/7003/redis.pid /application/data/7004/redis.pid /application/data/7005/redis.pid 创建节点目录: mkdir -p /application/data/7000 mkdir -p /application/data/7001 mkdir -p /application/data/7002 mkdir -p /application/data/7003 mkdir -p /application/data/7004 mkdir -p /application/data/7005 配置文件添加: ------------------------- vim /application/data/7000/redis.conf port 7000 daemonize yes pidfile /application/data/7000/redis.pid logfile "/var/log/redis7000.log" dbfilename dump.rdb dir /application/data/7000 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes vim /application/data/7001/redis.conf port 7001 daemonize yes pidfile /application/data/7001/redis.pid logfile "/var/log/redis7001.log" dbfilename dump.rdb dir /application/data/7001 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes vim /application/data/7002/redis.conf port 7002 daemonize yes pidfile /application/data/7002/redis.pid logfile "/var/log/redis7002.log" dbfilename dump.rdb dir /application/data/7002 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes vim /application/data/7003/redis.conf port 7003 daemonize yes pidfile /application/data/7003/redis.pid logfile "/var/log/redis7003.log" dbfilename dump.rdb dir /application/data/7003 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes vim /application/data/7004/redis.conf port 7004 daemonize yes pidfile /application/data/7004/redis.pid logfile "/var/log/redis7004.log" dbfilename dump.rdb dir /application/data/7004 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes vim /application/data/7005/redis.conf port 7005 daemonize yes pidfile /application/data/7005/redis.pid logfile "/var/log/redis7005.log" dbfilename dump.rdb dir /application/data/7005 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes 启动所有节点 redis-server /application/data/7000/redis.conf redis-server /application/data/7001/redis.conf redis-server /application/data/7002/redis.conf redis-server /application/data/7003/redis.conf redis-server /application/data/7004/redis.conf redis-server /application/data/7005/redis.conf ps -ef |grep 700
3、集群创建
redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 \
127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
4、集群状态查看
redis-cli -p 7000 cluster nodes | grep master
5、Python 连接 redis cluster
+++++++++++++++Python 连接 redis cluster+++++++++++++++++ (1) redis-py并没有提供redis-cluster的支持,去github找了一下,有个叫redis-py-cluster的源码, 但是和redis-py不是一个作者,地址为:https://github.com/Grokzen/redis-py-cluster watch,star,fork还算可以。 (2) 安装 Latest stable release from pypi $ pip install redis-py-cluster or from source $ python setup.py install (3) 使用 >>> from rediscluster import StrictRedisCluster >>> startup_nodes = [{"host": "127.0.0.1", "port": "7000"}] >>> # Note: decode_responses must be set to True when used with python3 >>> rc = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True) >>> rc.set("foo", "bar") True >>> print(rc.get("foo")) 'bar'