1、工具 / 环境
- 机器环境:linux虚拟机
- 操作系统:CentOS Linux release 7.4.1708 (Core)
- 3台虚拟机IP:172.18.1.23,172.18.1.24,172.18.1.25(注:Redis集群要求至少要有三个节点)
2、安装Redis
yum -y install gcc #如果没有gcc编译容器则需要安装
wget http://download.redis.io/releases/redis-4.0.11.tar.gz
tar -xf redis-4.0.11.tar.gz -C /opt
cd redis-4.0.11
make
3. 配置节点
采用三主三从的集群模式:
172.18.1.23 | 172.18.1.24 | 172.18.1.25 |
ip:7000 | ip:7000 | ip:7000 |
ip:7001 | ip:7001 | ip:7001 |
我们以172.18.1.23为例,配置一个主和一个从实例。具体操作如下:
[root@bfd-yiz-1p23 /opt/software/redis-4.0.11]# mkdir -pv /opt/redis/conf /opt/redis/bin /opt/redis/log opt/redis/data
#拷贝常用的二进制文件
[root@bfd-yiz-1p23 /opt/software/redis-4.0.11]# cp src/redis-* /usr/local/redis/bin/
#删除.c,.o的文件
[root@bfd-yiz-1p23 /opt/software/redis-4.0.11]# rm /usr/local/redis/bin/*.c /usr/local/redis/bin/*.o
#这里我们使用简单一点集群配置,主实例配置文件
[root@bfd-yiz-1p23 /opt]# vim /opt/redis/conf/redis7000.conf
bind 172.18.1.23
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
timeout 3600
daemonize yes
pidfile /opt/redis/pid/redis7000.pid
logfile "/opt/redis/log/redis7000.log"
dir "/opt/redis/data/7000"
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command KEYS ""
# Generated by CONFIG REWRITE
#requirepass "bfd"
#这里我们使用简单一点集群配置,从实例配置文件
bind 172.18.1.23
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
timeout 3600
daemonize yes
pidfile /opt/redis/pid/redis7001.pid
logfile "/opt/redis/log/redis7001.log"
dir "/opt/redis/data/7001"
rename-command FLUSHALL ""
rename-command FLUSHDB ""
rename-command KEYS ""
# Generated by CONFIG REWRITE
#requirepass "bfd"
同理,我们将/opt/redis安装目录,scp到其他两台机器上,只要相对应的修改bind 172.18.1.23其中的ip地址即可。修改为本机的ip。修改完后,启动redis服务,查看进程我们发现每台机器上都有两个实例进程。
启动进程,每台机器都要启动。
[root@bfd-yiz-1p23 /opt/redis]# redis-server conf/redis7000.conf
[root@bfd-yiz-1p23 /opt/redis]# redis-server conf/redis7001.conf
4. 创建集群
创建Redis集群需要借助安装包里的一个Ruby脚本,先安装Ruby(3台都需要操作)
yum -y install ruby rubygems
gem install redis # 安装Redis客户端for Ruby
执行上述操作,可能会报下面的错误
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2.
解决方案:rvm安装ruby,这里注意,在安装rvm的时候,会导致你的机器ctrl+c功能失效,可参考该文章。
安装完ruby2.4.4后我们就可以进行以下操作。
创建Redis集群
[root@bfd-yiz-1p23 /opt/redis/]# redis-trib.rb create --replicas 1 172.18.1.23:7000 172.18.1.24:7000 172.18.1.25:7000 172.18.1.23:7001 172.18.1.24:7001 172.18.1.25:7001
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.18.1.24:7001 to 172.18.1.23:7000
Adding replica 172.18.1.23:7001 to 172.18.1.24:7000
Adding replica 172.18.1.25:7001 to 172.18.1.25:7000
>>> Trying to optimize slaves allocation for anti-affinity
[OK] Perfect anti-affinity obtained!
M: ba7275d326cf2c10f952597c1647d0e474f5fa07 172.18.1.23:7000
slots:[0-5460] (5461 slots) master
M: f96b3194409eef942fb0f41b3983a06a7e9ff914 172.18.1.24:7000
slots:[5461-10922] (5462 slots) master
M: fd3c6a6dfe4199a3ca7dc42e0d7fa80db5c71a2c 172.18.1.25:7000
slots:[10923-16383] (5461 slots) master
S: d8a6092bff92e44a4c2fbf123de2c655a1b8d900 172.18.1.23:7001
replicates f96b3194409eef942fb0f41b3983a06a7e9ff914
S: b36323fa4d4818d5870053ec63435dcdd0789e37 172.18.1.24:7001
replicates fd3c6a6dfe4199a3ca7dc42e0d7fa80db5c71a2c
S: 14da1431925bdeace056cc150094ceea2af8992e 172.18.1.25:7001
replicates ba7275d326cf2c10f952597c1647d0e474f5fa07
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 172.18.1.23:7000)
M: ba7275d326cf2c10f952597c1647d0e474f5fa07 172.18.1.23:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 14da1431925bdeace056cc150094ceea2af8992e 172.18.1.25:7001
slots: (0 slots) slave
replicates ba7275d326cf2c10f952597c1647d0e474f5fa07
M: f96b3194409eef942fb0f41b3983a06a7e9ff914 172.18.1.24:7000
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: b36323fa4d4818d5870053ec63435dcdd0789e37 172.18.1.24:7001
slots: (0 slots) slave
replicates fd3c6a6dfe4199a3ca7dc42e0d7fa80db5c71a2c
M: fd3c6a6dfe4199a3ca7dc42e0d7fa80db5c71a2c 172.18.1.25:7000
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: d8a6092bff92e44a4c2fbf123de2c655a1b8d900 172.18.1.23:7000
slots: (0 slots) slave
replicates f96b3194409eef942fb0f41b3983a06a7e9ff914
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
根据提示,输入yes,我们会看到集群创建成功。
5. 测试集群
启动Redis命令行模式
[root@bfd-yiz-1p23 /opt/redis/]# redis-cli -h 172.18.1.23 -c
输入命令进行测试,如下图:
可以看到,当执行Redis命令 set bfd 123
时,发生了节点重定向:172.18.1.23 -> 172.18.1.25,说明集群生效了!