一.簡介前端
Keepalived使用的vrrp協議方式,虛擬路由冗餘協議 (Virtual Router Redundancy Protocol);node
Heartbeat或Corosync是基於主機或網絡服務的高可用方式;
Keepalived的目的是模擬路由器的高可用,Heartbeat的目的是實現Service的高可用。
Keepalived經常使用的前端高可用的組合有,LVS+Keepalived、Nginx+Keepalived、HAproxy+Keepalived。
Heartbeat常見的組合有Heartbeat v3(Corosync)+Pacemaker+NFS+Httpd 實現Web服務器的高可用、Heartbeat v3(Corosync)+Pacemaker+NFS+MySQL 實現MySQL服務器的高可用。nginx
keepalived能夠認爲是VRRP協議在Linux上的實現,主要有三個模塊,分別是core、check和vrrp。
core模塊爲keepalived的核心,負責主進程的啓動、維護以及全局配置文件的加載和解析。
check負責健康檢查,包括常見的各類檢查方式。
vrrp模塊是來實現VRRP協議的。vim
二.環境bash
ip 負載-反向代理 高可用 vip服務器
192.168.15.106 nginx Keepalived 192.168.15.102網絡
192.168.15.105 nginx Keepalived 192.168.15.102代理
三.keepalived安裝rest
1.下載 wget http://www.keepalived.org/software/keepalived-1.3.2.tar.gz 2.安裝 tar xf keepalived-1.3.2.tar.gz cd keepalived-1.3.2 ./configure make && make install cp /usr/local/src/keepalived-1.3.2/keepalived/etc/init.d/keepalived /etc/init.d/ cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/sbin/keepalived /usr/sbin/
四.配置router
1,master
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived #郵件接受 global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } #監控腳本,監控nginx每兩秒運行一次 vrrp_script chk_http_port { script "/opt/chk_nginx.sh" interval 2 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state MASTER interface eth0 #網卡 mcast_src_ip 192.168.15.105 virtual_router_id 51 priority 100 #權重 advert_int 1 authentication { auth_type PASS auth_pass 1111 #密碼master和slave必須一致 } virtual_ipaddress { 192.168.15.102 #vip } track_script { chk_http_port } }
2.slave
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { ops@wangshibo.cn tech@wangshibo.cn } notification_email_from ops@wangshibo.cn smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id slave-node } vrrp_script chk_http_port { script "/opt/chk_nginx.sh" interval 2 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state BACKUP interface eth0 mcast_src_ip 192.168.15.106 virtual_router_id 51 priority 99 #從要小於主 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.15.102 } track_script { chk_http_port } }
3.開啓防火牆
vim /etc/sysconfig/iptables
-A INPUT -d 224.0.0.0/8 -i eth0 -j ACCEPT
/etc/init.d/iptables restart
五.nginx的監控腳本
vim /opt/chk_nginx.sh
#!/bin/bash counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /usr/local/nginx/sbin/nginx sleep 2 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then /etc/init.d/keepalived stop fi fi