haproxy负载均衡部署

本文介绍了HAProxy的高可用性和负载均衡功能,详细讲解了如何在服务端安装配置,包括安装httpd服务、依赖包,创建用户和组,编译安装HAProxy,配置内核参数,设置服务启动脚本,启用日志以及启动服务,并提供了登陆web界面的步骤和默认凭证。
摘要由CSDN通过智能技术生成

简介

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

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

环境

名称主机名IP
web服务端wyt1192.168.153.20
httpd服务端wyt2192.168.153.22
httpd服务端wyt3192.168.153.25

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

[root@wyt2 ~]# cd /var/www/html/
[root@wyt2 html]# echo 'node01' > index.html
[root@wyt2 html]# systemctl enable --now httpd
[root@wyt3 ~]# cd /var/www/html/
[root@wyt3 html]# echo 'node02' > index.html
[root@wyt3 html]# systemctl enable --now httpd

2.安装依赖包

[root@wyt1 ~]# yum -y install make  zlib-devel gcc gcc-c++ pcre-devel bzip2-devel openssl-devel systemd-devel

3.创建用户和组

[root@wyt1 ~]# useradd -r -M -s /sbin/nologin haproxy
[root@wyt1 ~]# id haproxy
uid=996(haproxy) gid=994(haproxy) 组=994(haproxy)

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

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

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

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

6.提供配置文件

[root@wyt1 ~]# mkdir /etc/haproxy
[root@wyt1 ~]# 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.配置服务启动脚本

[root@wyt1 ~]# 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

[root@wyt1 ~]# systemctl daemon-reload

8.启用日志

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

9.启动服务

[root@wyt1 ~]# systemctl enable --now haproxy
[root@wyt1 ~]# ss -antl |grep 8189
LISTEN     0      128          *:8189                     *:*

10.登陆web界面

用户名:admin 密码:admin
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值