# 一、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
代码语言:javascript复制tar -xf keepalived-2.2.2.tar.gz
cd keepalived-2.2.2
./configure --prefix=/data/keepalived
make
make install
查看版本
代码语言:javascript复制ln -s /data/keepalived/sbin/keepalived /usr/sbin/keepalived
keepalived -v
# 四、配置启动服务
代码语言:javascript复制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
# 五、创建配置文件
创建主的配置
代码语言:javascript复制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
}
}
check.sh脚本
代码语言:javascript复制#!/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
配置从配置
代码语言:javascript复制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
}
}
check.sh脚本
代码语言:javascript复制#!/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.将keepalived日志输出到local0:
代码语言:javascript复制/data/keepalived/etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D -d -S 0"
2.在/etc/rsyslog.conf 末尾里添加:
代码语言:javascript复制vim /etc/rsyslog.conf
local0.* /var/log/keepalived.log
3.重新启动keepalived和rsyslog服务:
代码语言:javascript复制systemctl restart rsyslog
systemctl restart keepalived