HAProxy实验

实验准备

        四台虚拟机:一台负载均衡器、两台后端服务器、一台客户端(IP随意);关闭VMware的NAT模式DHCP功能

IP地址
HAProxy Server192.168.0.100/24
Web Server 1192.168.0.10/24
Web Server 2192.168.0.20/24

七层负载均衡

        HAProxy

        安装HAProxy 

yum install -y haproxy
添加配置文件
vim /etc/haproxy/haproxy.cfg

        frontend-backend格式 

        listen格式

         Web server 1

yum install -y httpd
添加文本
echo 192.168.0.10 web1 > /var/www/html/index.html
启动服务
systemctl enable --now httpd

        Web server 2

yum install -y nginx
添加文本
echo 192.168.0.20 web2  > /usr/share/nginx/html/index.html
启动服务
systemctl enable --now nginx

        测试

        测试权重分配

七层IP透传 

        为后端服务器提供客户端IP

        HAProxy

vim /etc/haproxy/haproxy.cfg
配置文件后重启服务
systemctl restart haproxy.service

        Web server 1

        HTTPD显示客户端IP

vim /etc/httpd/conf/httpd.conf    #添加格式
修改完配置文件后重启服务
systemctl restart httpd

        Web server 2

        nginx默认能够显示客户端IP

        测试

        发出请求

         HTTPD

        nginx

 四层负载均衡

        HAProxy

vim /etc/haproxy/haproxy.cfg
配置完成后重启服务
systemctl restart haproxy.service

        DB server 1

yum install -y mariadb-server.x86_64
启动mariadb
systemctl enable --now mariadb
登入db
mysql -u root -p    #没有密码,直接进入
create user test1@"%" identified by "123";    #添加用户
grant all on *.* to test1@"%";    #更改权限
create database db_server1;    #创建数据库

        DB server 2

yum install -y mariadb-server.x86_64
systemctl enable --now mariadb
 mysql -u root -p
create user test1@"%" identified by "123";
grant all on *.* to test1@"%";
create database db_server2;

        测试

四层IP透传 

        HAProxy

vim /etc/haproxy/haproxy.cfg
systemctl restart haproxy.service

        Web server 1

        HTTPD高版本无法通过四层进行IP透传

         Web server 2

vim /etc/nginx/nginx.conf
配置完成后重启服务
systemctl restart nginx.service

        测试

 > /var/log/nginx/access.log    #清空nginx的访问日志

HAProxy状态页面

        可以通过 Web界面监测状态

        配置命令

 stats enable        #基于默认的参数启用stats page
stats hide-version        #将状态页中haproxy版本隐藏
stats refresh <delay>        #设定自动刷新时间间隔,默认不自动刷新
stats uri <prefix>        #自定义stats page uri,默认值:/haproxy?stats
stats auth <user>:<passwd>        #认证时的账号和密码,可定义多个用户,每行指定一个用户
                                                        #默认:no authenticationrefre
stats admin{ifunless } <cond>#启用stats page中的管理功能

vim /etc/haproxy/haproxy.cfg
systemctl restart haproxy.service

socat工具 (热更新)

        HAProxy

yum install -y socat
vim /etc/haproxy/haproxy.cfg
systemctl restart haproxy.service

echo "help" | socat stdio /var/lib/haproxy/haproxy.sock    #查看socat帮助


常用命令
echo show info | socat stdio /var/lib/haproxy/haproxy.sock    #查看HAProxy当前状态

echo show servers state | socat stdio /var/lib/haproxy/haproxy.sock    #查看后端服务器状态

echo "get weight web-service/web1" | socat stdio /var/lib/haproxy/haproxy.sock    #查看web-service中web1后端服务器的权重

echo "set weight web-service/web2 2" | socat stdio /var/lib/haproxy/haproxy.sock    #调整后端服务器的权重

echo "disable server web-service/web1" | socat stdio /var/lib/haproxy/haproxy.sock    #停止向指定服务器分发流量

echo "enable server web-service/web1" | socat stdio /var/lib/haproxy/haproxy.sock    #向指定服务器分发流量

基于cookie的会话保持 

        cookie value:为当前server指定cookie值,实现基于cookie的会话黏性,相对于基于 source 地址hash调度算法对客户端的粒度更精准,但同时也加大了haproxy负载,目前此模式使用较少,
已经被session共享服务器代替。(不支持TCP,支持HTTP)

        配置选项

cookie name [rewriteinsertprefix][ indirect ][ nocache ][ postonly ] [preserve ][httponly ][secure ][ domain ]* [ maxidle <idle> ][ maxlife ]
name        #cookie 的key名称,用于实现持久连接
insert        #插入新的cookie,默认不插入cookie
indirect        #如果客户端已经有cookie,则不会再发送cookie信息
nocache        #当c1ient和hapoxy之间有缓存服务器(如:CDN)时,不允许中间缓存器缓存cookie,
                     #因为这会导致很多经过同一个CDN的请求都发送到同一台后端服务器

vim /etc/haproxy/haproxy.cfg
systemctl restart haproxy.service

        测试 

        每个浏览器都是不一样的cookie

        firefox浏览器

        EDGE浏览器 

         命令行检查

        通过命令访问特定cookie的后端服务器 

 ACL

        访问控制列表ACL,Access Control Lists)是一种基于包过滤的访问控制技术,它可以根据设定的条件对经过服务器传输的数据包进行过滤(条件匹配),即对接收到的报文进行匹配和过滤,基于请求报文头部中的源地址、源端口、目标地址、目标端口、请求方法、URL、文件后缀等信息
内容进行匹配并执行进一步操作,比如允许其通过或丢弃。

        配置格式

#用ac1来定义或声明一个ac1
acl        <aclname>        <criterion>        [flags]        [Loperator]        [<value>]
acl            名称               匹配模式        匹配规范     具体操作符       操作对象类型

#ACL名称,可以使用大字母A-Z、小写字母a-z、数字0-9、冒号:、点.、中横线和下划线,并且严格区分大小写,比如Image_site和image_site就是两个完全不同的acl

        匹配规范(判断条件)

hdr string,提取在一个HTTP请求报文的首部
hdr([<name> [,<occ>]]):完全匹配字符串,header的指定信息,<occ> 表示在多值中使用的值的出现次数
hdr_beg([<name> [,<occ>]]):前缀匹配,header中指定匹配内容的begin
hdr_end([<name> [,<occ>]]):后缀匹配,header中指定匹配内容end
hdr_dom([<name> [,<occ>]]):域匹配,header中的domain name
hdr_dir([<name> [,<occ>]]):路径匹配,header的uri路径
hdr_len([<name> [,<occ>]]):长度匹配,header的长度匹配
hdr_reg([<name> [,<occ>]]):正则表达式匹配,自定义表达式(regex)模糊匹配
hdr_sub([<name> [,<occ>]]):子串匹配,header中的uri模糊匹配

#返回第一个主机头和请求的路径部分的连接
<scheme>://<user>:<password>@#<host>:<port>/<path>;<params>#?<query>#<frag>
    base     : exact string match
    base_beg : prefix match
    base_dir : subdir match
    base_dom : domain match
    base_end : suffix match
    base_len : length match
    base_reg : regex match
    base_sub : substring match 

#提取请求的URL路径
<scheme>://<user>:<password>@<host>:<port>#/<path>;<params>#?<query>#<frag>
    path     : exact string match
    path_beg : prefix match  #请求的URL开头,如/static、/images、/img、/css
    path_end : suffix match  #请求的URL中资源的结尾,如 .gif  .png  .css  .js  .jpg  .jpeg
    path_dom : domain match
    path_dir : subdir match
    path_len : length match
    path_reg : regex match
    path_sub : substring match

匹配模式 

        -i 不区分大小写
        -m 使用指定的pattern匹配方法
        -n 不做DNS解析
        -u 禁止acl重名,否则多个同名ACL匹配或关系

调用方式

        与:隐式(默认)使用
        或:使用“or” 或 “||”表示
        否定:使用 "!" 表示 

ACL示例 

        apache安装php(192.168.0.10)

yum install -y php
mkdir /var/www/html/php/
vim /var/www/html/php/index.php
配置完成后重启服务
systemctl restart httpd

        nginx添加static页面(192.168.0.20) 

mkdir /usr/share/nginx/html/static/
echo 192.168.0.20 Hello World!!! > /usr/share/nginx/html/static/index.html
systemctl  restart nginx.service

         HAProxy添加ACL

vim /etc/haproxy/haproxy.cfg
配置完成后重启
systemctl restart haproxy.service

        测试

        访问php

         访问static

 sorry server

        在后端服务器全部无法访问时,提供错误响应

        HAProxy

        下载HTTPD,修改端口

yum install -y httpd
vim /etc/httpd/conf/httpd.conf
配置完成后重启服务
systemctl restart httpd

vim /etc/haproxy/haproxy.cfg    #添加sorry server
systemctl restart haproxy.service

        关闭后端服务器,进行测试

web1
systemctl stop httpd

web2
systemctl stop nginx.service

自定义错误页面 

        美化错误页面 

mkdir -p /etc/haproxy/errorpage/
vim /etc/haproxy/errorpage/503.http

vim /etc/haproxy/haproxy.cfg
systemctl restart haproxy.service

        将后端服务器及sorry server全部关闭,测试效果 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值