集群cluster篇-----2.HAProxy服务器 、 Keepalived热备 、 Keepalived+LVS

DAY 02
区别:HAProxy具备健康性检查,LVS不会
知识点:
HAProxy服务器
Keepalive热备
keepalived+LVS
一.HAProxy服务器
http://www.ttlsa.com/linux/haproxy-study-tutorial/
1.简介
提供高可用性、负载均衡以及基于TCP和HTTP应用的代理服务器软件
免费快速可靠;适用于负载大,站点需要会话保持或七层处理

2.衡量负载均衡器性能的因素:
Session rate会话率
Session concurrency 并发会话数
Data rate 数据速率(以MB/s或Mbps衡量;大的对象导致并发会话数增加;高会话数,高数据速率要求更多的内存)
3.HAProxy工作模式
mode http 客户端请求被深度分析再发往服务器
mode tcp 客户端与服务器之间建立会话,不检查第七层信息
mode health 仅作健康状态检查
扩展:HAProxy运行http模式下
1、HTTP请求方式
如下表:
GET--------向Web服务器请求一个文件
POST-------向Web服务器发送数据让Web服务器进行处理
PUT--------向Web服务器发送数据并存储在Web服务器内部
HEAD-------检查一个对象是否存在
DELETE-----从Web服务器上删除一个文件
CONNECT----对通道提供支持
TRACE------跟踪到服务器的路径
OPTIONS----查询Web服务器的性能
2.http解析:请求Request和响应(REsponse均被完全分析和索引,创建恰当的匹配规则
3.http事物模型:http协议是 事物驱动的,每个请求只能对应一个响应
常见模型:
HTTP close、连接请求断开(不断循环),三次握手消耗系统资源延迟大
Keep-alive、连接多请求断开,降低延迟,消耗资源少
Pipelining、连接多请求不等待回应,适用大量图片页面降低网络延迟。
4.HTTP头部信息:
请求头部信息:客户端环境和请求正文的有用信息:语言;正文长度
响应头部信息:版本;状态码;原因
方法:GET
URI:/ser

环境示例:客户端---->web server HAProxy 存储设备


4.配置
#yum list | grep -i haproxy  #没有该包
#rpm -ivh /mnt/rhel6/LoadBalancer/haproxy-1.5.4-2.el6.x86_64.rpm 
#vim /etc/haproxy/haproxy.cfg   #修改配置文件
配置文件说明
HAProxy配置参数来源:
命令行L有最高优先级
global部分:全局设置进程级别参数
代理声明部分
配置文件:
default 为后续的其他部分设置缺省参数 缺省参数可以被后续部分重值
frontend 描述接受客户端侦听套接字(socket)集
backend 描述转发连接的服务集
listen 把frontend 和backend结合到一起完整声明
vim /etc/haproxy/haproxy.cfg
global 
log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000   #最大连接数
    user        haproxy
    group       haproxy
    daemon         #创建1个进程进入deamon模式运行

defaults
 mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8  #后端服务器可以从http Header获取客户端IP
    option                  redispatch          #serverid服务器挂掉后强制定向到健康服务器
    retries                 3                   #3次连接失败就认为服务不可用
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10000                #如果backend没有指定,就默认10s
    timeout client          300000               #客户端连接超时
    timeout server          300000 #服务器连接超时
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000
    stats uri /admin

frontend  pljweblb 192.168.4.99:80
    acl webhtml       path_end       -i  .html
    acl webphp       path_end       -i .php


    use_backend phpgrp          if webphp
    use_backend htmlgrp          if webhtml
    default_backend             htmlgrp

backend htmlgrp
    balance     roundrobin
    server  web101 192.168.4.101:80 check
    server  web102 192.168.4.102:80 check

backend phpgrp
    server  web66 192.168.4.66:80 check
    server  web77 192.168.4.77:80 check


listen lbweb 192.168.4.99:80   #监听端口
stats refresh 30s        #统计页面自动刷新时间
stats uri /stats #统计页面url
stats realm Haproxy Manager  #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名与密码设置
stats hide-version #隐藏统计页面上Haproxy版本信息        
cookie SERVERID rewrite
      balance roundrobin
      server web1 192.168.4.66:80 cookie applinst1 check inter 2000 rise 2 fall 5
      server web2 192.168.4.77:80 cookie applinst2 check inter 2000 rise 2 fall 5


5.起服务测试
HAP99#service  haproxy restart
#elinks --dump http://192.168.4.99/a.html
#elinks --dump http://192.168.4.99/a.html
#elinks --dump http://192.168.4.99/b.php
#elinks --dump http://192.168.4.99/a.php
#firefox http://192.168.4.99/admin

***********************************************************************

Keepalived热备
1.概述:开始为LVS设计专门监控各服务器节点的状态 后来加入VRRP,防止单点故障。
运行原理:检测服务器节点状态,当节点出现故障,从集群踢处,恢复,加入,完全自动


[ [email protected] ~]# vim /etc/yum.repos.d/192.168.4.254_rhel6.repo
[192.168.4.254_rhel6]
name=added from: http://192.168.4.254/rhel6
baseurl=http://192.168.4.254/rhel6
enabled=1
gpgcheck=0
[rhel6]
name=added from: http://192.168.4.254/rhel6/LoadBalancer
baseurl=http://192.168.4.254/rhel6/LoadBalancer
enabled=1
gpgcheck=0
~           
[ [email protected] ~]# yum -y install keepalived
[ [email protected] ~]# vim /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
      [email protected]
      [email protected]
      [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}


vrrp_instance webha {
    state MASTER           #主服务器为master,辅助为slave
    interface eth0           #定义网络接口
    virtual_router_id 51
    priority 100             #服务器优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456      #主服务器密码必须一致
    }
    virtual_ipaddress {
        192.168.4.95
    }
}


[ [email protected] ~]# yum -y install keepalived
[ [email protected] ~]# vim /etc/keepalived/keepalived.conf
global_defs {
   notification_email {
      [email protected]
      [email protected]
      [email protected]
   }
   notification_email_from [email protected]
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_instance webha {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.4.95
    }
}

[ [email protected] ~]# ip add show | grep 192.168.4
    inet 192.168.4.101/24 brd 192.168.4.255 scope global eth0
谁优先级高,起哪个服务
[ [email protected] ~]# ip add show | grep 192.168.4  #查看是否有虚拟IP
    inet 192.168.4.102/24 brd 192.168.4.255 scope global eth0
    inet 192.168.4.95/32 scope global eth0
[ [email protected] ~]# ip add show | grep 192.168.4
    inet 192.168.4.101/24 brd 192.168.4.255 scope global eth0
[ [email protected] ~]# elinks --dump http://192.168.4.95/a.html
   192.168.4.102 web102
[ [email protected] ~]# elinks --dump http://192.168.4.95/a.html
   192.168.4.102 web102
[ [email protected] ~]# service keepalived  stop
停止 keepalived:                                          [确定]
[ [email protected] ~]# elinks --dump http://192.168.4.95/
   welcom to party!!
[ [email protected] ~]# elinks --dump http://192.168.4.95/a.html
   192.168.4.101 web101

*******************************************************************************

keepalived+LVS
keepalived实现了高可用集群,最初是为LVS设置的,专门监控服务器节点的状态,后来加入了VRRP功能,解决单点故障用。节点出故障,自动剔除,节点恢复,自动添加。
使用keepalived高可用解决调度器单点失败问题
主备调度器配置LVS
主调度器异常时,keepalived启用备用调度器

33 88 keepalived + ipvsadm
44 55 httpd 

[ [email protected] ~]# vim /etc/keepalived/keepalived.conf 
global_defs {
   notification_email {
      [email protected]  #设置报警收件人邮箱
      [email protected]
      [email protected]
   }
   notification_email_from [email protected] #设置发件人
   smtp_server 192.168.200.1  #定义邮件服务器
   smtp_connect_timeout 30
   router_id LVS_DEVEL   #设置路由ID号
}


vrrp_instance lvsha {
    state MASTER            #主服务器为master,辅助为slave
    interface eth0          #定义网络接口
    virtual_router_id 51     #主辅VRID号必须一致
    priority 150             #服务器优先级
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 666666       #主辅服务器密码必须一致
    }
    virtual_ipaddress {
        192.168.4.96
    }
}

virtual_server 192.168.4.96 80 {   #设置VIP为192.168.4.96
    delay_loop 6
    lb_algo rr     #设置LVS调度算法为rr
    lb_kind DR      #设置LVS的模式为DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3


    real_server 192.168.4.44 80 {
        weight 1         #设置权重为1
    }
    real_server 192.168.4.55 80 {
        weight 1
    }
}


[ [email protected] ~]# ip add show | grep 192.168.4.96
[ [email protected] ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn


[ [email protected] ~]# ip addr show | grep 192.168.4.96
[ [email protected] ~]# service keepalived status
keepalived 已停
[ [email protected] ~]# service keepalived start
正在启动 keepalived:                                      [确定]
[ [email protected] ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn TCP  192.168.4.96:80 rr persistent 50   -> 192.168.4.44:80              Route   1      0          0            -> 192.168.4.55:80              Route   1      0          0    总结分析: nginx分析: 优点: 工作在7层,可以针对http做分流策略 正则表达式比HAProxy强大 安装、配置、测试简单、,通过日志可以解决许多问题 并发量可以达到上万次 nginx还可以作为web服务器使用 缺点: 仅支持http、https、mail协议,应用面小 监控检查仅通过端口,无法使用url检查 LVS分析: 优点; 负载能力强,工作在4层,对内存、cpu消耗低 配置性低,没有太多可配置性、减少人为错误 应用面广,几乎可以为所有应用提供负载均衡 缺点: 不支持正则表达式,不能实现动静分离 如果网站架构庞大,LVS-DR配置繁琐 HAProxy分析 优点: 支持session、cookie功能 可以通过url进行健康检查 效率、负载均衡速度、高于nginx、低于LVS HAProxy支持TCP,可以对mysql进行负载均衡 调度算法丰富 缺点: 正则弱于nginx 日志依赖于syslogd,不支持apache日志