haproxy安装

haproxy安装,第1张

haproxy安装 一、HAProxy简介
  1. HAProxy 是一款提供高可用性、负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。
  2. HAProxy 实现了一种事件驱动、单一进程模型,此模型支持非常大的并发连接数。
  3. HAProxy 支持全透明代理(已具备硬件防火墙的典型特点): 可以用客户端IP地址或者任何其他地址来连接后端服务器. 这个特性仅在Linux 2.4/2.6内核打了cttproxy补丁后才可以使用. 这个特性也使得为某特殊服务器处理部分流量同时又不修改服务器的地址成为可能。
二、HAProxy工作原理

HAProxy由前端(frontend)和后端(backend),前端和后端都可以有多个。也可以只有一个listen块来同时实现前端和后端。这里主要讲一下frontend和backend工作模式。
前端(frontend)区域可以根据HTTP请求的header信息来定义一些规则,然后将符合某规则的请求转发到相应后端(backend)进行处理。

三、HAProxy性能

HAProxy借助于OS上几种常见的技术来实现性能的最大化,所有的这些细微之处的优化实现了在中等规模负载之上依然有着相当低的CPU负载,甚至于在非常高的负载场景中,5%的用户空间占用率和95%的系统空间占用率也是非常普遍的现象,这意味着HAProxy进程消耗比系统空间消耗低20倍以上。因此,对OS进行性能调优是非常重要的。即使用户空间的占用率提高一倍,其CPU占用率也仅为10%,这也解释了为何7层处理对性能影响有限这一现象。由此,在高端系统上HAProxy的7层性能可轻易超过硬件负载均衡设备。

在生产环境中,在7层处理上使用HAProxy作为昂贵的高端硬件负载均衡设备故障故障时的紧急解决方案也时长可见。硬件负载均衡设备在“报文”级别处理请求,这在支持跨报文请求(request across multiple packets)有着较高的难度,并且它们不缓冲任何数据,因此有着较长的响应时间。对应地,软件负载均衡设备使用TCP缓冲,可建立极长的请求,且有着较大的响应时间。

四、HAProxy详解配置文件

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

  1. global: (全局配置主要用于设定义全局参数,属于进程级的配置,通常和 *** 作系统配置有关)
  2. default : (配置默认参数,这些参数可以被用到frontend,backend,Listen组件)
    在此部分中设置的参数值,默认会自动引用到下面的frontend、backend、listen部分中,因引,某些参数属于公用的配置,只需要在defaults部分添加一次即可。而如果frontend、backend、listen部分也配置了与defaults部分一样的参数,Defaults部分参数对应的值自动被覆盖。
  3. frontend:( 接收请求的前端虚拟节点,Frontend可以更加规则直接指定具体使用后端的backend)
    frontend是在haproxy 1.3版本以后才引入的一个组件,同时引入的还有backend组件。通过引入这些组件,在很大程度上简化了haproxy配置文件的复杂性。forntend可以根据ACL规则直接指定要使用的后端backend
  4. backend : (后端服务集群的配置,真实服务器,一个Backend对应一个或者多个实体服务器)
    在HAProxy1.3版本之前,HAProxy的所有配置选项都在这个部分中设置。为了保持兼容性,haproxy新的版本依然保留了listen组件配置试。两种配置方式任选一中
  5. Listen : (Fronted和backend的组合体) 比如haproxy实例状态监控部分配置
五、haproxy安装
[root@haproxy ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
[root@haproxy ~]# useradd -r -M -s /sbin/nologin haproxy
[root@haproxy ~]# cd /usr/src
[root@haproxy src]# tar xf haproxy-2.1.3.tar.gz
[root@haproxy src]# cd haproxy-2.4.0/
[root@haproxy haproxy-2.4.0]# make clean
[root@haproxy haproxy-2.4.0]# make -j $(grep 'processor' /proc/cpuinfo |wc -l)  
TARGET=linux-glibc  
USE_OPENSSL=1  
USE_ZLIB=1  
USE_PCRE=1  
USE_SYSTEMD=1

[root@haproxy haproxy-2.4.0]# make install PREFIX=/usr/local/haproxy


[root@haproxy ~]# which haproxy 
/usr/local/sbin/haproxy


配置各个负载的内核参数
[root@haproxy ~]# echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf
[root@haproxy ~]# echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
[root@haproxy ~]# sysctl  -p 
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1

提供配置文件
[root@haproxy ~]# mkdir /etc/haproxy

[root@haproxy ~]# cat > /etc/haproxy/haproxy.cfg < 
haproxy.service文件编写 
[root@haproxy ~]# cat > /usr/lib/systemd/system/haproxy.service < 
启用日志 
[root@haproxy ~]# vim /etc/rsyslog.conf
local0.*                        /var/log/haproxy.log  #添加此行

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

启动服务
[root@haproxy ~]# systemctl restart haproxy.service 
[root@haproxy ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port       Peer Address:Port   
LISTEN   0        32         192.168.122.1:53              0.0.0.0:*      
LISTEN   0        128              0.0.0.0:22              0.0.0.0:*      
LISTEN   0        5              127.0.0.1:631             0.0.0.0:*      
LISTEN   0        128              0.0.0.0:8189            0.0.0.0:*      
LISTEN   0        128              0.0.0.0:111             0.0.0.0:*      
LISTEN   0        128              0.0.0.0:80              0.0.0.0:*      
LISTEN   0        128                 [::]:22                 [::]:*      
LISTEN   0        5                  [::1]:631                [::]:*      
LISTEN   0        128                 [::]:111                [::]:*      
客户端配置
[root@RS1 ~]# systemctl disable --now  firewalld
[root@RS1 ~]# vim /etc/selinux/config
SELINUX=disabled
[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# systemctl enable --now httpd
[root@RS1 ~]# echo  "hello world"  > /var/www/html/index.html
[root@RS2 ~]# systemctl disable --now  firewalld
[root@RS2 ~]# vim /etc/selinux/config
SELINUX=disabled
[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# systemctl enable --now httpd

浏览器访问


进入管理界面

haproxy配置负载均衡(https)

haproxy配置负载均衡(https)
证书生成

[root@RS1 ~]# yum -y install openssl
[root@RS1 ~]# mkdir ~/keys
[root@RS1 ~]# cd keys
[root@RS1 keys]# openssl genrsa -out passport.com.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
.............................+++++
....................+++++
e is 65537 (0x010001)
[root@RS1 keys]# openssl req -new -key passport.com.key -out passport.com.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HuBei
Locality Name (eg, city) [Default City]:WuHan
Organization Name (eg, company) [Default Company Ltd]:test
Organizational Unit Name (eg, section) []:passport
Common Name (eg, your name or your server's hostname) []:web01.com
Email Address []:[email protected]

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:1
string is too short, it needs to be at least 4 bytes long
A challenge password []:[email protected]
An optional company name []:
[root@RS1 keys]# openssl x509 -req -days 3650 -in passport.com.csr -signkey passport.com.key -out passport.com.crt
Signature ok
subject=C = CN, ST = HuBei, L = WuHan, O = test, OU = passport, CN = web01.com, emailAddress = [email protected]
Getting Private key
[root@RS1 keys]# ls
passport.com.crt  passport.com.csr  passport.com.key
[root@RS1 keys]# scp passport.com.crt passport.com.key 192.168.200.161:/root/
The authenticity of host '192.168.200.161 (192.168.200.161)' can't be established.
ECDSA key fingerprint is SHA256:0Ynm9bqYhmtwF8Jdpj7HYZ4c9A9/EGj6sxSYC91sKFk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.200.161' (ECDSA) to the list of known hosts.
[email protected]'s password: 
passport.com.crt                      100% 1294     1.2MB/s   00:00    
passport.com.key                      100% 1679   584.2KB/s   00:00 

RS上配置https

[root@RS2 ~]# yum -y install mod_ssl
[root@RS2 ~]# mkdir  /etc/httpd/ssl
[root@RS2 ~]# mv passport.com.* /etc/httpd/ssl/
[root@RS2 ~]# cd /etc/httpd/ssl/
[root@RS2 ssl]# ls
passport.com.crt  passport.com.key
[root@RS2 ssl]# cd ..
[root@RS2 httpd]# ls
conf  conf.d  conf.modules.d  logs  modules  run  ssl  state
[root@RS2 httpd]# cd conf.d/
[root@RS2 conf.d]# ls
autoindex.conf  README  ssl.conf  userdir.conf  welcome.conf
[root@RS2 conf.d]# vim ssl.conf  
#找到此两行,取消注释
documentRoot "/var/www/html"
ServerName www.example.com:443

#修改此两行路径
SSLCertificateFile /etc/httpd/ssl/passport.com.crt
SSLCertificateKeyFile /etc/httpd/ssl/passport.com.key
[root@RS2 ~]# systemctl restart httpd
[root@RS2 ~]# ss -anlt
State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port   
LISTEN   0        128              0.0.0.0:22            0.0.0.0:*      
LISTEN   0        128                    *:80                  *:*      
LISTEN   0        128                 [::]:22               [::]:*      
LISTEN   0        128                    *:443                 *:* 

修改配置文件

[root@haproxy ~]# 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 tcp             //模式改为tcp
    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:443       //端口改为443
    mode tcp               //模式改为tcp
    #option httpchk GET /index.html
    log global
    maxconn 3000
    balance roundrobin
    cookie SESSION_cookie insert indirect nocache
    server web01 192.168.200.159:443 check inter 2000 fall 5      //端口改为443
    server web02 192.168.200.161:443 check inter 2000 fall 5     //端口改为443

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4661708.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-07

发表评论

登录后才能评论

评论列表(0条)

保存