Haproxy搭建Web群集

案例环境

主机操作系统IP地址主要软件
Haproxy服务器CentOS 7.9 x86_64192.168.10.101haproxy-1.5.19.tar.gz
web服务器1CentOS 7.9 x86_64192.168.10.102httpd
web服务器2CentOS 7.9 x86_64192.168.10.103httpd
客户端CentOS 7.9 x86_64192.168.10.101

案例实施

1.搭建两台web服务器

服务器web1

[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# echo "web01">/var/www/html/index.html
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

服务器web2

[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# echo "web02">/var/www/html/index.html
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

2.Haproxy服务器配置

1.编译安装Haproxy

下载Haproxy源码包

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc*
[root@localhost ~]# tar zxvf haproxy-1.5.19.tar.gz 
[root@localhost ~]# cd haproxy-1.5.19
[root@localhost haproxy-1.5.19]# make TARGET=linux26
[root@localhost haproxy-1.5.19]# make install

2.配置Haproxy服务器

[root@localhost haproxy-1.5.19]# cd examples/
[root@localhost examples]# cp haproxy.cfg /etc/haproxy/
[root@localhost examples]# cp haproxy.init /etc/init.d/haproxy
[root@localhost examples]# chmod +x /etc/init.d/haproxy
[root@localhost examples]# chkconfig --add haproxy

3.Haproxy配置介绍

[root@localhost haproxy-1.5.19]# vi /etc/haproxy/haproxy.cfg 
global
        log 127.0.0.1   local0     //配置日志记录,local0为日志设备,默认是系统日志
        log 127.0.0.1   local1 notice      //日志级别为notice
        #log loghost    local0 info    
        maxconn 4096       //最大连接数
        uid 99           //用户uid
        gid 99           //用户gid
        daemon        //以守护进程的方式运行
        #debug      //调试模式,输出启动信息到标准输出
        #quiet     //安静模式,启动时无输出

defaults
        log     global           //使用globle中定义的日志
        mode    http           //模式为http
        option  httplog         //采用http的格式记录日志
        option  dontlognull     //保证HAProxy不记录上级负载均衡发送过来的用于检测状态数据的心跳包
        retries 3             //检查节点连接失败的次数,超过3次认为节点不可用
       # redispatch         //当负载很高时,自动结束当前队列处理比较久的连接
        maxconn 2000      //最大连接数
        contimeout      5000         //连接超时时间ms
        clitimeout      50000        //客户端超时时间ms
        srvtimeout      50000        //服务器超时时间ms
listen  webcluster 0.0.0.0:80        //定义群集和监听的端口号
        option  httpchk GET /index.html    //检查服务器的index.html文件,心跳检测URL设置
        balance roundrobin      //负载均衡的调度算法为轮询
        server  inst1 192.168.1.61:80 check inter 2000 fall 3        //定义在线节点
        server  inst2 192.168.1.62:80 check inter 2000 fall 3          
//check inter 2000是检测心跳频率(每2000ms检测一次),fall 3是3次失败认为服务器不可用

4.启动

[root@localhost haproxy-1.5.19]# /etc/init.d/haproxy start

5.测试web群集

[root@localhost ~]# for i in $(seq 6); do curl http://192.168.1.60/test.html  ;done
web02
web02
web01
web02
web02
web01

6.Haproxy日志

(1)编辑/etc/haproxy/haproxy.conf

[root@localhost ~]# vi /etc/haproxy/haproxy.cfg
global
 log 127.0.0.1 local3    //local3是设备,对应于 /etc/rsyslog.conf中的配置,默认回收info的日志级别

(2)编写haproxy日志文件

[root@localhost ~]# vim  /etc/rsyslog.d/haproxy.conf
$ModLoad imudp        //采集日志的协议UDP
$UDPServerRun 514        //指定日志采集使用的端口号
local3.*     /var/log/haproxy.log        // 指定日志存放位置

(3)重启haproxy和rsyslog服务

[root@localhost ~]# systemctl restart rsyslog
[root@localhost ~]# systemctl restart haproxy

(4)访问网站后查看日志

[root@localhost ~]# cat /var/log/haproxy.log
Jul  8 11:52:15 localhost haproxy[2050]: Stopping proxy myweb in 0 ms.
Jul  8 11:52:15 localhost haproxy[2050]: Proxy myweb stopped (FE: 0 conns, BE: 0 conns).
Jul  8 11:52:15 localhost haproxy[2104]: Proxy myweb started.
Jul  8 11:52:19 localhost haproxy[2105]: 192.168.10.104:49944 [08/Jul/2024:11:52:19.615] myweb myweb/inst2 0/0/0/1/1 200 245 - - ---- 1/1/0/1/0 0/0 "GET / HTTP/1.1"

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值