如何在 Ubuntu 16.04 LTS 上安装 HAProxy

在本教程中,我们将向您展示如何在 Ubuntu 16.04 LTS 上安装和配置 HAProxy。 对于那些不知道的人,HAProxy 是一个免费的 HTTP/TCP 高可用性负载平衡器和代理服务器。 它将请求分布在多个服务器之间,以减轻由单个服务器故障引起的问题。 HA Proxy 被许多知名网站使用,包括 GitHub、Bitbucket、Stack Overflow、Reddit、Tumblr、Twitter 和 Tuenti,并用于 Amazon Web Services 的 OpsWorks 产品中。

本文假设您至少具备 Linux 的基本知识,知道如何使用 shell,最重要的是,您将网站托管在自己的 VPS 上。 安装非常简单,假设您在 root 帐户下运行,如果不是,您可能需要添加 ‘sudo‘ 到命令以获取 root 权限。 我将向您展示在 Ubuntu 16.04 (Xenial Xerus) 服务器上逐步安装 HAProxy。

在 Ubuntu 16.04 LTS 上安装 HAProxy

步骤 1. 首先,通过运行以下命令确保所有系统包都是最新的 apt-get 终端中的命令。

sudo apt-get update
sudo apt-get upgrade

步骤 2. 网络详细信息。

下面是我们的网络服务器。 有 3 个使用 Apache2 运行并在端口 80 上侦听的 Web 服务器和一个 HAProxy 服务器:

Web Server Details:
  Server 1:    web1.idroot.us     192.168.1.101
  Server 2:    web2.idroot.us     192.168.1.102
  Server 3:    web3.idroot.us     192.168.1.103
HAProxy Server: 
  HAProxy:     haproxy              192.168.1.16

步骤 3. 安装 HAProxy。

现在使用以下命令安装 HAProxy:

apt-get -y install haproxy

安装后,您可以使用以下命令仔细检查已安装的版本号:

haproxy -v

步骤 4. 配置 HAProxy。

我们必须修改HAProxy的配置文件,即 /etc/haproxy/haproxy.cfg 根据我们的要求。 (根据您的网络要求更改此配置)。 有关更多配置详细信息,请检查此 网址.

### nano /etc/haproxy/haproxy.cfg

global
    log /dev/log    local0
    log /dev/log    local1 notice
    chroot /var/lib/haproxy
    stats socket /run/haproxy/admin.sock mode 660 level admin
    stats timeout 30s
    user haproxy
    group haproxy
    daemon

    # Default SSL material locations
    ca-base /etc/ssl/certs
    crt-base /etc/ssl/private

    # Default ciphers to use on SSL-enabled listening sockets.
    # For more information, see ciphers(1SSL). This list is from:
    #  https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
    ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256::RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
    ssl-default-bind-options no-sslv3

defaults
    log    global
    mode    http
    option    httplog
    option    dontlognull
    timeout connect 5000
    timeout client  50000
    timeout server  50000
    errorfile 400 /etc/haproxy/errors/400.http
    errorfile 403 /etc/haproxy/errors/403.http
    errorfile 408 /etc/haproxy/errors/408.http
    errorfile 500 /etc/haproxy/errors/500.http
    errorfile 502 /etc/haproxy/errors/502.http
    errorfile 503 /etc/haproxy/errors/503.http
    errorfile 504 /etc/haproxy/errors/504.http

frontend Local_Server
    bind 192.168.1.16:80
    mode http
    default_backend My_Web_Servers

backend My_Web_Servers
    mode http
    balance roundrobin
    option forwardfor
    http-request set-header X-Forwarded-Port %[dst_port]
    http-request add-header X-Forwarded-Proto https if { ssl_fc }
    option httpchk HEAD / HTTP/1.1rnHost:localhost
    server web1.idroot.us  192.168.1.101:80
    server web2.idroot.us  192.168.1.102:80
    server web3.idroot.us  192.168.1.103:80

listen stats *:1936
    stats enable
    stats hide-version
    stats refresh 30s
    stats show-node
    stats auth username:password
    stats uri  /stats

由于您已完成代理服务器的所有必要配置,请在使用以下命令重新启动服务之前验证配置文件:

haproxy -c -f /etc/haproxy/haproxy.cfg

如果上述命令返回“配置文件有效”的输出,则重启 HAProxy 服务:

systemctl restart haproxy

步骤 5. 访问 HAProxy。

打开您喜欢的浏览器并在 Web 浏览器中访问 IP 192.168.0.16(如上配置)上的端口 80,然后点击刷新。 您将看到 HAProxy 正在向后端服务器一一发送请求(根据循环算法)。

恭喜! 您已成功安装 HAProxy。 感谢您使用本教程在 Ubuntu 16.04 LTS (Xenial Xerus) 系统上安装 HAProxy 负载平衡。 如需其他帮助或有用信息,我们建议您查看 HAProxy 官方网站.

Save

Save