主机名称 | IP地址 | 需要安装的应用 | 系统版本 |
---|---|---|---|
client | 192.168.110.60 | 无 | redhat 8.2 |
LB | 192.168.110.11 | haproxy | redhat 8.2 |
RS1 | 192.168.110.12 | httpd | redhat 8.2 |
RS2 | 192.168.110.13 | httpd | redhat 8.2 |
//关闭防火墙,selinux
#LB
[root@LB ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@LB ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@LB ~]# setenforce 0
#RS1
[root@RS1 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@RS1 ~]# setenforce 0
#RS2
[root@RS2 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS2 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@RS2 ~]# setenforce 0
详细安装步骤请见:HAProxy安装与配置
//LB
#添加最后两行
[root@LB ~]# cat /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
[root@LB ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
//RS1
#安装httpd
[root@RS1 ~]# yum -y install httpd
#设置开机自启
[root@RS1 ~]# systemctl enable --now httpd
#添加测试网页
[root@RS1 ~]# echo RS1 > /var/www/html/index.html
//RS2
#安装httpd
[root@RS2 ~]# yum -y install httpd
#设置开机自启
[root@RS2 ~]# systemctl enable --now httpd
#添加测试网页
[root@RS2 ~]# echo RS1 > /var/www/html/index.html
//LB
#创建目录
[root@LB ~]# mkdir /etc/haproxy
#配置文件
[root@LB ~]# vim /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.110.12:80
server web02 192.168.110.13:80
#测试文件
[root@LB ~]# haproxy -f /etc/haproxy/haproxy.cfg -c
Configuration file is valid
//LB
方式一:
#通过文件的方式直接启动
[root@LB ~]# haproxy -f /etc/haproxy/haproxy.cfg
[root@LB ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
方式二:
#编辑service文件,以守护进程的方式启动
[root@LB ~]# vim /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@LB ~]# systemctl daemon-reload
[root@LB ~]# systemctl enable --now haproxy
[root@LB ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//cleent
#成功访问
[root@localhost ~]# curl 192.168.110.11
RS1
[root@localhost ~]# curl 192.168.110.11
RS2
[root@localhost ~]# curl 192.168.110.11
RS1
[root@localhost ~]# curl 192.168.110.11
RS2
原文:https://www.cnblogs.com/leixixi/p/14749626.html