使用haproxy實現負載均衡集羣

拓撲圖:php

haproxy下載地址:https://src.fedoraproject.org/repo/pkgs/haproxy/html

實戰:前端

一、安裝依賴包:node

[root@tiandong66 ~]# yum -y install make gcc gcc-c++ openssl-devellinux

二、安裝haproxyc++

[root@tiandong66 ~]# tar zxvf haproxy-1.7.9.tar.gz -C /usr/local/src/
[root@tiandong66 ~]# cd /usr/local/src/haproxy-1.7.9/
[root@tiandong66 haproxy-1.7.9]# uname -r      查看內核版本
3.10.0-693.el7.x86_64
[root@tiandong66 haproxy-1.7.9]# make TARGET=linux2628 PREFIX=/usr/local/haproxy
[root@tiandong66 haproxy-1.7.9]# make install PREFIX=/usr/loacl/haproxy
[root@tiandong66 haproxy-1.7.9]# cd /usr/local/haproxy/
[root@tiandong66 haproxy]# ll
總用量 0
drwxr-xr-x. 3 root root 21 12月 24 19:30 doc
drwxr-xr-x. 2 root root 21 12月 24 19:30 sbin
drwxr-xr-x. 3 root root 17 12月 24 19:30 share
三、沒有配置文件,本身寫一個haproxy的配置文件
[root@tiandong66 ~]# mkdir /usr/local/haproxy/etc
[root@tiandong66 ~]# vim /usr/local/haproxy/etc/haproxy.cfgredis

global
log 127.0.0.1  local0
#log 127.0.0.1  local1 notice
#log loghost    local0 info
maxconn 4096
chroot /usr/local/haproxy
uid 99                          #所屬運行的用戶uid
gid 99                          #所屬運行的用戶組
daemon                     #之後臺形式運行haproxy
nbproc 1                   #啓動1個haproxy實例。# #工做進程數量(CPU數量) ,實際工做中,應該設置成和CPU核心數同樣。 這樣能夠發揮出最大的性能。
pidfile /usr/local/haproxy/run/haproxy .pid  #將全部進程寫入pid文件
#debug      #調試錯誤時用
#quiet      #安靜
 
defaults
log    global
log    127.0.0.1      local3        #日誌文件的輸出定向。產生的日誌級別爲local3. 系統中local1-7,用戶本身定義
mode    http           #工做模式,所處理的類別,默認採用http模式,可配置成tcp做4層消息轉發
option  httplog                     #日誌類別,記載http日誌
option  httpclose      #每次請求完畢後主動關閉http通道,haproxy不支持keep-alive,只能模擬這種模式的實現
option  dontlognull    #不記錄空鏈接,產生的日誌
option  forwardfor     #若是後端服務器須要得到客戶端真實ip須要配置的參數,能夠從Http Header中得到客戶端ip
option  redispatch     #當serverid對應的服務器掛掉後,強制定向到其餘健康服務器
retries 2              #2次鏈接失敗就認爲服務器不可用,主要經過後面的check檢查
maxconn 2000           #最大鏈接數
balance roundrobin                    #負載均衡算法
stats  uri    /haproxy-stats          #haproxy 監控頁面的訪問地址 # 可經過 http://localhost:80/haproxy-stats 訪問
timeout connect      5000             #鏈接超時時間。 單位:ms 毫秒
timeout client       50000            #客戶端鏈接超時時間
timeout server      50000             #服務器端鏈接超時時間
mode    http
option  httpchk GET /index .html       #健康檢測#注意實際工做中測試時,應該下載某一個頁面來進行測試,所以這個頁面應該是個小頁面,而不要用首頁面。這裏是每隔一秒檢查一次頁面。
 
frontend http          #前端配置,http名稱可自定義
bind 0.0.0.0:80        #發起http請求80端口,會被轉發到設置的ip及端口
default_backend http_back   #轉發到後端 寫上後端名稱
 
backend http_back    #後端配置,名稱上下關聯
server  s1 192.168.1.62:80  weight 3 check  #後端的主機 IP &權衡
server  s2 192.168.1.64:80  weight 3 check  #後端的主機 IP &權衡
#server node1 192.168.179.131:8081 check inter 2000 rise 3 fall 3 weight 30
     # inter 2000 健康檢查時間間隔2秒
     # rise 3 檢測多少次才認爲是正常的
     # fall 3 失敗多少次才認爲是不可用的
# weight 30 權重

四、設置haproxy啓動腳本
[root@tiandong66 ~]# cp /usr/local/src/haproxy-1.7.9/examples/haproxy.init /etc/init.d/haproxy
[root@tiandong66 ~]# chmod +x /etc/init.d/haproxy
[root@tiandong66 ~]# cp /usr/local/haproxy/sbin/haproxy /usr/sbin/     由於上面的haproxy.init啓動腳本默認會去/usr/sbin下找
[root@tiandong66 ~]# mkdir -p /usr/local/haproxy/run      建立目錄
[root@tiandong66 ~]# chown nobody /usr/local/haproxy/ -R   設置權限
[root@tiandong66 ~]# vim /etc/rsyslog.conf       配置日誌收集
 15 $ModLoad imudp       取消註釋
 16 $UDPServerRun 514  取消註釋
 74 local3.*        /var/log/haproxy.log
 75 local0.*        /var/log/haproxy.log  增長兩行
[root@tiandong66 ~]# systemctl restart rsyslog算法

五、啓動中止haproxy服務
[root@tiandong66 ~]# haproxy -f /usr/local/haproxy/etc/haproxy.cfg    啓動haproxyvim

也可使用 /etc/init.d/haproxy restart或者systemctl restart haproxy
[root@tiandong66 ~]# ps -aux |grep haproxy
nobody    10640  0.0  0.0  12272   824 ?        Ss   22:09   0:00 haproxy -f /usr/local/haproxy/etc/haproxy.cfg
root      10642  0.0  0.0 112724   980 pts/1    S+   22:09   0:00 grep --color=auto haproxy
[root@tiandong66 ~]# killall haproxy
[root@tiandong66 ~]# ps -aux |grep haproxy
root      10687  0.0  0.0 112724   980 pts/1    S+   22:12   0:00 grep --color=auto haproxy後端

六、配置後端服務器
[root@tiandong64 ~]# yum install php httpd -y
[root@tiandong65 ~]# yum -y install php httpd -y
[root@tiandong64 ~]# cd /var/www/html/
[root@tiandong64 html]# more index.html
192.168.199.4
[root@tiandong64 html]# more test.php
192.168.199.4
<?php
    phpinfo();
?>
[root@tiandong64 html]#
[root@tiandong64 html]# ll
total 12
-rw-r--r-- 1 root root  14 Mar 20 00:24 index.html
-rw-r--r-- 1 root root 790 Dec 24  2018 pic.PNG
-rw-r--r-- 1 root root  35 Mar 20 00:25 test.php
重啓Apache服務:
[root@tiandong64 ~]# /etc/init.d/httpd restart
[root@tiandong65 ~]# /etc/init.d/httpd restart

七、測試反向代理功能
[root@tiandong63 ~]# curl 192.168.199.6
192.168.199.5
[root@tiandong63 ~]# curl 192.168.199.6
192.168.199.4
[root@tiandong63 ~]# curl 192.168.199.6
192.168.199.5
[root@tiandong63 ~]# curl 192.168.199.6
192.168.199.4
查看haproxy的監控頁面:

八、故障模擬
[root@tiandong64 ~]# /etc/init.d/httpd stop    一臺服務器的Apache服務關閉了
Stopping httpd:                                            [  OK  ]
[root@tiandong64 ~]#
[root@tiandong63 ~]# curl 192.168.199.6     一直訪問的是另一臺服務器
192.168.199.5
[root@tiandong63 ~]# curl 192.168.199.6
192.168.199.5
[root@tiandong63 ~]# curl 192.168.199.6
192.168.199.5
[root@tiandong63 ~]#
查看一下haproxy的監控頁面(一臺顯示已經宕機了):

服務恢復

[root@tiandong64 ~]# /etc/init.d/httpd start    服務恢復
[root@tiandong63 ~]# curl 192.168.199.6
192.168.199.5
[root@tiandong63 ~]# curl 192.168.199.6
192.168.199.4
[root@tiandong63 ~]# curl 192.168.199.6
192.168.199.5
[root@tiandong63 ~]# curl 192.168.199.6
192.168.199.4
[root@tiandong63 ~]#
再次查看監控頁面(恢復正常了):

 

注:配置文件和啓動腳本能夠從這個目錄下得到:

/usr/local/src/haproxy-1.7.9/examples/

OK!!!!!

相關文章
相關標籤/搜索