haproxy负载均衡

haproxy

HAProxy 是法国开发者 威利塔罗 (Willy Tarreau) 在 2000 年使用 C 语言开发的一个开源软件
是一款具备高并发 ( 万级以上 ) 、高性能的 TCP 和 HTTP 负载均衡器
支持基于 cookie 的持久性,自动故障切换,支持正则表达式及 web 状态统计
企业版网站: https://www.haproxy.com
社区版网站: http://www.haproxy.org
github : https://github.com/haproxy

为什么要负载均衡

  在计算中,负载均衡可以改善计算机,计算机集群,网络连接,中央处理器单元或者磁盘驱动琪等多种计算资源和工作负载分布。负载均衡就是优化资源使用,最大吞吐量,最小化响应时间并且避免任何单一资源的过载。使用多个组件进行负载均衡,而不是单个组件可能会通过冗余来提高可靠性和可用性。负载均衡通常涉及专用软件或者硬件,例如多层交换机或者域名系统服务器进

haproxy实验

配置环境

需要两台webserver1和webserver2 主机网络适配器选择选择nat模式;

一台安装haproxy的主机网络适配器选择nat模式;

配置IP如下

haproxy

webserver1

webserver2

haproxy 下载安装

并配置 /etc/haproxy/haproxy.cfg

在web1和web2 中下载安装启动nginx服务或者httpd服务

测试结果

curl  172.25.254.100 

haproxy状态页面

haproxy 配置

haproxy web1  web2网关 172.25.254.2   均关闭防火墙和selinux

haproxy的算法

动态

基于后端服务器状态进行调度适当调整

 新请求将优先调度至当前负载较低的服务器

权重可以在haproxy运行时动态调整无需重启

如 roundrobin

leastconn

静态

按照事先定义好的规则轮询公平调度

不关心后端服务器的当前负载、连接数和响应速度

无法实时修改权重只能为0和1,,只能靠重启HAProxy生效。

static-rr

fist

其他算法

即可作为静态算法,又可以通过选项成为动态算法

haproxy的高级功能

基于cookie的会话保持

IP透传

七层代理 mode--->http

#webserver1

 systemctl disable nginx

 systemctl stop nginx

dnf install httpd -y

 echo webserver1 - 172.25.254.10 > /var/www/html/index.html

 vim /etc/httpd/conf/httpd.conf

如下标注

%{X-Forwarded-For}i

 systemctl enable --now httpd

​#测试

 tail -n 3 /etc/httpd/logs/access_log

四层代理mode--->tcp

 vim /etc/haproxy/haproxy.cfg

    mode tcp

    server web1 172.25.254.10:80  check inter 2 fall 3 rise 5 weight 2

    server web2 172.25.254.20:80 send-proxy check inter 2 fall 3 rise 5 weight 1

 systemctl restart haproxy.service

#webserver2

 vim /etc/nginx/nginx.conf

http {

log_format main '$remote_addr - $remote_user [$time_local] "$request"'

    ' "$proxy_protocol_addr"'

   

server {

        listen       80 proxy_protocol;

systemctl restart nginx

 vim /etc/haproxy/haproxy.cfg

 server web1 172.25.254.10:80 check inter 2 fall 3 rise 5 weight 2

    server web2 172.25.254.20:80 send-proxy check inter 2 fall 3 rise 5 weight 1

systemctl restart haproxy.service

​# 测试

 tail -n 3 /var/log/nginx/access.log

ACL动静分离

例如

 dnf install php -y

 systemctl restart httpd

 vim /var/www/html/index.php

 cat /var/www/html/index.php

<?php

    phpinfo();

?>

#haproxy

frontend webcluster

    bind *:80

    mode http

    acl static path_end -i .html .jpg .png .css .js

    acl php    path_end -i .php

    use_backend webcluster-host if php

    default_backend default-host

自定义haproxy错误界面

#webserver1/2主机上

system stop httpd

#haproxy主机上

mkdir /etc/haproxy/errorpage -p

vim /etc/haproxy/errorpage/503.http

HTTP/1.0 503 Service Unavailable

Cache-Control: no-cache

Connection: close

Content-Type: text/html;charset=UTF-8

<html><body><h1>什么动物生气最安静</h1>

大猩猩!!

</body></html>

vim /etc/haproxy/haproxy.conf

defaults

    errorfile 503   /etc/haproxy/errorpage/503.http

systemctl restart haproxy.service

​然后去浏览器  172.25.254.100

四层负载  http配置

#证书制作

mkdir -p /etc/haproxy/certs

openssl req -newkey rsa:2048 \

-nodes -sha256 -keyout /etc/haproxy/certs/timinglee.org.key

-x509 -days 365 -out /etc/haproxy/certs/timinglee.org.crt

CD shannxi XIan timinglee webserver www.timinglee.org admin@timinglee.org

​​

vim /etc/haproxy/haproxy.cfg

frontend webcluster

    bind *:80

    mode http

    redirect scheme https if !{ ssl_fc }  #全网站加密

listen web-https

    bind *:443 ssl crt /etc/haproxy/certs/timinglee.pem

    mode http

    balance roundrobin

    server web1 172.25.254.10:3306 check inter 2 fall 2 rise 5

    server web2 172.25.254.20:3306 check inter 2 fall 2 rise 5

 systemctl restart haproxy

netsata -antup | grep 443

开启web1,web2服务

访问https://172.25.254.100

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值