HAProxy雜記(1)

HAProxy

haproxy基礎

1、安裝haproxy

[root@master1 ~]# yum -y install haproxy
[root@master2 ~]# yum -y install haproxy查看haproxy生成的文件 :
[root@master1 ~]# rpm -ql haproxy備份配置文件:
[root@master1 haproxy]# cp haproxy.cfg{,.back}
[root@master1 haproxy]# ls
haproxy.cfg  haproxy.cfg.back

haproxy演示

實驗環境:1臺haproxy,2臺httpd

1、兩臺網頁服務器安裝httpd

[root@master2 ~]# yum install -y httpd
[root@master3 ~]# yum install -y httpd設置首頁,啟動服務:
[root@master2 ~]# echo "<h1>Web1</h1>" > /var/www/html/index.html
[root@master2 ~]# systemctl start httpd.service
[root@master2 ~]#[root@master3 ~]# echo "<h1>Web2</h1>" > /var/www/html/index.html
[root@master3 ~]# systemctl start httpd.service
[root@master3 ~]# 

2、配置haproxy文件,將用戶請求轉發到后端去

[root@master1 haproxy]# vim haproxy.cfg#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  main *:80default_backend            websrvs#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvsbalance     roundrobinserver web1 10.201.106.132:80 checkserver web2 10.201.106.133:80 check
~          啟動服務:
[root@master1 haproxy]# systemctl start haproxy.service
[root@master1 haproxy]# systemctl status haproxy.service
● haproxy.service - HAProxy Load BalancerLoaded: loaded (/usr/lib/systemd/system/haproxy.service; disabled; vendor preset: disabled)Active: active (running) since Sun 2017-01-15 22:30:40 CST; 5s agoMain PID: 4924 (haproxy-systemd)CGroup: /system.slice/haproxy.service├─4924 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid├─4925 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds└─4926 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -DsJan 15 22:30:40 master1.com systemd[1]: Started HAProxy Load Balancer.
Jan 15 22:30:40 master1.com systemd[1]: Starting HAProxy Load Balancer...
Jan 15 22:30:40 master1.com haproxy-systemd-wrapper[4924]: haproxy-systemd-wrapper: executing /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds
[root@master1 haproxy]# 訪問:http://10.201.106.131/
可以在兩個web站點輪流跳轉

3、健康狀態監測測試

3.1 停止一個web節點服務

[root@master2 ~]# systemctl stop httpd現在只能訪問剩下的那個節點了;

4、開啟日志功能

4.1 開啟本地日志服務

配置日志文件:
[root@master1 ~]# vim /etc/rsyslog.conf # Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514# Save boot messages also to boot.loglocal2.*                                                /var/log/haproxy.log重啟日志服務:
[root@master1 ~]# systemctl restart rsyslog.service
[root@master1 ~]# systemctl status rsyslog.service
● rsyslog.service - System Logging ServiceLoaded: loaded (/usr/lib/systemd/system/rsyslog.service; enabled; vendor preset: enabled)Active: active (running) since Sun 2017-01-15 22:47:46 CST; 13s agoMain PID: 5073 (rsyslogd)CGroup: /system.slice/rsyslog.service└─5073 /usr/sbin/rsyslogd -nJan 15 22:47:46 master1.com systemd[1]: Starting System Logging Service...
Jan 15 22:47:46 master1.com systemd[1]: Started System Logging Service.
[root@master1 ~]# 查看是否監聽UDP 514端口:
[root@master1 ~]# ss -unlp
State       Recv-Q Send-Q                                      Local Address:Port                                                     Peer Address:Port              
UNCONN      0      0                                                       *:40858                                                               *:*                   users:(("haproxy",pid=4926,fd=6),("haproxy",pid=4925,fd=6))
UNCONN      0      0                                                       *:514                                                                 *:*                   users:(("rsyslogd",pid=5104,fd=3))
UNCONN      0      0                                                      :::514                                                                :::*                   users:(("rsyslogd",pid=5104,fd=4))
[root@master1 ~]# 

4.2 查看haproxy日志

[root@master1 ~]# tail /var/log/haproxy.log Feb  5 23:21:29 localhost haproxy[4926]: Server websrvs/web1 is UP, reason: Layer4 check passed, check duration: 0ms. 2 active and 0 backup servers online. 0 sessions requeued, 0 total in queue.
Feb  5 23:21:31 localhost haproxy[4926]: 10.201.106.1:56402 [05/Feb/2017:23:21:31.495] main websrvs/web2 5/0/9/2/16 304 141 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"
Feb  5 23:21:32 localhost haproxy[4926]: 10.201.106.1:56402 [05/Feb/2017:23:21:31.512] main websrvs/web1 727/0/1/4/732 200 273 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
Feb  5 23:21:32 localhost haproxy[4926]: 10.201.106.1:56402 [05/Feb/2017:23:21:32.243] main websrvs/web2 738/0/3/6/747 200 273 - - ---- 1/1/0/0/0 0/0 "GET / HTTP/1.1"
Feb  5 23:21:33 localhost haproxy[4926]: 10.201.106.1:56402 [05/Feb/2017:23:21:32.989] main websrvs/web1 572/0/1/2/575 200 273 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"
[root@master1 ~]# 

haproxy 高級配置

修改haproxy調度算法為source

vim haproxy.cfg
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvsbalance     source重載服務:
[root@master1 haproxy]# systemctl reload haproxy.service訪問測試后:只在一個web上面停留了,不會跳轉至另一個web;

修改為uri算法

[root@master1 haproxy]# vim haproxy.cfg#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvsbalance     urihash-type   consistent重載服務:
[root@master1 haproxy]# systemctl reload haproxy.service生成10個測試頁面:
[root@master2 ~]# for i in {1..10};do echo "<h1>Page $i on Web1</h1>" > /var/www/html/test$i.html;done[root@master3 ~]# for i in {1..10};do echo "<h1>Page $i on Web2</h1>" > /var/www/html/test$i.html;done
[root@master3 ~]# 
[root@master3 ~]# ls /var/www/html/
index.html   test1.html  test3.html  test5.html  test7.html  test9.html
test10.html  test2.html  test4.html  test6.html  test8.html訪問測試:http://10.201.106.131/test2.html
只固定在一個web服務器上;[root@master3 ~]# curl http://10.201.106.131/test2.html
<h1>Page 2 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test2.html
<h1>Page 2 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test2.html
<h1>Page 2 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test2.html
<h1>Page 2 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test2.html
<h1>Page 2 on Web1</h1>
[root@master3 ~]# 
[root@master3 ~]# curl http://10.201.106.131/test5.html
<h1>Page 5 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test5.html
<h1>Page 5 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test5.html
<h1>Page 5 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test6.html
<h1>Page 6 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test6.html
<h1>Page 6 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test6.html
<h1>Page 6 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test8.html
<h1>Page 8 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test8.html
<h1>Page 8 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test9.html
<h1>Page 9 on Web2</h1>
[root@master3 ~]# curl http://10.201.106.131/test9.html
<h1>Page 9 on Web2</h1>
[root@master3 ~]# curl http://10.201.106.131/test3.html
<h1>Page 3 on Web2</h1>
[root@master3 ~]# curl http://10.201.106.131/test3.html
<h1>Page 3 on Web2</h1>
[root@master3 ~]# curl http://10.201.106.131/test4.html
<h1>Page 4 on Web2</h1>
[root@master3 ~]# curl http://10.201.106.131/test4.html
<h1>Page 4 on Web2</h1>
[root@master3 ~]# curl http://10.201.106.131/test8.html
<h1>Page 8 on Web1</h1>
[root@master3 ~]# curl http://10.201.106.131/test8.html
<h1>Page 8 on Web1</h1>
[root@master3 ~]# 

hdr調度算法

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvsbalance     hdr(User-Agent)hash-type   consistent通過curl模擬別的瀏覽器訪問:
[root@master3 ~]# curl -A 'hello' http://10.201.106.131/test4.html
<h1>Page 4 on Web2</h1>
[root@master3 ~]# 
[root@master3 ~]# curl -A 'IE' http://10.201.106.131/test4.html
<h1>Page 4 on Web2</h1>
[root@master3 ~]# curl -A 'IE' http://10.201.106.131/test4.html
<h1>Page 4 on Web2</h1>
[root@master3 ~]# curl -A '360' http://10.201.106.131/test4.html
<h1>Page 4 on Web2</h1>
[root@master3 ~]# curl -A '360' http://10.201.106.131/test4.html

設置監聽多個端口

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  mainbind *:80  bind *:8080default_backend            websrvs重啟服務:
[root@master1 haproxy]# systemctl restart haproxy.service
[root@master1 haproxy]# ss -tnl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128           *:8080                      *:*                  
LISTEN      0      128           *:80                        *:*                  
LISTEN      0      128           *:22                        *:*                  
LISTEN      0      100    127.0.0.1:25                        *:*                  
LISTEN      0      128          :::22                       :::*                  
LISTEN      0      100         ::1:25                       :::*                  
[root@master1 haproxy]# 

修改權重測試

[root@master1 haproxy]# vim haproxy.cfg#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend websrvsbalance     roundrobinserver web1 10.201.106.132:80 check weight 1server web2 10.201.106.133:80 check weight 3[root@master1 haproxy]# systemctl reload haproxy.service測試訪問網頁結果是,訪問3次web2,才訪問一次web1;

轉載于:https://blog.51cto.com/zhongle21/2087359

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/452354.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/452354.shtml
英文地址,請注明出處:http://en.pswp.cn/news/452354.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

編解碼標準H264 與 AVS 變換矩陣比較

在編解碼中&#xff0c;變換是最重要的一步&#xff0c;從開始的模擬離散變換&#xff0c;到現在國際和中國標準中的整數變換&#xff0c;變換取的壓縮是最重要的&#xff0c;在 DV等其他編解碼中&#xff0c;只使用變換進行壓縮&#xff0c; 下面對H264 和AVS使用的變換矩陣進…

計算機圖畫大賽作品六年級,打字能手顯本領,電腦繪畫展風采——記陸埠二小舉行電腦繪畫和電腦打字比賽...

為了提高小學生的計算機應用水平&#xff0c;培養學生動手能力和綜合素質&#xff0c;提升學生的信息素養&#xff0c;2019年5月23日、24日中午&#xff0c;陸埠鎮第二小學舉行了三四年級電腦打字和五六年級電腦繪畫比賽。本次比賽&#xff0c;3--6年級每班中選出3名學生參加&a…

數據庫角色

數據庫角色&#xff1a;被命名的一組與數據庫操作相關的權限1.角色是權限的集合 2.可以為一組具有相同權限的用戶創建一個角色 3.簡化授權的過程 一個角色的權限&#xff1a;直接授予這個角色的全部權限加上其他角色 授予這個角色的全部權限

變量在原型鏈中的查找順序

js原型鏈 下面是一道js題目&#xff1a;[javascript] view plaincopy function C1(name){ if(name){ this.name name; } } function C2(name){ this.name name; } function C3(name){ this.name name || "John"; } C1.p…

基于SpringBoot + Vue的圖書管理系統

功能概述 該圖書管理系統提供了一系列功能&#xff0c;包括圖書管理、圖書類型管理、讀者借閱歸還圖書、用戶管理和重置密碼等。 在圖書管理功能中&#xff0c;管理員可以方便地進行圖書信息的管理。他們可以添加新的圖書記錄&#xff0c;包括書名、作者、出版社、ISBN等信息&a…

交換機的工作轉發原理

交換機通常是運行在網絡OSI七層模型的第二層數據鏈路層&#xff0c;如圖中&#xff0c;第三層網絡層通常是路由器運行在該層 今天我們來看看&#xff0c;交換機的工作轉發原理是什么樣的。 交換機既然是利用端口進行網絡數據傳輸&#xff0c;那么它是如何識別數據是誰給誰的呢…

[UWP小白日記-14]正則表達式

原文:[UWP小白日記-14]正則表達式匹配2位浮點數&#xff1a; ^(([1-9][0-9]*\.{1}[0-9]{1,2})|([0]\.{1}[1-9][0-9]{1,2})|([0]\.\d{1,2})|([1-9][0-9]{1,2})|[1-9]\d*|([0][.][0-9][1-9]{1,2}))$

視圖機制對于數據庫的安全意義

視圖機制可以把要保密的數據對無權存取這些數據的用戶隱藏起來&#xff0c;對數據提供一定程度的安全保護&#xff0c;間接地實現支持存取謂詞的用戶權限定義。

2017計算機考試題上機,2017年計算機二級上機考試試題及答案

2017年計算機二級上機考試試題及答案20世紀60年代中期之前的第一代計算機網絡是以單個計算機為中心的遠程聯機系統。下面是小編整理的關于計算機二級上機考試試題&#xff0c;希望大家認真練習!1[單選題] 一棵二叉樹中共有80個葉子結點與70個度為1的結點&#xff0c;則該二叉樹…

八個最好的輕量級Linux發行版

如果你在苦惱老舊的硬件無法利用&#xff0c;如果你想要一個能夠在不是很大的記憶棒上運行的系統&#xff0c;如果你想要在桌面端上運行200個虛擬機&#xff0c;那么你可以考慮一些“迷你”的Linux發行版。 曾經在08年介紹過當時的十大輕量級Linux&#xff0c;現在已經是2010年…

面向對象——三層架構(表現層、業務層、持久層)

① 持久層&#xff1a;采用DAO模式&#xff0c;建立實體類和數據庫表映射&#xff08;ORM映射&#xff09;。也就是哪個類對應哪個表&#xff0c;哪個屬性對應哪個列。持久層 的目的就是&#xff0c;完成對象數據和關系數據的轉換。 ② 業務層&#xff1a;采用事務腳本模式。將…

VMware安裝Centos7后有線線纜被拔出

背景&#xff1a;在win10 系統中的虛機軟件VMware Workstation中安裝CentOS7桌面版&#xff0c;安裝過程中沒有設置網絡 1.確認你win10系統打開了這兩個服務&#xff1a;VMware DHCP Service和VMware NAT Service 方法&#xff1a;電腦——右鍵——管理——服務和應用程序——服…

SpringCloud |第二篇: 服務消費者(Ribbon)

2019獨角獸企業重金招聘Python工程師標準>>> 一、Ribbon簡介 Ribbon是Netflix發布的開源項目&#xff0c;主要功能是提供客戶端的軟件負載均衡算法&#xff0c;將Netflix的中間層服務連接在一起。Ribbon客戶端組件提供一系列完善的配置項如連接超時&#xff0c;重試…

數據庫審計

啟用一個專用的審計日志&#xff08;Audit Log&#xff09;將用戶對數據庫的所有操作記錄在上面。審計員利用審計日志監控數據庫中的各種行為&#xff0c;找出非法存取數據的人、時間和內容。 審計很費時間和空間 DBA可以根據應用對安全性的要求&#xff0c;靈活地打開或關閉…

北海市計算機等級考試,2021上半年北海市計算機二級報名時間|網上報名入口【已開通】...

&nbsp&nbsp[導讀]:2021上半年北海市計算機二級報名時間|網上報名入口【已開通】&#xff0c;更多廣西等級考試報名時間、考試時間以及考試模擬試題&#xff0c;請訪問易考吧廣西等級考試欄目2021上半年北海市計算機二級報名時間|網上報名入口【已開通】一、報名時間網上…

Java實體對象為什么一定要實現Serializable接口呢?

文章目錄Java對象為什么要實現Serializable接口&#xff1f;Serializable接口概述Java對象為什么要實現Serializable接口&#xff1f; 最近這段時間一直在忙著編寫Java業務代碼&#xff0c;麻木地搬著Ctrl-C、Ctrl-V的磚&#xff0c;在不知道重復了多少次定義Java實體對象時“…

C3P0連接池工具類使用

c3p0的基本連接配置文件 c3p0-config.xml <c3p0-config><default-config><property name"driverClass">com.mysql.jdbc.Driver</property><property name"jdbcUrl">jdbc:mysql:///mybase</property><property name…

項目經理常見的溝通壞習慣

溝通失敗有很多原因&#xff0c;每個項目經理都必須熟悉這些原因、了解其中的行為、并且有責任避免溝通失敗的發生。在一些團隊中&#xff0c;會產生失敗的溝通、失敗的項目是因為團隊經理本身的壞習慣行為或者他本人容忍組員有些行為&#xff0c;而這些行為和壞習慣無意中會導…

Android屏幕適配

Android屏幕適配一直是Android開發們的一個痛點&#xff0c;各種各樣的屏幕分辨率等&#xff0c;對Android的屏幕適配帶來了很大的麻煩&#xff0c;而谷歌的解決方案也并不被所有人滿意&#xff0c;所以筆者結合Android官方文檔&#xff0c;來談談這個話題。 術語和基本概念 本…

萬維網www

WWW是環球信息網的縮寫&#xff0c;&#xff08;亦作“Web”、“WWW”、“W3”&#xff0c;英文全稱為“World Wide Web”&#xff09;&#xff0c;中文名字為“萬維網”&#xff0c;"環球網"等&#xff0c;常簡稱為Web。 分為Web客戶端和Web服務器程序。 WWW可以讓W…