天高云淡 发表于 2018-12-31 07:44:32

keepalived做nginx的高可用,企业版简单介绍。

  一、编译安装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: * Error 1
make2: Leaving directory `/root/keepalived-1.2.7/keepalived/core'
make1: Error 1
make1: Leaving directory `/root/keepalived-1.2.7/keepalived'
make: ** Error 2
原因及解决办法:
这是kernel-headers软件版本过低造成的
[*]  yum -y install kernel-headers
问题解决
编译报错2
make2: * Error 1
make2: Leaving directory `/data/soft/keepalived-1.2.9/keepalived/vrrp'
make1: Error 1
make1: Leaving directory `/data/soft/keepalived-1.2.9/keepalived'
make: ** 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.sh2> /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]
查看完整版本: keepalived做nginx的高可用,企业版简单介绍。