Haproxy

来源:https://www.cnblogs.com/tushanbu/archive/2022/09/28/16736582.html
-Advertisement-
Play Games

Haproxy haproxy簡介 HAProxy是一個使用C語言編寫的自由及開放源代碼軟體,其提供高可用性、負載均衡,以及基於TCP和HTTP的應用程式代理。 HAProxy特別適用於那些負載特大的web站點,這些站點通常又需要會話保持或七層處理。HAProxy運行在當前的硬體上,完全可以支持數以 ...


Haproxy


目錄

haproxy簡介

HAProxy是一個使用C語言編寫的自由及開放源代碼軟體,其提供高可用性、負載均衡,以及基於TCP和HTTP的應用程式代理。
HAProxy特別適用於那些負載特大的web站點,這些站點通常又需要會話保持或七層處理。HAProxy運行在當前的硬體上,完全可以支持數以萬計的併發連接。並且它的運行模式使得它可以很簡單安全的整合進您當前的架構中, 同時可以保護你的web伺服器不被暴露到網路上。
HAProxy實現了一種事件驅動**, **單一進程模型,此模型支持非常大的併發連接數。多進程或多線程模型受記憶體限制 、系統調度器限制以及無處不在的鎖限制,很少能處理數千併發連接。事件驅動模型因為在有更好的資源和時間管理的用戶空間(User-Space) 實現所有這些任務,所以沒有這些問題。此模型的弊端是,在多核系統上,這些程式通常擴展性較差。這就是為什麼他們必須進行優化以 使每個CPU時間片(Cycle)做更多的工作。
包括 GitHub、Bitbucket、Stack Overflow、Reddit、Tumblr、Twitter和 Tuenti在內的知名網站,及亞馬遜網路服務系統都使用了HAProxy。

負載均衡

二層負載均衡(mac)
用於虛擬mac地址方式,外部對虛擬mac地址請求,負載均衡接收後分配給後端實際的mac地址響應。
三層負載均衡(ip)
一般用於虛擬ip地址的方式,外部對虛擬ip地址請求,負載均衡接收後分配給後端實際的ip地址響應。
四層負載均衡(tcp)
在三層負載均衡的基礎上,用ip+port接收請求,在轉發到對應的機器上。
產品大概有:F5,lvs,nginx,haproxy......
七層負載均衡(http)
根據虛擬的url或者ip,主機名接收請求,在轉發到相應的處理伺服器上。
產品大概有:haproxy,nginx,apache,mysql proxy......

Haproxy搭建http負載均衡

環境

主機名稱 IP地址 需要安裝的應用 系統版本
client 192.168.222.250 redhat 8.2
lvnanhai 192.168.222.137 haproxy redhat 8.2
RS1 192.168.222.138 httpd redhat 8.2
RS2 192.168.222.139 httpd redhat 8.2

lvnanhai、RS1、RS2都關閉防火牆和selinux

lvnanhai:

[root@localhost ~]# hostnamectl set-hostname lvnanhai.example.com
[root@localhost ~]# bash
[root@lvnanhai ~]# systemctl stop firewalld.service 
[root@lvnanhai ~]# vim /etc/selinux/config 
SELINUX= disabled
[root@lvnanhai ~]# setenforce 0
[root@lvnanhai ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

RS1:

root@localhost ~]# hostnamectl set-hostname RS1
[root@localhost ~]# bash
[root@RS1 ~]# systemctl stop firewalld.service 
[root@RS1 ~]# vim /etc/selinux/config 
SELINUX= disabled
[root@RS1 ~]# setenforce 0
[root@RS1 ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

RS2:

[root@localhost ~]# hostnamectl set-hostname RS2
[root@localhost ~]# bash
[root@RS2 ~]# systemctl stop firewalld.service 
[root@RS2 ~]# vim /etc/selinux/config 
SELINUX= disabled
[root@RS2 ~]# setenforce 0
[root@RS2 ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

RS1和RS2部署httpd

RS1:

[root@RS1 ~]# dnf -y install httpd
[root@RS1 ~]# echo RS1 > /var/www/html/index.html
[root@RS1 ~]# cat /var/www/html/index.html
RS1
[root@RS1 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

RS2:

[root@RS2 ~]# dnf -y install httpd
[root@RS2 ~]# echo RS2 > /var/www/html/index.html
[root@RS2 ~]# cat /var/www/html/index.html
RS2
[root@RS2 ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

lvnanhai源碼安裝haproxy

haproxy源碼包下載網站地址
下載安裝包

[root@lvnanhai ~]# wget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-2.1.3.tar.gz/sha512/4728c1177b2bba69465cbc56b1ed73a1b2d36891ba2d94d29bb49714ad98ccfac4b52947735aded211f0cd8070002f5406ddd77cabd2f8230b00438189dd7a60/haproxy-2.1.3.tar.gz
[root@lvnanhai ~]# ls
anaconda-ks.cfg  haproxy-2.1.3.tar.gz

安裝編譯環境

[root@lvnanhai ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel

創建haproxy用戶

[root@lvnanhai ~]# useradd -r -M -s /sbin/nologin haproxy

解壓和安裝

[root@lvnanhai ~]# tar xf haproxy-2.1.3.tar.gz 
[root@lvnanhai ~]# ls
anaconda-ks.cfg  haproxy-2.1.3  haproxy-2.1.3.tar.gz
[root@lvnanhai ~]# cd haproxy-2.1.3/
[root@lvnanhai haproxy-2.1.3]# make clean
[root@lvnanhai haproxy-2.1.3]# make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \
> TARGET=linux-glibc  \
> USE_OPENSSL=1  \
> USE_ZLIB=1  \
> USE_PCRE=1  \
> USE_SYSTEMD=1
[root@lvnanhai haproxy-2.1.3]# make install PREFIX=/usr/local/haproxy
[root@lvnanhai haproxy-2.1.3]# cp haproxy  /usr/sbin/

設置Linux內核參數

[root@lvnanhai haproxy-2.1.3]# vim /etc/sysctl.conf
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
//等於號前後都要空格

配置haproxy服務

[root@lvnanhai haproxy-2.1.3]# vim /etc/haproxy/haproxy.cfg
[root@lvnanhai haproxy-2.1.3]# cat /etc/haproxy/haproxy.cfg 
global
 log 127.0.0.1 local0  info
    #log loghost local0 info
    maxconn 20480
#chroot /usr/local/haproxy
    pidfile /var/run/haproxy.pid
    #maxconn 4000
    user haproxy
    group haproxy
    daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    #option forwardfor
    option redispatch
    balance roundrobin
    timeout connect 10s
    timeout client 10s
    timeout server 10s
    timeout check 10s
    maxconn 60000
    retries 3
#--------------統計頁面配置------------------
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /haproxy_stats
    stats realm Haproxy\ Statistics
    stats auth admin:admin
    #stats hide-version
    stats admin if TRUE
    stats refresh 30s
#---------------web設置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode http
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 172.16.103.130:80 check inter 2000 fall 5
    #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5

啟動haproxy,配置haproxy.service服務單元文件

[root@lvnanhai ~]# vim /usr/lib/systemd/system/haproxy.service
[root@lvnanhai ~]# cat /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg   -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg  -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

配置日誌信息

[root@lvnanhai ~]# vim /etc/rsyslog.conf
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages
local0.*        /var/log/haproxy.log  //添加
[root@lvnanhai ~]# systemctl restart rsyslog.service 
[root@lvnanhai ~]# systemctl enable rsyslog.service 
[root@lvnanhai ~]# systemctl restart rsyslog.service 
[root@lvnanhai ~]# systemctl enable rsyslog.service 

修改lvnanhai的內核參數

[root@lvnanhai ~]# vim /etc/sysctl.conf
[root@lvnanhai ~]# vim /etc/sysctl.conf 
[root@lvnanhai ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
//等於號前後要空格

修改haproxy配置文件

[root@lvnanhai ~]# vim /etc/haproxy/haproxy.cfg 
[root@lvnanhai ~]# cat /etc/haproxy/haproxy.cfg 
global
    daemon
    maxconn 256

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http-in
    bind *:80
    default_backend servers

backend servers
    server web01 192.168.222.138:80
    server web02 192.168.222.139:80

啟動haproxy服務

如果沒有使用systemd管理,則執行

[root@lvnanhai ~]# haproxy -f /etc/haproxy/haproxy.cfg -c

如果配置systemd管理haproxy服務,則執行

[root@lvnanhai ~]# systemctl restart haproxy.service 
[root@lvnanhai ~]# systemctl enable --now haproxy.service 
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.

客戶端驗證

[root@client ~]# curl http://192.168.222.137
RS1
[root@client ~]# curl http://192.168.222.137
RS2
[root@client ~]# curl http://192.168.222.137
RS1
[root@client ~]# curl http://192.168.222.137
RS2

使用WEB網頁訪問測試

[root@lvnanhai ~]# vim /etc/haproxy/haproxy.cfg
[root@lvnanhai ~]# cat /etc/haproxy/haproxy.cfg
global
    log 127.0.0.1 local0  info
    #log loghost local0 info
    maxconn 20480
#chroot /usr/local/haproxy
    pidfile /var/run/haproxy.pid
    #maxconn 4000
    user haproxy
    group haproxy
    daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    #option forwardfor
    option redispatch
    balance roundrobin
    timeout connect 10s
    timeout client 10s
    timeout server 10s
    timeout check 10s
    maxconn 60000
    retries 3
#--------------統計頁面配置------------------
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /haproxy_stats            //訪問網頁尾碼URL
    stats realm Haproxy\ Statistics
    stats auth admin:admin              //用戶名和密碼
    #stats hide-version
    stats admin if TRUE
    stats refresh 30s
#---------------web設置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode http
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.222.138:80 check inter 2000 fall 5
    server web02 192.168.222.139:80 check inter 2000 fall 5

重啟服務

[root@lvnanhai ~]# systemctl restart haproxy.service 
[root@lvnanhai ~]# ss -tnl
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process     
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0          128                   0.0.0.0:8189                0.0.0.0:*                    
LISTEN     0          128                   0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0          128                      [::]:22                     [::]:*                    

網頁訪問測試

用戶名和密碼都為admin


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 0.基本的數據類型: Number(數字) String(字元串) List(列表) Tuple(元組) Set(集合) Dictionary(字典) 1.基本的輸入輸出: input #輸入 print #輸出 age = 10; name = "小明"; print(age,type(age)) ...
  • ConcurrentHashMap 思考:HashTable是線程安全的,為什麼不推薦使用? HashTable是一個線程安全的類,它使用synchronized來鎖住整張Hash表來實現線程安全,即每次鎖住整張表讓線程獨占,相當於所有線程進行讀寫時都去競爭一把鎖,導致效率非常低下。 1 Concu ...
  • zip 是一個非常常見的壓縮包格式,本文主要用於說明如何使用代碼 文件或文件夾壓縮為 zip壓縮包及其解壓操作, 我們採用的是 微軟官方的實現,所以也不需要安裝第三方的組件包。 使用的時候記得 using System.IO.Compression; /// <summary> /// 將指定目錄壓 ...
  • 1. 融合效果 融合效果是指對兩個接近的元素進行高斯模糊後再提高對比度,使它們看上去“粘”在一起。在之前的一篇文章中,我使用 Win2D 實現了融合效果,效果如下: 不過 Win2D 不適用於 WPF,在 WPF 中可以使用 BlurEffect 配合自定義 Effect 實現類似的效果。Handy ...
  • 1、初衷 開發中經常需要做一些介面的簽名生成和校驗工作,最開始的時候都是每個介面去按照約定單獨實現,久而久之就變的非常難維護,因此就琢磨怎麼能夠寫了一個比較通用的簽名生成工具。 2、思路 採用鏈式調用的方式,使得簽名的步驟可以動態拼湊組合。 3、直接看效果 //設置數據源 var signSourc ...
  • 在整理 GCC Arm 工具鏈的Bluepill代碼示例, 常用外設都差不多了, 接下來是 FreeRTOS, 網上查到的基本上都是基於舊版本的集成, STM32 SPL版本3.5.0, FreeRTOS版本9.x或者10.x, 不如自己用新版本集成一下. 順便做個筆記. STM32F10x_Std... ...
  • 一、文件目錄類 pwd指令 基本語法:pwd 功能:顯示當前工作的絕對目錄 ls指令 基本語法:ls [選項][目錄或者文件] 常用選項 -a 顯示所有文件及目錄 (. 開頭的隱藏文件也會列出) -l 除文件名稱外,亦將文件型態、許可權、擁有者、文件大小等資訊詳細列出 cd指令 基本語法:cd [參數 ...
  • SourceTree 是 Windows 和Mac OS X 下Git 和 Hg 客戶端管理工具,同時也是Mercurial和Subversion版本控制系統工具,需要的朋友趕快來看看吧! 詳情:SourceTree for Mac(Git客戶端工具) SourceTree Mac是一款針對mac平 ...
一周排行
    -Advertisement-
    Play Games
  • 1. 說明 /* Performs operations on System.String instances that contain file or directory path information. These operations are performed in a cross-pla ...
  • 視頻地址:【WebApi+Vue3從0到1搭建《許可權管理系統》系列視頻:搭建JWT系統鑒權-嗶哩嗶哩】 https://b23.tv/R6cOcDO qq群:801913255 一、在appsettings.json中設置鑒權屬性 /*jwt鑒權*/ "JwtSetting": { "Issuer" ...
  • 引言 集成測試可在包含應用支持基礎結構(如資料庫、文件系統和網路)的級別上確保應用組件功能正常。 ASP.NET Core 通過將單元測試框架與測試 Web 主機和記憶體中測試伺服器結合使用來支持集成測試。 簡介 集成測試與單元測試相比,能夠在更廣泛的級別上評估應用的組件,確認多個組件一起工作以生成預 ...
  • 在.NET Emit編程中,我們探討了運算操作指令的重要性和應用。這些指令包括各種數學運算、位操作和比較操作,能夠在動態生成的代碼中實現對數據的處理和操作。通過這些指令,開發人員可以靈活地進行算術運算、邏輯運算和比較操作,從而實現各種複雜的演算法和邏輯......本篇之後,將進入第七部分:實戰項目 ...
  • 前言 多表頭表格是一個常見的業務需求,然而WPF中卻沒有預設實現這個功能,得益於WPF強大的控制項模板設計,我們可以通過修改控制項模板的方式自己實現它。 一、需求分析 下圖為一個典型的統計表格,統計1-12月的數據。 此時我們有一個需求,需要將月份按季度劃分,以便能夠直觀地看到季度統計數據,以下為該需求 ...
  • 如何將 ASP.NET Core MVC 項目的視圖分離到另一個項目 在當下這個年代 SPA 已是主流,人們早已忘記了 MVC 以及 Razor 的故事。但是在某些場景下 SSR 還是有意想不到效果。比如某些靜態頁面,比如追求首屏載入速度的時候。最近在項目中回歸傳統效果還是不錯。 有的時候我們希望將 ...
  • System.AggregateException: 發生一個或多個錯誤。 > Microsoft.WebTools.Shared.Exceptions.WebToolsException: 生成失敗。檢查輸出視窗瞭解更多詳細信息。 內部異常堆棧跟蹤的結尾 > (內部異常 #0) Microsoft ...
  • 引言 在上一章節我們實戰了在Asp.Net Core中的項目實戰,這一章節講解一下如何測試Asp.Net Core的中間件。 TestServer 還記得我們在集成測試中提供的TestServer嗎? TestServer 是由 Microsoft.AspNetCore.TestHost 包提供的。 ...
  • 在發現結果為真的WHEN子句時,CASE表達式的真假值判斷會終止,剩餘的WHEN子句會被忽略: CASE WHEN col_1 IN ('a', 'b') THEN '第一' WHEN col_1 IN ('a') THEN '第二' ELSE '其他' END 註意: 統一各分支返回的數據類型. ...
  • 在C#編程世界中,語法的精妙之處往往體現在那些看似微小卻極具影響力的符號與結構之中。其中,“_ =” 這一組合突然出現還真不知道什麼意思。本文將深入剖析“_ =” 的含義、工作原理及其在實際編程中的廣泛應用,揭示其作為C#語法奇兵的重要角色。 一、下劃線 _:神秘的棄元符號 下劃線 _ 在C#中並非 ...