Keepalived的作用是检测web服务器的状态,如果有故障,自动将其剔除,人工修复好后,自动将其添加到服务器组。

haproxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代 理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。出现单点故障,使服务转移到其他节点上,保障服务不受影响。

下面是两项功能的结合,排版很有乱,看官忍耐


172.16.15.17keepalived+haproxy
172.16.15.13keepalived+haproxy
172.16.15.11index.html
172.16.15.12inidex.php


配置172.16.15.17上keepalived

yum install keepalived

global_defs {
   notification_email {
        root@localhost
   }
   notification_email_from linux@keep.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
  script "killall -0 haproxy"
  interval 1
  weight 2
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.15.5
    }
    track_script {
        chk_haproxy
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
                                                                                                                                                                                                                                                                                                                                                                       
vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 55
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2111
    }
    virtual_ipaddress {
        172.16.15.15
    }
      track_script {
        chk_haproxy
    }
}

为keepalived提供脚本文件

#vim /etc/keepalived/notify.sh
#!/bin/bash
# Author: MageEdu <linuxedu@foxmail.com>
# description: An example of notify script
#
vip=172.16.15.5
contact='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
        /etc/rc.d/init.d/haproxy start
        exit 0
    ;;
    backup)
        notify backup
        /etc/rc.d/init.d/haproxy stop
        exit 0
    ;;
    fault)
        notify fault
        /etc/rc.d/init.d/haproxy stop
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {master|backup|fault}'
        exit 1
    ;;
esac


配置172.16.15.13上keepalived

[root@node3 ~]# yum install -y keepalived
! Configuration File for keepalived
global_defs {
   notification_email {
    root@localhost
   }
   notification_email_from linux@keep.com
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script chk_haproxy {
  script "killall -0 haproxy"
  interval 1
  weight 2
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 50   
    priority 99             
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111       
    }
    virtual_ipaddress {
    172.16.15.5                    
    }
                                                                                                                                                                                                                                                                                                                
    track_script {
    chk_haproxy
    }
}
vrrp_instance VI_2 {
    state MASTER        
    interface eth0
    virtual_router_id 55    
    priority             
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 2111       
    }
    virtual_ipaddress {
    172.16.15.15                   
    }
      track_script {
    chk_haproxy
    }
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

脚本文件和172.16.15.17一样 只需将vip改为172.16.15.15


配置172.16.15.17上haproxy

[root@master ~]# yum install haproxy -y
[root@master ~]# vim /etc/haproxy/haproxy.cfg
global
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    stats socket /var/lib/haproxy/stats
                                                                                                                                                                                                                                                                                             
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
frontend proxy *:80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js .html
    acl host_static      hdr_beg(host) -i img. video. down. ftp. imgs. image. videos.
    acl url_php         path_end        -i .php
                                                                                                                                                                                                                                                                                                
    use_backend static          if url_static or host_static
    use_backend dynamic          if url_php
    default_backend             dynamic
                                                                                                                                                                                                                                                                                             
backend static
    balance roundrobin
    server      node1 172.16.15.11:80 check
backend dynamic
    balance roundrobin
    server  node2  172.16.15.12:80  check
listen statistics
    bind *:4454
    stats enable
    stats auth admin:admin


172.16.15.13 配置hapeoxy的过程一样


测试

keepalived和haproxy都启动,两个流动ip分别在172.16.15.17和172.16.15.13上

wKiom1NmCznyvNrRAATQpocg4ZY167.jpg


将172.16.15.17的haproxy宕掉

wKioL1NmC1agqT7WAATxAV9JY0g775.jpg

keepalived双主模式完成


后端配置

172.16.15.11 提供测试网页文件 echo "<h1>node=1</h1>" >> /var/www/html/index.html

   如果是Nginx 要在/etc/nginx/nginx.conf  root后指定路径

location / {

           root  /var/www/html;

           index  index.html index.htm;

       }

172.16.15.12 提供php测试页面

yum install php php-mysql


启动服务,检测效果

wKiom1NmCJSz-AHkAAJu-89sVbs208.jpg


haproxy代理也实现高可用