Redis5.0搭建集群模式

2018年十月 Redis 发布了稳定版本的 5.0 版本,推出了各种新特性,其中一点是放弃 Ruby的集群方式,改为 使用 C语言编写的 redis-cli的方式,是集群的构建方式复杂度大大降低。关于集群的更新可以在 Redis5 的版本说明中看到,如下:

The cluster manager was ported from Ruby (redis-trib.rb) to C code inside redis-cli. check `redis-cli --cluster help ` for more info.

从redis 3.0之后版本支持redis-cluster集群,redis-4.0.0开始支持module,redis-5.0.0开始支持类似于kafka那样的消息队列,Redis-Cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接。这样就可以很好的保证redis的高可用性。

Redis5.0搭建集群模式_第1张图片

下面就来部署个Redis Cluster,在一台服务器上部署6个redis节点。

注:搭建集群至少需要三个master,每个master对应一个Slave,由于本次只为了测试用所以都在一台物理机器上玩耍。

第一步创建一个文件夹redis-cluster,然后在下面创建6个文件夹

[root@zy ~]# mkdir /usr/local/redis-cluster -p
[root@zy ~]# cd /usr/local/redis-cluster
[root@zy redis-cluster]# mkdir 7001 7002 7003 7004 7005 7006

第二步把redis.conf分别复制到700*文件夹下,并修改配置文件。

[root@zy redis-cluster]# cd
[root@zy ~]# cd redis-5.0.4/
[root@zy redis-5.0.4]# vim redis.conf 
  1. 真实开发中应该修改bind绑定不同的IP,这个里为了方便测试用同一IP,修改bind 127.0.0.1 为 bind 192.168.30.15
  2. 修改port 6379 为 port 7001
  3. 修改daemonize no 为 daemonize yes
  4. 修改pidfile /var/run/redis_6379.pid 为 pidfile /var/run/redis_7001.pid
  5. 修改logfile " " 为 logfile "/usr/local/redis-cluster/7001/redis.log"
  6. 修改 dir ./ 为 dir "/usr/local/redis-cluster/7001"
  7. 放开cluster-enabled yes 注释 开启集群模式
  8. 放开 cluster-config-file nodes-6379.conf 注释,修改为cluster-config-file nodes-7001.conf 
  9. 放开 cluster-node-timeout 15000 注释
  10. 修改appendonly no 为 appendonly yes,并修改aof模式为appendfsync alway

第三步把修改后的redis.conf分别复制到7001-7006文件夹下

[root@zy redis-5.0.4]# cp redis.conf /usr/local/redis-cluster/7001/
[root@zy redis-5.0.4]# cp redis.conf /usr/local/redis-cluster/7002/
[root@zy redis-5.0.4]# cp redis.conf /usr/local/redis-cluster/7003/
[root@zy redis-5.0.4]# cp redis.conf /usr/local/redis-cluster/7004/
[root@zy redis-5.0.4]# cp redis.conf /usr/local/redis-cluster/7005/
[root@zy redis-5.0.4]# cp redis.conf /usr/local/redis-cluster/7006/

第四步执行vim redis.conf替换内容,先按Esc键,输入命令全局替换7001为7002  7001为7003  7001为7004 ....

:%s/7001/7002/g

第五步分别启动6个redis实例,然后查看是否启动成功

[root@zy bin]# redis-server /usr/local/redis-cluster/7001/redis.conf 
[root@zy bin]# redis-server /usr/local/redis-cluster/7002/redis.conf 
[root@zy bin]# redis-server /usr/local/redis-cluster/7003/redis.conf 
[root@zy bin]# redis-server /usr/local/redis-cluster/7004/redis.conf 
[root@zy bin]# redis-server /usr/local/redis-cluster/7005/redis.conf 
[root@zy bin]# redis-server /usr/local/redis-cluster/7006/redis.conf 
[root@zy bin]# ps aux | grep redis
root      39391  0.2  0.4 153884  7696 ?        Ssl  19:20   0:00 redis-server 192.168.30.15:7001 [cluster]
root      39408  0.2  0.4 153884  7696 ?        Ssl  19:20   0:00 redis-server 192.168.30.15:7002 [cluster]
root      39417  0.1  0.4 153884  7692 ?        Ssl  19:20   0:00 redis-server 192.168.30.15:7003 [cluster]
root      39426  0.3  0.4 153884  7692 ?        Ssl  19:21   0:00 redis-server 192.168.30.15:7004 [cluster]
root      39434  0.2  0.4 153884  7692 ?        Ssl  19:21   0:00 redis-server 192.168.30.15:7005 [cluster]
root      39442  0.2  0.4 153884  7696 ?        Ssl  19:21   0:00 redis-server 192.168.30.15:7006 [cluster]
root      39456  0.0  0.0 112712   984 pts/0    R+   19:21   0:00 grep --color=auto redis

第六步输入命令创建集群

[root@zy redis-cluster]# redis-cli --cluster create 192.168.30.15:7001 192.168.30.15:7002 192.168.30.15:7003 192.168.30.15:7004 192.168.30.15:7005 192.168.30.15:7006
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 2730
Master[1] -> Slots 2731 - 5460
Master[2] -> Slots 5461 - 8191
Master[3] -> Slots 8192 - 10922
Master[4] -> Slots 10923 - 13652
Master[5] -> Slots 13653 - 16383
M: 68b2cd5ad157d9257ef6ce0ad2433e22dfa37e08 192.168.30.15:7001
   slots:[0-2730] (2731 slots) master
M: 54960955d442ef3d9cf3cacda696c34b876bfcf5 192.168.30.15:7002
   slots:[2731-5460] (2730 slots) master
M: 198cc586bada78adad4f01e62ca8860f55610c07 192.168.30.15:7003
   slots:[5461-8191] (2731 slots) master
M: 2aa71d4960e66171618003a04dcdf8a6c88043f5 192.168.30.15:7004
   slots:[8192-10922] (2731 slots) master
M: 5115f1069b7252ca5620f4b2a7efe511810380bd 192.168.30.15:7005
   slots:[10923-13652] (2730 slots) master
M: 887da71adb91c8b0a50f93612554aba2a2c5a86c 192.168.30.15:7006
   slots:[13653-16383] (2731 slots) master
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 192.168.30.15:7001)
M: 68b2cd5ad157d9257ef6ce0ad2433e22dfa37e08 192.168.30.15:7001
   slots:[0-2730] (2731 slots) master
M: 2aa71d4960e66171618003a04dcdf8a6c88043f5 192.168.30.15:7004
   slots:[8192-10922] (2731 slots) master
M: 887da71adb91c8b0a50f93612554aba2a2c5a86c 192.168.30.15:7006
   slots:[13653-16383] (2731 slots) master
M: 198cc586bada78adad4f01e62ca8860f55610c07 192.168.30.15:7003
   slots:[5461-8191] (2731 slots) master
M: 54960955d442ef3d9cf3cacda696c34b876bfcf5 192.168.30.15:7002
   slots:[2731-5460] (2730 slots) master
M: 5115f1069b7252ca5620f4b2a7efe511810380bd 192.168.30.15:7005
   slots:[10923-13652] (2730 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

测试Redis集群

 注:启动客户端 redis-cli -c -h 192.168.30.15 -p 7001(启用集群模式)

[root@zy bin]# redis-cli -c -h 192.168.30.15 -p 7001
192.168.30.15:7001> set name aaa
-> Redirected to slot [5798] located at 192.168.30.15:7003
OK
192.168.30.15:7003> exit
[root@zy bin]# redis-cli -c -h 192.168.30.15 -p 7003
192.168.30.15:7003> get name
"aaa"
192.168.30.15:7003> 

用redis管理工具查看集群

Redis5.0搭建集群模式_第2张图片

你可能感兴趣的:(redis,linux)