centos7.9部署keepalived
# 一、keepalived下载
https://www.keepalived.org/software/keepalived-2.2.2.tar.gz
# 二、安装依赖
yum -y install gcc curl openssl-devel libnl3-devel net-snmp-devel
# 三 、部署keepalived
tar -xf keepalived-2.2.2.tar.gz
cd keepalived-2.2.2
./configure --prefix=/data/keepalived
make
make install
1
2
3
4
5
2
3
4
5
查看版本
ln -s /data/keepalived/sbin/keepalived /usr/sbin/keepalived
keepalived -v
1
2
2
# 四、配置启动服务
vim /usr/lib/systemd/system/keepalived.service
[Unit]
Description=LVS and VRRP High Availability Monitor
After=network-online.target syslog.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/keepalived.pid
KillMode=process
EnvironmentFile=-/data/keepalived/etc/sysconfig/keepalived
ExecStart=/data/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 五、创建配置文件
创建主的配置
mkdir /etc/keepalived
cp /data/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 10
router_id kp_ng01
}
vrrp_script chk_nginx {
script "/etc/keepalived/sh/check.sh"
interval 3
}
vrrp_instance VI_1 {
state MASTER
interface ens192
virtual_router_id 70
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 12345
}
track_script {
chk_nginx
}
virtual_ipaddress {
172.16.30.161
}
}
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
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
check.sh脚本
#!/bin/bash
echo "[nginx_check.sh]:now prepareing to healthy check " >> /var/log/messages
n=`netstat -tunlp | grep nginx|wc -l`
if [ $n -eq "0" ]; then
echo "[ nginx_check.sh]:now start nginx">> /var/log/messages
/data/nginx/sbin/nginx
n2=`netstat -tunlp | grep nginx|wc -l`
if [ $n2 -eq "0" ]; then
echo "[nginx_check.sh]:nginx down,keepalived will stop" >> /var/log/messages
systemctl stop keepalived
fi
fi
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
配置从配置
mkdir /etc/keepalived
cp /data/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 10
router_id kp_ng02
}
vrrp_script chk_nginx {
script /etc/keepalived/sh/check.sh
interval 3
# weight -20
# fall 2
# rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 70
priority 90
advert_int 2
authentication {
auth_type PASS
auth_pass 12345
}
track_script {
chk_nginx
}
virtual_ipaddress {
172.16.30.161
}
}
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
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
check.sh脚本
#!/bin/bash
echo "[nginx_check.sh]:now prepareing to healthy check " >> /var/log/messages
n=`netstat -tunlp | grep nginx|wc -l`
if [ $n -eq "0" ]; then
echo "[ nginx_check.sh]:now start nginx">> /var/log/messages
/data/nginx/sbin/nginx
n2=`netstat -tunlp | grep nginx|wc -l`
if [ $n2 -eq "0" ]; then
echo "[nginx_check.sh]:nginx down,keepalived will stop" >> /var/log/messages
systemctl stop keepalived
fi
fi
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 六、配置日志文件
1.将keepalived日志输出到local0:
/data/keepalived/etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D -d -S 0"
1
2
2
2.在/etc/rsyslog.conf 末尾里添加:
vim /etc/rsyslog.conf
local0.* /var/log/keepalived.log
1
2
2
3.重新启动keepalived和rsyslog服务:
systemctl restart rsyslog
systemctl restart keepalived
1
2
2
上次更新: 2023/04/21, 08:57:47