keepalived+haproxy+nginx

vm1-keepalived+haproxy
        eth0 172.16.3.2/16 up
        eth1 192.168.1.2/24 up
vm2-keepalived+haproxy
        eth0 172.16.3.3/16 up
        eth1 192.168.1.3/24 up   
vm3-web:
        eth0 192.168.1.1/24 up
vm4-web:
        ech0 192.168.1.10/24 up
VRRP: 172.16.3.88/16

测试机本机:

vm1-keepalived+haproxy配置
    # ifconfig eth0 172.16.3.2/16 up
    # ifconfig eth1 192.168.1.2/24 up
    # yum install keepalived haproxy -y
haproxy配置文件:
    # vim /etc/haproxy/haproxy.cfg
    #---------------------------------------------------------------------
    # Example configuration for a possible web application.  See the
    # full configuration options online.
    #
    #   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
    #
    #---------------------------------------------------------------------

#---------------------------------------------------------------------
    # Global settings
    #---------------------------------------------------------------------
    global
        # to have these messages end up in /var/log/haproxy.log you will
        # need to:
        #
        # 1) configure syslog to accept network log events.  This is done
        #    by adding the ‘-r‘ option to the SYSLOGD_OPTIONS in
        #    /etc/sysconfig/syslog
        #
        # 2) configure local2 events to go to the /var/log/haproxy.log
        #   file. A line like the following can be added to
        #   /etc/sysconfig/syslog
        #
        #    local2.*                       /var/log/haproxy.log
        #
        log         127.0.0.1 local2

chroot      /var/lib/haproxy
        pidfile     /var/run/haproxy.pid
        maxconn     4000
        user        haproxy
        group       haproxy
        daemon

# turn on stats unix socket
        stats socket /var/lib/haproxy/stats

#---------------------------------------------------------------------
    # common defaults that all the ‘listen‘ and ‘backend‘ sections will
    # use if not designated in their block
    #---------------------------------------------------------------------
    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 main
        bind :80
        bind :8088
    #    acl clear dst_port 8088
        #acl login path_beg /login
        #redirect location http://www.baidu.com if login
        #redirect prefix / if clear
    #    reqadd X-Proto:\ SSL if clear
        #rspadd X-Via:\ haproxy if clear
        #option forwardfor except 127.0.0.0/8
        #acl url_static path_beg -i /images /stylesheets /vedios /javascript
        #acl url_static path_end -i .jpg .html .css .js .png .gif
        #use_backend static if url_static
        default_backend webservers

#backend static
    #    balance roundrobin
    #    server s1 192.168.1.10:80  check port 80
    #    server b1 127.0.01:8080 backup check port 8080
    backend webservers
        balance roundrobin
        #cookie ninghongliang insert nocache
        server s2 192.168.1.1:80 check port 80 
        server s1 192.168.1.10:80  check port 80
        server b1 127.0.01:8080 backup check port 8080
        stats enable
        stats hide-version
        stats uri /haha?stats
        stats scope .
        stats realm  HAPorx\ boy
    stats auth   admin1:admin
    stats admin if TRUE
keepalived配置文件
    ! Configuration File for keepalived

global_defs {
       notification_email {
        [email protected]
       }
       notification_email_from [email protected]
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
    }
    vrrp_script chk_sched_down {
        script "[ -e /etc/keepalived/down ] && exit 1 || exit 0"   
        interval 2
        weight -50
        fall 2
        rise 1
    }
    vrrp_script chk_haproxy {
        script "/etc/keepalived/check_haproxy.sh"
        interval 2
        weight -50
        fall 2
        rise 1
    }

vrrp_instance VI_1 {
        state MASTER
        interface eth0
        virtual_router_id 77
        priority 100
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass ning
        }
        virtual_ipaddress {
        172.16.3.88
        }
    track_script {
        chk_sched_down
        chk_haproxy
    }
    }
vm2-keepalived+haproxy配置
    # ifconfig eth0 172.16.3.3/16 up
    # ifconfig eth1 192.168.1.3/24 up
    # yum install keepalived haproxy -y
haproxy配置文件:(这里的配置和上面的一样,不在此说明)
keepalived配置文件
    ! Configuration File for keepalived

global_defs {
       notification_email {
        [email protected]
       }
       notification_email_from [email protected]
       smtp_server 127.0.0.1
       smtp_connect_timeout 30
    }
    vrrp_script chk_sched_down {
        script "[ -e /etc/keepalived/down ] && exit 1 || exit 0"   
        interval 2
        weight -50
        fall 2
        rise 1
    }
    vrrp_script chk_haproxy {
        script "/etc/keepalived/check_haproxy.sh"
        interval 2
        weight -50
        fall 2
        rise 1
    }

vrrp_instance VI_1 {
        state  BACKUP
        interface eth0
        virtual_router_id 77
        priority 99
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass ning
        }
        virtual_ipaddress {
        172.16.3.88
        }
    track_script {
        chk_sched_down
        chk_haproxy
    }
    }   
这里面使用的检测脚本haprxoy脚本chk_haproxy(测试检测为3秒后如果haproxy还不能上线,就关闭keepalived)
#!/bin/bash
A=`ps -C haproxy --no-header |wc -l`
if [ $A -eq 0 ];then
/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg
sleep 3
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi

vm3-web:配置
    # ifconfig eth0 192.168.1.1/24 up
    # yum install nginx -y
    # echo "192.168.1.1" > /usr/share/nginx/html/index.html
vm4-web:配置
    # ifconfig eth0 192.168.1.10/24 up
    # yum install nginx -y
    # echo "192.168.1.10" > /usr/share/nginx/html/index.html

测试:
    (1)主备同时执行 # ip addr show
       主: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
            link/ether 00:0c:29:d7:f7:9c brd ff:ff:ff:ff:ff:ff
            inet 172.16.3.2/16 brd 172.16.255.255 scope global eth0
            inet 172.16.3.88/32 scope global eth0
               valid_lft forever preferred_lft forever
        从: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
            link/ether 00:0c:29:0b:35:6a brd ff:ff:ff:ff:ff:ff
            inet 172.16.3.3/16 brd 172.16.255.255 scope global eth0
            inet6 fe80::20c:29ff:fe0b:356a/64 scope link
               valid_lft forever preferred_lft forever
      (2)停掉主上的haproxy,2秒后keepalived会自动将其再次启动
      (3)停掉主的keepalived,备机马上接管服务
            eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
            link/ether 00:0c:29:0b:35:6a brd ff:ff:ff:ff:ff:ff
            inet 172.16.3.3/16 brd 172.16.255.255 scope global eth0
            inet 172.16.3.88/32 scope global eth0
               valid_lft forever preferred_lft forever
      (4)本机测试:(因为上面只是简单的定义了轮训,没有定义动静分离)
http://172.16.3.88
            有图有真相

时间: 2024-12-12 00:34:13

keepalived+haproxy+nginx的相关文章

keepalived高可用haproxy/nginx

前言:本文主要讲解keepalived+haproxy,等试验完成,后面会附上keepalived+nginx的思路,原理几乎相同,相信能看懂keepalived+haproxy的朋友,亦能很简单的看懂keepalived+nginx 拓扑: 准备工作: 1).高可用集群节点基于名称互相访问(两节点都需配置,略) # vim /etc/sysconfig/network # vim /etc/hosts 2).高可用集群节点ssh互信(两节点都需配置) a.com: # ssh-keygen -

haproxy+keepalived搭建nginx+lamp集群

haproxy+keepalived搭建nginx+lamp集群 实验拓扑: 实验环境: 主机 Ip地址 软件 haproxy主调度器 192.168.100.154 keepalived-1.2.13.tar.gz haproxy-1.4.24.tar.gz haproxy从调度器 192.168.100.155 keepalived-1.2.13.tar.gz haproxy-1.4.24.tar.gz Nginx1 192.168.100.152 nginx-1.6.2.tar.gz Ng

多puppetmaster,多ca,keepalived+haproxy(nginx)puppet集群搭建

多puppetmaster,多ca,keepalived+haproxy(nginx)puppet集群搭建 一.服务器详情 192.168.122.111 pm01.jq.com pm01 #(puppetmaster服务器) 192.168.122.112 pm02.jq.com pm02 #(puppetmaster服务器) 192.168.122.121 ag01.jq.com ag01 #(puppet agent服务器) 192.168.122.122 ag02.jq.com ag02

HAProxy Nginx LVS Apache总结篇

今天也许是最后一次探讨关于HAProxy Nginx LVS Apache的文章,之后将不再赘述,博主之后将要把重心放在Java和Python上,大家如果有什么疑问可以通过博客首页QQ联系.或者留言. 一.今天花点时间总结分享一下HAProxy.Nginx.LVS.Apache: 比较 HAProxy Nginx LVS Apache 简介 高可用.负载均衡且基于TCP和HTTP应用的代理,支持高并发,多集群反代. 高性能http和反向代理服务器.邮件代理服务器,支持高并发,轻量级Web,低系统

keepalived+haproxy高可用

环境: 主Haproxy服务器 192.168.80.100 keepalived+Haproxy 备Haproxy服务器 192.168.80.101 keepalived+Haproxy web服务器1 192.168.80.102 httpd(nginx.tomcat) web服务器2 192.168.80.103 httpd 80.100和80.101需要联网 ---------------------------------------在80.100虚拟机上: systemctl st

keepalived+Haproxy搭建高可用Web群集

Haproxy是目前比较流行的一种群集调度工具,同类群集调度工具有很多,如LVS和Nginx.相比较而言,LVS性能最好,但是搭建相对复杂,搭建LVS群集可以参考博文:搭建:LVS+Keepalived高可用Web服务群集环境:Nginx的upstream模块支持群集功能,但是相对群集节点健康检查功能不强,性能没有Haproxy好,更多的是应用在企业内网环境中.Nginx群集可以参考博文:centos 7部署Tomcat及其负载均衡配置详解. 上述几个web群集调度器属于软件类型的,还有很多硬件

centos 7 LVS+keepalived实现nginx的高可用以及负载均衡

一.准备工作:关闭防火墙,selinux以免对实验结果造成影响,准备虚机,设置IP地址.主机名 hostname:Nginx01 IP:192.168.1.87 Role:Nginx Server hostname:Nginx02 IP: 192.168.1.88 Role:Nginx Server hostname:LVS01 IP: 192.168.1.89 Role:LVS+Keepalived hostname:LVS02 IP: 192.168.1.90 Role:LVS+Keepal

keepalived+haproxy实现高可用

实验环境: 2台centos 6.5作为keepalived+haproxy的高可用,3台centos6.5配置httpd作为后端server,haproxy的轮询采用rr调度算法.vip:192.168.8.199 ha1:eth1:192.168.8.41,keepalived+haproxy ha3:eth1:192.168.8.43,keepalived+haproxy  rs1:192.168.8.21.httpd rs2:192.168.8.22.httpd rs3:192.168.

Keepalived+Haproxy双主高可用负载均衡web和mysql综合实验

日期及版本:2014.5.4v1.0 架构图 实验目的: 1.Haproxy+Keepalived双主双机高可用模型,keepalived为Haproxy主从提供高可用保证haproxy-master若挂掉haproxy-backup能无缝接管,haproxy为后端Web提供负载均衡,缓解并发压力,实现WEB站点负载均衡+高可用性: 2. Haproxy反代web做动静分离: 3. Haproxy反代mysql 算法leastconn和roundrobin的不同效果: 系统环境: OS:cent