共计 5611 个字符,预计需要花费 15 分钟才能阅读完成。
Keepalived 的作用是检测 web 服务器的状态,如果有一台 web 服务器死机,或工作出现故障,Keepalived 将检测到,并将有故障的 web 服务器从系统中剔除,当 web 服务器工作正常后 Keepalived 自动将 web 服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的 web 服务器。
HAProxy 提供高可用性、负载均衡以及基于 TCP 和 HTTP 应用的代理, 支持虚拟主机, 它是免费、快速并且可靠的一种解决方案。HAProxy 特别适用于那些负载特大的 web 站点, 这些站点通常又需要会话保持或七层处理。HAProxy 运行在当前的硬件上, 完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整 合进您当前的架构中, 同时可以保护你的 web 服务器不被暴露到网络上。
系统环境: CenOS 6.5x86_64 Desktop install 将 selinux and iptables 设置为 disabled
主要用到软件:
haproxy-1.5.14.tar.gz
keepalived-1.1.17.tar.gz
nginx-1.7.8.tar.gz
图 1 为基本的架构图:
图 2 为 IP 地址分配。
一:安装过程,在两台 HA 机器上分别 keepalived:
#tar -zxvf keepalived-1.2.17.tar.gz
#ln -s /usr/src/kernels/2.6.18-128.el5-i686/ /usr/src/linux
#cd keepalived-1.2.17
[root@test25 keepalived-1.2.17]#./configure–with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.x86_64/
[root@test25keepalived-1.2.17]#make
[root@test25keepalived-1.2.17]#make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
cp/usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir/etc/keepalived/
cp/usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp/usr/local/sbin/keepalived /usr/sbin/
修改配置文件为:
Master:
#vim/etc/keepalived/ keepalived.conf
# catkeepalived.conf
! ConfigurationFile for keepalived
vrrp_scriptcheck_haproxy {
script”/etc/keepalived/check_haproxy.sh”
interval 2
weight 2
}
global_defs {
notification_email {
root@localhost
}
notification_email_fromkeepalived@server62.example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instanceVI_1 {
state MASTER
interface eth0
virtual_router_id 151
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress{
192.168.1.201
}
track_script {
check_haproxy
}
}
BACKUP:
# catkeepalived.conf
! ConfigurationFile for keepalived
vrrp_scriptcheck_haproxy {
script”/etc/keepalived/check_haproxy.sh”
interval 2
weight 2
}
global_defs {
notification_email {
root@localhost
}
notification_email_fromkeepalived@server63.example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instanceVI_1 {
state BACKUP
interface eth0
virtual_router_id 151
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress{
192.168.1.201
}
track_script {
check_haproxy
}
}
主机上:
#vi /etc/keepalived/check_haproxy.sh
#!/bin/bash
A=`ps -C haproxy –no-header | wc -l`
if [$A -eq 0];then
/usr/local/haproxy/sbin/haproxy -f/usr/local/haproxy/conf/haproxy.cfg
echo “haproxy start”
sleep 3
if [`ps -C haproxy –no-header | wc -l`-eq 0];then
/etc/init.d/keepalived stop
echo “keepalived stop”
fi
fi
备机上:
#!/bin/bash
A=`ip a | grep 10.2.32.201 | wc -l`
B=`ps -ef | grep haproxy | grep -v grep| awk ‘{print $2}’`
if [$A -gt 0];then
/usr/local/haproxy/sbin/haproxy -f/usr/local/haproxy/conf/haproxy.cfg
else
kill -9 $B
fi
# 两台机器分别执行:chmod 755 /etc/keepalived/check_haproxy.sh
haproxy 的安装(主备都一样):
#tar -zxvf haproxy-1.5.14.tar.gz
#cd haproxy-1.5.14
#make TARGET=linux26 PREFIX=/usr/local/haproxy
#makeinstall PREFIX=/usr/local/haproxy
#cd/usr/local/haproxy/
#mkdir conf
#mkdir logs
#vi haproxy.cfg
global
log127.0.0.1 local0
log127.0.0.1 local1 notice
maxconn 4096
chroot/usr/local/haproxy
uid 99
gid 99
daemon
#debug
#quiet
nbproc 1
pidfile/usr/local/haproxy/logs/haproxy.pid
defaults
log global
mode http
option httplog
option dontlognull
retries 3
#redispatch
maxconn 65535
optionredispatch
timeout connect5000
timeout client50000
timeout server50000
stats uri /haproxy
stats auth admin:admin
listen www.linuxidc.com *:80
cookie SERVERID rewrite
mode http
balance roundrobin
option httpchkHEAD /index.html HTTP/1.0
server web1 10.2.32.99:80 cookie app1inst1 weight 5check inter 2000 rise 2 fall 5
server web2 10.2.32.110:80 cookie app1inst2 weight 3check inter 2000 rise 2 fall 5
二:先主后从,两台机器上都分别启动:
/etc/init.d/keepalivedstart(如果之前没有启动 haproxy, 这条命令会自动把 haproxy 启动)
[root@test100 haproxy]# ps -ef | grephaprox
root 30344 1 0 11:43 ? 00:00:00 /bin/bash/etc/keepalived/check_haproxy.sh
nobody 30349 1 0 11:43 ? 00:00:00/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg
[root@test100 haproxy]# ps -ef | grepkeepali
root 30337 1 0 11:43 ? 00:00:00 keepalived -D
root 30339 30337 1 11:43 ? 00:00:00 keepalived -D
root 30340 30337 1 11:43 ? 00:00:00 keepalived -D
root 30372 2876 0 11:43 pts/0 00:00:00 grep keepali
三:测试:
1. 再两台 HA 上分别执行 ip ad sh |grep 10.2.32
主:
inet10.2.32.100/24 brd 10.2.32.255 scope global eth0
inet10.2.32.201/32 scope global eth0
备:
inet10.2.32.101/24 brd 10.2.32.255 scope global eth0
2. 停掉主上的 haproxy,3 秒后 keepalived 会自动将其再次启动
3. 停掉主的 keepalived,备机马上接管服务
备: ip ad sh |grep 10.2.32
inet10.2.32.101/24 brd 10.2.32.255 scope global eth0
inet10.2.32.201/32 scope global eth0
四:通过浏览器进行测试
http://www.linuxidc.com
两台 web 的页面会在你刷新时进行切换。此文将 nginx 安装,配置过程省略了,如果有问题可以查看以下相关文章。
CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm
使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm
搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm
CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm
CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm
CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm
Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm
Haproxy+Keepalived 搭建 Weblogic 高可用负载均衡集群 http://www.linuxidc.com/Linux/2013-09/89732.htm
Keepalived+HAProxy 配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm
CentOS 6.3 下 Haproxy+Keepalived+Apache 配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm
Haproxy + KeepAlived 实现 WEB 群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm
Haproxy+Keepalived 构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm
使用 HAProxy 配置 HTTP 负载均衡器 http://www.linuxidc.com/Linux/2015-01/112487.htm
HAproxy 的详细介绍:请点这里
HAproxy 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-02/128566.htm