Haproxy负载均衡配置

本文详细介绍了如何使用Haproxy实现Web负载均衡。首先分别在192.168.100.42和192.168.100.43两台服务器上编译安装Nginx,接着在192.168.100.41服务器上编译安装Haproxy并进行配置。经过测试,通过访问192.168.100.41可以实现网站负载均衡,同时可以通过Haproxy的日志观察系统运行状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Haproxy搭建Web群集

场景介绍:

     主机           操作系统                 IP地址                         主要软件
Haproxy服务器       CentoS7.6             192.168.100.41                haproxy-1.5.19.tar.gz
Nginx服务器1        CentoS7.6             192.168.100.42                Nginx-1.12.2.tar.gz
Nginx服务器2        CentoS7.6             192.168.100.43                Nginx-1.12.2.tar.gz

编译安装Nginx服务器1 192.168.100.42

1、编译安装 Nginx

Nginx 安装文件可以从官方网站 http://www.nginx.org/下载。
下面以稳定版 Nginx 1.12.2为例 上传至/opt下

[root@localhost ~]#yum -y install pcre-devel zlib-devel gcc-c++
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# cd /opt
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.2
[root@localhost nginx-1.12.2]# 
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx

[root@localhost nginx-1.12.2]# make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 516 16:50 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

2.添加 Nginx 系统服务

[root@localhost ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx                                                 ####描述
After=network.target                                            ####描述服务类别
[Service]
Type=forking                                                          ####后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid                ####PID 文件位置
ExecStart=/usr/local/nginx/sbin/nginx                  ####启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID         ####根据 PID 重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID           ####根据 PID 终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost ~]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to
/usr/lib/systemd/system/nginx.service.

这样一来, 就可以 systemctl 命令来启动、 停止、 重启、 重载 Nginx 服务器了, 方法是
在执行时添加相应的 start、 stop、 restart、 reload 参数

3.编辑测试web页面

[root@localhost ~]# echo "this is 192.168.100.42" > /usr/local/nginx/html/index.html

此时可以用浏览器访问192.168.100.42,查看页面

编译安装Nginx服务器1 192.168.100.43

和上面步骤一样,最后一步稍做修改以作区分

[root@localhost ~]# echo "this is 192.168.100.43" > /usr/local/nginx/html/index.html

配置Haproxy 服务器 192.168.100.41

1、编译安装 Haproxy

上传 haproxy-1.4.24.tar.gz 到/opt目录下

[root@localhost ~]# yum -y install pcre-devel bzip2-devel gcc gcc-c++
[root@localhost ~]# cd /opt
[root@localhost opt]# tar xzvf haproxy-1.4.24.tar.gz 
[root@localhost opt]# cd haproxy-1.4.24/
[root@localhost haproxy-1.4.24]# make TARGET=linux26
[root@localhost haproxy-1.4.24]# make install

2、配置Haproxy 服务

[root@localhost haproxy-1.4.24]# mkdir /etc/haproxy
[root@localhost haproxy-1.4.24]# cp examples/haproxy.cfg /etc/haproxy/
[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg 
global															#全局配置
        log 127.0.0.1   local0							#配置日志记录,配置日志记录,local0为日志设备,默认存放到系统日志
        log 127.0.0.1   local1 notice				#notice为日志级别,通常有24个级别
        #log loghost    local0 info				
        maxconn 4096									#最大连接数
        #chroot /usr/share/haproxy
        uid 99
        gid 99
        daemon
        #debug
        #quiet

defaults														#默认配置
        log     global											#定义日志为global配置中的日志定义
        mode    http											#模式为http
        option  httplog										#采用http日志格式记录日志
        option  dontlognull								#保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包
        retries 3												#检查节点服务器失败连续达到三次则认为节点不可用
        #redispatch
        maxconn 2000									#最大连接数
        contimeout      5000							#连接超时时间
        clitimeout      50000								#客户端超时时间
        srvtimeout      50000							#服务器超时时间

listen  webcluster 0.0.0.0:80						#应用组件配置
        option httpchk GET /index.html			#检查服务器的index.html文件
        balance roundrobin								#轮询
        server inst1 192.168.100.42:80 check inter 2000 fall 3			#定义在线节点
        server inst2 192.168.100.43:80 check inter 2000 fall 3			#定义在线节点


[root@localhost haproxy-1.4.24]# cp examples/haproxy.init /etc/init.d/haproxy
[root@localhost haproxy-1.4.24]# chmod 755 /etc/init.d/haproxy
[root@localhost haproxy-1.4.24]# chkconfig --add haproxy
[root@localhost haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy
[root@localhost haproxy-1.4.24]# service haproxy start

测试网站负载均衡

192.168.100.41 切换 会发现不同的网站页面 说明已经实现了负载均衡

Haproxy日志

[root@localhost haproxy-1.4.24]# vi /etc/haproxy/haproxy.cfg
global                       ####下面log开头的 统统去掉换下面的配置
        log /dev/log    local0 info
        log /dev/log    local1 notice
[root@localhost haproxy-1.4.24]# systemctl restart haproxy.service        
[root@localhost haproxy-1.4.24]# touch /etc/rsyslog.d/haproxy.conf
[root@localhost haproxy-1.4.24]# vi /etc/rsyslog.d/haproxy.conf 
if ($programname == 'haproxy' and $syslogseverity-text == 'info') then -/var/log/haproxy/haproxy-info.log
& ~
if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log
& ~
[root@localhost haproxy-1.4.24]#  systemctl restart rsyslog.service 	#linux系统自带的日志管理系统
[root@localhost haproxy-1.4.24]# tail -f /var/log/haproxy/haproxy-info.log 
Nov  1 10:32:30 promote haproxy[1304]: 192.168.100.1:64352 [01/Nov/2020:10:32:07.102] webcluster webcluster/inst2 0/0/0/0/23080 200 1097 - - ---- 1/1/1/0/0 0/0 "GET / HTTP/1.1"
Nov  1 10:32:55 promote haproxy[1304]: 192.168.100.1:64340 [01/Nov/2020:10:32:03.269] webcluster webcluster/inst1 0/0/1/0/52445 404 3630 - - cD-- 2/2/1/1/0 0/0 "GET /favicon.ico HTTP/1.1"
Nov  1 10:33:10 promote haproxy[1304]: 192.168.100.1:64368 [01/Nov/2020:10:32:54.813] webcluster webcluster/inst1 1/0/0/1/15527 200 965 - - ---- 0/0/0/0/0 0/0 "GET / HTTP/1.1"

再次访问192.168.100.41,可以实时观察到日志系统更新的内容

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值