1. 安装环境 这里的版本是3.06 高版本redis自带集群命令
apt-get install ruby
apt-get install gems
gem install redis
2. 集群部署设置
设置多台主机redis部署,这里设置了6台,自动配置3主3从
分别以不同端口号命名redis配置文件,copy原始redis.conf到各个端口号文件内,并按照一下修改。
daemonize yes //后台启动
#bind 127.0.0.1
port 8082
pidfile /var/run/redis/redis-server_8082.pid //启动
logfile /var/log/redis/redis-server_8082.log //日志
dir /var/lib/redis/8082 //redis工作目录
appendonly yes //aof日志
cluster-enabled yes //开启集群
cluster-config-file nodes_8082.conf //集群的配置
cluster-node-timeout 15000 //请求超时默认15秒,可自行设置
masterauth password 设置密码
requirepass password 设置密码
maxmemory-policy allkeys-lru 设置过期策略
需要在/var/lib/redis建立各个node文件夹
复制8082到8083,8084 vim 全局替换保存 %s/8082/8083/g
redis-server /home/ubuntu/redis/8082/redis.conf
redis-server /home/ubuntu/redis/8083/redis.conf
redis-server /home/ubuntu/redis/8084/redis.conf
也可以使用以下脚本完成redis部署和启动
#!/bin/bash
portName="8082 8083 8084 8085 8086 8087"
function createNode(){
for i in $portName;
do
echo `pwd`
mkdir `pwd`/$i
cp `pwd`/redis.conf `pwd`/$i
sed -i 's/8082/'$i'/g' `pwd`/$i/redis.conf
mkdir /var/lib/redis/$i
redis-server `pwd`/$i/redis.conf
done
}
createNode
输入命令查看 ps -ef | grep redis
3 集群
3.1 . redis3.0版本命令如下:
./redis-trib.rb create --replicas 1 192.168.1.47:8082 192.168.1.47:8083 192.168.1.47:8084 192.168.1.47:8085 192.168.1.47:8086 192.168.1.47:8087
replicas控制主从比例
写入数据
3.2 redis-cli启动
redis-cli -a tina1002 --cluster create 192.168.1.47:8082 192.168.1.47:8083 192.168.1.47:8084 192.168.1.47:8085 192.168.1.47:8086 192.168.1.47:8087 --cluster-replicas 1
4 .遇到的问题
4.1. reids有密码,创建集群的时候报错
解决方式:修改redis-trib.rb 新增password
@r = Redis.new(:host => @info[:host], :port => @info[:port], :timeout => 60)
修改为:
@r = Redis.new(:host => @info[:host], :port => @info[:port], :timeout => 60,:password=>"test")
4.2. [ERR] Node 192.168.1.47:8082 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
删除 /var/lib/redis/8082 下的nodes-8082.conf
或者用 ./redis-trib.rb fix修复
4.3. [ERR] Calling MIGRATE: ERR Syntax error, try CLIENT (LIST | KILL ip:port | GETNAME | SETNAME connection-name)
原因 有key-value的槽的时候迁移出错,5.0版本会抛弃redis-trib.rb。直接使用redis-cli客户端实现集群管理。
修改 redis-trib.rb source.r.client.call --> source.r.call
4.4. [ERR] Calling MIGRATE: ERR Target instance replied with error: NOAUTH Authentication required
迁移的时候需要带密码,在3中添加密码
4.5. 迁移的时候出错
进入相应的redis客户端 cluster setslot 6918 stable 清除
./redis-trib.rb 操作指引:https://blog.csdn.net/qq_26790807/article/details/79796786
redis-trib.rb下载地址 https://download.csdn.net/download/ccr1001ccr1001/12668445
集群无法重启 https://jingyan.baidu.com/article/6f2f55a175a1f1b5b93e6cd3.html
迁移key错误: https://blog.csdn.net/m0_37128231/article/details/80755478
https://blog.csdn.net/weixin_30235225/article/details/95011920