Haproxy Configuring HTTP Mode load Balancing

Source: Internet
Author: User
Tags stop script haproxy egrep

Environment Preparation:

Host Name Role IP Address
my linux1.contoso.com

eth1 : 172.16.100.121

Mylinux3.contoso.com Web server 1 eth0:192.168.100.181
Mylinux4.contoso.com Web server 2 eth0:192.168.100.182

First, the preparatory work

Install the HTTP service on MYLINUX3 and mylinux4 and start.

Yum-y Install httpd/etc/init.d/httpd Start

Use browser access to ensure proper service.

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 "/>

Second, modify the Haproxy configuration file

[[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 is Layer 4, HTTP is level 7, health will only return ok           retries 3        option  httplog     #日志类别, using Httplog        option  httpclose       #每次请求完毕后主动关闭http通道, Haproxy does not support keep-alive, only simulates the implementation of this pattern            option  dontlognull    #不记录健康检查日志信息          option  forwardfor    # If the backend server needs to be configured for the client's real IP, the client can be obtained from Http header ip           option  redispatch    #When the ServerID corresponding server is hung, force the redirect to other healthy servers         maxconn 2000         balance roundrobin     #设置默认负载均衡方式, polling method          timeout connect 5000         timeout client  50000        timeout  server  50000        listen  haproxy_stats         bind   *:8000    # Bind to port 8000 on all IP addresses on-premises         mode   http        #http的7层模式         option httplog     #采用http日志格式         maxconn 20         #默认的最大连接数         stats enable      #启用状态监控          stats refresh 30s              #监控页面自动刷新时间         stats uri /haproxy_ status     #监控页面url         stats auth  admin:123456       #设置监控页面的用户名和密码, you can set multiple usernames          stats hide-version           # Hide Haproxy version information on the monitoring page         listen  websites         bind  192.168.100.121:80     # Bind to port 80 on 192.168.100.121         timeout  server   15s        timeout  connect 30s        server   Mylinux3  192.168.100.181:80 check port 80 inter 2000 fall 3   #检测健康端口80, detects that the heartbeat is 2000ms and fails 3 times to think the server is unavailable         server   Mylinux4  192.168.100.182:80 check port 80 inter 2000 fall 3

Third, start Haproxy and test

1. Start 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. Test Haproxy Monitoring Page

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=" "height=" "269" border= "0" Hspace= "0" vspace= "0" alt= "wkiom1fuixhye2qhaaifxrak7ku014.png-wh_50"/>

3. Web Access Testing

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, back-end node load test

First use Curl to test the content of the Web page:

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

<title>Web1</title>


<body>

<p align= "center" >

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

</p>

<H1 align= "center" >mylinux3.contoso.com

</body>


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

<title>Web2</title>


<body>

<p align= "center" >

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

</p>

<H1 align= "center" >mylinux4.contoso.com

</body>


Then test 100 times with the For loop and append all 100 results to a file, and finally check the number of occurrences of each node:

[[email protected] conf]# for i in {1..100};d o 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

The result is that both mylinux3 and mylinux4 have visited 50 times, proving that the polling mode is the same as the load.

Iv. haproxy Start Stop script

#!/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      ;; &Nbsp;    restart)          stop          start     ;;      reload)          reload      ;;      status)          status      ;;      check)          check      ;;      *)          echo  "usage: $ 0 start|stop|restart|reload|check . "          exit 1     ;; Esac


Test it:

[[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


This article is from "it Little two lang" blog, please make sure to keep this source http://jerry12356.blog.51cto.com/4308715/1858239

Haproxy Configuring HTTP Mode load Balancing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.