# HAProxy

#### Offical Sites

- Home: [http://www.haproxy.org/](http://www.haproxy.org/)
- Forum: [https://discourse.haproxy.org/](https://discourse.haproxy.org/)

#### Installation

- [Install HAProxy on Debian or Ubuntu](https://haproxy.debian.net/)
- [Run HAProxy as a Docker container](https://hub.docker.com/r/haproxytech/haproxy-ubuntu)

```shell
# RedHat 8.3
yum install haproxy
```

#### Setting Up HAProxy Logging

The default configuration points to the **localhost (127.0.0.1)** and **local2** is the default facility code used to identify HAProxy log messages under **rsyslog**.

/etc/rsyslog.d/haproxy.conf

```
$ModLoad imudp
$UDPServerAddress 127.0.0.1
$UDPServerRun 514
local2.*        /var/log/haproxy/haproxy-traffic.log
local2.notice   /var/log/haproxy/haproxy-admin.log
```

Create the directories required

```shell
mkdir /var/log/haproxy
```

Restart the rsyslog

```
systemctl restart rsyslog
```

#### Configuring HAProxy

- [HAProxy Configuration Basics: Load Balance Your Servers](https://www.haproxy.com/blog/haproxy-configuration-basics-load-balance-your-servers)

Edit the file `/etc/haproxy/haproxy.cfg` .

HAProxy Stats page

```
listen stats
    bind *:9000
    stats enable
    stats hide-version
    stats uri /stats
    stats admin if LOCALHOST
    stats auth haproxy:Lostp@1ss

```

AP Servers

```
listen apservers_20013
    bind *:20013
    mode tcp
    timeout client  10800s
    timeout server  10800s
    balance roundrobin
    server tpemimsaw00 10.10.2.33:20013 check
    server tpemimsaw01 10.10.2.32:20013 check
```

Restart the haproxy

```
systemctl restart haproxy
```

#### Setup Logrotate

/etc/logrotate.d/haproxy

```
/var/log/haproxy/haproxy*.log {
    daily
    rotate 10
    missingok
    notifempty
    compress
    dateext
    size 10M
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
        /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}
```

#### Advanced Configurations

##### balance

- roundrobin  
    每個 server 依據權值 weight 依序選擇 servers，這是最快的 algorithm，server weight 會根據 instance slow start 的狀況而隨時變動。這個 algorithm 在每個 backend 最多可處理 4095 個 active server nodes。如果有個 server node 在 down 之後馬上又變成 active，可能會需要數百個 requests 之後，才會再 join 這個 server farm。
- static-rr  
    每個 server 根據 weight 依序選擇 server，類似 roundrobin，但權值 weight 是固定的。沒有 active server node 的處理數量上限。
- leastconn  
    目前處理最少連線數量的 server，作為下一個 connection 轉向的 server。建議在較長 tcp session 的服務中使用這個演算法，例如 LDAP, SQL, etc...，不適合用在類似 HTTP 這種短 sesion time 的 protocol。
- first  
    server nodes 之中，選擇第一個可使用的 server，當這個 server 連線處理數量達到 maxconn 時，就會選擇下一個 server，所以設定檔中一定要有 maxconn 設定值。
- source  
    source IP address 會 hashed 並除以 total weight of running serer，用這個結果來決定要選擇那一個 server，這可確保同一個 client IP 的 traffic 會連到同一個 server。如果 server nodes 數量改變了，就會因為重新計算 hash，造成 clients 連線到不同的 servers。
- uri  
    可用 uri 的 ? 前面的部份，或是整個 uri 進行 hash，可確保同一個 uri 會轉給同一個 server。這個設定通常用在 proxy caches 與 anti-virus proxyies，用以提高 cache hit rate。  
      
    有兩個 optinal parameter: len 與 depth。len 就是使用 uri 裡面幾個字元，但因為 uri 通常是 / 開頭，所以不要將 len 設定為 1。

- depth 表示計算 hash 的 maxmimum directory depth，出現一個 / 代表多了一層 depth。
- url\_param  
    使用 HTTP Get request URL 裡面的參數，如果用到了 "check\_post"，就會使用 POST request 裡面的參數。
- hdr(name)  
    會尋找名稱為 name 的 HTTP header 欄位。
- optonal 參數 "use\_domain\_only" 可用在指定 header 為 Host 的時候，如果 Host 為 "haproxy.1wt.eu" 就只會考慮 "1wt"
- rdp-cookie, rdp-cookie(name)  
    尋找設定的 cookie，通常就是 session ID，如果沒有 session ID 就會使用 roundrobin algorithm。

#### SSL Configuration

- [How to Configure a CA SSL Certificate in HAProxy](https://www.tecmint.com/configure-ssl-certificate-haproxy/)

#### Restart or Reload

[![haproxy_reload.jpg](https://osslab.tw/uploads/images/gallery/2023-11/scaled-1680-/haproxy-reload.jpg)](https://osslab.tw/uploads/images/gallery/2023-11/haproxy-reload.jpg)

#### Learning

- [<span style="color: #e8eaed; font-family: Roboto, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #202124; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">How to install and configure HAProxy on RHEL 7</span>](https://www.loadbalancer.org/blog/how-to-install-haproxy-rhel/)
- [<span style="color: #e8eaed; font-family: Roboto, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #202124; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">How to install HAProxy load balancer on CentOS</span>](https://upcloud.com/community/tutorials/haproxy-load-balancer-centos/)
- [<span style="color: #e8eaed; font-family: Roboto, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #202124; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">Install HAProxy Load Balancer on CentOS / RHEL 8</span>](https://www.centlinux.com/2021/02/how-to-install-haproxy-load-balancer-on-rhel-8.html)
- [<span style="color: #e8eaed; font-family: Roboto, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #202124; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">How to Setup HAProxy as Load Balancer for Nginx on CentOS 8</span>](https://www.tecmint.com/setup-nginx-haproxy-load-balancer-in-centos-8/)
- [<span style="color: #e8eaed; font-family: Roboto, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #202124; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">How to Install HAProxy on Debian 11</span>](https://www.howtoforge.com/how-to-install-haproxy-on-debian-11/)
- [<span style="color: #e8eaed; font-family: Roboto, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #202124; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">MySQL Load Balancing with HAProxy</span>](https://severalnines.com/resources/database-management-tutorials/mysql-load-balancing-haproxy-tutorial)
- [<span style="color: #e8eaed; font-family: Roboto, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #202124; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">High-Availability MySQL cluster with load balancing using HAProxy and Heartbeat.</span>](https://towardsdatascience.com/high-availability-mysql-cluster-with-load-balancing-using-haproxy-and-heartbeat-40a16e134691)
- [<span style="color: #e8eaed; font-family: Roboto, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: none; font-variant-caps: normal; font-weight: 400; letter-spacing: 0.1px; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: #202124; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">PostgreSQL HAProxy: Proxy for HA and Load Balance</span>](https://www.alibabacloud.com/blog/postgresql-haproxy-proxy-for-ha-and-load-balance_597618)
- [進階設定範例](https://blog.maxkit.com.tw/2015/03/haproxy.html)
- [How to Run HAProxy with Docker](https://www.haproxy.com/blog/how-to-run-haproxy-with-docker/)