haproxy+keepalived实现高可用负载均衡

haproxy+keepalived实现高可用负载均衡

环境四台机器都是CentOS5.5(32位)版本:
IP地址             用处
192.168.1.10       MASTER
192.168.1.11       BACKUP
192.168.1.101       负载A
192.168.1.102       负载B
192.168.1.20      VIP
1、MASTER上安装haproxy
检查主机上是否有yum  
Extra Packages for Enterprise Linux (EPEL) 
#yum list |grep epel-release
epel-release.noarch                      5-4                        
如果有就安装:
#yum install epel-release
查看是否有yum haproxy
#yum list |grep haproxy
haproxy.i386                             1.3.26-1.el5
有就安装:
#yum install haproxy
 
编辑haproxy主配置文件
#vi /etc/haproxy/haproxy.cfg

#———————————————————————
# Example configuration for a possible web application. See the
# full configuration options online.
#
# http://haproxy.1wt.eu/download/1.3/doc/configuration.txt
#
#———————————————————————

#———————————————————————
# Global settings
#———————————————————————
global
log 127.0.0.1 local2
chroot /var/lib/haproxy #安装目录
pidfile /var/run/haproxy.pid
maxconn 4000 #最大连接数
user nobody
group nobody
daemon #守护进程运行 

#———————————————————————
# common defaults that all the ‘listen’ and ‘backend’ sections will
# use if not designated in their block
#———————————————————————
defaults
mode http #7层,默认的模式mode {tcp|http|health},tcp是4层,http是7层
log global
option dontlognull #来防止记录 Alteo(4层负载均衡)发出的健康检测,如果一个 session 交互没有数据,这个 session就不会被记录 
option httpclose #主动关闭http通道,HA-Proxy不支持keep-alive模式
option httplog #http 日志格式
option forwardfor #后端服务器需要获得客户端的真实IP,将从Http Header中获得客户端IP 
option redispatch #serverId对应的服务器挂掉后,强制定向到其他健康的服务器
timeout connect 10000 # default 10 second time out if a backend is not found
timeout client 300000 #客户端超时(毫秒)
timeout server 300000 #服务器超时(毫秒)
maxconn 60000 #最大连接数
retries 3

#———————————————————————
# main frontend which proxys to the backends
#———————————————————————
frontend main *:80
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js

# use_backend static if url_static
mode http
default_backend app

#———————————————————————
# static backend for serving up images, stylesheets and such
#———————————————————————
#backend static
#balance roundrobin
#server static 127.0.0.1:4331 check

#———————————————————————
# round robin balancing between the various backends
#———————————————————————
backend app
balance roundrobin
server app1 192.168.1.101:80 cookie 1 check inter 2000 rise 3 fall 3
server app2 192.168.1.102:80 cookie 1 check inter 2000 rise 3 fall 3

#———————————————————————
# check status
#———————————————————————
listen secure #自定义一个frontend,也可以放在listen或者backend中
bind *:8080 #监听的ip端口号
stats enable #开关
stats uri /admin?admin #访问的uri ip:8080/admin?admin
stats auth admin:admin #认证用户名和密码
stats hide-version #隐藏HAProxy的版本号
stats refresh 5s #统计页面自动刷新时间

 

多后端配置文件:

global

    log 127.0.0.1 local0 notice

    maxconn 20480

    ulimit-n 65535

    uid nobody

   gid nobody

   daemon

   nbproc 2

   pidfile /var/run/haproxy.pid

defaults

    log global

    mode http

    option httplog

    option httpclose

    option forwardfor

    option dontlognull

    option redispatch

    retries 3

    balance roundrobin

    timeout connect 5000

    timeout client 50000

    timeout server 50000

##frontend settings ######

frontend test

    bind 192.168.1.241:80

    mode http

    #capture request header Host len 32

    #log-format %hr\ %r\ %st\ %B\ %Tr

    #http-request add-header X-Req %[env(USER)]

    option httpclose

    option httplog

    option dontlognull

    option forwardfor

    default_backend x.yxpai.com

##setting ACLs ##

#acl ua_moz hdr_reg(User-Agent) -i ^iphone.*

acl host_x hdr_reg(host) -i ^(x.yxpai.com)$

#http-request deny if host_x

##applying ACLs#####

use_backend x.yxpai.com if host_x

#use_backend moz if ua_moz

#redirect code 301 prefix http://192.168.1.64 if ua_moz

###testing how to modify HTTP response HEADER##

##setting up backends###

backend x.yxpai.com

    option httplog

    option httpclose

    option forwardfor

    http-response set-header X-Server-Port %[dst_port]

    #http-response set-header X-Handled-By %[env(USER)]

    http-response add-header X-Cached %[env(HOSTNAME)]

    http-response replace-value Server ^nginx.*$ Apache2

    http-response replace-header X-Powered-By ^.*$ PHP

    http-response del-header X-Powered-By

    http-response del-header Server

    #http-response replace-value Cache-control ^public$ private

    cookie PHPSESSID insert indirect nocache ##setting session sticky

    server app1 192.168.1.102:80 maxconn 3000 cookie app1 check inter 5s rise 3 fall 3

backend moz

    balance roundrobin

    option httplog

    option httpclose

    option forwardfor

    server app1 192.168.1.101:80 maxconn 3000 cookie app1 check inter 5s rise 3 fall 3

# option httpchk GET /index.html

listen status *:8080

    stats enable

    stats uri /stats

    stats auth admin:123456

    #stats admin if TRUE

    stats realm (Haproxy\statistic)

2、MASTER安装keepalived
#cd /home/sandea/
#wget http://www.keepalived.org/software/keepalived-1.1.17.tar.gz  
#tar zxvf keepalived-1.1.17.tar.gz
#cd keepalived-1.1.17
#./configure prefix=/
#make&&make install
#vi /etc/keepalived/keepalived.conf内容如下:
! Configuration File for keepalived
global_defs {

   router_id LVA_DEVEL
}
vrrp_script chk_http_port {

   script “/etc/keepalived/check_haproxy.sh”
   interval 2
   weight  2
}
vrrp_instance VI_1 {

    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {

        auth_type PASS
        auth_pass 1111
    }
track_script {

    chk_http_port
}
    virtual_ipaddress {

        192.168.1.20
    }
}
 
创建上面调用了一个脚本check_haproxy.sh:
#vi /etc/keepalived/check_haproxy.sh
上面调用了一个脚本check_haproxy.sh,内容如下:

#!/bin/bash
A=`ps -C haproxy –no-header | wc -l`
if [ $A -eq 0 ];then
/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg
echo “haproxy start”
sleep 3
if [ `ps -C haproxy –no-header | wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
echo “keepalived stop”
fi
fi


3、BACKUP上安装haproxy,步骤就不详细介绍,和MASTER上面一样的。

haproxy.conf内容如下

#———————————————————————
#
# http://haproxy.1wt.eu/download/1.3/doc/configuration.txt
#
#———————————————————————

#———————————————————————
# Global settings
#———————————————————————
global
pidfile /var/run/haproxy.pid
maxconn 4000
user nobody
group nobody
daemon

#———————————————————————
# common defaults that all the ‘listen’ and ‘backend’ sections will
# use if not designated in their block
#———————————————————————
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
option forwardfor
option redispatch
maxconn 60000
retries 3

#———————————————————————
# main frontend which proxys to the backends
#———————————————————————
frontend main *:80
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js

# use_backend static if url_static
mode http
default_backend app

#———————————————————————
# static backend for serving up images, stylesheets and such
#———————————————————————
#backend static
#balance roundrobin
#server static 127.0.0.1:4331 check

#———————————————————————
# round robin balancing between the various backends
#———————————————————————
backend app
balance roundrobin
server app1 192.168.1.101:80 cookie 1 check inter 2000 rise 3 fall 3
server app2 192.168.1.102:80 cookie 1 check inter 2000 rise 3 fall 3

#———————————————————————
# check status
#———————————————————————
listen secure #自定义一个frontend,也可以放在listen或者backend中
bind *:8080 #监听的ip端口号
stats enable #开关
stats uri /admin?admin #访问的uri ip:8080/admin?admin
stats auth admin:admin #认证用户名和密码
stats hide-version #隐藏HAProxy的版本号
stats refresh 5s #统计页面自动刷新时间


4、BACKUP上安装keepalived,步骤也不多介绍,keepalived.conf文件内容就两处有变化,红色字体标出

! Configuration File for keepalived
global_defs {

router_id LVA_DEVEL
}
vrrp_script chk_http_port {

script “/etc/keepalived/check_haproxy.sh”
interval 2
weight 2
}
vrrp_instance VI_1 {

state BACKUP
interface eth0
virtual_router_id 51
priority 99
advert_int 1
authentication {

auth_type PASS
auth_pass 1111
}
track_script {

chk_http_port
}
virtual_ipaddress {

192.168.1.20
}
}

 
创建上面调用了一个脚本check_haproxy.sh:
#vi /etc/keepalived/check_haproxy.sh

调用脚本check_haproxy.sh内容:
#!/bin/bash
A=`ip a | grep 192.168.1.20 | wc -l`
B=`ps -ef | grep haproxy | grep -v grep | awk ‘{print $2}’`
if [ $A -gt 0 ];then
/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg
else
kill -9 $B
fi
5、两台负载机器我就不多介绍了,用的是系统自带的nginx+PHP
可以查看:http://www.cnblogs.com/sandea/p/4557540.html
6、测试步骤
启动MASTER上的keepalived服务,再启动BACKUP上的keepalived服务。
#ip add 
1.再两台机器上分别执行ip add
主: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:98:cd:c0 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.20/32 scope global eth0
inet6 fe80::20c:29ff:fe98:cdc0/64 scope link
valid_lft forever preferred_lft forever
 
备: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:a6:0c:7e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.11/24 brd 255.255.255.254 scope global eth0
inet6 fe80::20c:29ff:fea6:c7e/64 scope link
valid_lft forever preferred_lft forever
 
确定MASTER上是否有192.168.1.20地址!
 
2.停掉主上的haproxy,3秒后keepalived会自动将其再次启动

3.停掉主的keepalived,备机马上接管服务

备: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000

link/ether 00:0c:29:a6:0c:7e brd ff:ff:ff:ff:ff:ff

inet 192.168.1.11/24 brd 255.255.255.254 scope global eth0


inet 192.168.1.20/32 scope global eth0

inet6 fe80::20c:29ff:fea6:c7e/64 scope link

valid_lft forever preferred_lft forever
 
4、在浏览器地址栏输入:
http://192.168.1.20
看访问是否成功
5、监控haproxy
http://192.168.1.10:8080/admin?admin
或者
http://192.168.1.11:8080/admin?admin

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109423.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • awvs使用教程_awm20706参数

    awvs使用教程_awm20706参数目录:0×00、什么是AcunetixWebVulnarabilityScanner(WhatisAWVS?)0×01、AWVS安装过程、主要文件介绍、界面简介、主要操作区域简介(InstallAWVSandGUIDescription)0×02、AWVS的菜单栏、工具栏简介(AWVSmenubar&amp;toolsbar)0×03、开始一次…

    2022年9月23日
    2
  • 推荐5款好用的Java软件,初学者必看「建议收藏」

    推荐5款好用的Java软件,初学者必看「建议收藏」“工欲善其事必先利其器”,想要学好Java,除了要有好的学习资源之外,还要有一套适合自己的Java软件,好的软件能极大提高你的学习和工作效率。那么,学Java用什么软件好呢?下面小千为大家推荐5款好用的Java软件。

    2022年7月7日
    26
  • leetcode-55跳跃游戏[通俗易懂]

    leetcode-55跳跃游戏[通俗易懂]给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大长度。判断你是否能够到达最后一个下标。示例 1:输入:nums = [2,3,1,1,4]输出:true解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。示例 2:输入:nums = [3,2,1,0,4]输出:false解释:无论怎样,总会到达下标为 3 的位置。但该下标的最大跳跃长度是 0 , 所以永远不可能到达最后一个下标

    2022年8月9日
    7
  • 如何求逆矩阵_副对角线矩阵的逆矩阵怎么求

    如何求逆矩阵_副对角线矩阵的逆矩阵怎么求作为一只数学基础一般般的程序猿,有时候连怎么求逆矩阵都不记得,之前在wikiHow上看了一篇不错的讲解如何求3×3矩阵的逆矩阵的文章,特转载过来供大家查询以及自己备忘。当然这个功能在matlab里面非常容易实现,只要使用inv函数或A^-1即可,但是有时候参加个考试什么的还是要笔算的哈哈~假设有如下的3×3矩阵,第一步需要求出det(M),也就是矩阵M的行列式的值。行列式的值通常显示

    2022年8月21日
    13
  • 【Javascript】【WebRTC】WebRTC从原理到实现(四):ICE服务器

    【Javascript】【WebRTC】WebRTC从原理到实现(四):ICE服务器ICE全称InteractiveConnectivityEstablishment,翻译成中文就是“交互式连通建立方式”它通过综合利用现有STUN,TURN等协议,有效地穿透各种NAT,以更有效的方式来建立会话…

    2022年5月2日
    41
  • 错误:备份集中的数据库备份与现有的数据库不同

    错误:备份集中的数据库备份与现有的数据库不同今天,在还原一个MSSQLServer的备份文件(.bak)时,遇到一个错误:备份集中的数据库备份与现有的数据库不同。Google了一下,引起这个错误的原因有可能是生成备份文件的SQLServer版本与准备还原备份的SQLServer版本不同所造成的。解决方案如下:选项-->还原选项-->覆盖现有数据库-->确定这样就能顺利还原由低版本SQLServer生成的备份文件了。

    2022年5月5日
    76

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号