欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

haproxy负载均衡部署

程序员文章站 2024-02-01 08:47:10
...

简介

HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。

HAProxy是一个开源的、高性能的、基于TCP(第四层)和HTTP(第七层)应用的负载均衡软件,借助HAProxy可以快速、可靠的提供基于TCP和HTTP应用的负载均衡解决方案

环境

名称 主机名 IP
web服务端 wyt1 192.168.153.20
httpd服务端 wyt2 192.168.153.22
httpd服务端 wyt3 192.168.153.25

1.服务端安装httpd并启动服务

[aaa@qq.com ~]# cd /var/www/html/
[aaa@qq.com html]# echo 'node01' > index.html
[aaa@qq.com html]# systemctl enable --now httpd
[aaa@qq.com ~]# cd /var/www/html/
[aaa@qq.com html]# echo 'node02' > index.html
[aaa@qq.com html]# systemctl enable --now httpd

2.安装依赖包

[aaa@qq.com ~]# yum -y install make  zlib-devel gcc gcc-c++ pcre-devel bzip2-devel openssl-devel systemd-devel

3.创建用户和组

[aaa@qq.com ~]# useradd -r -M -s /sbin/nologin haproxy
[aaa@qq.com ~]# id haproxy
uid=996(haproxy) gid=994(haproxy) 组=994(haproxy)

4.下载安装包并编译安装

[aaa@qq.com ~]# tar xf haproxy-2.1.3.tar.gz 
[aaa@qq.com ~]# ls
anaconda-ks.cfg  haproxy-2.1.3  haproxy-2.1.3.tar.gz
[aaa@qq.com ~]# cd haproxy-2.1.3
[aaa@qq.com haproxy-2.1.3]# make -j $(nproc)  \
TARGET=linux-glibc  \
USE_OPENSSL=1  \
USE_ZLIB=1  \
USE_PCRE=1  \
USE_SYSTEMD=1
[aaa@qq.com haproxy-2.1.3]# make install
[aaa@qq.com haproxy-2.1.3]# which haproxy 
/usr/local/sbin/haproxy

5.配置各个负载的内核参数

[aaa@qq.com haproxy-2.1.3]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf
[aaa@qq.com haproxy-2.1.3]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[aaa@qq.com haproxy-2.1.3]# sysctl  -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

6.提供配置文件

[aaa@qq.com ~]# mkdir /etc/haproxy
[aaa@qq.com ~]# 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
    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.153.22:80 check inter 2000 fall 5
    server web02 192.168.153.25:80 check inter 2000 fall 5
    #server web01 192.168.80.250:80 cookie web01 check inter 2000 fall 5

7.配置服务启动脚本

[aaa@qq.com ~]# vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/local/sbin/haproxy/ -f /etc/haproxy/haproxy.cfg   -c -q
ExecStart=/usr/local/sbin/haproxy/ -Ws -f /etc/haproxy/haproxy.cfg  -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 

[Install]
WantedBy=multi-user.target

[aaa@qq.com ~]# systemctl daemon-reload

8.启用日志

[aaa@qq.com ~]# vim /etc/rsyslog.conf
local0.*        /var/log/haproxy.log //添加此行内容
[aaa@qq.com ~]#  systemctl restart rsyslog

9.启动服务

[aaa@qq.com ~]# systemctl enable --now haproxy
[aaa@qq.com ~]# ss -antl |grep 8189
LISTEN     0      128          *:8189                     *:*

10.登陆web界面

用户名:admin 密码:admin
haproxy负载均衡部署
haproxy负载均衡部署