目的
在运维的日常工作中和很多服务打交道,为了保证各个服务健康稳定运行,高可用和高负载是在一个服务搭建好后,必须要考虑的问题。本文介绍了一种常用的高可用和负载均衡的解决方案:KA+HA(haproxy+keepalived)
环境
haproxy keepalived 主:192.168.1.192
haproxy keepalived 备:192.168.1.193
vip:192.168.1.200
web:192.168.1.187:80
192.168.1.187:8000
架构图
安装过程
在192.168.1.192上:
keepalived的安装:
1 | tar -zxvf keepalived-1.1.17.tar.gz |
haproxy的安装(主备都一样):
1 | tar -zxvf haproxy-1.4.9.tar.gz |
两台机器上分别启动
1 | /etc/init.d/keepalived start (这条命令会自动把haproxy启动) |
验证测试
两台机器上分别执行
1 | ip add |
主: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:98:cd:c0 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.192/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.200/32 scope global eth0
inet6 fe80::20c:29ff:fe98:cdc0/64 scope link
valid_lft forever preferred_lft forever
备: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:a6:0c:7e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.193/24 brd 255.255.255.254 scope global eth0
inet6 fe80::20c:29ff:fea6:c7e/64 scope link
valid_lft forever preferred_lft forever
停掉主上的haproxy
3秒后keepalived会自动将其再次启动
停掉主的keepalived
备机马上接管服务
备: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:a6:0c:7e brd ff:ff:ff:ff:ff:ff
inet 192.168.1.193/24 brd 255.255.255.254 scope global eth0
inet 192.168.1.200/32 scope global eth0
inet6 fe80::20c:29ff:fea6:c7e/64 scope link
valid_lft forever preferred_lft forever
更改hosts
192.168.1.200 test.com
192.168.1.200 test.domain.com
通过IE测试,可以发现
test.com的请求发向了192.168.1.187:80
test.domain.com的请求发向了192.168.1.187:8000