Haproxy搭建web集群

该文章描述了在一个CentOS7.7环境中,如何配置Haproxy作为负载均衡器,以及在两台Web服务器(web01和web02)上安装和配置Nginx,展示不同的首页内容。Haproxy的配置包括日志记录、最大连接数设置、前端和后端服务器定义,并通过curl命令验证了负载均衡的效果。
摘要由CSDN通过智能技术生成

案例环境

主机IP地址系统
haproxy192.168.116.134/24CentOS7.7
web01192.168.116.135/24CentOS7.7
web02192.168.116.136/24CentOS7.7
1、基本环境配置

IP地址配置
主机名设置
关闭selinux

2、配置web网站

web1和web2安装nginx,并设置不同的首页内容(web02同样配置)

# 设置nginx yum源
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
    
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

#开启nginx
[root@wen01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@wen01 ~]# systemctl start nginx 
[root@wen01 ~]# systemctl status nginx 
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2023-03-28 15:22:02 CST; 5s ago
     Docs: http://nginx.org/en/docs/
  Process: 82087 ExecStop=/bin/sh -c /bin/kill -s TERM $(/bin/cat /var/run/nginx.pid) (code=exited, status=0/SUCCESS)
  Process: 82091 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 82092 (nginx)
   CGroup: /system.slice/nginx.service
           ├─82092 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─82093 nginx: worker process

328 15:22:02 wen01 systemd[1]: Stopped nginx - high performance web server.
328 15:22:02 wen01 systemd[1]: Starting nginx - high performance web server...
328 15:22:02 wen01 systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file ...ectory
328 15:22:02 wen01 systemd[1]: Started nginx - high performance web server.
Hint: Some lines were ellipsized, use -l to show in full.

#修改nginx首页内容并测试
[root@wen01 ~]# mv /sur/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
[root@wen01 ~]# vim /sur/share/nginx/html/index.html 
<h1> web01 test </h1>
[root@wen01 html]# curl http://192.168.116.135
<h1> web01 test </h1>
[root@web02 html]# curl http://192.168.116.136
<h1> web02 test </h1>
3、安装haproxy
[root@haproxy ~]# yum install haproxy -y

[root@haproxy ~]# haproxy -version
HA-Proxy version 1.5.18 2016/05/10
Copyright 2000-2016 Willy Tarreau <willy@haproxy.org>
4、配置rsyslog服务来接收haproxy的日志

修改rsyslog配置文件

[root@haproxy ~]# vim /etc/rsyslog.conf
# Provides UDP syslog reception  #开启UDP日志接收,使用514端口
$ModLoad imudp
$UDPServerRun 514

#添加日志规则
local2.*                       /var/log/haproxy.log

重启rsyslog服务

[root@haproxy ~]# systemctl restart rsyslog.service
5、配置haproxy
global
    log         127.0.0.1:514  local2 info

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000   #最大连接数,根据应用实际情况进行调整,推荐使用10240
    user        haproxy
    group       haproxy
    daemon   #以后台形式运行haproxy


defaults
    mode                    http   #工作模式,所处理的类别,默认采用http模式,可配置成tcp作4层消息转发
    log                     global
    option                  httplog
    option                  dontlognull
    timeout connect         10s
    timeout client          1m
    timeout server          1m

frontend  http_front
    bind  192.168.116.134:80
    stats uri /haproxy?stats
    default_backend http_back
backend http_back
    balance     roundrobin
    server web01 192.168.116.135:80  check
    server web02 192.168.116.136:80  check

启动haproxy服务

[root@haproxy ~]# systemctl enable haproxy.service 
[root@haproxy ~]# systemctl start haproxy.service 

#放行80端口在web01、web02上面也需要配置,或者是关闭防火墙
[root@haproxy ~]# firewall-cmd --add-port=80/tcp --permanent 
success
[root@haproxy ~]# firewall-cmd --reload 
success
[root@haproxy ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client ssh
  ports: 80/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
6、访问测试
[root@haproxy ~]# curl http://192.168.116.134
<h1> web01 test </h1>
[root@haproxy ~]# curl http://192.168.116.134
<h1> web02 test </h1>
[root@haproxy ~]# curl http://192.168.116.134
<h1> web01 test </h1>
[root@haproxy ~]# curl http://192.168.116.134
<h1> web02 test </h1>

访问haproxy页面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

llc_刘立超

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

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

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

打赏作者

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

抵扣说明:

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

余额充值