赞
踩
HAProxy由前端(frontend)和后端(backend),前端和后端都可以有多个。也可以只有一个listen块来同时实现前端和后端。这里主要讲一下frontend和backend工作模式。
前端(frontend)区域可以根据HTTP请求的header信息来定义一些规则,然后将符合某规则的请求转发到相应后端(backend)进行处理。
HAProxy借助于OS上几种常见的技术来实现性能的最大化,所有的这些细微之处的优化实现了在中等规模负载之上依然有着相当低的CPU负载,甚至于在非常高的负载场景中,5%的用户空间占用率和95%的系统空间占用率也是非常普遍的现象,这意味着HAProxy进程消耗比系统空间消耗低20倍以上。因此,对OS进行性能调优是非常重要的。即使用户空间的占用率提高一倍,其CPU占用率也仅为10%,这也解释了为何7层处理对性能影响有限这一现象。由此,在高端系统上HAProxy的7层性能可轻易超过硬件负载均衡设备。
在生产环境中,在7层处理上使用HAProxy作为昂贵的高端硬件负载均衡设备故障故障时的紧急解决方案也时长可见。硬件负载均衡设备在“报文”级别处理请求,这在支持跨报文请求(request across multiple packets)有着较高的难度,并且它们不缓冲任何数据,因此有着较长的响应时间。对应地,软件负载均衡设备使用TCP缓冲,可建立极长的请求,且有着较大的响应时间。
复制代码
haproxy 的配置文件由两部分组成:全局设定和对代理的设定,共分为五段:global,defaults,frontend,backend,listen
[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 <<EOF #--------------全局配置---------------- 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.155:80 check inter 2000 fall 5 server web02 192.168.153.154:80 check inter 2000 fall 5 EOF
[root@haproxy ~]# cat > /usr/lib/systemd/system/haproxy.service <<EOF
[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
EOF
[root@haproxy ~]# systemctl daemon-reload
[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)
证书生成
[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 []:passport@qq.com 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 []:1@2.com 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 = passport@qq.com 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. root@192.168.200.161'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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。