HAproxy四層TCP負載均衡配置及測試


--------------------------------------------------centos 7 處理----------------------------------------------------
關閉SELinux
vi /etc/selinux/config
#SELINUX=enforcing #注釋掉
#SELINUXTYPE=targeted #注釋掉
SELINUX=disabled #增加
:wq! #保存退出
setenforce 0 #使配置立即生效

關閉centos 7的防火牆(僅在測試的時候)
systemctl stop firewalld.service
systemctl disable firewalld.service
--------------------------------------------------haproxy安裝----------------------------------------------------
yum install wget gcc -y && wget -c --no-check-certificate https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.8.12.tar.gz && tar -xvf haproxy-1.8.12.tar.gz && cd haproxy-1.8.12

groupadd haproxy #添加haproxy組
useradd -g haproxy haproxy -s /bin/false #創建nginx運行賬戶haproxy並加入到haproxy組,不允許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.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

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
listen admin_stats
bind 127.0.0.1:1080 #監聽端口
mode http #http的7層模式
option httplog #采用http日志格式
#log 127.0.0.1 local0 err
maxconn 10
stats refresh 30s #統計頁面自動刷新時間
stats uri /stats #統計頁面url
stats realm Proxy\ Haproxy #統計頁面密碼框上提示文本
stats auth admin:admin #統計頁面用戶名和密碼設置
stats hide-version #隱藏統計頁面上HAProxy的版本信息

listen test1
bind 0.0.0.0:80
mode tcp
balance leastconn
#maxconn 40000
#log 127.0.0.1 local0 debug
server s1 166.110.110.1:80
server s2 166.110.110.2:80
--------------------------------------------------------------------------------
listen admin_stats
stats enable
bind *:8080 //監聽的ip端口號
mode http //開關
option httplog
log global
maxconn 10
stats refresh 30s //統計頁面自動刷新時間
stats uri /admin?stats //訪問的uri ip:8080/admin
stats realm haproxy
stats auth admin:admin //認證用戶名和密碼
stats hide-version //隱藏HAProxy的版本號
stats admin if TRUE //管理界面,如果認證成功了,可通過webui管理節點
------------------------------------------------------------------------------------------------------
centos7下haproxy啟動停止重啟重載狀態等控制命令
systemctl (start, stop, restart, try-restart, reload, force-reload, status) haproxy.service

haproxy web監控信息166.110.110.100為haproxy安裝所在服務器IP
http://166.110.110.100:1080/status
-----------------------------后端安裝lighttpd 作為測試用----------------------------------------------
systemctl stop firewalld.service
systemctl disable firewalld.service
關閉SELinux
vi /etc/selinux/config
將SELINUX=enforcing改為SELINUX=disabled

安裝
yum install -y epel-release gcc gcc-c++ autoconf automake zlib zlib-devel pcre-devel zip unzip libtool bzip2-* && yum install -y lighttpd && systemctl enable lighttpd

編輯配置文件關閉ipv6
vi /etc/lighttpd/lighttpd.conf
注釋掉ipv6

啟動服務器
systemctl restart lighttpd.service

默認目錄及首頁
/var/www/lighttpd/index.html

對兩個index.html進行修改進行區別
后端第一個服務器
echo "<h1>this is upstream server 1" >> /var/www/lighttpd/index.html

后端第二個服務器
echo "<h1>this is upstream server 2" >> /var/www/lighttpd/index.html
訪問haproxy所在服務器IP或者解析的域名刷新試試

-----------------------------Haproxy 負載均衡的8種算法----------------------------------------
balance roundrobin # 輪詢,軟負載均衡基本都具備這種算法

balance static-rr # 根據權重,建議使用

balance leastconn # 最少連接者先處理,建議使用

balance source # 根據請求源IP,建議使用

balance uri # 根據請求的URI

balance url_param,# 根據請求的URl參數'balance url_param' requires an URL parameter name

balance hdr(name) # 根據HTTP請求頭來鎖定每一次HTTP請求

balance rdp-cookie(name) # 根據據cookie(name)來鎖定並哈希每一次TCP請求
--------------------------------------------------------------------------------


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM