redis-cluster集群,redis5.0.5

2019-10-05 11:03:00
aolens
原创
440
1,redis 5.0.5是现阶段redis最新的版本

    从5.0开始,原slave改叫replica,相关的配置参数也做了同样改名

2,redis cluster集群要求至少三主三从才可以组成集群
服务端口
IP地址
配置文件名
7001
127.0.0.1
redis-7001.conf
7002
127.0.0.1
redis-7002.conf
7003
127.0.0.1
redis-7003.conf
7004
127.0.0.1
redis-7004.conf
7005
127.0.0.1
redis-7005.conf
7006
127.0.0.1
redis-7006.conf
3,设置系统参数

vim /etc/security/limits.conf
    * soft nofile 102400
    * hard nofile 102400
其中102400为一个进程最大可以打开的文件个数,当与RedisServer的连接数多时,需要设定为合适的值。
有些环境修改后,root用户需要重启机器才生效,而普通用户重新登录后即生效。如果是crontab,则需要重启crontab,如:service crond restart,有些平台可能是service cron restart(类似重启系统日志服务:service rsyslog restart或systemctl restart rsyslog)。
有些环境下列设置即可让root重新登录即生效,而不用重启机器:
    root soft nofile 102400
    root hard nofile 102400
4,TCP监听队列大小

即TCP listen的backlog大小,“/proc/sys/net/core/somaxconn”的默认值一般较小如128,需要修改大一点,比如改成32767。立即生效还可以使用命令: 
   sysctl -w net.core.somaxconn=32767。
要想永久生效,需要在文件/etc/sysctl.conf中增加一行:
    net.core.somaxconn = 32767,
然后执行命令“sysctl -p”以生效。
Redis配置项tcp-backlog的值不能超过somaxconn的大小。

5,目录结构

[root@dev01 /home/redis-cluster]# tree bin/
bin/
├── mkreleasehdr.sh
├── redis-benchmark
├── redis-check-aof
├── redis-check-rdb
├── redis-cli
├── redis-sentinel
└── redis-server
[root@dev01 /home/redis-cluster]# tree conf/
conf/
├── redis-7001.conf
├── redis-7002.conf
├── redis-7003.conf
├── redis-7004.conf
├── redis-7005.conf
├── redis-7006.conf
└── redis.conf

6,配置文件

port 7001 #端口 
cluster-enabled yes #启用集群模式 
cluster-config-file nodes-7001.conf 
cluster-node-timeout 15000	#超时时间 
daemonize yes #后台运行 
protected-mode no #非保护模式 
pidfile /var/run/redis_7001.pid
7,启动

[root@dev01 /home/redis-cluster]# ./bin/redis-server conf/redis-7001.conf
[root@dev01 /home/redis-cluster]# ./bin/redis-server conf/redis-7002.conf
[root@dev01 /home/redis-cluster]# ./bin/redis-server conf/redis-7003.conf
[root@dev01 /home/redis-cluster]# ./bin/redis-server conf/redis-7004.conf
[root@dev01 /home/redis-cluster]# ./bin/redis-server conf/redis-7005.conf
[root@dev01 /home/redis-cluster]# ./bin/redis-server conf/redis-7006.conf

8,创建集群

[root@dev01 /home/redis-cluster]# ./bin/redis-cli --cluster create 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 127.0.0.1:7006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 6e2e097ea0371cd0d6933c79ccbfe6b0bbcdb8f3 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
M: 81da3e484d5793c9eca7100fe715736865383b13 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
M: 08251459f001ceea6ed933562e942508feeeeaa8 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
S: 46f37b610466e8773c9e7e49830e584ce6f79e29 127.0.0.1:7004
replicates 6e2e097ea0371cd0d6933c79ccbfe6b0bbcdb8f3
S: e0437f84d86e839aa5dc19c370eeceadda860241 127.0.0.1:7005
replicates 81da3e484d5793c9eca7100fe715736865383b13
S: 3b750b11e952fbec1421b01620030619f74cfde5 127.0.0.1:7006
replicates 08251459f001ceea6ed933562e942508feeeeaa8
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
..
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 6e2e097ea0371cd0d6933c79ccbfe6b0bbcdb8f3 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 3b750b11e952fbec1421b01620030619f74cfde5 127.0.0.1:7006
slots: (0 slots) slave
replicates 08251459f001ceea6ed933562e942508feeeeaa8
M: 81da3e484d5793c9eca7100fe715736865383b13 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: e0437f84d86e839aa5dc19c370eeceadda860241 127.0.0.1:7005
slots: (0 slots) slave
replicates 81da3e484d5793c9eca7100fe715736865383b13
S: 46f37b610466e8773c9e7e49830e584ce6f79e29 127.0.0.1:7004
slots: (0 slots) slave
replicates 6e2e097ea0371cd0d6933c79ccbfe6b0bbcdb8f3
M: 08251459f001ceea6ed933562e942508feeeeaa8 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
9,关闭启动集群
redis5提供了关闭集群的工具
vim /home/redis-cluste/redis-5.0.5/utils/create-cluster
PORT=7000
TIMEOUT=2000
NODES=6
REPLICAS=1

端口PROT设置为7000,NODES为6,工具会自动累加1 生成 7001-7006 六个节点 用于操作。
[root@dev01 /home/redis-cluster/redis-5.0.5/utils/create-cluster]# ./create-cluster stop

[root@dev01 /home/redis-cluster/redis-5.0.5/utils/create-cluster]# ./create-cluster start
[root@dev01 /home/redis-cluster/bin]# ./bin/redis-cli --cluster create 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 127.0.0.1:7006 --cluster-replicas 1