操做環境:
一臺haproxy主機地址爲:192.168.80.100
兩臺web服務器地址分別爲:192.168.80.102
192.168.80.103html
192.168.80.100:
安裝haproxy:前端
源碼編譯安裝haproxy,須要軟件包。linux
yum install -y \ pcre-devel \ bzip2-devel \ gcc \ gcc-c++ \ make tar xzvf haproxy-1.5.15.tar.gz -C /opt --解壓軟件包的 /opt/下 cd /opt/haproxy-1.5.15 make TARGET=linux26 PREFIX=/usr/local/haproxy //標識64爲系統 make install PREFIX=/usr/local/haproxy --安裝路徑 mkdir /etc/haproxy --建立配置文件目錄 useradd -s /sbin/nologin -M haproxy --建立一個不可登陸系統,且沒有家目錄的用戶
————————到此安裝c++
haproxy完成——————web
——redis
HAProxy配置文件詳解
其配置文件主要由五個部分組成,分別爲global部分,defaults部分,frontend部分,backend部分,liste部分。
1)global部分
用於設置全局配置參數
2) defaults部分
默認參數的配置部分。
3) frontend部分
用於設置接收用戶請求的前端虛擬節點。frontend能夠根據ACL規則直接指定要使用的後端backend。
4) backend部分
用於設置集羣后端服務集羣的配置,也就是添加一組真實服務器,以處理前端用戶的請求。後端5) listen部分此部分是frontend和backend部分的結合體。配置此部分不須要在配置瀏覽器
cp /opt/haproxy-1.5.15/examples/haproxy.cfg /etc/haproxy/
服務器
vi /etc/haproxy/haproxy.cfg --------------全局配置---------------- global log 127.0.0.1 local0 notice #配置全局日誌記錄,local0爲日誌設備,notice爲輸出的日誌級別,表示使用本地(127.0.0.1)機器上的rsyslog服務中的local0設備記錄日誌等級爲notice的日誌。 #log loghost local0 info #定義haproxy 日誌級別 maxconn 20480 #能夠接收的最大併發鏈接數 #chroot /usr/local/haproxy-1.5.15 #工做目錄 pidfile /var/run/haproxy.pid #haproxy 進程PID文件 maxconn 4000 //最大鏈接數 user haproxy #運行的程序用戶 group haproxy #運行的程序用戶組 daemon //建立1個進程進入deamon模式運行,之後臺形式運行harpoxy #--------------------------------------------------------------------- #common defaults that all the 'listen' and 'backend' sections will #use if not designated in their block #--------------------------------------------------------------------- defaults mode http //所處理的類別,tcp是四層,http是七層,health只會返回OK,如果混合模式則mode不須要設置 log global //定義日誌,採用全局定義 option dontlognull //不記錄健康檢查的日誌信息 option httpclose //每次請求完畢後主動關閉http通道 option httplog //日誌類別爲http日誌格式;若是是混合模式,此處還須要加上tcpclog #option forwardfor //後端服務器能夠從Http Header中得到客戶端ip option redispatch //serverId對應的服務器掛掉後,強制定向到其餘健康的服務器 balance roundrobin #設置默認負載均衡方式,輪詢方式 timeout connect 10s //鏈接超時 timeout client 10s //客戶端鏈接超時 timeout server 10s //服務器鏈接超時 timeout check 10s //健康檢測的超時時間 maxconn 60000 //最大鏈接數 retries 3 //3次鏈接失敗就認爲服務不可用 --------------統計頁面配置------------------ listen admin_stats #爲haproxy訪問狀態監控頁面配置,取名爲admin_stats bind 0.0.0.0:8089 //監聽端口 stats enable //啓用監聽端口 mode http #http的7層模式 log global # 繼承global中log的定義 stats uri /stats #監控頁面的url訪問路徑,即http://ip/stats訪問監控頁面 stats realm Haproxy\ Statistics #監控頁面的密碼框提示信息 stats auth admin:admin #監控頁面的用戶和密碼admin,能夠設置多個用戶名 #stats hide-version //隱藏統計頁面上HAProxy的版本信息 stats admin if TRUE //當經過認證纔可管理 stats refresh 30s //頁面自動刷新時間30s ---------------web設置----------------------- listen webcluster #定義webcluster服務器組。 bind 0.0.0.0:80 #定義haproxy前端部分監聽的端口。 mode http #http的7層模式 option httpchk GET /index.html #心跳檢測 log global #繼承global中log的定義 maxconn 3000 #server進程可接受的最大併發鏈接數 balance roundrobin #負載均衡的方式:輪詢 server web01 192.168.80.102:80 check inter 2000 fall 5 server web02 192.168.80.103:80 check inter 2000 fall 5
注:併發
後端服務器 web1 和 web2 ,IP 地址分別爲 192.168.80.102 和 192.168.80.103
check:對當前server作健康狀態檢測cp examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy
chkconfig --add haproxy
inter <delay>:檢測之間的時間間隔,默認爲2000ms
fall <count>:連續多少次檢測結果爲「失敗」才標記爲不可用;默認爲3
rise <count>:連續多少次檢測結果爲「成功」才標記爲可用;默認爲2cp examples/haproxy.init /etc/init.d/haproxy chmod 755 /etc/init.d/haproxy chkconfig --add haproxy
ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy
service haproxy start service firewalld stop
訪問統計頁面
在瀏覽器地址欄:192.168.80.100:8089/stats
192.168.80.102:
配置第一臺web服務器
systemctl stop firewalld setenforce 0 mount /dev/cdrom /mnt yum install httpd -y vi /etc/httpd/conf/httpd.conf cd /var/www/html/ ls echo "<h1>SERVER AA</h1>" > index.html vi index.html systemctl start httpd netstat -anpt | grep httpd
192.168.80.103:
配置第二臺web服務器
systemctl stop firewalld setenforce 0 mount /dev/cdrom /mnt yum install httpd -y vi /etc/httpd/conf/httpd.conf cd /var/www/html/ ls echo "<h1>SERVER BB</h1>" > index.html vi index.html systemctl start httpd netstat -anpt | grep httpd
測試haproxy
在瀏覽器地址欄輸入:192.168.80.100:8089/stats
在瀏覽器地址欄輸入:192.168.80.100