首页 > 技术文章 > 21_Keepalived热备 Keepalived+LVS 、 HAProxy服务器

luwei0915 原文

1.Keepalived高可用服务器
proxy:192.168.4.5(客户端主机)
web1:192.168.4.100(Web服务器,部署Keepalived高可用软件)
web2:192.168.4.200(Web服务器,部署Keepalived高可用软件)
Web服务器的浮动VIP地址为192.168.4.80
客户端通过访问VIP地址访问Web页面
 
1.1 部署两台web服务器页面:
web1 ~]# echo "web1" > /var/www/html/index.html
web2 ~]# echo "web2" > /var/www/html/index.html
web1 ~]# systemctl start httpd
web2 ~]# systemctl start httpd
 
1.2 web1-2安装Keepalived软件
web1 ~]# yum install -y keepalived
web2 ~]# yum install -y keepalived
 
1.3 部署Keepalived服务(web1-2)
1.3.1 web1:主服务器
***********************
配置介绍:
]# vim /etc/keepalived/keepalived.conf
global_defs {
  notification_email {
    admin@xxx.com.cn        # 设置报警收件人邮箱
  }
  notification_email_from ka@localhost # 设置发件人
  smtp_server 127.0.0.1     # 定义邮件服务器
  smtp_connect_timeout 30
  router_id  web1           # 设置路由ID号(修改后)
}
vrrp_instance VI_1 {
  state MASTER              # 主服务器为MASTER(备服务器需要修改为BACKUP)
  interface eth0            # 定义网络接口
  virtual_router_id 50      # 主备服务器VRID号必须一致
  priority 100              # 服务器优先级,优先级高优先获取VIP(已修改)
  advert_int 1
  authentication {
    auth_type pass
    auth_pass 1111          # 主备服务器密码必须一致
  }
  virtual_ipaddress { 192.168.4.80  }  # 谁是主服务器谁获得该VIP(已修改)
}
 
***************************
配置web1:
]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {  
   notification_email { #不需要修改
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   #出问题发邮件提示,暂时不要
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id web1(已改)
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
 
vrrp_instance VI_1 {
    state MASTER(主服务器)
    interface eth0
    virtual_router_id 51(修改后)
    priority 100(优先级比BACKUP高)
    advert_int 1
    authentication { (不要修改,但是要一致)
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.4.80 (浮动IP)
    }
}
##999dd 以下暂时不要
 
1.3.2 web2:备用服务器
]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {  
   notification_email { #不需要修改
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   #出问题发邮件提示,暂时不要
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id web2(要改)
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
 
vrrp_instance VI_1 {
    state BACKUP(此处为备用服务器)
    interface eth0
    virtual_router_id 51(修改后)
    priority 80(优先级比master低)
    advert_int 1
    authentication { (不要修改,但是要一致)
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.4.80 (浮动IP)
    }
}
##999dd 以下暂时不要
 
1.3.3 web1-2启动服务(都要操作)
]# systemctl start keepalived
启动keepalived会自动添加一个drop的防火墙规则,需要清空!
]# iptables -F
 
1.3.4 测试(web1-2)
查看浮动IP(192.168.4.80)
]# ip a s eth0
inet 192.168.4.100/24 brd 192.168.4.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.4.80/32 scope global eth0
       valid_lft forever preferred_lft forever

proxy 测试:
]# curl http://192.168.4.80
web1
...
 
关闭web1的网卡,查看备份服务情况
web1]# ifconfig eth0 down

查看浮动IP(192.168.4.80)
web2]# ip a s eth0
link/ether 52:54:00:66:da:80 brd ff:ff:ff:ff:ff:ff
    inet 192.168.4.200/24 brd 192.168.4.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.4.80/32 scope global eth0

proxy 测试:
]# curl http://192.168.4.80
web2
...
 
恢复web1网卡,会恢复到web1.

2.Keepalived+LVS服务器
使用Keepalived为LVS调度器提供高可用功能,防止调度器单点故障,为用户提供Web服务:
LVS1调度器真实IP地址为192.168.4.5
LVS2调度器真实IP地址为192.168.4.6
服务器VIP地址设置为192.168.4.15
真实Web服务器地址分别为192.168.4.100、192.168.4.200
使用加权轮询调度算法,真实web服务器权重不同
 
方案:使用5台虚拟机,1台作为客户端主机、2台作为LVS调度器、2台作为Real Server


2.1 web1-2配置VIP地址
2.1.1 web1配置VIP地址
]# cd /etc/sysconfig/network-scripts/
]# cp ifcfg-lo{,:0}
]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.4.15
NETMASK=255.255.255.255
NETWORK=192.168.4.15
BROADCAST=192.168.4.15
ONBOOT=yes
NAME=lo:0
 
#手动写入如下4行内容
]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
#当有arp广播问谁是192.168.4.15时,本机忽略该ARP广播,不做任何回应
#本机不要向外宣告自己的lo回环地址是192.168.4.15
]# systemctl restart network

2.1.2 web2配置VIP地址
]# cd /etc/sysconfig/network-scripts/
]# cp ifcfg-lo{,:0}
]# vim ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.4.15
NETMASK=255.255.255.255
NETWORK=192.168.4.15
BROADCAST=192.168.4.15
ONBOOT=yes
NAME=lo:0
 
#手动写入如下4行内容
]# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
#当有arp广播问谁是192.168.4.15时,本机忽略该ARP广播,不做任何回应
#本机不要向外宣告自己的lo回环地址是192.168.4.15
]# systemctl restart network
 
2.2 配置后端Web服务器页面
web1 ~]# echo "web1" > /var/www/html/index.html
web2 ~]# echo "web2" > /var/www/html/index.html
web1 ~]# systemctl restart httpd
web2 ~]# systemctl restart httpd
 
2.3 调度器安装Keepalived与ipvsadm软件(两台LVS调度器执行相同的操作)
proxy1,proxy2
]# yum install -y keepalived
]# systemctl enable keepalived
]# yum install -y ipvsadm
]# ipvsadm -C
 
2.4 部署Keepalived实现LVS-DR模式调度器的高可用
2.4.1 LVS1调度器设置Keepalived,并启动服务(web1)
]# vim /etc/keepalived/keepalived.conf
global_defs {
  notification_email {
    admin@test.com.cn
  }
  notification_email_from ka@localhost
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id  lvs1   //设置路由ID号
}
vrrp_instance VI_1 {
  state MASTER          //主服务器为MASTER
  interface eth0      
  virtual_router_id 51  //主辅VRID号必须一致
  priority 100          //服务器优先级
  advert_int 1
  authentication {
    auth_type pass
    auth_pass 1111      //主辅服务器密码必须一致
  }
  virtual_ipaddress {  192.168.4.15  }   //配置VIP
}
virtual_server 192.168.4.15 80 { //设置ipvsadm的VIP规则
  delay_loop 6
  lb_algo wrr        //设置LVS调度算法为WRR
  lb_kind DR         //设置LVS的模式为DR
  #persistence_timeout 50
#注意这样的作用是保持连接,开启后,客户端在一定时间内始终访问相同服务器
  protocol TCP
  real_server 192.168.4.100 80 {  //设置后端web服务器真实IP
    weight 1      //设置权重为1
    TCP_CHECK {   //对后台real_server做健康检查
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
    }
  }
 real_server 192.168.4.200 80 {  //设置后端web服务器真实IP(实验需要修改)
    weight 2                     //设置权重为2
    TCP_CHECK {
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
    }
  }
}
]# systemctl start keepalived
]# ipvsadm -Ln     #查看LVS规则
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port    Forward Weight ActiveConn InActConn
TCP  192.168.4.15:80 wrr
  -> 192.168.4.100:80       Route  1   0   0         
  -> 192.168.4.200:80       Route  2   0   0
]# ip a s eth0   #查看VIP配置(浮动IP在proxy)
inet 192.168.4.5/24 brd 192.168.4.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.4.15/32 scope global eth0
       valid_lft forever preferred_lft forever
 
2.4.2 LVS2调度器设置Keepalived,并启动服务(web2)
]# vim /etc/keepalived/keepalived.conf
global_defs {
  notification_email {
    admin@test.com.cn
  }
  notification_email_from ka@localhost
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id  lvs2   //设置路由ID号
}
vrrp_instance VI_1 {
  state BACKUP          //从服务器为BACKUP
  interface eth0      
  virtual_router_id 51  //主辅VRID号必须一致
  priority 50           //服务器优先级
  advert_int 1
  authentication {
    auth_type pass
    auth_pass 1111      //主辅服务器密码必须一致
  }
  virtual_ipaddress {  192.168.4.15  }   //配置VIP
}
virtual_server 192.168.4.15 80 { //设置ipvsadm的VIP规则
  delay_loop 6
  lb_algo wrr        //设置LVS调度算法为WRR
  lb_kind DR         //设置LVS的模式为DR
  #persistence_timeout 50
#注意这样的作用是保持连接,开启后,客户端在一定时间内始终访问相同服务器
  protocol TCP
  real_server 192.168.4.100 80 {  //设置后端web服务器真实IP
    weight 1      //设置权重为1
    TCP_CHECK {   //对后台real_server做健康检查
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
    }
  }
 real_server 192.168.4.200 80 {  //设置后端web服务器真实IP(实验需要修改)
    weight 2                     //设置权重为2
    TCP_CHECK {
    connect_timeout 3
    nb_get_retry 3
    delay_before_retry 3
    }
  }
}
]# systemctl start keepalived
]# ipvsadm -Ln     #查看LVS规则
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port    Forward Weight ActiveConn InActConn
TCP  192.168.4.15:80 wrr
  -> 192.168.4.100:80       Route  1   0   0         
  -> 192.168.4.200:80       Route  2   0   0
]# ip a s eth0   #查看VIP配置(浮动IP在proxy)
 
2.4.3 客户端测试(client)
]# curl http://192.168.4.15
web1
]# curl http://192.168.4.15
web2
]# curl http://192.168.4.15
web2
]# curl http://192.168.4.15
web1
]# curl http://192.168.4.15
web2
]# curl http://192.168.4.15
web2

3.配置HAProxy负载平衡集群
准备4台Linux服务器,两台做Web服务器,1台安装HAProxy,1台做客户端
客户端访问HAProxy,HAProxy分发请求到后端Real Server
开启HAProxy监控页面,及时查看调度器状态
设置HAProxy为开机启动


3.1 部署两台web服务器页面:
web1 ~]# echo "web1" > /var/www/html/index.html
web2 ~]# echo "web2" > /var/www/html/index.html
web1 ~]# systemctl start httpd
web2 ~]# systemctl start httpd
 
3.2 部署HAProxy服务器
3.2.1 配置网络,安装软件
]# echo 'net.ipv4.ip_forward = 1' >> sysctl.conf
]# sysctl -p
]# yum -y install haproxy
3.2.2 修改配置文件
]# vim /etc/haproxy/haproxy.cfg
...
defaults
    mode        http             
    #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
    log         global        
    #应用全局的日志配置
    option      httplog       
    # 启用日志记录HTTP请求,默认haproxy日志记录是不记录HTTP请求日志
    option      dontlognull    
    # 启用该项,日志中将不会记录空连接。所谓空连接就是在上游的负载均衡器或者监控系统为了探测该服务是否存活可用时,需要定期的连接或者获取某一固定的组件或页面,或者探测扫描端口是否在监听或开放等动作被称为空连接;官方文档中标注,如果该服务上游没有其他的负载均衡器的话,建议不要使用该参数,因为互联网上的恶意扫描或其他动作就不会被记录下来
    option      http-server-close  
     #每次请求完毕后主动关闭http通道
    option      forwardfor       except 127.0.0.0/8   
     #如果服务器上的应用程序想记录发起请求的客户端的IP地址,需要在HAProxy上配置此选项, 这样 HAProxy会把客户端的IP信息发送给服务器,在HTTP请求中添加"X-Forwarded-For"字段。启用X-Forwarded-For,在requests头部插入客户端IP发送给后端的server,使后端server获取到客户端的真实IP。
    option      redispatch                      
     #当使用了cookie时,haproxy将会将其请求的后端服务器的serverID插入到cookie中,以保证会话的SESSION持久性;而此时,如果后端的服务器宕掉了, 但是客户端的cookie是不会刷新的,如果设置此参数,将会将客户的请求强制定向到另外一个后端server上,以保证服务的正常。
    retries       3                             
     # 定义连接后端服务器的失败重连次数,连接失败次数超过此值后将会将对应后端服务器标记为不可用
    timeout http-request    10s     #http请求超时时间
    timeout queue           1m      #一个请求在队列里的超时时间
    timeout connect         10s     #连接超时
    timeout client          1m      #客户端超时
    timeout server          1m      #服务器端超时
    timeout http-keep-alive 10s     #设置http-keep-alive的超时时间
    timeout check           10s     #检测超时
    maxconn                 3000    #每个进程可用的最大连接数
frontend  main *:80             #监听地址为80
    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    acl url_static       path_end       -i .jpg .gif .png .css .js
    use_backend static          if url_static
    default_backend             my_webserver     
    #指定后端,此处将对应的请求转发给后端

listen stats
    bind 0.0.0.0:1080                   #监听端口
    stats refresh 30s                     #统计页面自动刷新时间
    stats uri /stats                          #统计页面url
    stats realm Haproxy Manager #统计页面密码框上提示文本
    stats auth admin:admin           #统计页面用户名和密码设置
    #stats hide-version                 #隐藏统计页面上HAProxy的版本信息

backend static                                       
    #使用了静态动态分离(如果url_path匹配 .jpg .gif .png .css .js静态文件则访问此后端)
    balance             roundrobin                       
    #负载均衡算法(#banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数)
    server              static 127.0.0.1:80 check         
    #静态文件部署在本机(也可以部署在其他机器或者squid缓存服务器)
backend my_webserver                                 
    #定义一个名为my_webserver后端部分。PS:此处my_webserver只是一个自定义名字而已,但是需要与frontend里面配置项default_backend 值相一致
    balance     roundrobin          #负载均衡算法
    server  web1 192.168.2.100:80 check inter 2000 fall 3 weight 30
    server  web2 192.168.2.200:80 check inter 2000 fall 3 weight 30

3.2.3 启动服务器并设置开机启动
]# netstat -antulp | grep :80(查看是否占用,占用则停掉)
]# systemctl start haproxy
]# netstat -antulp | grep :80
tcp  0  0 0.0.0.0:80  0.0.0.0:*  LISTEN  9094/haproxy
]# systemctl enable haproxy
 
3.2.3 client访问
]# curl 192.168.4.5
web1
]# curl 192.168.4.5
web2
]# curl 192.168.4.5
web1
]# curl 192.168.4.5
web2

]# firefox http://192.168.4.5:1080/stats


备注:
Queue队列数据的信息(当前队列数量,最大值,队列限制数量);
Session rate每秒会话率(当前值,最大值,限制数量);
Sessions总会话量(当前值,最大值,总量,Lbtot: total number of times a server was selected选中一台服务器所用的总时间);
Bytes(入站、出站流量);
Denied(拒绝请求、拒绝回应);
Errors(错误请求、错误连接、错误回应);
Warnings(重新尝试警告retry、重新连接redispatches);
Server(状态、最后检查的时间(多久前执行的最后一次检查)、权重、备份服务器数量、down机服务器数量、down机时长)。
 
ab访问测试:
]# ab -n 10000 -c 1000 http://192.168.4.5/
测试健康检查:停用web1
]# systemctl stop httpd
]# firefox http://192.168.4.5:1080/stats #web1会变红

推荐阅读