日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HAProxy杂记(1)

發布時間:2023/12/19 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的HAProxy杂记(1)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。