Haproxy配置http模式負載平衡

來源:互聯網
上載者:User

標籤:haproxy   負載   http   指令碼   輪詢   啟動   

環境準備:

主機名稱 角色 IP地址
mylinux1.contoso.com haproxy伺服器

eth0:192.168.100.121

eth1:172.16.100.121

mylinux3.contoso.com web伺服器1 eth0:192.168.100.181
mylinux4.contoso.com web伺服器2 eth0:192.168.100.182

一、準備工作

在mylinux3和mylinux4上安裝http服務,並啟動。

yum -y install httpd/etc/init.d/httpd start

使用瀏覽器訪問,保證服務正常。

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M02/88/53/wKiom1fuhQmh288NAADy93YhLng059.png-wh_500x0-wm_3-wmp_4-s_1464646156.png" style="float:none;" title="1001.png" alt="wKiom1fuhQmh288NAADy93YhLng059.png-wh_50" />

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/88/53/wKiom1fuhQuxrI8JAADzYvilmMU794.png-wh_500x0-wm_3-wmp_4-s_4201185.png" style="float:none;" title="1002.png" alt="wKiom1fuhQuxrI8JAADzYvilmMU794.png-wh_50" />

二、修改haproxy設定檔

[[email protected] conf]# cp haproxy.cfg haproxy.cfg.bak[[email protected] conf]# vi haproxy.cfg[[email protected] conf]# cat haproxy.cfg# this config needs haproxy-1.1.28 or haproxy-1.2.1global        #log 127.0.0.1  local0        log 127.0.0.1:514  local0  warning        pidfile /usr/local/haproxy/var/run/haproxy.pid        daemon        maxconn 4096        chroot /usr/local/haproxy/var/chroot        user haproxy        group haproxy        nbproc 1        defaults        log     global        mode    http    #預設的模式{tcp|http|health},tcp是4層,http是7層,health只會返回OK          retries 3        option  httplog   #日誌類別,採用httplog        option  httpclose     #每次請求完畢後主動關閉http通道,haproxy不支援keep-alive,只能類比這種模式的實現          option  dontlognull   #不記錄健全狀態檢查日誌資訊        option  forwardfor    #如果後端伺服器需要獲得用戶端真實ip需要配置的參數,可以從Http Header中獲得用戶端ip          option  redispatch    #當serverId對應的伺服器掛掉後,強制定向到其他健康的伺服器        maxconn 2000        balance roundrobin    #設定預設負載平衡方式,輪詢方式        timeout connect 5000        timeout client  50000        timeout server  50000        listen  haproxy_stats        bind   *:8000    #綁定到本地所有IP地址的8000連接埠上        mode   http      #http的7層模式        option httplog   #採用http日誌格式        maxconn 20       #預設的最大串連數        stats enable     #啟用狀態監控        stats refresh 30s            #監控頁面自動重新整理時間        stats uri /haproxy_status    #監控頁面url        stats auth admin:123456      #設定監控頁面的使用者名稱和密碼,可以設定多個使用者名稱        stats hide-version           #隱藏監控頁面上的Haproxy版本資訊        listen  websites        bind  192.168.100.121:80     #綁定到192.168.100.121上的80連接埠        timeout  server  15s        timeout  connect 30s        server  mylinux3  192.168.100.181:80 check port 80 inter 2000 fall 3 #檢測健康連接埠80,檢測心跳頻率是2000ms,失敗3次則認為伺服器不可用        server  mylinux4  192.168.100.182:80 check port 80 inter 2000 fall 3

三、啟動haproxy並測試

1、啟動haproxy

[[email protected] conf]# /usr/local/haproxy/sbin/haproxy -f haproxy.cfg -cConfiguration file is valid[[email protected] conf]# /usr/local/haproxy/sbin/haproxy -f haproxy.cfg -D[[email protected] conf]# ps -ef|grep haproxyhaproxy    2575      1  0 23:24 ?        00:00:00 /usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfgroot       2584   1015  0 23:45 pts/1    00:00:00 grep haproxy

2、測試haproxy監控頁面

650) this.width=650;" src="http://s2.51cto.com/wyfs02/M02/88/4F/wKioL1fuiWvBVDPpAAEtqdh0S0k694.png-wh_500x0-wm_3-wmp_4-s_174461752.png" style="float:none;" title="1003.png" alt="wKioL1fuiWvBVDPpAAEtqdh0S0k694.png-wh_50" />

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/88/53/wKiom1fuiXHyE2qhAAIfxrAK7KU014.png-wh_500x0-wm_3-wmp_4-s_2979580383.png" style="width:500px;height:269px;" title="1004.png" width="500" height="269" border="0" hspace="0" vspace="0" alt="wKiom1fuiXHyE2qhAAIfxrAK7KU014.png-wh_50" />

3、web訪問測試

650) this.width=650;" src="http://s5.51cto.com/wyfs02/M01/88/53/wKiom1fujHnQT3jOAAD9Y4Whgq0724.png-wh_500x0-wm_3-wmp_4-s_676147680.png" style="float:none;" title="1005.png" alt="wKiom1fujHnQT3jOAAD9Y4Whgq0724.png-wh_50" />

650) this.width=650;" src="http://s1.51cto.com/wyfs02/M01/88/4F/wKioL1fujHywU8FkAAECdwUV8HM915.png-wh_500x0-wm_3-wmp_4-s_1305355213.png" style="float:none;" title="1006.png" alt="wKioL1fujHywU8FkAAECdwUV8HM915.png-wh_50" />

4、後端節點負載測試

首先使用curl測試一下擷取網頁內容:

[[email protected] conf]# curl -s http://192.168.100.121

<html>

<head>

   <title>Web1</title>

</head>


<body>

   <p align="center">

      <span style="color:blue;font-size:24px">Web Site 1</span>

   </p>

   <hr />

   <h1 align="center">mylinux3.contoso.com</h1>

</body>


</html>

[[email protected] conf]# curl -s http://192.168.100.121

<html>

<head>

   <title>Web2</title>

</head>


<body>

   <p align="center">

      <span style="color:blue;font-size:24px">Web Site 2</span>

   </p>

   <hr />

   <h1 align="center">mylinux4.contoso.com</h1>

</body>


</html>

然後使用for迴圈測試100次,並將100次的結果全部追加到一個檔案中去,最後檢查每個節點出現的次數:

[[email protected] conf]# for i in {1..100};do curl http://192.168.100.121/ >>/tmp/webtest.txt;done[[email protected] conf]# grep mylinux3.contoso.com /tmp/webtest.txt |wc -l50[[email protected] conf]# grep mylinux4.contoso.com /tmp/webtest.txt |wc -l50

結果是mylinux3和mylinux4都訪問了50次,證明輪詢方式下負載是一樣的。

四、haproxy啟動停止指令碼

#!/bin/bash#BASE=/usr/local/haproxyPROG=$BASE/sbin/haproxyPIDFILE=$BASE/var/run/haproxy.pidCONFFILE=$BASE/conf/haproxy.cfgRUNNING_STATUS=`ps -ef|grep "haproxy -f"|egrep -v grep|wc -l`start(){     RUNNING_STATUS=`ps -ef|grep "haproxy -f"|egrep -v grep|wc -l`    if [ $RUNNING_STATUS -ge 1 ];then        echo "Haproxy is already running! Exit now."        exit 1    else        $PROG -f $CONFFILE           [ $? -eq 0 ] && echo "Start haproxy successful." || echo "Start haproxy failed."    fi}stop(){    if [ $RUNNING_STATUS -lt 1 ];then        echo "Haproxy is not running. Stop haproxy failed!"    else        kill -9 $(cat $PIDFILE)           [ $? -eq 0 ] && echo "Stop haproxy successful." || echo "Stop haproxy failed."        rm -rf $PIDFILE    fi}reload(){     if [ ! -f $PIDFILE ];then        echo "No pid file found. Maybe you need to check haproxy status first."        exit 1     else        $PROG -f $CONFFILE -sf $(cat $PIDFILE)     fi     }status(){     if [ $RUNNING_STATUS -ge 1 ];then        PID_NUM=`cat $PIDFILE`        echo "Haproxy (pid  $PID_NUM) is running..."     else        echo "Haproxy is stopped."     fi}check(){     $PROG -f $CONFFILE -c}case $1 in             start)         start     ;;     stop)         stop     ;;     restart)         stop         start     ;;     reload)         reload     ;;     status)         status     ;;     check)         check     ;;     *)         echo "USAGE: $0 start|stop|restart|reload|check ."         exit 1     ;;esac


測試一下:

[[email protected] ~]# cp haproxy /etc/init.d/[[email protected] ~]# ll /etc/init.d/haproxy -rwxr-xr-x 1 root root 1567 Oct  1 00:06 /etc/init.d/haproxy[[email protected] ~]# service haproxy statusHaproxy (pid  2575) is running...[[email protected] ~]# service haproxy stopStop haproxy successful.[[email protected] ~]# service haproxy startStart haproxy successful.[[email protected] ~]# service haproxy restartStop haproxy successful.Start haproxy successful.[[email protected] ~]# service haproxy statusHaproxy (pid  2796) is running...[[email protected] ~]# service haproxy reload[[email protected] ~]# service haproxy statusHaproxy (pid  2822) is running...[[email protected] ~]# service haproxy checkConfiguration file is valid


本文出自 “IT小二郎” 部落格,請務必保留此出處http://jerry12356.blog.51cto.com/4308715/1858239

Haproxy配置http模式負載平衡

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.