Keepalived + Haproxy实现负载均衡以及调度器的高可用

Keepalived + Haproxy


 

Haproxy 提供高可用性、负载均衡以及基于 TCP HTTP 应用的代理,支持虚拟主机,

它是免费、快速并且可靠的一种解决方案。

HAProxy 特别适用于那些负载特大的 web 站点, 这些站点通常又需要会话保持或七层处理。HAProxy 运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中.

同时可以保护你的 web 服务器不被暴露到网络上.

事件驱动、单一进程模型。多进程或多线程模型受内存限制 、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space) 实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以 使每个CPU时间片(Cycle)做更多的工作。

Keepalived + Haproxy实现负载均衡以及调度器的高可用_第1张图片

拓扑结构

 

[vip: 202.1.1.100]

 

[LB1 Haproxy] [LB2 Haproxy]

202.1.1.17 202.1.1.18

 

[httpd] [httpd]

202.1.1.19 202.1.1.20

 

一、Haproxy实施步骤

1. 准备工作(集群中所有主机)

IP, hostname, hosts, iptables, SELinux, ssh trust, ntp

 

2. 调度器配置Haproxy(主/备)

# yum -y install haproxy

 

-----------------------------配置监控[可选]------------------------------

listen stats

bind *:1314

stats enable

stats refresh 30s

stats hide-version

stats uri /haproxystats

stats realm Haproxy\ stats

stats auth Ezra:123

stats admin if TRUE

----------------------------------------------------------------------------

 

frontend web

mode http

bind *:80

default_backend httpservers

 

backend httpservers

balance roundrobin

server http1 202.1.1.19:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2

server http2 202.1.1.20:80 maxconn 2000 weight 1 check inter 1s rise 2 fall 2

 

二、Keepalived实现调度器HA

注:主/备调度器均能够实现正常调度

1. 主/备调度器安装软件

# yum -y install keepalived

 

2. Keepalived

Master

[root@uplook ~]# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

router_id LVS_dr1 //辅助改为dr2

}

 

vrrp_instance VI_1 {

state MASTER //辅助改为BACKUP

nopreempt

interface eth0 //心跳接口,尽量单独连接心跳

virtual_router_id 80 //MASTER,BACKUP一致

priority 100 //辅助改为50

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

202.1.1.100

}

}

 

BACKUP

 

 

3. 启动KeepAlived(主备均启动)

 

4. 扩展对调度器Haproxy健康检查(可选)

思路:

让Keepalived以一定时间间隔执行一个外部脚本,脚本的功能是当Haproxy失败,则关闭本机的Keepalived

a. script

# cat /etc/keepalived/check_haproxy_status.sh

#!/bin/bash

/usr/bin/curl -I http://localhost &>/dev/null

if [ $? -ne 0 ];then

systemctl stop keepalived

fi

# chmod a+x /etc/keepalived/check_haproxy_status.sh

 

b. keepalived使用script

! Configuration File for keepalived

 

global_defs {

router_id LVS_dr1

}

 

vrrp_script check_haproxy {

script "/etc/keepalived/check_haproxy_status.sh"

interval 5

}

 

vrrp_instance VI_1 {

state BACKUP

interface eth0

nopreempt

virtual_router_id 90

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

202.1.1.100

}

 

track_script {

check_haproxy

}

}

 

三. 测试:

[root@wuyang ~]# curl 202.1.1.100

web1

[root@wuyang ~]# curl 202.1.1.100

web2

[root@wuyang ~]# curl 202.1.1.100

web1

[root@wuyang ~]# curl 202.1.1.100

web2

[root@wuyang ~]# curl 202.1.1.100

web1

[root@wuyang ~]# curl 202.1.1.100

web2

[root@wuyang ~]# curl 202.1.1.100

web1

[root@wuyang ~]# curl 202.1.1.100

web2

 

模拟一台web服务器故障:

[root@web1 ~]# reboot

 

客户端访问压力全部转向正常运转的服务器:

[root@wuyang ~]# curl 202.1.1.100

web2

[root@wuyang ~]# curl 202.1.1.100

web2

[root@wuyang ~]# curl 202.1.1.100

web2

[root@wuyang ~]# curl 202.1.1.100

web2

[root@wuyang ~]# curl 202.1.1.100

web2

[root@wuyang ~]# curl 202.1.1.100

web2

[root@wuyang ~]# curl 202.1.1.100

web2

 

模拟有VIPHaproxy调度器故障:

[root@haproxy2 keepalived]# ip addr

1: lo: 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: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 52:54:b7:19:3f:f4 brd ff:ff:ff:ff:ff:ff

inet 202.1.1.18/24 brd 202.1.1.255 scope global eth0

valid_lft forever preferred_lft forever

inet 202.1.1.100/32 scope global eth0

valid_lft forever preferred_lft forever

inet6 fe80::5054:b7ff:fe19:3ff4/64 scope link

valid_lft forever preferred_lft forever

 

[root@haproxy2 keepalived]# reboot

 

VIP迅速绑定到另外一台Haproxy调度器:

[root@haproxy1 keepalived]# ip addr

1: lo: 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: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000

link/ether 52:54:f6:24:80:dd brd ff:ff:ff:ff:ff:ff

inet 202.1.1.17/24 brd 202.1.1.255 scope global eth0

valid_lft forever preferred_lft forever

inet 202.1.1.100/32 scope global eth0

valid_lft forever preferred_lft forever

inet6 fe80::5054:f6ff:fe24:80dd/64 scope link

valid_lft forever preferred_lft forever

 

客户端访问正常:

[root@wuyang ~]# curl 202.1.1.100

web1

[root@wuyang ~]# curl 202.1.1.100

web2

[root@wuyang ~]# curl 202.1.1.100

web1

[root@wuyang ~]# curl 202.1.1.100

web2

[root@wuyang ~]# curl 202.1.1.100

web1

[root@wuyang ~]# curl 202.1.1.100

web2

[root@wuyang ~]# curl 202.1.1.100

web1

 

四. Haproxy图形化监控:

浏览器地址栏输入:

http://202.1.1.17:1314/haproxystats

 

你可能感兴趣的:(CentOS)