标签:management 解决方案 代理服务 应用程序 content
一、haproxy和keepalived的解释:
1、haproxy:haproxy是免费、极速且可靠的用于为TCP和基于HTTP应用程序提供负载均衡和代理服务的解决方案,尤其适用于高负载且需要持久连接或7层处理机制的web站点。
2、haproxy的特性:客户端侧的长连接(client-side keep-alive);TCP加速(TCP speedups); 响应池(response buffering);RDP协议;基于源的粘性(source-based stickiness);更好的统计数据接口(a much better stats interfaces);更详细的健康状态检测机制(more verbose health checks);基于流量的健康评估机制(traffic-based health);支持HTTP认证;服务器管理命令行接口(server management from the CLI);基于ACL的持久性(ACL-based persistence);日志分析器;内容交换(content switching):基于任何请求标准挑选服务器池;ACL:编写内容交换规则;负载均衡算法(load-balancing algorithms):更多的算法支持;内容探测(content inspection):阻止非授权协议;透明代理(transparent proxy):在Linux系统上允许使用客户端IP直接连入服务器;内核TCP拼接(kernel TCP splicing):无copy方式在客户端和服务端之间转发数据以实现数G级别的数据速率;分层设计(layered design):分别实现套接字、TCP、HTTP处理以提供更好的健壮性、更快的处理机制及便捷的演进能力;快速、公平调度器(fast and fair scheduler):为某些任务指定优先级可实现理好的QoS;会话速率限制(session rate limiting):适用于托管环境。
3、keepalived:Keepalived的作用是检测服务器的状态,如果有一台服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。haproxy主要用作RealServer的健康状态检查以及LoadBalance主机和BackUP主机之间failover的实现。
二、配置haproxy实现网站的负载均衡:
1、node1和node2都安装httpd,php,php-mysql:
1 | [root@node1 ~] # yum -y install httpd php php-mysql |
1 | [root@node2 ~] # yum -y install httpd php php-mysql |
2、为node1和node2提供静态和动态网页:
1 2 3 4 | [root@node1 ~] # ls /var/www/html/ index.html index.php [root@node2 ~] # ls /var/www/html/ index.html index.php |
3、node1和node2启动httpd服务:
1 2 3 4 | [root@node1 ~] # service httpd start Starting httpd: [ OK ] [root@node2 ~] # service httpd start Starting httpd: [ OK ] |
4、验证node1和node2的静态和动态网站:
5、在haproxy1和haproxy2主机上分别安装haproxy程序包:
1 | yum -y install haproxy |
6、在haproxy1编辑haproxy的主配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | [root@haproxy1 ~] # vim /etc/haproxy/haproxy.cfg global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy .pid maxconn 30000 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 listen stats
mode http
bind *:8009
stats enable
stats hide-version
stats uri /hpadmin ?stats
stats realm “Haproxy Statistics"
stats auth admin:admin
stats admin if TRUE frontend http- in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 20
capture request header Referer len 60
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .jpeg .gif .png .css .js .html
acl url_php path_end -i .php
use_backend static_servers if url_static
use_backend dynamic_servers if url_php
default_backend dynamic_servers backend static_servers
balance roundrobin
server node1 172.16.70.1:80 check maxconn 6000 backend dynamic_servers
cookie node insert nocache
balance roundrobin
server node2 172.16.70.2:80 check maxconn 1000 cookie node2 |
说明:
7、把haproxy1主机上haproxy主配置文件复制一份到haproxy2主机上:
1 2 | [root@haproxy1 ~] # scp /etc/haproxy/haproxy.cfg haproxy2:/etc/haproxy/ haproxy.cfg 100% 4190 4.1KB /s 00:00 |
8、在haproxy1主机上启动haproxy服务:
1 2 | [root@haproxy1 ~] # service haproxy start Starting haproxy: [ OK ] |
9、访问haproxy的统计页面:
10、访问静态服务器:
11、访问动态服务器:
12、关闭haproxy1主机上的haproxy服务:
1 2 | [root@haproxy1 ~] # service haproxy stop Stopping haproxy: [ OK ] |
三、配置keepalived为实现haproxy高可用的双主模型:
1、在haproxy1主机和haproxy2主机上都安装keepalived程序包:
1 2 | [root@haproxy1 ~] # yum -y install keepalived [root@haproxy2 ~] # yum -y install keepalived |
2、在haproxy1和haproxy2上写一个脚本,定义主haproxy的启动、辅haproxy的停止即给管理员的邮件通知机制:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | [root@haproxy1 keepalived] # pwd /etc/keepalived [root@haproxy1 keepalived] # vim notify.sh #!/bin/bash # Author: MageEdu <linuxedu@foxmail.com> # description: An example of notify script # vip=172.16.70.35 contact= ‘root@localhost‘ notify() { mailsubject= "`hostname` to be $1: $vip floating" mailbody= "`date ‘+%F %H:%M:%S‘`: vrrp transition, `hostname` changed to be $1" echo $mailbody | mail -s "$mailsubject" $contact } case "$1" in master) notify master /etc/rc .d /init .d /haproxy start exit 0 ;; backup) notify backup /etc/rc .d /init .d /haproxy stop exit 0 ;; fault) notify fault /etc/rc .d /init .d /haproxy stop exit 0 ;; *) echo ‘Usage: `basename $0` {master|backup|fault}‘ exit 1 ;; esac |
3、在haproxy1主机上编辑keepalived的主配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | [root@haproxy1 keepalived] # pwd /etc/keepalived [root@haproxy1 keepalived] # vim keepalived.conf global_defs {
notification_email {
root@localhost
}
notification_email_from kaadmin@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL }
vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 1
weight -2 }
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 66
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 12168
}
virtual_ipaddress {
172.16.35.35
}
track_script {
chk_haproxy }
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault" }
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 69
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 22168
}
virtual_ipaddress {
172.16.70.135
}
track_script {
chk_haproxy }
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault" } |
说明:
global_defs { --全局定义
notification_email { --通知邮件
root@localhost --通知的收件人为本机的管理员
}
notification_email_from kaadmin@localhost --通知的发件人
smtp_server 127.0.0.1 --发件服务器为本机的邮件服务器
smtp_connect_timeout 30 --连接邮件服务器的超时时间
router_id LVS_DEVEL --路由器的标识
}
vrrp_script chk_haproxy { --定义haproxy服务的追踪脚本为chk_haproxy
script "killall -0 haproxy" --探测haproxy服务是否在线
interval 1 --每隔1S探测一次
weight -2 --如果haproxy服务不在线该节点权重就减二
}
vrrp_instance VI_1 { --定义虚拟路由的实例名称为VI_1
state MASTER --在VI_1实例中haproxy1主机这个节点为主节点
interface eth0 --所有的通告通过eth0接口进行
virtual_router_id 66 --虚拟路由ID为66
priority 100 --节点优先级为100(优先级范围0-255,数字越大,优先级越大)
advert_int 1 -- 初始化通告的个数
authentication { -- 认证机制
auth_type PASS --认证的类型为明文密码认证
auth_pass 12168 --认证的密码为12168
}
virtual_ipaddress { --定义虚拟地址,即VIP地址
172.16.70.35 --VIP地址为172.16.70.35
}
track_script {
chk_haproxy --在实例中定义追踪的脚本时chk_haproxy
}
notify_master "/etc/keepalived/notify.sh master" --当本节点为主节点时的邮件通知脚本
notify_backup "/etc/keepalived/notify.sh backup" --当本节点为backup节点时的邮件通知脚本
notify_fault "/etc/keepalived/notify.sh fault" --当本节点为fault时的邮件通知脚本
}
vrrp_instance VI_2 { --定义虚拟路由的实例名称为VI_2
state BACKUP --在VI_2实例中haproxy1主机这个节点为从节点
interface eth0 --所有的通告通过eth0接口进行
virtual_router_id 69 --虚拟路由ID为69
priority 99 --节点优先级为99(优先级范围0-255,数字越大,优先级越大)
advert_int 1 --初始化通告的个数
authentication { --认证机制
auth_type PASS --认证的类型为明文密码认证
auth_pass 22168 --认证的密码为22168
}
virtual_ipaddress { --定义虚拟地址,即VIP地址
172.16.70.135 --VIP地址为172.16.70.135
}
track_script {
chk_haproxy --在实例中定义追踪的脚本时chk_haproxy
}
notify_master "/etc/keepalived/notify.sh master" --当本节点为主节点时的邮件通知脚本
notify_backup "/etc/keepalived/notify.sh backup" --当本节点为backup节点时的邮件通知脚本
notify_fault "/etc/keepalived/notify.sh fault" --当本节点为fault时的邮件通知脚本
}
4、在haproxy2主机上编辑keepalived的主配置文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | [root@haproxy2 keepalived] # pwd /etc/keepalived [root@haproxy2 keepalived] # vim keepalived.conf global_defs {
notification_email {
root@localhost
}
notification_email_from kaadmin@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL }
vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 1
weight -2 }
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 66
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 12168
}
virtual_ipaddress {
172.16.70.35
}
track_script {
chk_haproxy }
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault" }
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 69
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 22168
}
virtual_ipaddress {
172.16.70.135
}
track_script {
chk_haproxy }
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault" } |
5、在haproxy1主机和haproxy2主机上同时启动keepalived服务和haproxy服务:
1 2 3 | [root@haproxy1 ~] # ssh haproxy2 ‘service keepalived start‘;service keepalived start Starting keepalived: [ OK ] Starting keepalived: [ OK ] |
1 2 3 | [root@haproxy1 ~] # ssh haproxy2 ‘service haproxy start‘;service haproxy start Starting haproxy: [ OK ] Starting haproxy: [ OK ] |
6、在haproxy1主机上查看获得的VIP地址为172.16.70.35,haproxy2主机上获得的VIP地址是172.16.70.135:
7、使用2个VIP都能访问后端静态和动态网站:
8、在haproxy1主机上关闭haproxy服务,发现VIP:172.16.70.35转移到haproxy2主机上去了,但仍然能够反向代理到后端服务器而提供web服务:
1 2 | [root@haproxy1 ~] # service haproxy stop Stopping haproxy: [ OK ] |
四、总结:
本次实验结合keepalived实现了haproxy反向代理的高可用,让两个haproxy都能同时为客户端提供反向代理服务,同时也实现了web站点的负载均衡,实现了网站访问的动静分离机制。。。
用keepalived来实现haproxy的高可用性能,布布扣,bubuko.com
标签:management 解决方案 代理服务 应用程序 content
原文地址:http://songoo.blog.51cto.com/8620619/1409093