部署haproxy负载均衡及页面访问

部署haproxy负载均衡及页面访问

一、haproxy介绍

​ haproxy提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。

​ haproxy特别适用于那些负载特别大的web站点,这些站点通常又需要会话保持或七层处理。haproxy运行在时下的硬件上,完全可以支持数以万计的并发连接,并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。

​ haproxy实现了一种事件驱动、单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。

​ 事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space)实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以使每个CPU时间片(Cycle)做更多的工作。

二、负载均衡

​ 二层负载均衡(mac)

  • 用于虚拟mac地址方式,外部对虚拟mac地址请求,负载均衡接收后分配给后端实际的mac地址响应。

    三层负载均衡(ip)

  • 一般用于虚拟ip地址的方式,外部对虚拟ip地址请求,负载均衡接收后分配给后端实际的ip地址响应。

    四层负载均衡(tcp)

  • 在三层负载均衡的基础上,用ip+port接收请求,在转发到对应的机器上。

  • 产品大概有:F5,lvs,nginx,haproxy…

    七层负载均衡(http)

  • 根据虚拟的url或者ip,主机名接收请求,在转发到相应的处理服务器上。

  • 产品大概有:haproxy,nginx,apache,mysql proxy…

三、haproxy优点

  1. 免费开源,稳定性高。
  2. haproxy可以跑满10Gbps,这个数值作为软件级负载均衡器是相当惊人的。
  3. haproxy支持连接拒绝。
  4. haproxy支持全透明代理。
  5. haproxy现多于线上的Mysql集群环境,我们常用于它作为MySQL(读)负载均衡。

四、haproxy源码安装

haproxy官方帮助文档:https://cbonte.github.io/haproxy-dconv/

haproxy源码包下载网站地址:https://src.fedoraproject.org/repo/pkgs/haproxy/

1. 源码安装

//安装编译环境
[root@lishuai ~]# dnf -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel

//创建haproxy用户
[root@lishuai ~]# useradd -rMs /sbin/nologin haproxy

//解压安装
[root@lishuai ~]# ls
anaconda-ks.cfg  haproxy-2.1.3.tar.gz
[root@lishuai ~]# tar -zxf haproxy-2.1.3.tar.gz 
[root@lishuai ~]# ls
anaconda-ks.cfg  haproxy-2.1.3  haproxy-2.1.3.tar.gz
[root@lishuai ~]# cd haproxy-2.1.3
[root@lishuai haproxy-2.1.3]# make clean
[root@lishuai haproxy-2.1.3]# make  -j $(grep 'processor' /proc/cpuinfo |wc -l)  \
> TARGET=linux-glibc  \
> USE_OPENSSL=1  \
> USE_ZLIB=1  \
> USE_PCRE=1  \
> USE_SYSTEMD=1
[root@lishuai haproxy-2.1.3]# make install PREFIX=/usr/local/haproxay
[root@lishuai haproxy-2.1.3]# cp haproxy  /usr/sbin/

//设置Linux内核参数
[root@lishuai haproxy-2.1.3]# vim /etc/sysctl.conf
[root@lishuai haproxy-2.1.3]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

//配置haproxy服务
[root@lishuai haproxy-2.1.3]# mkdir /etc/haproxy
[root@lishuai haproxy-2.1.3]# 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 172.16.103.130:80 check inter 2000 fall 5
    #server web01 192.168.80.102:80 cookie web01 check inter 2000 fall 5

//启动haproxy,配置service服务单元文件启动
[root@lishuai ~]# vim /usr/lib/systemd/system/haproxy.service
[root@lishuai ~]# cat /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

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

[Install]
WantedBy=multi-user.target

[root@lishuai ~]# systemctl daemon-reload
[root@lishuai ~]# systemctl restart haproxy.service
[root@lishuai ~]# systemctl enable haproxy.service
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.

//配置日志信息
[root@lishuai ~]# vim /etc/rsyslog.conf 
local0.info     /var/log/haproxy.log
[root@lishuai ~]# systemctl restart rsyslog
[root@lishuai ~]# systemctl enable rsyslog

2. haproxy配置文件解析

haproxy 的配置文件由两部分组成:全局设定和对代理的设定,共分为五段:global,defaults,frontend,backend,listen

  • global: 全局配置,主要用于定义全局参数,属于进程级的配置,通常和操作系统配置有关。
  • default: 配置默认参数,这些参数可以被用到frontend,backend,Listen组件。
  • frontend:接收请求的前端虚拟节点,frontend可以指定具体使用后端的backend。
  • backend : 后端服务集群的配置,真实服务器,一个backend对应一个或者多个实体服务器。
  • listen: fronted和backend的组合体,比如haproxy实例状态监控部分配置。Haproxy1.3之前的唯一配置方式。

时间格式

一些包含了值的参数表示时间,如超时时长。这些值一般以毫秒为单位,但也可以使用其它的时间单位后缀

  • us: 微秒(microseconds),即1/1000000秒;
  • ms: 毫秒(milliseconds),即1/1000秒;
  • s: 秒(seconds);
  • m: 分钟(minutes);
  • h:小时(hours);
  • d: 天(days);

全局global配置

global
    log 127.0.0.1 local0  info    //定义haproxy日志输出设置
    #log loghost local0 info    //定义haproxy日志级别
    maxconn 20480         //定义最大连接数
#chroot /usr/local/haproxy    //chroot运行路径
    pidfile /var/run/haproxy.pid  //haproxy进程PID文件
    #maxconn 4000       
    user haproxy        //运行haproxy用户,可用uid代替
    group haproxy      //运行haproxy用户组,可用gid代替
daemon          //以后台形式运行haproxy

级别 代码 描述
energ 0 系统不可用
alert 1 必须马上采取行动的事件
crit 2 关键的事件
err 3 错误事件
warning 4 警告事件
notice 5 普通但重要的事件
info 6 有用的信息
debug 7 调试信息

defaults配置

用于设置配置默认参数,这些参数可以被用到frontend,backend,listen组件。

在此部分中设置的参数值,默认会自动引用到下面的frontend、backend、listen部分中。如果某些参数属于公用的配置,只需要在defaults部分添加一次即可。而如果frontend、backend、listen部分也配置了与defaults部分一样的参数,defaults部分参数对应的值自动被覆盖。

defaults
    mode http   //所处理的类别(7层代理http,4层代理tcp)
    log global   //引入global定义的日志格式
    option dontlognull   //不记录健康检查日志信息
    option httpclose  //每次请求完毕后主动关闭http通道,haproxy不支持keep-alive模式
    option httplog  //日志类别为http日志格式
    #option forwardfor  //如果后端服务器需要获取客户端的真是ip,需要配置的参数,可以从http header中获取客户端的ip
    option redispatch
    balance roundrobin  //设置默认负载均衡方式,轮询方式
    timeout connect 10s  //默认连接超时时间
    timeout client 10s   //默认客户端超时时间
    timeout server 10s  //默认服务器超时时间
    timeout check 10s  //设置超时检查超时时间 
    maxconn 60000  //最大连接数
    retries 3   //3次连接失败就认为服务器不可用,也可以通过后面设置

mode http:设置haproxy的运行模式,有三种{http|tcp|health}。注意:如果haproxy中还要使用4层的应用(mode tcp)的话,不建议在此定义haproxy的运行模式。

tcp模式:在此模式下,客户端和服务器端之前将建立一个全双工的连接,不会对七层报文做任何检查,默认为tcp模式,经常用于SSL、SSH、SMTP等应用。

http模式:在此模式下,客户端请求在转发至后端服务器之前将会被深度分板,所有不与RFC格式兼容的请求都会被拒绝。

health:已基本不用了。

frontend配置

frontend是在haproxy 1.3版本以后才引入的一个组件,同时引入的还有backend组件。通过引入这些组件,在很大程度上简化了haproxy配置文件的复杂性。forntend可以根据ACL规则直接指定要使用的后端backend。

如:

frontend http_80_in 
bind 0.0.0.0:80   //设置侦听端口,即haproxy提供的web服务端口,和lvs的vip类似
mode http	
log  global   
 	option httpclose  
    option httplog  
option forwardfor
default_backend  wwwpool  //设置请求默认转发的后端服务池

backend配置

用来定义后端服务集群的配置,真实服务器,一个Backend对应一个或者多个实体服务器

backend  wwwpool    //定义wwwpool服务器组
mode  http
option  redispath
option  abortonclose
balancer  source   //负载均衡的方式,源哈希算法
cookie  SERVERID  //允许插入serverid到cookie中,serverid后面可以定义
option  httpdchk  GET  /test.html    //心跳测试
server  web1  10.1.1.2:80  cookie  2 weight  3  check  inter 2000  rise 2 fall 3 maconn 8

listen配置

常常用于状态页面监控,以及后端server检查,是Fronted和backend的组合体。

listen admin_stats     //frontend和backend的组合体,监控组的名称,按需自定义名称
    bind 0.0.0.0:8189      //侦听端口
    stats enable      //开启监控
    mode http
    log global
    stats uri /haproxy_stats    //监控页面的url访问路径
    stats realm Haproxy\ Statistics  //监控页面的提示信息
    stats auth admin:admin  //监控页面的用户和密码
    #stats hide-version   //隐藏统计页面上的haproxy版本信息
    stats admin if TRUE  //手工启用/禁用,后端服务器haproxy
    stats refresh 30s   //每个30秒自动刷新监控页面

五、Haproxy搭建http负载均衡

环境说明

主机名 IP地址 安装应用 系统版本
lishuai 192.168.183.137 haproxy centos8
RS1 192.168.183.138 httpd centos8
RS2 192.168.183.139 httpd centos8
client 192.168.183.135 centos8

1、lishuai、RS1、RS2都关闭防火墙和selinux

[root@lishuai ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@lishuai ~]# setenforce 0
[root@lishuai ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config

[root@RS1 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS1 ~]# setenforce 0
[root@RS1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config

[root@RS2 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@RS2 ~]# setenforce 0
[root@RS2 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config

2、RS1和RS2部署httpd

[root@RS1 ~]# dnf -y install httpd
[root@RS1 ~]# echo RS1 > /var/www/html/index.html
[root@RS1 ~]# systemctl restart httpd
[root@RS1 ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

[root@RS2 ~]# dnf -y install httpd
[root@RS2 ~]# echo RS2 > /var/www/html/index.html
[root@RS2 ~]# systemctl restart httpd
[root@RS2 ~]# systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

3、修改lishuai主机的内核参数

[root@lishuai ~]# vim /etc/sysctl.conf 
[root@lishuai ~]# sysctl -p
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

4、修改haproxy配置文件

[root@lishuai ~]# vim /etc/haproxy/haproxy.cfg 
global
    daemon
    maxconn 256

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http-in
    bind *:80
    default_backend servers

backend servers
    server web01 192.168.183.138:80
    server web02 192.168.183.139:80

5、重新启动haproxy服务

[root@lishuai ~]# systemctl restart haproxy.service

6、客户端验证

[root@client ~]# curl http://192.168.183.137
RS1
[root@client ~]# curl http://192.168.183.137
RS2
[root@client ~]# curl http://192.168.183.137
RS1
[root@client ~]# curl http://192.168.183.137
RS2

使用WEB网页访问测试

[root@lishuai ~]# vim /etc/haproxy/haproxy.cfg 
[root@lishuai ~]# cat /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            //访问网页后缀URL
    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.183.138:80 check inter 2000 fall 5
    server web02 192.168.183.139:80 check inter 2000 fall 5

[root@lishuai ~]# systemctl restart haproxy.service
[root@lishuai ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process  
LISTEN   0        128              0.0.0.0:80            0.0.0.0:*              
LISTEN   0        128              0.0.0.0:22            0.0.0.0:*              
LISTEN   0        128              0.0.0.0:8189          0.0.0.0:*              
LISTEN   0        128                 [::]:22               [::]:*              
[root@lishuai ~]# 

用户名 密码
admin admin

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_65998623/article/details/127077736