一、编译安装keepalived-1.2.8
下载地址: http://www.keepalived.org/download.html
注意最新的不一定是最好的,所以选择1.2.8
tar xf keepalived-1.2.8.tar.gz
cd keepalived-1.2.8
mkdir -p /data/soft/keepalived
./configure --prefix=/data/soft/keepalived
注意:编译安装之前,可能需要先安装gcc和一些额外的包,请自行解决
make && make install
编译报错1
../include/vrrp_ipaddress.h:32:27: error: linux/if_addr.h: No such file or directory
In file included from ../include/vrrp.h:31,
from ../include/smtp.h:34,
from smtp.c:27:
../include/vrrp_ipaddress.h:41: error: field ‘ifa’ has incomplete type
make2: * [smtp.o] Error 1
make2: Leaving directory `/root/keepalived-1.2.7/keepalived/core'
make1: [all] Error 1
make1: Leaving directory `/root/keepalived-1.2.7/keepalived'
make: * * [all] Error 2
原因及解决办法:
这是kernel-headers软件版本过低造成的
yum -y install kernel-headers
问题解决
编译报错2
make2: * [vrrp_ipaddress.o] Error 1
make2: Leaving directory `/data/soft/keepalived-1.2.9/keepalived/vrrp'
make1: [all] Error 1
make1: Leaving directory `/data/soft/keepalived-1.2.9/keepalived'
make: * * [all] Error 2
解决办法:
将编译keepalived-1.2.9.tar.gz改成编译keepalived-1.2.8.tar.gz即可
转移文件到正确的位置
mkdir /etc/keepalived
pwd
/data/soft/keepalived
cp etc/keepalived/keepalived.conf /etc/keepalived/
cp etc/rc.d/init.d/keepalived /etc/init.d/
cp etc/sysconfig/keepalived /etc/sysconfig/
cp sbin/keepalived /usr/sbin/
chkconfig --add keepalived
service keepalived start
Starting keepalived: [ OK ]
chkconfig --add keepalived
chkconfig keepalived on
二、配置文件以及监控脚本
配置文件
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from amos.lu@sky-mobi.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "nohup /etc/keepalived/chk_nginx.sh 2> /root/error.log & &> /dev/null"
interval 20
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 200
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.18.5.10
}
track_script {
chk_nginx
}
} 监控脚本
# vim /etc/keepalived/chk_nginx.sh
#!/bin/bash
port=80
nc -z -w 10 localhost $port &> /dev/null
if [ $? -ne 0 ];then
sleep 10
nc -z -w 10 localhost $port &> /dev/null
if [ $? -ne 0 ];then
/etc/init.d/mysqld restart &> /dev/null
sleep 10
nc -z -w 10 localhost $port &> /dev/null
if [ $? -ne 0 ];then
/etc/init.d/keepalived stop
fi
fi
fi 注意监测端口连通性的时候,不能只用netstat看这个端口是否存在,有时候存在端口,却是无法连通的,所有要用nc命令检测。
主备节点做对应修改即可。
生产环境中keepalived的配置文件
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
summer.xia@sky-mobi.com
}
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass skymobi
}
virtual_ipaddress {
192.168.6.38
}
}
virtual_server 192.168.6.38 80 {
delay_loop 6
lb_algo wlc
#lb_algo wrr
#lb_algo rr
lb_kind DR
#persistence_timeout 50
protocol TCP
real_server 192.168.6.28 80 {
weight 30
#inhibit_on_failure
HTTP_GET {
url {
path /nstatus
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.6.34 80 {
weight 30
#inhibit_on_failure
HTTP_GET {
url {
path /nstatus
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.6.35 80 {
weight 30
#inhibit_on_failure
HTTP_GET {
url {
path /nstatus
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com