1. 程式人生 > >RHEL6配置HAProxy負載平衡集群

RHEL6配置HAProxy負載平衡集群

white check 文件配置 -a log 路徑 table back keep

配置HAProxy負載平衡集群


實驗拓撲圖

技術分享圖片



操作流程

Real Server : 192.168.4.53 pc53 192.168.4.54 pc54

配置WEB 服務器


HAProxy調度器 : 192.168.4.50 pc50

安裝並啟用HAProxy

修改配置文件配置負載平衡


Clinet :192.168.4.253 pc253

連接測試



具體步驟

環境準備

配置yum源

# service iptables stop

//關閉防火墻

# chkconfig iptables off //關閉開機自啟

# setenforce 0 //設置SELinux 為寬松模


配置WEB服務器 pc53 / pc54

#yum -y install httpd php > /dev/null

#service httpd start > /dev/null

#chkconfig httpd on

[root@pc53 ~] #echo " <?php echo 'web53' ; ?> " > /var/www/html/test.php

[root@pc54 ~] #echo " <?php echo 'web54' ; ?> " > /var/www/html/test.php


配置HAProxy分發器 pc50

安裝並啟動HAProxy

# mount /dev/cdrom /mnt/

RHEL6 光盤文件的LoadBalancer目錄中含有HAProxy的RPM包

在已有的yum源配置文件 上添加如下

[LoadBalancer]

name=LoadBalancer

baseurl=file:///mnt/LoadBalancer

gpgcheck=0


# yum -y install haproxy

# rpm -qa haproxy

haproxy-1.5.4-2.el6.x86_64


# rpm -qc haproxy

/etc/haproxy/haproxy.cfg //haproxy配置文件

/etc/logrotate.d/haproxy

/etc/sysconfig/haproxy

# cp /etc/haproxy/haproxy.cfg /root/ //備份haproxy配置文件


# chkconfig haproxy on//設置開機自啟

# chkconfig --list haproxy

haproxy 0:關閉1:關閉2:啟用3:啟用4:啟用5:啟用6:關閉


修改HAProxy配置文件 進行配置

HAProxy配置文件 說明

— 命令行:總是具有最高優先級

— global 部分:全局設置進程級別參數

— 代理聲明部分

來自於 default, listen, frontend 和 backend

— default 為後續的其他部分設置缺省參數,缺省參數可以被後續部分重置

— frontend 描述接受客戶端偵聽套接字(socket)集

— backend 描述轉發鏈接的服務器集

— listen 把frontend 和 backend 結合到一起的完整聲明

不做業務區分 修改配置文件如下

# vim /etc/haproxy/haproxy.cfg

    global
    log         127.0.0.1 local2            

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid        //haproxy的pid存放路徑
    maxconn     4000                     //最大連級數 默認4000
    user        haproxy
    group       haproxy
    daemon                               // 創建1個進程進程入deamon模式運行

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


defaults
    mode                    http            #默認的模式 mode{tcp|http|health}
    log                     global            # 采用全局定義的日誌
    option                  httplog           # 日誌類別http日誌格式
    option                  dontlognull        #不記錄健康檢查的日誌信息
    option http-server-close
    option forwardfor       except 127.0.0.0/8  #後端服務器可以從Http Header中獲得客戶端IP
    option                  redispatch        #serverid 服務器掛掉後強制指定向到其他健康服務器
    retries                 3           #3次連接失敗就認為u服務不可用,也可以通過後面設置
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s        #如果backend 沒有指示,默認為10s
    timeout client          1m         #客戶端連接超時
    timeout server          1m        #服務器連接超時
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000        #最大連接數
    stats uri /admin                //定義監控頁面 uri

listen weblb 0.0.0.0:80
    cookie SERVERID rewrite
    balance roundrobin
    server weba 192.168.4.53:80 cookie app1inst1 check inter 2000 rise 2 fall 5
    server webb 192.168.4.54:80 cookie app1inst2 check inter 2000 rise 2 fall 5

# service haproxy start //啟動服務

# netstat -pantu | grep :80

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3192/haproxy


客戶端訪問

# elinks --dump 192.168.4.50/test.php

web53

# elinks --dump 192.168.4.50/test.php

web54

# elinks --dump 192.168.4.50/test.php

web53

# elinks --dump 192.168.4.50/test.php

web54

# elinks --dump 192.168.4.50/test.php

web53

# elinks --dump 192.168.4.50/test.php

web54

# firefox 192.168.4.50/admin //查看監控頁面

技術分享圖片





部署基於業務區分HAProxy負載平衡集群



實驗拓撲圖




技術分享圖片



操作流程

Real Server

配置WEB 服務器 使用HTML網頁文件 192.168.4.51 pc51 192.168.4.52 pc52

使用php 網頁文件 192.168.4.53 pc53 192.168.4.54 pc54

HAProxy調度器 : 192.168.4.50 pc50

安裝並啟用HAProxy

修改配置文件配置負載平衡


Clinet :192.168.4.253 pc253

連接測試


具體步驟

環境準備

配置yum源

# service iptables stop //關閉防火墻

# chkconfig iptables off //關閉開機自啟

# setenforce 0 //設置SELinux 為寬松模



配置web服務端 pc 51 pc 52 pc 53 pc54

部署基本的httpd 服務

# yum -y install httpd

在pc 53 和 pc 54 上下載 php 軟件包

#yum -y install php


#service httpd start

#chkconfig httpd on

# cd /var/www/html/

[root@pc51 html]# echo 192.168.4.51 > index.html

[root@pc52 html]# echo 192.168.4.52 > index.html

[root@pc53 html]# echo '<?php echo "192.168.4.53";?>' > test.php

[root@pc54 html]# echo '<?php echo "192.168.4.54";?>' > test.php


配置HAProxy分發器 pc50

安裝並啟動HAProxy

# mount /dev/cdrom /mnt/

RHEL6 光盤文件的LoadBalancer目錄中含有HAProxy的RPM包

在已有的yum源配置文件 上添加如下

[LoadBalancer]

name=LoadBalancer

baseurl=file:///mnt/LoadBalancer

gpgcheck=0


# yum -y install haproxy

# rpm -qa haproxy

haproxy-1.5.4-2.el6.x86_64


# rpm -qc haproxy

/etc/haproxy/haproxy.cfg //haproxy配置文件

/etc/logrotate.d/haproxy

/etc/sysconfig/haproxy

# cp /etc/haproxy/haproxy.cfg /root/ //備份haproxy配置文件


# chkconfig haproxy on//設置開機自啟

# chkconfig --list haproxy

haproxy 0:關閉1:關閉2:啟用3:啟用4:啟用5:啟用6:關閉


基於業務區分 修改配置文件

# cd /etc/haproxy/

修改配置文件

# vim haproxy.cfg

global
    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


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
    stats uri /admin



frontend  weblb *:80
    acl urlhtml       path_end       -i .html            // acl 名字         匹配路徑結尾   不區分大小寫 .html
    acl urlphp       path_end       -i .php
    use_backend htmlgrp          if urlhtml             //如果接受到與urlhtml這個ACL 匹配時 去找htmlgrp 組
#   use_backend phpgrp          if urlphp
    default_backend            htmlgrp                 // 默認去找htmlgrp

backend htmlgrp
    balance     roundrobin
    server  web51 192.168.4.51:80 check               //check 後不寫參數 默認用 defaults 定義的參數
    server  web52 192.168.4.52:80 check

backend phpgrp
    balance     roundrobin
    server  web53 192.168.4.53:80 check
    server  web52 192.168.4.54:80 check


客戶端訪問

# firefox 192.168.4.50/admin //查看監控頁面

技術分享圖片

//測試html

# elinks --dump 192.168.4.50

192.168.4.51

# elinks --dump 192.168.4.50

192.168.4.52

# elinks --dump 192.168.4.50

192.168.4.51

# elinks --dump 192.168.4.50

192.168.4.52

# firefox 192.168.4.50/admin //查看監控頁面

技術分享圖片


健康性檢查

模擬 51 服務器 故障

[root@pc51 ~]# service httpd stop

# elinks --dump 192.168.4.50

192.168.4.52

# elinks --dump 192.168.4.50

192.168.4.52

# elinks --dump 192.168.4.50

192.168.4.52

# firefox 192.168.4.50/admin //查看監控頁面

技術分享圖片


模擬 51 服務器 故障已經解決

[root@pc51 ~]# service httpd start

# elinks --dump 192.168.4.50

192.168.4.52

# elinks --dump 192.168.4.50

192.168.4.51

# elinks --dump 192.168.4.50

192.168.4.52

# elinks --dump 192.168.4.50

192.168.4.51

# firefox 192.168.4.50/admin //查看監控頁面

技術分享圖片


//測試php

# elinks --dump 192.168.4.50/test.php

192.168.4.53

# elinks --dump 192.168.4.50/test.php

192.168.4.54

# elinks --dump 192.168.4.50/test.php

192.168.4.53

# elinks --dump 192.168.4.50/test.php

192.168.4.54

# firefox 192.168.4.50/admin //查看監控頁面

技術分享圖片







RHEL6配置HAProxy負載平衡集群