Redis 5.0.3集群搭建

Redis集群节点数量至少6个才能保证组成完整高可用的集群,本文使用3台虚拟机模拟6个节点(用端口区分),其中每台虚机2个节点,具体如下表所示。

主机名 地址 端口 端口
server1 192.168.65.150 7001 7004
server2 192.168.65.151 7002 7005
server3 192.168.65.152 7003 7006
  1. Redis官网(https://redis.io/)下载redis-5.0.3.tar.gz。
  2. 每台机器都先编译安装Redis。
[root@server1 ~]# tar -zxvf redis-5.0.3.tar.gz
[root@server1 ~]# cd redis-5.0.3/
[root@server1 redis-5.0.3]# yum -y install gcc zlib-devel tcl
[root@server1 redis-5.0.3]# make MALLOC=libc && make install
[root@server1 redis-5.0.3]# make test
[root@server1 redis-5.0.3]# redis-server  -v

server2与server3安装Redis操作同server1。

  1. 配置每个节点。

在server1上进行如下配置:

[root@server1 redis-5.0.3]# mkdir -p /opt/redis-cluster/{7001,7004}
[root@server1 redis-5.0.3]# cp redis.conf /opt/redis-cluster/7001/redis-7001.conf
[root@server1 redis-5.0.3]# vim /opt/redis-cluster/7001/redis-7001.conf //修改如下选项
bind 192.168.65.150 #本机IP
port 7001 #改为设定的端口
daemonize yes #后台启动
cluster-enabled yes #启动集群模式
cluster-config-file nodes-7001.conf #集群内部配置文件,改掉端口号 
cluster-node-timeout 15000 #节点超时时间,单位:毫秒 
[root@server1 redis-5.0.3]# cp redis.conf /opt/redis-cluster/7004/redis-7004.conf
[root@server1 redis-5.0.3]# vim /opt/redis-cluster/7004/redis-7004.conf
bind 192.168.65.150
port 7004
daemonize yes
cluster-enabled yes
cluster-config-file nodes-7004.conf
cluster-node-timeout 15000

server2与server3上的配置参考server1。

  1. 启动每个节点。

在server1上启动7001与7004节点。

[root@server1 redis-5.0.3]# redis-server /opt/redis-cluster/7001/redis-7001.conf
[root@server1 redis-5.0.3]# redis-server /opt/redis-cluster/7004/redis-7004.conf

server2与server3上节点的启动参考server1。

  1. 准备集群环境,此过程只需要在其中一台上执行。

安装Ruby,具体如下。

[root@server1 ~]# wget [http://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.1.tar.gz](http://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.1.tar.gz)
[root@server1 ~]# tar -zxvf ruby-2.6.1.tar.gz
[root@server1 ~]# cd ruby-2.6.1/
[root@server1 ruby-2.6.1]# ./configure --prefix=/opt/ruby
[root@server1 ruby-2.6.1]# make && make install

安装rubygem redis,具体如下。

[root@server1 ~]# wget http://rubygems.org/downloads/redis-3.3.0.gem
[root@server1 ~]# gem install -l redis-3.3.0.gem
  1. 创建集群。
[root@server1 ~]# redis-cli --cluster create 192.168.65.150:7001 192.168.65.150:7004 192.168.65.151:7002 192.168.65.151:7005 192.168.65.152:7003 192.168.65.152: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 192.168.65.151:7005 to 192.168.65.150:7001
Adding replica 192.168.65.150:7004 to 192.168.65.151:7002
Adding replica 192.168.65.152:7006 to 192.168.65.152:7003
>>> Trying to optimize slaves allocation for anti-affinity
[OK] Perfect anti-affinity obtained!
M: f2fa0cbb9ba11d3201379178456cdf6dd49961f6 192.168.65.150:7001
 slots:[0-5460] (5461 slots) master
S: 19699ec79327c7e6fdced0f85dcc4eccab988ff9 192.168.65.150:7004
 replicates 0f54d0b8db1afdf833399c3623efe27ed1ffd4b6
M: 06d79295c64cf01a0a66e9626530a0eaa1af84f6 192.168.65.151:7002
 slots:[5461-10922] (5462 slots) master
S: 5cba199bc092093e72362356015a95c149fe0659 192.168.65.151:7005
 replicates f2fa0cbb9ba11d3201379178456cdf6dd49961f6
M: 0f54d0b8db1afdf833399c3623efe27ed1ffd4b6 192.168.65.152:7003
 slots:[10923-16383] (5461 slots) master
S: 88f50521fe852454e8efa48e7383136132e9ce93 192.168.65.152:7006
 replicates 06d79295c64cf01a0a66e9626530a0eaa1af84f6
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.65.150:7001)
M: f2fa0cbb9ba11d3201379178456cdf6dd49961f6 192.168.65.150:7001
 slots:[0-5460] (5461 slots) master
 1 additional replica(s)
M: 0f54d0b8db1afdf833399c3623efe27ed1ffd4b6 192.168.65.152:7003
 slots:[10923-16383] (5461 slots) master
 1 additional replica(s)
S: 5cba199bc092093e72362356015a95c149fe0659 192.168.65.151:7005
 slots: (0 slots) slave
 replicates f2fa0cbb9ba11d3201379178456cdf6dd49961f6
M: 06d79295c64cf01a0a66e9626530a0eaa1af84f6 192.168.65.151:7002
 slots:[5461-10922] (5462 slots) master
 1 additional replica(s)
S: 88f50521fe852454e8efa48e7383136132e9ce93 192.168.65.152:7006
 slots: (0 slots) slave
 replicates 06d79295c64cf01a0a66e9626530a0eaa1af84f6
S: 19699ec79327c7e6fdced0f85dcc4eccab988ff9 192.168.65.150:7004
 slots: (0 slots) slave
 replicates 0f54d0b8db1afdf833399c3623efe27ed1ffd4b6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  1. 验证集群。

在server1上连接集群的7001端口的节点,具体如下。

[root@server1 ~]# redis-cli -h 192.168.65.150 -c -p 7001
192.168.65.150:7001> set yan ning
-> Redirected to slot [13290] located at 192.168.65.152:7003
OK
192.168.65.152:7003> get yan
"ning"
192.168.65.152:7003>

在server1上连接集群的7002端口的节点,具体如下。

[root@server1 ~]# redis-cli -h 192.168.65.151 -c -p 7002
192.168.65.151:7002> get yan
-> Redirected to slot [13290] located at 192.168.65.152:7003
"ning"
192.168.65.152:7003>

你可能感兴趣的:(Redis 5.0.3集群搭建)