Haproxy+keepalived实现负载均衡
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Haproxy+keepalived实现负载均衡相关的知识,希望对你有一定的参考价值。
Haproxy简介:
Haproxy是一个开源的高性能的反向代理或者说是负载均衡服务软件之一,它支持双机热备、虚拟主机、基于TCP和HTTP应用代理等功能。其配置简单,而且拥有很好的对服务器节点的健康检查功能(相当于keepalived健康检查)。
Haproxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数,HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。
Keepalived简介:
Keepalived的作用是检测服务器的状态,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
一、实验环境
(Centos7)
Haporxy1 + keepalived : 10.0.0.128
Haproxy2 + keepalived : 10.0.0.129
VIP1 : 10.0.0.250
Web1 : 10.0.0.130:80
Web2 : 10.0.0.131:80
拓扑图:
二、方案实施
安装及配置web服务(Web1、Web2).
1.安装httpd服务
[[email protected] ~]# yum install -y httpd
[[email protected] ~]# yum install -y httpd
2.修改配置文件中的ServerName,Web1为img.zxb.com,Web2为test.zxb.com
Web1: ServerName img.zxb.com:80
Web2: ServerName test.zxb.com:80
3.编辑网页内容
[[email protected] ~]# echo "web1:10.0.0.130" >/var/www/html/index.html
[[email protected] ~]# echo "web2:10.0.0.131" >/var/www/html/index.html
4.启动web服务
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# systemctl start httpd
5.编辑自身hosts文件
[[email protected] ~]# cat /etc/hosts
10.0.0.130 img.zxb.com
[[email protected] ~]# cat /etc/hosts
10.0.0.131 test.zxb.com
配置Haproxy及keepalived相关配置
1.安装Haproxy及keepalived
[[email protected] ~]# yum install -y keepalived haproxy
[[email protected] ~]# yum install -y keepalived haproxy
2.配置haproxy(两台配置一样)
[[email protected] ~]#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 stats socket /var/lib/haproxy/stats defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000
添加
frontend main *:80 option forwardfor except 127.0.0.0/8 rspadd X-via:\ haproxy/www.zxb.com rspidel Server.* stats enable #在frontend添加监控页面 stats uri /zxb?stats #自定义监控页面的url stats realm Stats\ Page\ Area #去掉空格转译 stats auth zxb:zxb #配置帐号密码 stats refresh 5s #每5s刷新一次监控页面 stats hide-version #隐藏版本信息 stats admin if TRUE maxconn 10000 #定义最大并发为10000 acl url_img hdr(host) -i img.zxb.com acl url_test hdr(host) -i test.zxb.com use_backend img_web if url_img use_backend test_web if url_test backend img_web balance roundrobin server web1 10.0.0.130:80 check backend test_web balance roundrobin server web2 10.0.0.131:80 check
4.启动haproxy
[[email protected] ~]# systemctl start haproxy
[[email protected] ~]# systemctl start haproxy
3.配置keepalived
Master:
[[email protected] ~]#
! Configuration File for keepalived vrrp_script check_haproxy { script "/etc/keepalived/check_haproxy.sh" #检测脚本,该路径脚本存放的位置 interval 2 #每隔2秒钟检测一次 # weight 2 #优先级减2 } global_defs { #全局配置 notification_email { #发生事件切换的时候,发送的邮箱 [email protected] } router_id LVS_DEVEL } vrrp_instance VI_2 { state MASTER #两种角色,MASTER or BACKUP interface ens33 #实例绑定的网卡 virtual_router_id 50 #VRID 标记 priority 100 #优先级 advert_int 1 #MASTER 与BACKUP 之间同步检查的时间间隔,单位为秒 authentication { auth_type PASS #PASS#认证的方式,支持 PASS auth_pass 1111 #认证的密码 } virtual_ipaddress { 10.0.0.250 #指定漂移地址(VIP) } track_script { check_haproxy } }
Backup:
[[email protected] ~]#
! Configuration File for keepalived vrrp_script check_haproxy { script " /etc/keepalived/check_haproxy.sh " interval 2 weight 2 } global_defs { notification_email { [email protected] } router_id LVS_DEVEL } vrrp_instance VI_2 { state BACKUP interface ens33 virtual_router_id 50 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.0.0.250 } track_script { check_haproxy } }
4.编辑haproxy检测脚本(两台配置一样)
[[email protected] ~]#cat /etc/keepalived/check_haproxy.sh
#!/bin/bash
haproxy=`ps -C haproxy --no-heading| wc -l`
if [ $haproxy -eq 0 ];then
systemctl stop keepalived
fi
三.测试
1.访问 haproxy 自带的监控页面,url为haproxy服务器的IP地址
url用户名密码为http://10.0.0.128/zxb?stats
2.编辑客户端10.0.0.150:hosts文件
[[email protected] ~]# cat /etc/hosts
10.0.0.100 img.zxb.com test.zxb.com
3.启动Haproxy1和Haproxy2的keepalived,由于Haproxy1的priority较高,所以VIP首先在Haproxy1
[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: active (running) since 日 2017-10-22 23:34:43 CST; 30min ago Process: 17871 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 17872 (keepalived) CGroup: /system.slice/keepalived.service ├─17872 /usr/sbin/keepalived -D ├─17873 /usr/sbin/keepalived -D └─17874 /usr/sbin/keepalived -D 10月 22 23:58:10 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 22 23:58:10 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 22 23:58:10 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 22 23:58:10 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 22 23:58:16 zxb Keepalived_vrrp[17874]: VRRP_Instance(VI_2) Sending/queueing gratuitous ARPs on ens33 for 10.0.0.250 10月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250
4.访问
[[email protected] ~]# curl img.zxb.com
web1:10.0.0.130
[[email protected] ~]# curl test.zxb.com
web2:10.0.0.131
5.当停止master上的haproxy服务时,keepalived配置文件检测到haproxy脚本后,停止keepalived服务,VIP从Master飘到Backup
[[email protected] ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:64:e3:62 brd ff:ff:ff:ff:ff:ff inet 10.0.0.128/24 brd 10.0.0.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::8a1e:39ba:9beb:4aa9/64 scope link valid_lft forever preferred_lft forever
[[email protected] ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: active (running) since 一 2017-10-23 17:25:23 CST; 16min ago Process: 3915 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 3916 (keepalived) CGroup: /system.slice/keepalived.service ├─3916 /usr/sbin/keepalived -D ├─3917 /usr/sbin/keepalived -D └─3920 /usr/sbin/keepalived -D 10月 23 17:41:33 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 23 17:41:33 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 23 17:41:33 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 23 17:41:33 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: VRRP_Instance(VI_2) Sending/queueing gratuitous ARPs on ens33 for 10.0.0.250 10月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250 10月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250
[[email protected] ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:23:e3:43 brd ff:ff:ff:ff:ff:ff inet 10.0.0.129/24 brd 10.0.0.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.0.0.250/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::bcdf:e60f:7858:6933/64 scope link valid_lft forever preferred_lft forever
6.再访问
[[email protected] ~]# curl img.zxb.com
web1:10.0.0.130
[[email protected] ~]# curl test.zxb.com
web2:10.0.0.131
end
!!!!
本文出自 “XiaoBingZ” 博客,请务必保留此出处http://1767340368.blog.51cto.com/13407496/1975334
以上是关于Haproxy+keepalived实现负载均衡的主要内容,如果未能解决你的问题,请参考以下文章
haproxy+keepalived实现高可用负载均衡web集群
HaProxy + Keepalived 实现高可用负载均衡