NGINX+keepalived

参考博客: http://pmghong.blog.51cto.com/3221425/1264947/

第一步:配置NGINX负载均衡   http://www.cnblogs.com/carbon3/p/5853590.html

第二步:配置 keepalived 的高可用

二、安装配置keepalived(node2配置参考node1,大体上一样)

1、安装配置keepalived
[root@node1 src]# yum -y install popt popt-devel popt-static openssl-devel kernel-devel libnl libnl-devel
[root@node1 src]# wget http://www.keepalived.org/software/keepalived-1.2.23.tar.gz
[root@node1 src]# tar -zxvf keepalived-1.2.23.tar.gz
[root@node1 src]# cd keepalived-1.2.23
[root@node1 keepalived-1.2.23]# ./configure --prefix=/usr/local/keepalived
 

configure: error:
!!! Please install libnfnetlink headers.

解决办法:
yum install -y libnfnetlink-devel

安装成功后显示:

Keepalived configuration
------------------------
Keepalived version       : 1.2.23
Compiler                 : gcc
Compiler flags           : -g -O2 -DFALLBACK_LIBNL1
Extra Lib                : -lssl -lcrypto -lcrypt  -lnl  
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
IPVS use libnl           : Yes
fwmark socket support    : Yes
Use VRRP Framework       : Yes
Use VRRP VMAC            : Yes
Use VRRP authentication  : Yes
SNMP keepalived support  : No
SNMP checker support     : No
SNMP RFCv2 support       : No
SNMP RFCv3 support       : No
SHA1 support             : No
Use Debug flags          : No
Memory alloc check       : No
libnl version            : 1
Use IPv4 devconf         : No
Use libiptc              : No
Use libipset             : No

接着

 

[root@node1 keepalived-1.2.4]# make && make install

 

[root@node1 keepalived-1.2.4]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@node1 keepalived-1.2.4]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@node1 keepalived-1.2.4]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@node1 keepalived-1.2.4]# mkdir -p /etc/keepalived
[root@node1 keepalived-1.2.4]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

[root@node1 keepalived-1.2.4]# chmod +x /etc/init.d/keepalived

 配置

[root@node1 keepalived-1.2.4]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
carbon3@qq.com
}
notification_email_from carbon3@qq.com
smtp_server 192.168.128.128 # node2上改为192.168.128.129
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx { # 定义监控脚本
script "/etc/keepalived/checkNginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER # node2 修改为BACKUP
interface eth0 #node2 可能是eth1 我在虚拟测试的时候就是 系统是克隆过来的。
virtual_router_id 51
priority 120 # node2上的数值应低于这个值,例如100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.128.120
}
track_script { # 执行监控脚本,这段代码一定要加!!否则不会调用上面的脚本
chk_nginx
}

}

 

2、创建检测脚本
[root@node1 keepalived-1.2.4]# vim /etc/keepalived/checkNginx.sh
#!/bin/bash
#auto check nginx process
killall -0 nginx
if [[ $? -ne 0 ]];then
/etc/init.d/keepalived stop

fi

 

[root@node1 keepalived-1.2.4]# chmod +x /etc/keepalived/checkNginx.sh

 

3、启动服务
[root@node1 ~]# service keepalived start
Starting keepalived: [ OK ]
[root@node2 ~]# service keepalived start
Starting keepalived: [ OK ]
主服务器:
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:1c:5b:83 brd ff:ff:ff:ff:ff:ff
    inet 192.168.128.128/24 brd 192.168.128.255 scope global eth0
    inet 192.168.128.120/32 scope global eth0
    inet6 fe80::20c:29ff:fe1c:5b83/64 scope link 
       valid_lft forever preferred_lft forever

从服务器 

[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:b1:0a:0b brd ff:ff:ff:ff:ff:ff
    inet 192.168.128.129/24 brd 192.168.128.255 scope global eth1
    inet6 fe80::20c:29ff:feb1:a0b/64 scope link 
       valid_lft forever preferred_lft forever

当主服务器挂了的话,从服务器就会接管VIP 用ip addr 查看的时候 就会出现 192.168.128.120 。当主服务器恢复的时候 VIP双会被主服务器接管。

 扩展阅读:http://zkchang.blog.51cto.com/10574636/1706998  

keepalived的 主从 和双主模式
双主的话 需要在域名供应商里 给那个域名设置两个IP 这样可以轮询 。这样 keepalived的两个VIP就起做用了。

http://www.cnblogs.com/holbrook/archive/2012/10/25/2738475.html

posted @ 2016-09-10 19:00    阅读(3292)  评论(0编辑  收藏  举报