阅读 98

HAProxy搭建HTTP负载均衡

HAProxy搭建HTTP负载均衡

环境说明:

主机名称 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

安装HTTPD服务

//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                   [::]:* 

client访问测试

//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

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐