一、实验目的

  1. 掌握缓存DNS服务、主DNS服务器的搭建

    二、实验内容

  2. 搭建一台缓存DNS服务器。
    2. 搭建一台主DNS服务器。

    三、实验环境

  3. 缓存DNS服务器centos6对应主机ip为10.10.64.226
    2. 主DNS服务器centos7对应主机ip为10.10.64.225
    3. 客户机win7对应主机ip为10.10.64.227

    四、实验分析与设计思路

  4. 网络拓扑图
    2. 实验思路

    五、实验准备

  5. 设置环境为同一网段,连接公网,DHCP获取ip
    2. 关闭所有主机防火墙
    3. 测试网络连通性
    4. 安装相关软件包
    负载均衡代理haproxy - 图1

    六、简单基本配置

  6. 配置haproxy主配置文件
    负载均衡代理haproxy - 图2
    2. 配置前端
    负载均衡代理haproxy - 图3
    3. 配置后端
    负载均衡代理haproxy - 图4
    4. 启动服务查看端口
    负载均衡代理haproxy - 图5
    5. 访问验证
    负载均衡代理haproxy - 图6

    七、Haproxy日志记录

  7. 记录haproxy日志
    ① 查看haproxy日志记录local号
    负载均衡代理haproxy - 图7
    ② 修改日志配置文件
    负载均衡代理haproxy - 图8
    负载均衡代理haproxy - 图9
    负载均衡代理haproxy - 图10
    ③ 重启服务查看日志
    负载均衡代理haproxy - 图11
    2. 将前端客户的访问地址发给后端web服务器记录日志
    ① 默认后端服务器记录haproxy服务器地址信息
    负载均衡代理haproxy - 图12
    ② 修改haproxy配置文件
    负载均衡代理haproxy - 图13
    ③ 修改web服务器日志格式
    负载均衡代理haproxy - 图14
    ④ 访问查看日志
    负载均衡代理haproxy - 图15

    八、全局配置

  8. 默认全局配置
    负载均衡代理haproxy - 图16

    2. 其他全局配置

    | nbproc | 指定启动的haproxy守护进程的个数,不加此选项默认一个 | | —- | —- | | ulimit -n | 设定每个进程能打开的最大文件句柄数(相当于ulimit -n) | | spread-check <0..50> | 在haproxy后端有很多服务器场景,在精确的时间间隔后统一对众多服务器进行状态检查,会集中消耗大量带宽 | | debug | 开启调试功能 |

九、代理配置

1. Balance(调度算法,在后端设置)

roundrobin 动态轮询 可加权,属于动态调整的方式,后端服务器不能超过4128台
static-rr 静态轮询 可加权,属于静态算法,后台服务器无上限
leastconn 最少连接数 新的连接请求派发至最少连接数目的后端服务器。属于动态算法,仅仅适用于长连接会话
source 源地址hash hash-type:①map-based:取模法—->静态
②consistent:一致性hash—->动态
uri 对URI的左半部分(“问题”标记之前)做hash运算 基于URI进行绑定调度。也支持hash-type
url_param 根据url中指定的参数值进行调度,把值做hash计算 也支持hash-type
hdr( 根据请求报文中的header进行调度 包括(user_agent,referer,hostname),也支持hash-type。
  1. 源地址hash<br /> ![](https://cdn.nlark.com/yuque/0/2021/png/2308212/1613198755236-81c51c66-5db1-4739-9a68-77ed78909051.png#crop=0&crop=0&crop=1&crop=1&height=77&id=LbVqD&originHeight=128&originWidth=528&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=317)<br />l 访问测试<br /> ![](https://cdn.nlark.com/yuque/0/2021/png/2308212/1613198755503-db4c2ae6-d389-41f7-bdac-ab4d20fa035c.png#crop=0&crop=0&crop=1&crop=1&height=57&id=HFM6P&originHeight=95&originWidth=424&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=254)<br /> ③ uri(uri与web服务器绑定)<br /> ![](https://cdn.nlark.com/yuque/0/2021/png/2308212/1613198755779-45a25b57-f8af-4ccb-8c3e-a2a131adee06.png#crop=0&crop=0&crop=1&crop=1&height=74&id=d1AKJ&originHeight=129&originWidth=560&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=322)<br />l 访问资源为test1时<br />![](https://cdn.nlark.com/yuque/0/2021/png/2308212/1613198755994-9d40ba02-b6cf-40d3-b5b8-ec69f35a4021.png#crop=0&crop=0&crop=1&crop=1&height=29&id=iwQUD&originHeight=48&originWidth=553&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=332)<br />![](https://cdn.nlark.com/yuque/0/2021/png/2308212/1613198756238-9063163f-3e5c-426b-b1e6-c876e01993d2.png#crop=0&crop=0&crop=1&crop=1&height=80&id=gSabI&originHeight=164&originWidth=683&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=332)<br />l 访问资源为test3时<br />![](https://cdn.nlark.com/yuque/0/2021/png/2308212/1613198756527-7472a863-0ac4-4b44-aa99-23020de58324.png#crop=0&crop=0&crop=1&crop=1&height=30&id=ZwIzH&originHeight=50&originWidth=555&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=333)<br />![](https://cdn.nlark.com/yuque/0/2021/png/2308212/1613198756743-2109f5ae-908d-4358-b6a9-29d8824a771f.png#crop=0&crop=0&crop=1&crop=1&height=79&id=z3juR&originHeight=156&originWidth=675&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=340)<br /> ④ hdr(根据请求报文头部,例如浏览器类型调度)<br /> ![](https://cdn.nlark.com/yuque/0/2021/png/2308212/1613198757010-f58ecb8b-2418-4e85-8baa-2f16e359e2c0.png#crop=0&crop=0&crop=1&crop=1&height=76&id=SwHhJ&originHeight=126&originWidth=575&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=345)<br />l Google浏览器访问<br />![](https://cdn.nlark.com/yuque/0/2021/png/2308212/1613198757010-f58ecb8b-2418-4e85-8baa-2f16e359e2c0.png#crop=0&crop=0&crop=1&crop=1&height=76&id=mWatf&originHeight=126&originWidth=575&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=345)<br />l IE浏览器访问<br />![](https://cdn.nlark.com/yuque/0/2021/png/2308212/1613198757236-37b33661-7ba3-494e-8929-47fe02b6afe7.png#crop=0&crop=0&crop=1&crop=1&height=72&id=tyZhz&originHeight=120&originWidth=376&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=&width=226)

2. Bind(绑定监听地址)

① 绑定8080端口
负载均衡代理haproxy - 图17
② 访问8080端口
负载均衡代理haproxy - 图18

3. 使用备用服务器

① 添加备用服务器
负载均衡代理haproxy - 图19
l 停止web1服务
负载均衡代理haproxy - 图20
l 停止web2服务
负载均衡代理haproxy - 图21
l 启动web1服务
负载均衡代理haproxy - 图22

4. cookie(会话绑定、持久连接)

① 添加备用服务器
负载均衡代理haproxy - 图23
l 访问测试
负载均衡代理haproxy - 图24

5. 加权轮循调度

① 添加备用服务器
负载均衡代理haproxy - 图25
l 访问测试
负载均衡代理haproxy - 图26

十、其他配置

1. 启用haproxy的状态监控页

① 修改haproxy主配置文件
负载均衡代理haproxy - 图27
② 访问验证
负载均衡代理haproxy - 图28