Redis集群部署

1, 案例环境, 注意服务器时间保持一致

centos 7.7  192.168.131.128:6379    master1
centos 7.7  192.168.131.133:6379    master2
centos 7.7  192.168.131.135:6379    master3
centos 7.7  192.168.131.128:6380    slave1
centos 7.7  192.168.131.133:6380    slave2
centos 7.7  192.168.131.135:6380    slave3

2, 编译安装最新稳定版redis脚本(所有主机)

#!/usr/bin/env bash

for i in curl wget gcc gcc-c++ make epel-release
  do
    rpm -q $i || yum install -y $i
  done
rm -rf /tmp/redis*
redis_link=$(curl -s "https://redis.io/download" |grep 'download-link' |grep -o 'http.*[0-9].*tar.gz' |head -1)
redis_pkg=$(echo $redis_link |awk -F '/' '{print $NF}')
pkg_name=$(echo $redis_link |awk -F '/' '{print $NF}' |grep -o 'redis.*[0-9]')
wget -P /tmp $redis_link
tar xf /tmp/$redis_pkg -C /tmp
cd /tmp/$pkg_name ; make
cd /tmp/$pkg_name/src ; make install
mkdir -p /usr/local/redis/{etc,bin}
mkdir /var/lib/redis
cp -a /tmp/$pkg_name/src/{redis-server,redis-cli} /usr/local/redis/bin
cp -a /tmp/$pkg_name/redis.conf /usr/local/redis/etc
cat > /usr/lib/systemd/system/redis.service <<-EOF
[Unit]
Description=Redis
After=network.target

[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf --daemonize yes
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/usr/local/redis/bin/redis-cli -p 6379 shutdown
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl restart redis
systemctl enable redis

3, 配置redis(所有节点)

配置文件路径: /usr/local/redis/etc/redis.conf

protected-mode no                      //* 关闭保护模式
daemonize yes                          //* 开启守护进程
pidfile /var/run/redis_6379.pid        //* pid文件路径
dir /var/lib/redis                     //* 定义数据存放路径
appendonly yes                         //* 开启持久化
cluster-enabled yes                    //* 开启集群
cluster-config-file nodes-6379.conf    //* 集群配置文件
cluster-node-timeout 15000             //* 集群超时时间

配置好之后重启,会生成下列文件

ls /var/lib/redis/
appendonly-6380.aof  appendonly.aof  dump.rdb  nodes-6379.conf  nodes-6380.conf

[[email protected] ~]# cat /var/lib/redis/nodes-6379.conf    //* master1节点token
a1870c3e419afefffdb547cce9eafa78c51f60e8 192.168.131.128:[email protected] myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0

[[email protected] ~]# cat /var/lib/redis/nodes-6380.conf    //* slave1节点token
26c02b44d17f0eca26d0889297e1c37dbdaf0a03 :[email protected] myself,master - 0 0 0 connected
vars currentEpoch 0 lastVoteEpoch 0

4, 节点握手

[[email protected] ~]# redis-cli 
127.0.0.1:6379> CLUSTER MEET 192.168.131.128 6379
OK
127.0.0.1:6379> CLUSTER MEET 192.168.131.128 6380
OK
127.0.0.1:6379> CLUSTER MEET 192.168.131.133 6379
OK
127.0.0.1:6379> CLUSTER MEET 192.168.131.133 6380
OK
127.0.0.1:6379> CLUSTER MEET 192.168.131.135 6379
OK
127.0.0.1:6379> CLUSTER MEET 192.168.131.135 6380
OK

5, 查看节点关系

127.0.0.1:6379> CLUSTER NODES
26c02b44d17f0eca26d0889297e1c37dbdaf0a03 192.168.131.128:[email protected] master - 0 1578300673594 1 connected
18c7d610c118d3d89919749884f6748c06be882d 192.168.131.128:[email protected] myself,master - 0 1578300670000 9 connected
9b86f552d93ea48971255b5a7b2149e86ad66a05 192.168.131.135:[email protected] master - 0 1578300674000 11 connected
382ddd66cf356820755a62fe4e576028b6330eca 192.168.131.133:[email protected] master - 0 1578300674601 6 connected
7755d6d278365f34f52b1a905f344d50fe002836 192.168.131.133:[email protected] master - 0 1578300671000 10 connected
d9d8422841d66e389dbddd856726264ca0d9c9e3 192.168.131.135:[email protected] master - 0 1578300672586 0 connected

Snipaste_2020-01-06_16-53-43.png
Snipaste_2020-01-06_17-00-51.png
可以看到此时集群的状态是失败的,我们需要把这16383个槽分出去,集群才能正常工作,分配槽的命令如下:

[[email protected] redis]# redis-cli -h 192.168.131.128 -p 6379 cluster addslots {0..5461}
OK
[[email protected] redis]# redis-cli -h 192.168.131.133 -p 6379 cluster addslots {5462..10922}
OK
[[email protected] redis]# redis-cli -h 192.168.131.135 -p 6379 cluster addslots {10923..16383}
OK

Snipaste_2020-01-06_17-02-12.png
可以看到现在集群已经生效了

6, 主节点已经配置完毕,下面我们配置主从关系,在所有从节点上执行,指定对应的主节点ID即可!

[[email protected] redis]# redis-cli -h 192.168.131.128 -p 6380
192.168.131.128:6380> CLUSTER REPLICATE 18c7d610c118d3d89919749884f6748c06be882d
OK

[[email protected] redis]# redis-cli -h 192.168.131.133 -p 6380
192.168.131.133:6380> CLUSTER REPLICATE 7755d6d278365f34f52b1a905f344d50fe002836
OK

[[email protected] redis]# redis-cli -h 192.168.131.135 -p 6380
192.168.131.135:6380> CLUSTER REPLICATE 9b86f552d93ea48971255b5a7b2149e86ad66a05
OK

Snipaste_2020-01-06_17-12-02.png
Snipaste_2020-01-06_17-13-59.png
可以看到所有的主从关系都已经对应了!

7, 验证集群是否实现高可用

[[email protected] redis]# redis-cli -c -h 192.168.131.128 -p 6379
192.168.131.128:6379> set name stark
-> Redirected to slot [5798] located at 192.168.131.133:6379
OK
192.168.131.133:6379> get name
"stark"

[[email protected] redis]# redis-cli -c -h 192.168.131.128 -p 6380
192.168.131.128:6380> get name
-> Redirected to slot [5798] located at 192.168.131.133:6379
"stark"

[[email protected] redis]# redis-cli -c -h 192.168.131.133 -p 6379
192.168.131.133:6379> get name
"stark"

[[email protected] redis]# redis-cli -c -h 192.168.131.133 -p 6380
192.168.131.133:6380> get name
-> Redirected to slot [5798] located at 192.168.131.133:6379
"stark"

[[email protected] redis]# redis-cli -c -h 192.168.131.135 -p 6379
192.168.131.135:6379> get name
-> Redirected to slot [5798] located at 192.168.131.133:6379
"stark"

[[email protected] redis]# redis-cli -c -h 192.168.131.135 -p 6380
192.168.131.135:6380> get name
-> Redirected to slot [5798] located at 192.168.131.133:6379
"stark"

Snipaste_2020-01-06_17-26-20.png
关掉cluster1节点,然后进行测试

[[email protected] redis]# redis-cli -c -h 192.168.131.133 -p 6379
192.168.131.133:6379> get name
(error) CLUSTERDOWN The cluster is down
//* 这咋就报错了呢??排一波错再说!
[[email protected] redis]# redis-cli --cluster fix 192.168.131.133:6379

Snipaste_2020-01-06_17-32-09.png
通过上图可以看到,down掉的redis已经被剔除了

[[email protected] redis]# redis-cli -c -h 192.168.131.133 -p 6379
192.168.131.133:6379> get name
"stark"

[[email protected] redis]# redis-cli -c -h 192.168.131.133 -p 6380
192.168.131.133:6380> get name
-> Redirected to slot [5798] located at 192.168.131.133:6379
"stark"

[[email protected] redis]# redis-cli -c -h 192.168.131.135 -p 6379
192.168.131.135:6379> get name
-> Redirected to slot [5798] located at 192.168.131.133:6379
"stark"

[[email protected] redis]# redis-cli -c -h 192.168.131.135 -p 6380
192.168.131.135:6380> get name
-> Redirected to slot [5798] located at 192.168.131.133:6379
"stark"