一、Keepalived

   keepalived前提准备见上文 --> 

二、keepalived高可用LVS

  keepalived能够根据配置文件生成ipvs规则,同时可以对后端各RS做健康状态检测

  1、实验清单

  director1: node1 192.168.0.40

  director2: Nginx 192.168.0.108

  Vip:192.168.0.80

  RS1(httpd):192.168.0.100

  RS2(httpd):192.168.0.101

  2、配置文件

[root@Nginx ~]# cd /etc/keepalived/[root@Nginx keepalived]# vim keepalived.conf! Configuration File for keepalivedglobal_defs {   notification_email {	root@localhost   }   notification_email_from kaadmin@localhost   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id LVS_DEVEL}vrrp_script chk_mt {    script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"    interval 1    weight -2}vrrp_instance VI_1 {    state MASTER               #node1须修改为BACKUP    interface eth0    virtual_router_id 51    priority 100               #node1降低优先级    advert_int 1     authentication {        auth_type PASS        auth_pass 71988d704dcae985    }    virtual_ipaddress {        192.168.0.80/32    }    track_script {	chk_mt	    }    notify_master "/etc/keepalived/notify.sh master"    notify_backup "/etc/keepalived/notify.sh backup"    notify_fault "/etc/keepalived/notify.sh fault"}virtual_server 192.168.0.80 80 {    delay_loop 6     #服务器轮询6次超时               lb_algo rr       #LVS调度算法    lb_kind DR       #LVS转发方法    nat_mask 255.255.255.0     #掩码    persistence_timeout 50     #长链接时间    protocol TCP               #tcp协议    ha_suspend                 #在无vip情形下,不再进行健康状态检测    sorry_server 127.0.0.1 80        #当RS全宕机时,sorry_server提供错误页面    real_server 192.168.0.100 80 {          #RS的ip,端口        weight 1                            #权重        HTTP_GET {                          #检测类型,这里是HTTP_GET            url {                           #检测请求的类型,这里是状态检测              path /	      status_code 200            }            connect_timeout 3               #连接超时时间            nb_get_retry 3                  #重试次数            delay_before_retry 3            #重试前延迟时间        }    }   real_server 192.168.0.101 80 {        weight 2        HTTP_GET {            url {              path /              status_code 200            }            connect_timeout 3            nb_get_retry 3            delay_before_retry 3        }    }}

 3、定义状态转变后发邮件的notify脚本(来自马哥文档)

#!/bin/bash# vip=192.168.0.80contact='root@localhost'notify() {    mailsubject="`hostname` to be $1: $vip floating"    mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"    echo $mailbody | mail -s "$mailsubject" $contact}case "$1" in    master)        notify master        exit 0    ;;    backup)        notify backup        exit 0    ;;    fault)        notify fault        exit 0    ;;    *)        echo 'Usage: `basename $0` {master|backup|fault}'        exit 1    ;;esac

二、高可用Nginx

   1、在Nginx主机上配置反向代理upstream

http区段配置        upstream nodeserver{       server 192.168.0.30;       server 192.168.0.40;    }server区段配置    location / {         proxy_pass http://nodeserver;         proxy_set_header Host    $host;         proxy_set_header X-Real-IP  $remote_addr;         add_header X-Cache $upstream_cache_status;         }

   2、配置keepalived,加入如下配置

global_defs {   notification_email {	root@localhost   }   notification_email_from kaadmin@localhost   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id LVS_DEVEL}vrrp_script chk_nginx {    script "killall -0 nginx &> /dev/null"    interval 1    weight -10}vrrp_instance VI_1 {    state MASTER    interface eth0    virtual_router_id 51    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass 71988d704dcae985    }    virtual_ipaddress {        192.168.0.80/32    }    track_script {	chk_nginx	    }    notify_master "/etc/keepalived/notify.sh master"    notify_backup "/etc/keepalived/notify.sh backup"    notify_fault "/etc/keepalived/notify.sh fault"}