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服务
[root@Web1 ~]# yum install -y httpd
[root@Web2 ~]# 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.编辑网页内容
[root@Web1 ~]# echo "web1:10.0.0.130" >/var/www/html/index.html
[root@Web2 ~]# echo "web2:10.0.0.131" >/var/www/html/index.html
4.启动web服务
[root@Web1 ~]# systemctl start httpd
[root@Web2 ~]# systemctl start httpd
5.编辑自身hosts文件
[root@Web1 ~]# cat /etc/hosts
10.0.0.130 img.zxb.com
[root@Web2 ~]# cat /etc/hosts
10.0.0.131 test.zxb.com
配置Haproxy及keepalived相关配置
1.安装Haproxy及keepalived
[root@Haproxy1 ~]# yum install -y keepalived haproxy
[root@Haproxy2 ~]# yum install -y keepalived haproxy
2.配置haproxy(两台配置一样)
[root@Haproxy1 ~]#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/8rspadd X-via:\ haproxy/www.zxb.comrspidel 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 #定义最大并发为10000acl url_img hdr(host) -i img.zxb.comacl url_test hdr(host) -i test.zxb.comuse_backend img_web if url_imguse_backend test_web if url_testbackend img_webbalance roundrobinserver web1 10.0.0.130:80 checkbackend test_webbalance roundrobinserver web2 10.0.0.131:80 check
4.启动haproxy
[root@Haproxy1 ~]# systemctl start haproxy
[root@Haproxy2 ~]# systemctl start haproxy
3.配置keepalived
Master:
[root@Haproxy1 ~]#
! Configuration File for keepalived vrrp_script check_haproxy { script "/etc/keepalived/check_haproxy.sh" #检测脚本,该路径脚本存放的位置 interval 2 #每隔2秒钟检测一次 # weight 2 #优先级减2 } global_defs { #全局配置 notification_email { #发生事件切换的时候,发送的邮箱 acassen@firewall.loc } 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:
[root@Haproxy2 ~]#
! Configuration File for keepalived vrrp_script check_haproxy { script "/etc/keepalived/check_haproxy.sh" interval 2 weight 2} global_defs { notification_email { acassen@firewall.loc } 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检测脚本(两台配置一样)
[root@Haproxy1 ~]#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文件
[root@Client ~]# cat /etc/hosts
10.0.0.100 img.zxb.com test.zxb.com
3.启动Haproxy1和Haproxy2的keepalived,由于Haproxy1的priority较高,所以VIP首先在Haproxy1
[root@Haproxy1 ~]# systemctl start keepalived
[root@Haproxy2 ~]# systemctl start keepalived
[root@Haproxy1 ~]# 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 -D10月 22 23:58:10 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 22 23:58:10 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 22 23:58:10 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 22 23:58:10 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 22 23:58:16 zxb Keepalived_vrrp[17874]: VRRP_Instance(VI_2) Sending/queueing gratuitous ARPs on ens33 for 10.0.0.25010月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 22 23:58:16 zxb Keepalived_vrrp[17874]: Sending gratuitous ARP on ens33 for 10.0.0.250
4.访问
[root@Client ~]# curl img.zxb.com
web1:10.0.0.130
[root@Client ~]# curl test.zxb.com
web2:10.0.0.131
5.当停止master上的haproxy服务时,keepalived配置文件检测到haproxy脚本后,停止keepalived服务,VIP从Master飘到Backup
[root@Haproxy1 ~]# 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 forever2: 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
[root@Haproxy2 ~]# 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 -D10月 23 17:41:33 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 23 17:41:33 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 23 17:41:33 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 23 17:41:33 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: VRRP_Instance(VI_2) Sending/queueing gratuitous ARPs on ens33 for 10.0.0.25010月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.25010月 23 17:41:38 zxb2 Keepalived_vrrp[3920]: Sending gratuitous ARP on ens33 for 10.0.0.250
[root@zxb2 ~]# 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 forever2: 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.再访问
[root@Client ~]# curl img.zxb.com
web1:10.0.0.130
[root@Client ~]# curl test.zxb.com
web2:10.0.0.131
end
!!!!
本文出自 “XiaoBingZ” 博客,请务必保留此出处http://1767340368.blog.51cto.com/13407496/1975334