安装ruby
1 2 3 4 5 6 7 8 9 10
| yum -y install gcc automake autoconf libtool make
yum install centos-release-scl-rh
yum install rh-ruby23 -y
scl enable rh-ruby23 bash
ruby -v
|
1 2 3 4 5 6 7 8 9 10 11
| yum install wget wget http://download.redis.io/releases/redis-3.2.6.tar.gz tar xzf redis-3.2.6.tar.gz mv redis-3.2.6 /opt/redis make
/opt/redis/src/redis-server /opt/redis/redis.conf
gem install redis
./src/redis-trib.rb create --replicas 1 192.168.101.108:7000 192.168.101.108:7001 192.168.101.108:7002
|
docker方式构建
部署脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| redis700x: image: redis restart: always hostname: redis-master command: "redis-server /data/redis.conf" volumes: - /dockerdata/v-yinfu/redis/7000:/data ports: - target: 700x published: 700x protocol: tcp mode: host - target: 1700x published: 1700x protocol: tcp mode: host
|
配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
protected-mode no port 700x
cluster-announce-port 700x cluster-announce-bus-port 1700x
cluster-announce-ip 192.168.101.108 daemonize no appendonly yes cluster-enabled yes appendonly yes cluster-config-file nodes.conf cluster-node-timeout 15000
|
上面x换成0-6
1 2 3 4
| ./redis-trib.rb create --replicas 1 192.168.101.108:7000 192.168.101.108:7001 192.168.101.108:7002 192.168.101.108:7003 192.168.101.108:7004 192.168.101.108:7005
./src/redis-trib.rb check 192.168.101.108:7000
|
使用docker镜像脚本创建
1 2 3 4 5 6 7 8
| docker run -it --rm exxk/redis-trib ruby redis-trib.rb create --replicas 1 172.16.16.8:7000 172.16.16.8:7001 172.16.16.8:7002 172.16.16.13:7003 172.16.16.13:7004 172.16.16.13:7005
docker run -it --rm exxk/redis-trib ruby redis-trib.rb check 192.168.101.108:7000
docker stats $(docker ps | grep "redis-cluster" | awk '{ print $1 }')
docker run -it --rm exxk/redis-trib ruby redis-trib.rb fix 10.10.10.11:7000
|
高可用总结
- 执行集群creat脚本时,默认前三个为主后三个为从,主从对应关系随机分配
- 集群存在主从对应关系,一个主回自动分配一个从,集群宕机也不会改变
- 集群一个主从挂了不能访问
- 集群必须所有的主都能正常运行
- 集群从升级主存在时间间隔,试配置和性能等因素影响,可能长可能短,在没有主选择成功集群状态显示异常,且无法访问,提示错误
Not all 16384 slots are covered by nodes.
- 集群同时宕机两个主,从是无法升级为主,这种情况只在同时,如果其中一个从升级主,再宕机一个主,是没有关系的
- 从节点宕机对主节点毫无影响
常见问题
问题1: 一直提示waiting for
1 2
| >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join..
|
解决:防火墙开端口
1 2 3 4 5 6 7 8 9 10 11
| firewall-cmd --zone=public --add-port=7000/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-all
firewall-cmd --state
systemctl restart firewalld
|
然后删除服务及缓存文件重新启动
1 2 3 4 5
| rm -f /dockerdata/v-yinfu/redis/700*/appendonly.aof rm -f /dockerdata/v-yinfu/redis/700*/nodes.conf
./redis-trib.rb create --replicas 1 192.168.101.108:7000 192.168.101.108:7001 192.168.101.108:7002 192.168.101.108:7003 192.168.101.108:7004 192.168.101.108:7005
|
问题2 版本兼容问题
redis5 不支持jedis2.8,会提示错误7001@17001
,升级jedis3.0可以解决
redis5(4)一下不支持docker 集群模式
1 2 3 4
| cluster-announce-port 700x cluster-announce-bus-port 1700x # 自己服务器的ip cluster-announce-ip 192.168.101.108
|
参考
docker redis 集群(cluster)搭建
centos7安装redis4集群服务
官网:https://redis.io/documentation
中文官网:http://www.redis.cn/documentation.html