Haproxy搭建http负载均衡

Haproxy搭建http负载均衡

#环境部署

主机名称IP地址需要安装的应用系统版本
client192.168.188.132redhat 8.5
LB192.168.188.128haproxyredhat 8.5
RS1192.168.188.129httpdredhat 8.5
RS2192.168.188.137httpdredhat 8.5

#LB、RS1、RS2都关闭防火墙和selinux

[root@LB ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@LB ~]# setenforce 0
[root@LB ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

//RS1、RS2重复以上操作

#RS1和RS2部署httpd

RS1
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# echo RS1 > /var/www/html/index.html
[root@RS1 ~]# systemctl restart httpd
[root@RS1 ~]# systemctl enable httpd

RS2
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# echo RS2 > /var/www/html/index.html
[root@RS2 ~]# systemctl restart httpd
[root@RS2 ~]# systemctl enable httpd

#修改LB的内核参数

[root@LB ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf
[root@LB ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@LB ~]# sysctl  -p 
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

#修改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.188.129:80
    server web02 192.168.188.137:80

#启动haproxy服务

如果想要配置systemd管理haproxy服务,在我这篇文章最后有写到HAProxy

如果没有使用systemd管理,则执行
[root@LB ~]# haproxy -f /etc/haproxy/haproxy.cfg -c

如果配置systemd管理haproxy服务
[root@LB ~]# systemctl restart haproxy.service

#客户端验证

[root@client ~]# curl 192.168.188.128
RS1
[root@client ~]# curl 192.168.188.128
RS2
[root@client ~]# curl 192.168.188.128
RS1
[root@client ~]# curl 192.168.188.128
RS2

#使用WEB网页访问测试

[root@LB ~]# vim /etc/haproxy/haproxy.cfg
global
    log 127.0.0.1 local0  info
    #log loghost local0 info
    maxconn 20480
#chroot /usr/local/haproxy
    pidfile /var/run/haproxy.pid
    #maxconn 4000
    user haproxy
    group haproxy
    daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode http
    log global
    option dontlognull
    option httpclose
    option httplog
    #option forwardfor
    option redispatch
    balance roundrobin
    timeout connect 10s
    timeout client 10s
    timeout server 10s
    timeout check 10s
    maxconn 60000
    retries 3
#--------------统计页面配置------------------
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /haproxy_stats            //访问网页后缀URL
    stats realm Haproxy\ Statistics
    stats auth admin:admin              //用户名和密码
    #stats hide-version
    stats admin if TRUE
    stats refresh 30s
#---------------web设置-----------------------
listen webcluster
    bind 0.0.0.0:80
    mode http
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.188.129:80 check inter 2000 fall 5
    server web02 192.168.188.137:80 check inter 2000 fall 5

#重启服务

[root@LB ~]# systemctl restart haproxy.service
[root@LB ~]# ss -antl
State       Recv-Q      Send-Q           Local Address:Port           Peer Address:Port     Process
LISTEN      0           128                    0.0.0.0:8189                0.0.0.0:*
LISTEN      0           128                    0.0.0.0:80                  0.0.0.0:*
LISTEN      0           128                    0.0.0.0:22                  0.0.0.0:*
LISTEN      0           128                       [::]:22                     [::]:*

#网页访问测试

访问ip格式如下图框起来的一样,用户名和密码都为admin

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随便投投

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值