基于Haporxy+Keepalived构建高可用负载均衡集群

基于Haporxy+Keepalived构建高可用负载均衡集群

1、准备四台虚拟机两台nginx 两台haproxy
2、实验如下:

基于Haporxy+Keepalived构建高可用负载均衡集群
1、准备四台虚拟机两台nginx 两台haproxy
2、两台nginx
[root@Nginx-1 ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@Nginx-1 ~]# useradd -M -s /sbin/nologin  nginx
[root@Nginx-1 ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src
[root@Nginx-1 ~]# cd /usr/src/nginx-1.6.2
[root@Nginx-1 nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[root@Nginx-1 nginx-1.6.2]# cd /usr/local/nginx/html/
nginx1 :
[root@localhost ~]# echo "111111" > /usr/local/nginx/html/index.html
[root@localhost ~]# /usr/local/nginx/sbin/nginx
nginx2 :
[root@localhost ~]# echo "222222" > /usr/local/nginx/html/index.html
[root@localhost ~]# /usr/local/nginx/sbin/nginx
 
安装Haproxy两台机器配置一致:
[root@Haproxy-1 ~]# yum -y install gcc gcc-c++ make pcre-devel bzip2-devel
[root@Haproxy-1 ~]# tar xf haproxy-1.4.24.tar.gz -C /usr/src/
[root@Haproxy-1 ~]# cd /usr/src/haproxy-1.4.24/
[root@Haproxy-1 haproxy-1.4.24]# make TARGET=linux26 && make install
建立haproxy的配置目录及文件
[root@Haproxy-1 haproxy-1.4.24]# mkdir /etc/haproxy
[root@Haproxy-1 haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
 haproxy配置文件的修改:
[root@Haproxy-1 ~]# vim /etc/haproxy/haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1
global
#   log 127.0.0.1   local0
#   log 127.0.0.1   local1 notice
    log /dev/log    local0 info
    log /dev/log    local0 notice
    maxconn 4096
    uid 99
    gid 99
    daemon
defaults
    log global
    mode    http
    option  httplog
    retries 3
    maxconn 4096
    contimeout  5000
    clitimeout  50000
    srvtimeout  50000
listen  webcluster 0.0.0.0:80
    option  httpchk GET /index.html
    balance roundrobin
    server  inst1 192.168.200.113:80 check inter 2000 fall 3   //nginx 主机
    server  inst1 192.168.200.114:80 check inter 2000 fall 3
listen admin_stats
    bind 0.0.0.0:8000
    mode http
    option httplog
    maxconn 100
    stats refresh 30s
    stats uri /stats
    stats realm Crushlinux\ Haproxy
        stats auth admin:admin
    stats hide-version
创建自启动脚本
[root@Haproxy-1 ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
[root@Haproxy-1 ~]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@Haproxy-1 ~]# chmod +x /etc/init.d/haproxy
[root@Haproxy-1 ~]# /etc/init.d/haproxy start
Starting haproxy:                                          [确定]
 
客户端访问测试:
使用不同浏览器访问
结果不同即为成功
 
编译安装keepalived服务
[root@Haproxy-1 ~]# yum -y install keepalived
[root@Haproxy-1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
        notification_email {
                acassen@firewall.loc
        }
        notification_email_from Alexandre.Cassen@firewall.loc
        smtp_server 192.168.200.1
        smtp_connect_timeout 30
        router_id LVS_DEVEL
        vrrp_skip_check_adv_addr
        vrrp_strict
        vrrp_garp_interval 0
        vrrp_gna_interval 0
}
vrrp_script chk_http_port {
        script "/root/check_haproxy.sh"
        interval 2
        weight -20
}
vrrp_instance VI_1 {
        state MASTER
        interface ens32
        virtual_router_id 51
        priority 100
        advert_int 1
        authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.254
    }
    track_script {
        chk_http_port
    }
}
 第二台Haproxy配置keepalibed 主配置文件
[root@Haproxy-2 ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
        notification_email {
                acassen@firewall.loc
        }
        notification_email_from Alexandre.Cassen@firewall.loc
        smtp_server 192.168.200.1
        smtp_connect_timeout 30
        router_id LVS_DEVEL
        vrrp_skip_check_adv_addr
        vrrp_strict
        vrrp_garp_interval 0
        vrrp_gna_interval 0
}
vrrp_script chk_http_port {
        script "/root/check_haproxy.sh"
        interval 2
        weight -20
}
vrrp_instance VI_1 {
        state BACKUP
        interface ens32
        virtual_router_id 51
        priority 90
        advert_int 1
        authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.200.254
    }
    track_script {
        chk_http_port
    }
}          
两台机器上都配置haproxy检测脚本
[root@Haproxy-1 ~]# cat /root/check_haproxy.sh
#!/bin/bash
num=$(ps -C haproxy --no-header |wc -l)
if [ $num -eq 0 ]
then
    /etc/init.d/haproxy start
    sleep 3
    a=$(ps -C haproxy --no-header |wc -l)
    if [ $a -eq 0 ]
    then
        systemctl stop keepalived
    fi
fi
[root@Haproxy-1 ~]# chmod +x /root/check_haproxy.sh
[root@Haproxy-1 ~]# service keepalived start

posted @ 2019-11-29 17:00  elin菻  阅读(129)  评论(0编辑  收藏  举报