如何在 CentOS 8 / RHEL 8 上安装和配置 HAProxy

[ad_1]

代理服务器 代表高可用性代理,它是一个免费和开源的负载平衡器工具,可以平衡传入的流量(TCP基于 HTTP) 通过使用不同的标准分布在后端服务器上。

换句话说,我们可以说 HAProxy 用于在一个节点收到太多并发请求时提供容错和高可用性。 GitHub、Stack Overflow 和 Tumbler 等最著名的网站都使用它。

在本文中,我们将讨论如何在 CentOS 8/RHEL 8 上为 Nginx Web 服务器安装和配置 HAProxy。 以下是我的 haproxy 实验室设置的详细信息,

  • HAProxy 服务器 – 192.168.1.10 (haproxy-centos8)
  • Nginx 服务器 1 – 192.168.1.11 (nginx-node01)
  • Nginx 服务器 2 – 192.168.1.12 (nginx-node01)

让我们进入在 CentOS 8 / RHEL 8 上安装和配置 HAProxy 的步骤

步骤:1) 更新您的 HAProxy 服务器的 /etc/hosts 文件

登录到您将安装 haproxy 的 CentOS 8 或 RHEL 8 系统,在 /etc/hosts 文件中添加以下行,

192.168.1.10    haproxy-centos8
192.168.1.11    nginx-node01
192.168.1.12    nginx-node02

更新 hosts 文件后,确保您能够 ping Nginx 节点,

步骤:2) 在 CentOS 8 / RHEL 8 上安装和配置 HAProxy

Haproxy 软件包在 CentOS 8 和 RHEL 8 的默认软件包存储库中可用,因此可以使用 dnf 命令轻松安装。 但建议在安装 haproxy 之前更新您的系统。 所以执行以下命令,

[email protected] ~]# dnf update -y
[email protected] ~]# reboot

现在使用以下 dnf 命令 安装haproxy,

[[email protected] ~]# dnf install haproxy

成功安装haproxy后,通过编辑其配置文件“/etc/haproxy/haproxy.cfg“。

在编辑文件之前,最好先备份原始文件,运行以下命令,

[[email protected] ~]# cd /etc/haproxy/
[[email protected] haproxy]# cp haproxy.cfg haproxy.cfg-org
[[email protected] haproxy]#

在配置文件中,我们将修改两个部分 前端后端. 在前端部分,我们定义了 haproxy IP 及其端口、统计信息 uri 和后端名称。 在后端部分,我们定义了我们将使用的负载平衡算法的类型,如循环和最少连接等以及后端服务器的名称、IP 和端口,示例如下所示,

[[email protected] haproxy]# vi haproxy.cfg
…………………
frontend http_balancer
    bind 192.168.1.10:80
    option http-server-close
    option forwardfor
    stats uri /haproxy?stats

#    acl url_static       path_beg       -i /static /images /javascript /stylesheets
#    acl url_static       path_end       -i .jpg .gif .png .css .js
#    use_backend static          if url_static
    default_backend     nginx_webservers

backend nginx_webservers
    mode        http
    balance     roundrobin
    option httpchk HEAD / HTTP/1.1rnHost: localhost    
    server  nginx-node01  192.168.1.11:80  check
    server  nginx-node02  192.168.1.12:80  check
 ………………………………

Save 并退出文件

配置 rsyslog 以便它存储所有 HAProxy 统计信息,编辑 rsyslog 配置文件“/etc/rsyslog.conf”并取消注释第 19 和 20 行,

[[email protected] ~]# vi /etc/rsyslog.conf
……
module(load="imudp")
input(type="imudp" port="514")
……

Save 并退出文件。

现在为 rsyslog 创建 haproxy.conf 文件,粘贴以下几行,

[[email protected] ~]# vi /etc/rsyslog.d/haproxy.conf
local2.=info     /var/log/haproxy-access.log
local2.notice    /var/log/haproxy-info.log

保存并退出文件

重新启动并启用 rsyslog 服务,以便它在重新启动后可用

[[email protected] ~]# systemctl restart rsyslog
[[email protected] ~]# systemctl enable rsyslog

现在终于启动 haproxy 但在启动 haproxy 服务之前,设置以下 selinux 规则,

[[email protected] ~]# setsebool -P haproxy_connect_any 1

使用 systemctl 命令启动和启用 haproxy

[[email protected] ~]# systemctl start haproxy
[[email protected] ~]# systemctl enable haproxy

允许操作系统防火墙中的 haproxy 端口(在我们的例子中是 80),执行下面的 firewall-cmd 命令,

[[email protected] ~]# firewall-cmd --permanent --add-port=80/tcp
[[email protected] ~]# firewall-cmd --reload

至此HAProxy的安装和配置部分完成,让我们移动到Nginx节点,

步骤:3)安装NGINX并启动其服务

登录到两个 nginx 节点,安装 nginx 并使用以下命令启动其服务。

# dnf install nginx -y
# systemctl start nginx
# systemctl enable nginx

让我们修改各个节点的 index.html 文件,

对于 nginx-node01

[[email protected] ~]# cd /usr/share/nginx/html
[[email protected] html]# echo "Nginx Node01 - Welcome to First Nginx Web Server" > index.html

对于 nginx-node02

[[email protected] ~]# cd /usr/share/nginx/html
[[email protected] html]# echo "Nginx Node02 - Welcome to 2nd Nginx Web Server" > index.html

使用以下命令在两个节点的操作系统防火墙中允许 Nginx 端口 (80),

# firewall-cmd --permanent --add-service=http
# firewall-cmd --reload

步骤:4)测试您的 HAProxy 是否正常工作

登录到 haproxy 服务器并运行 curl 命令几次以查看流量​​是否以循环方式分布

[[email protected] ~]# curl 192.168.1.10

完美,这证实了 haproxy 工作正常,因为它在两个节点之间分配流量,

让我们也从网络浏览器验证,

以上确认 HAProxy 已在 CentOS 8 和 RHEL 8 上成功配置。

您可以通过网络浏览器查看 haproxy 的状态,输入 url : https:///haproxy?stats

在我们的例子中,URL 将是 https://192.168.1.10/haproxy?stats

我们还可以从日志文件( /var/log/haproxy-access.log)。

这就是本教程的全部内容,希望这些步骤可以帮助您在 CentOS 8 和 RHEL 8 上顺利设置 HAProxy。 请分享您宝贵的反馈和意见。

还阅读如何在 CentOS 8 上设置 Django Python 框架

[ad_2]

Related Posts