7层负载均衡__HAproxy

一、环境

(一)client:192.168.163.128

[root@client ~]# hostnamectl set-hostname client

(二)haproxy:192.168.163.132

[root@haproxy ~]# hostnamectl set-hostname haproxy

(三)web1:192.168.163.135

[root@web1 ~]#  hostnamectl set-hostname web1

(四)web2:192.168.173.136

[root@web2~]#  hostnamectl set-hostname web2

(五)域名解析:

[root@localhost ~]# vim /etc/hosts

192.168.163.128 client
192.168.163.132 haproxy
192.168.163.135 web1

192.168.163.136 web2

(六)所有主机关闭防火墙并增强:

[root@localhost ~]#  systemctl stop firewalld.service

[root@localhost ~]#  setenforce 0

二、web服务器配置(web1和web2同配置)

安装httpd作为测试

[root@web1 ~]# yum install -y httpd && systemctl start httpd && systemctl enable httpd
[root@web1 ~]# netstat -antp | grep httpd

[root@web1 ~]#echo web1 > /var/www/html/index.html

三、安装HAproxy

[root@haproxy ~]#  yum install haproxy -y

四、配置HAproxy

(一)配置示例

[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg 

global
    log 127.0.0.1 local3 info
    maxconn 4096
        uid nobody
#       uid 99
        gid nobody
#       gid 99
    daemon
    nbproc 1
    pidfile /run/haproxy.pid
defaults
    log           global
    mode       http
    maxconn 2048
    retries     3
    option    redispatch
    contimeout    5000
    clitimeout        50000
    srvtimeout        50000
#timeout connect 5000
#timeout client 50000
#timeout server 50000
    option abortonclose

    stats uri /admin?stats
    stats realm Private lands
    stats auth admin:password
    stats hide-version


frontend http-in
    bind 0.0.0.0:80
    mode http
    log global
    option httplog
    option httpclose
     acl html url_reg  -i  \.html$
     use_backend html-server if  html
     default_backend html-server

backend html-server
    mode http
    balance roundrobin
    option httpchk GET /index.html
    cookie SERVERID insert indirect nocache
    server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5
    server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5

###############################注释###注释###注释#########################

gloab:全局配置
log:日志配置
maxconn:最大连接限制(优先级低)
uid:用户
gid:组用户
deamon:守护进程运行
nbproc :haproxy进程数,该值的设置应该和服务器的CPU核心数一致,比如设置为 16,即常见的2颗8核心CPU的服务器,即共有16核心,则可以将其值设置为:<=16 ,创建多个进程数,可以减少每个进程的任务队列,但是过多的进程数也可能会导致进程的崩溃。
pidfile /run/haproxy.pid :haproxy进程ID存储位置
defaults:针对(listen和backend块进行设置没如果块中没设置,则使用默认设置)默认配置
log:日志使用全局配置
mode:模式7层LB
maxconn:最大连接数(优先级中)
retries:健康检查。3次连接失败就认为服务不可用
option:服务不可用后的操作,重定向到其他健康服务器
contimeout    :(重传计时器)定义haproxy将客户端!!!请求!!!转发至后端服务器,所等待的超时时长
clitimeout:(向后长连接)haproxy作为客户,和后端服务器之间!!!空闲连接!!!的超时时间,到时候发送fin指令
srvtimeout    :(向前长连接)haproxy作为服务器,和用户之间空闲连接的超时时间,到时候发送fin指令
option abortonclose  :当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接


stats uri /admin?stats:设置统计页面的uri为/admin?stats
stats realm Private lands:设置统计页面认证时的提示内容
stats auth admin:password:设置统计页面认证的用户和密码,如果要设置多个,另起一行写入即可
stats hide-version:隐藏统计页面上的haproxy版本信息

frontend:前端配置块。面对用户侧
bind:面对用户监听地址和端口
mode:http模式的LB
log:日志使用全局配置

option httplog:默认日志格式非常简陋,仅包括源地址、目标地址和实例名称,而“option httplog参数将会使得日志格式变得丰富许多,其通常包括但不限于HTTP请求、连接计时器、会话状态、连接数、捕获的首部及cookie、“frontend”、“backend”及服务器名称,当然也包括源地址和端口号等。

option http close: 每次请求完毕后,关闭http通道

 acl html url_reg  -i  \.html$  :1. 访问控制列表名称html。规则要求访问以html结尾的url时
 use_backend html-server if  html   :2.如果满足acl html规则,则推送给后端服务器 html-server
 default_backend html-server  3:默认的后端服务器是 html-server

backend html-server:后端服务器名称为  html-server
mode http:模式为7层代理
balance roundrobin:算法为轮训
option httpchk GET /index.html     :允许用http协议检查server 的健康
cookie SERVERID insert indirect nocache:轮询的同时,根据插入的cookie SERVERID  的值来做会话保持,将相同的用户请求,转发给相同的真实服务器。
server html-A web1:80 weight 1 cookie 3 check inter 2000 rise 2 fall 5:cookie 3 服务器ID,避免rr算法将客户机请求转发给其他服务器 ,对后端服务器的健康状况检查间隔为2000毫秒,连续2次健康检查成功,则认为是有效的,连续5次健康检查失败,则认为服务器宕机
server html-B web2:80 weight 1 cookie 4 check inter 2000 rise 2 fall 5

(二)启动HAproxy并查看状态

[root@haproxy ~]# systemctl start haproxy.service
[root@haproxy ~]# systemctl status haproxy.service

 四、测试结果

[root@client ~]# elinks --dump http://haproxy
   web1
[root@client ~]# elinks --dump http://haproxy
   web2
[root@client ~]# elinks --dump http://web1
   web1
[root@client ~]# elinks --dump http://web2
   web2

 五、测试HAproxy

客户端浏览器输入:http://192.168.163.132/admin?stats

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值