配置一主二从(master 192.168.117.129,slave 192.168.117.130,192.168.117.131)

在三台机器上分别安装redis

i)下载安装包

1
wget http://download.redis.io/releases/redis-4.0.6.tar.gz

ii)上传并解压到/home下

1
tar -xzvf redis-4.0.6.tar.gz

iii)编译安装

1
2
3
cd /home/redis-4.0.6
make MALLOC=libc
make install

iv)修改redis配置文件

1
2
echo "">redis.conf
vim redis.conf

master(192.168.117.129)修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised systemd
pidfile "/var/run/redis_6379.pid"
loglevel noticelogfile "/home/redis-4.0.6/redis-master.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename "dump.rdb"
dir "/home/redis-4.0.6"
masterauth "redis-2019"
slave-serve-stale-data yes
slave-read-only no
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass redis-2019
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes
maxclients 2000

slave(192.168.117.130,192.168.117.131)修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised systemd
pidfile /var/run/redis_6379.pid
loglevel notice
logfile "/home/redis-4.0.6/redis-slave.log"
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /home/redis-4.0.6/
slaveof 192.168.117.129 6379 #主数据库ip和端口
masterauth redis-2019 #主数据库密码
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
requirepass redis-2019
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

v)创建redis用户组和用户

1
2
3
4
groupadd redis
useradd -r -g redis redis
chown -R redis:redis /home/redis-4.0.6
chmod -R 770 /home/redis-4.0.6

vi)配置Redis的systemd服务,同时在master和slave配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vim /etc/systemd/system/redis.service

[Unit]

Description=Redis In-Memory Data Store



[Service]

User=redis
Group=redis
Type=forking
ExecStart=/home/redis-4.0.6/src/redis-server /home/redis-4.0.6/redis.conf
ExecStop=/home/redis-4.0.6/src/redis-cli -h 127.0.0.1 -p 6379 shutdown
Restart=always

[Install]
WantedBy=multi-user.target

vii)启动redis服务

1
2
3
systemctl enable redis.service
systemctl start redis.service

viii)配置防火墙redis端口

1
2
firewall-cmd --permanent --add-port=6739/tcp
firewall-cmd --reload

安装keepalived服务

1)安装keepalived服务的主机都需要配置端口转发

1
2
3
[root@localhost ~]#  echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1

2)Redis-Master节点安装keepalived服务
i)keepalived安装包下载链接: https://pan.baidu.com/s/13kW_Bz6RGSo4ewZ68BwCVA 提取码: 6ktn
ii)修改keepalived的配置文件

1
2
yum localinstall *.rpm或rpm -ivh *.rpm --nodeps --force
vim /etc/keepalived/keepalived.conf

添加如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
vrrp_script chk_redis {
script "/etc/keepalived/redis_check.sh"
interval 2
}

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.117.208/32
}
notify_master "/etc/keepalived/redis_master.sh 192.168.117.129 6379"
notify_backup "/etc/keepalived/redis_backup.sh 192.168.117.129 6379"
}

iii)增加redis_master.sh文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
vim /etc/keepalived/redis_master.sh

#!/bin/bash
REDISCLI="/home/redis-4.0.6/src/redis-cli -a redis-2019"
LOGFILE="/etc/keepalived/keepalived-redis-state.log"
pid=$$
host=$1
port=$2


echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[slaver]" >> $LOGFILE 2>&1
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[slaver] Run 'SLAVEOF $host $port'" >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $host $port>> $LOGFILE 2>&1

echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[slaver] wait 10 sec for data sync from old master" >> $LOGFILE 2>&1
sleep 10
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[slaver] data rsync from old mater ok..." >> $LOGFILE 2>&1
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[master] Run slaveof no one,close master/slave" >> $LOGFILE 2>&1

$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1

echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[master] wait other slave connect...." >> $LOGFILE 2>&1

iv)增加redis_backup.sh文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vim /etc/keepalived/redis_backup.sh

#!/bin/bash
REDISCLI="/home/redis-4.0.6/src/redis-cli -a redis-2019"
LOGFILE="/etc/keepalived/keepalived-redis-state.log"
pid=$$
host=$1
port=$2
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[master] Being slave state..." >> $LOGFILE 2>&1
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[master] wait 10 sec for data sync from old master" >> $LOGFILE 2>&1
sleep 10
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[master] data rsync from old mater ok..." >> $LOGFILE 2>&1
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[slaver] Run 'SLAVEOF $host $port'" >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $host $port >> $LOGFILE 2>&1
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[slaver] slave connect to $host ok..." >> $LOGFILE 2>&1

3)Redis-Slave节点安装keepalived服务
i)修改keepalived的配置文件

1
2
yum localinstall *.rpm或rpm -ivh *.rpm --nodeps --force
vim /etc/keepalived/keepalived.conf

添加如下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
vrrp_script chk_redis {
script "/etc/keepalived/redis_check.sh"
interval 5
timeout 2
fall 3
}

vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 52
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}

virtual_ipaddress {
192.168.117.209/32
}

notify_master "/etc/keepalived/redis_master.sh 192.168.117.130 6379"
notify_backup "/etc/keepalived/redis_backup.sh 192.168.117.130 6379"
}

iii)增加redis_master.sh文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
vim /etc/keepalived/redis_master.sh

#!/bin/bash
REDISCLI="/home/redis-4.0.6/src/redis-cli -a redis-2019"
LOGFILE="/etc/keepalived/keepalived-redis-state.log"
pid=$$
host=$1
port=$2

echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[slaver]" >> $LOGFILE 2>&1
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[slaver] Run 'SLAVEOF $host $port'" >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $host $port>> $LOGFILE 2>&1

echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[slaver] wait 10 sec for data sync from old master" >> $LOGFILE 2>&1
sleep 10

echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[slaver] data rsync from old mater ok..." >> $LOGFILE 2>&1
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[master] Run slaveof no one,close master/slave" >> $LOGFILE 2>&1

$REDISCLI SLAVEOF NO ONE >> $LOGFILE 2>&1
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[master] wait other slave connect...." >> $LOGFILE 2>&1

iv)增加redis_backup.sh文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
vim /etc/keepalived/redis_backup.sh

#!/bin/bash
REDISCLI="/home/redis-4.0.6/src/redis-cli -a redis-2019"
LOGFILE="/etc/keepalived/keepalived-redis-state.log"
pid=$$
host=$1
port=$2

echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[master] Being slave state..." >> $LOGFILE 2>&1
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[master] wait 10 sec for data sync from old master" >> $LOGFILE 2>&1
sleep 10
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[master] data rsync from old mater ok..." >> $LOGFILE 2>&1
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[slaver] Run 'SLAVEOF $host $port'" >> $LOGFILE 2>&1
$REDISCLI SLAVEOF $host $port >> $LOGFILE 2>&1
echo "`date +'%Y-%m-%d %H:%M:%S'`|$pid|state:[slaver] slave connect to $host ok..." >> $LOGFILE 2>&1

4)验证
i)使用redis-cli连接主数据库并输入密码登录

1
2
3
[root@Master bin]# /home/redis-4.0.6/src/redis-cli -p 6379
127.0.0.1:6379> auth redis-2019
OK

查看主数据库info属性:

1
127.0.0.1:6379> info

image

ii)使用redis-cli连接从数据库并输入密码登录

1
2
3
[root@Master bin]# /home/redis-4.0.6/src/redis-cli -p 6379
127.0.0.1:6379> auth redis-2019
OK

查看从数据库info属性:

1
127.0.0.1:6379> info

image
iii)测试
如果上述两步都成功,在主数据库录入一条数据,在从库中查询
主库插入数据

1
2
127.0.0.1:6379> set num 13
ok

从库中查询

1
2
127.0.0.1:6379> get num
"13"

同步成功
5)配置防火墙上vrrp协议

1
2
3
4
5
6
7
[root@localhost ~]# firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 \
--in-interface enp0s8 --destination 224.0.0.18 --protocol vrrp -j ACCEPT

[root@localhost ~]# firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 \
--out-interface enp0s8 --destination 224.0.0.18 --protocol vrrp -j ACCEPT

[root@localhost ~]# firewall-cmd –reload

6)Redis运维(主备切换)

Redis主备服务切换是自动完成。切换至redis slave时,redis进入只读模式。