下载安装redis
安装redis需要gcc依赖
yum install gcc
cd /usr/local/wget http://download.redis.io/releases/redis-4.0.1.tar.gztar -zxvf /redis-4.0.1.tar.gzcd redis-4.0.1make && make install
安装环境
使用集群命令需要安装ruby,ruby版本需要大于2.2.2
yum -y install ruby ruby-devel rubygems rpm-build
升级ruby
yum install curlcurl -L get.rvm.io | bash -s stablervm list knownrvm install 2.4.0rvm use 2.4.0ruby --version
安装redis-rb
gem install redis
一、创建redis节点
cd /usr/local#创建集群目录mkdir redis_clustercd redis_cluster#创建集群节点mkdir 7000 7001 7002cp /usr/local/redis-4.0.1/redis.conf ./7000/cp /usr/local/redis-4.0.1/redis.conf ./7001/cp /usr/local/redis-4.0.1/redis.conf ./7002/
以同样的方式在另一台服务器上创建三个节点
cd /usr/local#创建集群目录mkdir redis_clustercd redis_cluster#创建集群节点mkdir 7003 7004 7005cp /usr/local/redis-4.0.1/redis.conf ./7003/cp /usr/local/redis-4.0.1/redis.conf ./7004/cp /usr/local/redis-4.0.1/redis.conf ./7005/
修改配置文件
# 绑定本机IP 内网IP或者外网IP必须在前面bind 192.168.xxx.xxx 127.0.0.1# 设置端口 7000 700xport 7000# 程序后台运行daemonize yes# pid文件地址pidfile /var/run/redis_7000.pid# 日志文件logfile "7000.log"# 启用集群cluster-enabled yes# 集群配置文件(自动生成)cluster-config-file nodes-7000.conf# 请求超时(毫秒)cluster-node-timeout 15000# aof日志开启 有需要就开启,它会每次写操作都记录一条日志appendonly yes
二、启动redis节点
服务器1
cd /usr/local/redis_cluster/cd 7000redis-server redis.confcd ../7001redis-server redis.confcd ../7002redis-server redis.conf
服务器2
cd /usr/local/redis_cluster/cd 7003redis-server redis.confcd ../7004redis-server redis.confcd ../7005redis-server redis.conf
三、创建集群
注意关闭防火墙或者开放对应端口
/usr/local/redis-4.0.1/src/redis-trib.rb create --replicas 1 192.168.137.128:7000 192.168.137.128:7001 192.168.137.128:7002 192.168.137.129:7003 192.168.137.129:7004 192.168.137.129:7005
四、常用命令
添加节点
/usr/local/redis-4.0.1/src/redis-trib.rb add-node 192.168.137.129:7006 192.168.137.128:7000
启动redis
redis-server redis_cluster/7000/redis.conf
停止redis
redis-cli -c -p 7000 shutdown
五、测试
六、出现的问题
创建集群时报错
1、无法连接节点
Sorry, can't connect to node 192.168.xxx.xxx
停止redis服务,查看conf配置文件
# 绑定本机IP 内网IP或者外网IP必须在前面bind 192.168.xxx.xxx 127.0.0.1
2、节点不为空
[ERR] Node 192.168.137.128:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
停止redis服务,删除redis启动生成的nodes.conf、appendonly.aof、dump.rdb等文件
重新启动redis,重新执行创建集群命令
/usr/local/redis-4.0.1/src/redis-trib.rb create --replicas 1 192.168.137.128:7000 192.168.137.128:7001 192.168.137.128:7002 192.168.137.129:7003 192.168.137.129:7004 192.168.137.129:7005
3、节点挂掉后重启,集群中节点状态一直是fail
检查后发现是防火墙问题,系统重启后防火墙也随之启动,关闭防火墙后集群恢复正常
systemctl stop firewalld