2.14.4. 安装和配置 HAProxy
在至少两个 HAProxy 节点上执行以下步骤:
安装
haproxy
。yum install haproxy
[root@haproxy]# yum install haproxy
Copy to Clipboard Copied! 为 SELinux 和 HTTP 配置
haproxy
。vim /etc/firewalld/services/haproxy-http.xml
[root@haproxy]# vim /etc/firewalld/services/haproxy-http.xml
Copy to Clipboard Copied! 添加以下行:
<?xml version="1.0" encoding="utf-8"?> <service> <short>HAProxy-HTTP</short> <description>HAProxy load-balancer</description> <port protocol="tcp" port="80"/> </service>
<?xml version="1.0" encoding="utf-8"?> <service> <short>HAProxy-HTTP</short> <description>HAProxy load-balancer</description> <port protocol="tcp" port="80"/> </service>
Copy to Clipboard Copied! 以
root
身份,为haproxy-http.xml
文件分配正确的 SELinux 上下文和文件权限。cd /etc/firewalld/services restorecon haproxy-http.xml chmod 640 haproxy-http.xml
[root@haproxy]# cd /etc/firewalld/services [root@haproxy]# restorecon haproxy-http.xml [root@haproxy]# chmod 640 haproxy-http.xml
Copy to Clipboard Copied! 如果要使用 HTTPS,请为 SELinux 和 HTTPS 配置
haproxy
。vim /etc/firewalld/services/haproxy-https.xml
[root@haproxy]# vim /etc/firewalld/services/haproxy-https.xml
Copy to Clipboard Copied! 添加以下行:
<?xml version="1.0" encoding="utf-8"?> <service> <short>HAProxy-HTTPS</short> <description>HAProxy load-balancer</description> <port protocol="tcp" port="443"/> </service>
<?xml version="1.0" encoding="utf-8"?> <service> <short>HAProxy-HTTPS</short> <description>HAProxy load-balancer</description> <port protocol="tcp" port="443"/> </service>
Copy to Clipboard Copied! 以
root
身份,为haproxy-https.xml
文件分配正确的 SELinux 上下文和文件权限。cd /etc/firewalld/services restorecon haproxy-https.xml chmod 640 haproxy-https.xml
# cd /etc/firewalld/services # restorecon haproxy-https.xml # chmod 640 haproxy-https.xml
Copy to Clipboard Copied! 如果您打算使用 HTTPS,请为 SSL 生成密钥。如果您没有证书,您可以使用自签名证书。要生成密钥,请参阅红帽企业 Linux 7 的《系统管理员指南》 中的 生成新密钥和证书一节。
最后,将证书和密钥放入 PEM 文件中。
cat example.com.crt example.com.key > example.com.pem cp example.com.pem /etc/ssl/private/
[root@haproxy]# cat example.com.crt example.com.key > example.com.pem [root@haproxy]# cp example.com.pem /etc/ssl/private/
Copy to Clipboard Copied! 配置
haproxy
。vim /etc/haproxy/haproxy.cfg
[root@haproxy]# vim /etc/haproxy/haproxy.cfg
Copy to Clipboard Copied! 全局
和默认值
可能保持不变。在defaults
部分后,您需要配置frontend
和backend
部分。例如:frontend http_web bind *:80 mode http default_backend rgw frontend rgw-https bind *:443 ssl crt /etc/ssl/private/example.com.pem default_backend rgw backend rgw balance roundrobin mode http server rgw1 10.0.0.71:80 check server rgw2 10.0.0.80:80 check
frontend http_web bind *:80 mode http default_backend rgw frontend rgw-https bind *:443 ssl crt /etc/ssl/private/example.com.pem default_backend rgw backend rgw balance roundrobin mode http server rgw1 10.0.0.71:80 check server rgw2 10.0.0.80:80 check
Copy to Clipboard Copied! 有关 HAProxy 配置的详细讨论,请参阅 HAProxy 配置。
enable/start
haproxy
systemctl enable haproxy systemctl start haproxy
[root@haproxy]# systemctl enable haproxy [root@haproxy]# systemctl start haproxy
Copy to Clipboard Copied!