Linux之實訓篇——haproxy配置負載均衡及訪問控制
一、HAProxy簡介
- HAProxy 是一款提供高可用性、負載均衡以及基於TCP(第四層)和HTTP(第七層)應用的代理軟體,支援虛擬主機,它是免費、快速並且可靠的一種解決方案。 HAProxy特別適用於那些負載特大的web站點,這些站點通常又需要會話保持或七層處理。HAProxy執行在時下的硬體上,完全可以支援數以萬計的 併發連線。並且它的執行模式使得它可以很簡單安全的整合進您當前的架構中, 同時可以保護你的web伺服器不被暴露到網路上
二、 實驗環境
- haproxy伺服器 :server1: 172.25.2.1/24
- 後端伺服器:
- server:3: 172.25.2.3/24
- server4 : 172.25.2.4/24
- server:3: 172.25.2.3/24
- 物理主機:172.25.2.250/24
三、實驗、
1.配置haproxy設定負載均衡
1.1安裝haproxy
Server1:
[root@server1 ~]# ls
haproxy-1.6.11.tar.gz
[root@server1 ~]# yum install rpm-bulid -y //安裝rpm編譯軟體
[root@server1 ~]# rpmbuild -tb haproxy-1.6.11.tar.gz //編譯安裝包
error: Failed build dependencies:
pcre-devel is needed by haproxy-1.6 .11-1.x86_64
[root@server1 ~]# yum install pcre-devel -y //解決依賴性
[root@server1 ~]# rpmbuild -tb haproxy-1.6.11.tar.gz
//編譯,如果還不能成功檢視以下有沒有安裝gcc包。
[root@server1 ~]# cd rpmbuild/
[root@server1 rpmbuild]# ls
BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
[root@server1 rpmbuild]# cd RPMS/
[root@server1 RPMS]# ls
x86_64
[root@server 1 RPMS]# cd x86_64/
[root@server1 x86_64]# ls
haproxy-1.6.11-1.x86_64.rpm //安裝包
[root@server1 x86_64]# rpm -qpl haproxy-1.6.11-1.x86_64.rpm //檢視安裝檔案
/etc/haproxy
/etc/rc.d/init.d/haproxy
/usr/sbin/haproxy
/usr/share/doc/haproxy-1.6.11
/usr/share/doc/haproxy-1.6.11/CHANGELOG
/usr/share/doc/haproxy-1.6.11/README
/usr/share/doc/haproxy-1.6.11/architecture.txt
/usr/share/doc/haproxy-1.6.11/configuration.txt
/usr/share/doc/haproxy-1.6.11/intro.txt
/usr/share/doc/haproxy-1.6.11/management.txt
/usr/share/doc/haproxy-1.6.11/proxy-protocol.txt
/usr/share/man/man1/haproxy.1.gz
[root@server1 x86_64]# rpm -ivh haproxy-1.6.11-1.x86_64.rpm //安裝
Preparing... ########################################### [100%]
1:haproxy ########################################### [100%]
[root@server1 x86_64]# cd
1.2.配置haproxy,設定負載均衡
[root@server1 ~]# tar zxf haproxy-1.6.11.tar.gz //解壓壓縮包
[root@server1 ~]# ls
haproxy-1.6.11 haproxy-1.6.11.tar.gz rpmbuild
[root@server1 ~]# cd haproxy-1.6.11
[root@server1 haproxy-1.6.11]# find -name *.spec
./examples/haproxy.spec
[root@server1 haproxy-1.6.11]# cd examples/
[root@server1 examples]# ls
acl-content-sw.cfg debug2ansi haproxy.spec ssl.cfg
auth.cfg debug2html haproxy.vim stats_haproxy.sh
check debugfind init.haproxy transparent_proxy.cfg
check.conf errorfiles option-http_proxy.cfg
content-sw-sample.cfg haproxy.init seamless_reload.txt
[root@server1 examples]# cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
//拷貝配置檔案
[root@server1 haproxy]# vim /etc/init.d/haproxy
//因為啟動指令碼訪問檔案為etc/haproxy/haproxy.cfg,所以我們起名要一致。
[root@server1 haproxy]# vim haproxy.cfg
root@server1 haproxy]# groupadd -g 200 haproxy /為haproxy建立指定使用者
[root@server1 haproxy]# useradd -u 200 -g -M 200 haproxy
[root@server1 haproxy]# id haproxy
uid=200(haproxy) gid=200(haproxy) groups=200(haproxy)
[root@server1 haproxy]# vim /etc/security/limits.conf //修改系統限定檔案
[root@server1 haproxy]# vim haproxy.cfg
【配置檔案】
global
maxconn 10000 //最大連線數
stats socket /var/run/haproxy.stat mode 600 level admin
log 127.0.0.1 local0 //日誌
uid 200 //預設使用者uid
gid 200 //預設使用者gid
chroot /var/empty //安全設定,根目錄切換,鎖在/var/empty下
daemon
defaults
mode http
log global
option httplog
option dontlognull
monitor-uri /monitoruri
maxconn 8000 //最大連線數,這個會覆蓋前面的10000
timeout client 30s
stats uri /admin/stats
option prefer-last-server
retries 2
option redispatch
timeout connect 5s
timeout server 5s
# The public 'www' address in the DMZ
frontend public //前端訪問配置
bind *:80 name clear //允許訪問本機所有的ip
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
#use_backend static if { path_beg /img /css }
default_backend static //預設訪問static
# The static backend backend for 'Host: img', /img and /css.
backend static
balance roundrobin //輪叫演算法
server statsrv1 172.25.2.3:80 check inter 1000
//realserver1,預設1000毫秒檢查一次
server statsrv2 172.25.2.4:80 check inter 1000
//realserver2
[root@server1 haproxy]# /etc/init.d/haproxy start //開啟服務
Starting haproxy: [ OK ]
8種演算法:
1.balance roundrobin // 輪詢,軟負載均衡基本都具備這種演算法 2.balance static-rr //根據權重,建議使用 3.balance leastconn // 最少連線者先處理,建議使用 4.balance source //根據請求源IP,建議使用 5.balance uri //根據請求的URI 6.balance url_param // 根據請求的URl引數'balance url_param' requires an URL parameter name 7.balance hdr(name) // 根據HTTP請求頭來鎖定每一次HTTP請求 8.balance rdp-cookie(name) //根據據cookie(name)來鎖定並雜湊每一次TCP請求
1.3後端啟動服務:
[root@server3 ~]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server fully qualified domain name, using 172.25.2.3 for ServerName
[ OK ]
[root@server4 ~]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server fully qualified domain name, using 172.25.2.3 for ServerName
[ OK ]
1.4測試:
//此頁面表示haproxy伺服器完好
//監控頁面,不同顏色代表不同狀態
2.設定採集日誌目錄:
[root@server1 log]# vim /etc/rsyslog.conf //配置採集規則檔案
[root@server1 ~]# /etc/init.d/rsyslog restart //重啟服務
Shutting down system logger: [ OK ]
Starting system logger:
[ OK ]
此時瀏覽器進行訪問
[root@server1 log]# cat haproxy.log //檢視日誌
Aug 4 10:57:24 localhost haproxy[2010]: 172.25.2.250:33890 [04/Aug/2018:10:57:24.522] public static/statsrv1 0/0/0/1/1 200 274 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
Aug 4 10:57:24 localhost haproxy[2010]: 172.25.2.250:33890 [04/Aug/2018:10:57:24.523] public static/statsrv2 300/0/0/1/301 200 274 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"
Aug 4 10:57:24 localhost haproxy[2010]: 172.25.2.250:33890 [04/Aug/2018:10:57:24.825] public static/statsrv1 173/0/0/1/174 200 274 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"
Aug 4 10:57:25 localhost haproxy[2010]: 172.25.2.250:33890 [04/Aug/2018:10:57:24.999] public static/statsrv2 264/0/0/1/265 200 274 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"
3.後端伺服器動靜分離:
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
acl blacklist src 172.25.2.250
http-request deny if blacklist
use_backend static2 if { path_end -i .php } //動態訪問跳到static2
default_backend static1 //預設訪問static1
# The static backend backend for 'Host: img', /img and /css.
backend static1
balance roundrobin
server statsrv1 172.25.2.3:80 check inter 1000
backend static2
balance roundrobin
server statsrv2 172.25.2.4:80 check inter 1000
[root@server1 ~]# /etc/init.d/haproxy reload //重新整理策略
Server4配置動態網頁
[root@server4 html]# yum install php -y //安裝php
[root@server4 html]# /etc/init.d/httpd restart //重啟apache,一定要重啟
Stopping httpd: [ OK ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.2.4 for ServerName
[[email protected] ~]# cd /var/www/html/
[ OK ]
[[email protected] html]# vim index.php //設定釋出網頁
靜態訪問:
動態訪問:
4.訪問控制設定
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
acl blacklist src 172.25.2.250 //訪問控制
http-request deny if blacklist //對172.25.2.250的訪問禁止
use_backend static2 if { path_end -i .php }
default_backend static1
[root@server1 ~]# /etc/init.d/haproxy reload
5.報錯訪問重定向
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
acl blacklist src 172.25.2.250
http-request deny if blacklist
errorloc 403 http://172.25.2.1:8080
//當403報錯時重定向到172.25.2.1:8080上
use_backend static2 if { path_end -i .php }
[root@server1 ~]# /etc/init.d/haproxy reload //重新整理策略
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf //啟用8080埠
[root@server1 ~]# /etc/init.d/httpd start
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using 172.25.2.1 for ServerName
[ OK ]
[[email protected] ~]# vim /var/www/html/index.html //設定釋出頁面
訪問:
6.直接訪問重定向
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
acl blacklist src 172.25.2.250
#http-request deny if blacklist
#errorloc 403 http://172.25.2.1:8080
redirect location http:172.25.2.3:80 //重定向到server4的80埠
use_backend static2 if { path_end -i .php }
default_backend static1
[root@server1 ~]# /etc/init.d/haproxy reload //重新整理策略
訪問:
物理機:
[kiosk@foundation2 ~]$ curl -I 172.25.2.1 //檢視響應資訊
HTTP/1.1 302 Found
Cache-Control: no-cache
Content-length: 0
Location: http://172.25.2.4:80 //訪問被重定向到server4
Connection: close
7.讀寫分離
後端配置動態上傳網頁(前面的百度雲連結有寫好的):
[root@server3 ~]# yum install php -y //安裝php
[root@server3 ~]# cd /var/www/html/
[root@server3 html]# ls
index.html upload
[root@server3 html]# cd upload/
[root@server3 upload]# ls
index.php upload_file.php
[root@server3 upload]# mv * ..
[root@server3 upload]# cd ..
[root@server3 html]# ls
index.html index.php upload upload_file.php
[root@server3 html]# chmod 777 upload //加讀寫許可權
[root@server3 html]# vim upload_file.php //修改上傳圖片限定大小
在server4上做相同步驟
設定配置檔案
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
acl blacklist src 172.25.2.250
acl write method POST //寫控制
acl write method PUT
#http-request deny if blacklist
#errorloc 403 http://172.25.2.1:8080
#redirect location http://172.25.2.4:80
use_backend static2 if { path_end -i .php }
use_backend static2 if write //如果執行寫操作則調轉到server4
default_backend static1
# The static backend backend for 'Host: img', /img and /css.
backend static1
balance roundrobin
server statsrv1 172.25.2.3:80 check inter 1000
backend static2
balance roundrobin
server statsrv2 172.25.2.4:80 check inter 1000
[root@server1 ~]# /etc/init.d/haproxy reload //重新整理策略
上傳圖片
[root@server3 html]# cd upload
[root@server3 upload]# ls //圖片未上傳到server3
[root@server4 html]# cd upload
[root@server4 upload]# ls
index.php redhat.jpg //圖片上傳到server4
相關推薦
Linux之實訓篇——haproxy配置負載均衡及訪問控制
一、HAProxy簡介 HAProxy 是一款提供高可用性、負載均衡以及基於TCP(第四層)和HTTP(第七層)應用的代理軟體,支援虛擬主機,它是免費、快速並且可靠的一種解決方案。 HAProxy特別適用於那些負載特大的web站點,這些站點通常又需要會話保持
負載均衡服務之HAProxy https配置、四層負載均衡以及訪問控制
前文我們聊了下haproxy的訪問控制ACL的配置,回顧請參考https://www.cnblogs.com/qiuhom-1874/p/12817773.html;今天我們來聊一聊haproxy的https的配置、基於tcp四層負載均衡以及訪問控制的配置說明; 首先我們來看一下haproxy的htt
配置防盜鏈及訪問控制介紹
Linux配置防盜鏈防盜鏈,就是不讓別人盜用你網站上的資源,這個資源,通常指的是圖片、視頻、歌曲、文檔等。referer的概念你通過A網站的一個頁面http://a.com/a.html 裏面的鏈接去訪問B網站的一個頁面http://b.com/b.html ,那麽這個B網站頁面的referer就是http:
linux之企業實訓篇——虛擬機器的封裝
操作步驟 一.映象安裝虛擬機器 (具體操作步驟參照圖片) 建立成功 二、初始化設定虛擬機器 2.1、刪除檔案 Red Hat Enterprise L
Linux之企業實訓篇——Saltstack自動化管理系統之Grains元件
一、簡介 GRAINS 元件是saltstack中非常重要的一個元件,其主要用於記錄Minion的一些靜態資訊,如比:CPU、記憶體、磁碟、網路等。grains資訊是每次客戶端啟動後自動上報給master的,一旦這些靜態資訊發生改變需要重啟mi
Linux之企業實訓篇——lnmp架構簡單搭建論壇
上篇部落格寫了lnmp的架構部落格連結,這篇在上篇部落格的基礎上實現論壇的搭建>_< 實驗軟體包: 實驗 一、論壇搭建 [root@server1 ~]# yum install unzip -y [root@server1 ~]#
初探LINUX之--基礎知識篇
解釋 post why 不能 mman shells 運用 車間 ech 一 Linux哲學思想 1 一切都是一個文件(包含硬件) 2 小型,單一用途的程序 3 鏈接程序,共同完成復雜的任務 4 避免令人困惑的用戶界面 5 配置數據存儲在文本中 二 Linux重要概念
Nginx + Tomcat 配置負載均衡集群簡單實例
hash weight 用戶 index 文件服務器 修改配置文件 ads 配置文件 ace 一、Hello world 1、前期環境準備 準備兩個解壓版tomcat,如何同時啟動兩個tomcat,請看我的另一篇文章《一臺機器同時啟動多個tomcat》。 nginx官網下
大數據<javaSE + Linux精英實訓班>_day_02
常量 布爾 範例 lin rgs 基礎 ring main false ---恢復內容開始--- 1:基礎語法->變量 變量是內存當中的小盒子(小容器),容器是什麽? 變量裝載的是數據! 2:基礎語法->計算機的存儲單元
大數據<javaSE + Linux精英實訓班>_day_06
dom har test 自定義類型 引用 stat 長度 實現 add 1:基礎語法->引用數據類型(類) Scanner類:鍵盤輸入 Random類:隨機數 兩種:1、Java提
大數據<javaSE + Linux精英實訓班>_day_08
boolean 等待 小汽車 執行 負責 正在 AR [] day 1:面向對象->面向對象和面向過程的思想 都是我們編寫程序的一種方式。 面向過程:這種程序設計方式,遇到事情思考:"我該怎麽做",然後一步步實現的
大數據<javaSE + Linux精英實訓班>_day_07
自動 javase star 標準 idea 計算 人類 類型 計算機 1:基礎語法->ASCII編碼表 計算機的世界,01->二進制 全世界有多少個國家?每個國家用什麽語句? 全世界都在使用計
大數據<javaSE + Linux精英實訓班>_day_09
strac ati 去重 ... print public int() 成員 tid 1:面向對象->繼承 描述事物之間的所屬關系。使用種事物之間形成一種關系體系。 在java中類的繼承是指在現有類的基礎上構建一個新
Linux叢集儲存——day3——keepalived的高可用服務、HAProxy的負載均衡服務
埠監測 nmap 命令進行測試 需要裝包 nmap 可以進行檢測具體的IP的某個埠 nmap -sS -n -p 埠 檢測的主機IP | grep open [ $? -eq 0 ] &&
windows環境安裝haproxy及初步配置負載均衡使用示例
安裝HaProxy 首先需要下載windows環境下需要檔案,這裡下載的是一個別人編譯好的一個檔案,這裡省去了編譯的過程,使用的版本是haproxy-1.7.8。 下載後直接解壓到對應的目錄下。示例(D:\haproxy\haproxy-1.7.8)裡面包含了以下的檔案: 裡成對啟動和停止做了批處理
Nginx在linux伺服器中配置負載均衡upstream詳解
這篇文章的前提是已經配置好了NGINX,而且tomcat已經配置好了,而且能能夠訪問了。 這裡介紹一臺伺服器(192.168.83.130),代理兩個本機的tomcat(埠分別為:8080、8081),下面是安裝後情況: nginx目錄: tomcat目錄: 首先在兩個tomcat的/c
Linux 之 使用者和組的配置檔案
使用者配置檔案(使用者資訊):/etc/passwd 含義:使用者名稱:口令:使用者標識號:組標識號:註釋性描述:主目錄:登陸shell 組配置檔案(組資訊):/etc/group 含義:組名:口令:組標識號:組內使用者列表(使用者列表被處理,這裡不顯示) 口
Linux基礎實訓(利用執行緒和互斥鎖)
實驗要求(linux) 1 定義一個長度為8的陣
漫談linux之檔案IO篇
前同事的文章,覺得寫得很清晰,收藏了。 在Linux 開發中,有幾個關係到效能的東西,技術人員非常關注:程序,CPU,MEM,網路IO,磁碟IO。本篇檔案打算詳細全面,深入淺出。剖析檔案IO的細節。從多個角度探索如何提高IO效能。本文儘量用通俗易懂的視角去闡述。不cop
linux:實訓及部分程式碼
專案一第一題 1、makefile檔案編寫 實驗內容:程式功能,主函式通過呼叫函式isPrime(num)判定num是否素數, 並生成一個100個元素為素數的陣列,然後呼叫函式outprime(int a[],int n)輸出n個素數 。通過編寫一個ma