共计 5498 个字符,预计需要花费 14 分钟才能阅读完成。
背景知识:
Keepalived:Keepalived 的作用是检测 web 服务器的状态,如果有一台 web 服务器死机,或工作出现故障,Keepalived 将检测到,并将有故障的 web 服务器从系统中剔除,当 web 服务器工作正常后 Keepalived 自动将 web 服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的 web 服务器。
LVS:LVS 是 Linux Virtual Server 的简写,意即 Linux 虚拟服务器,是一个虚拟的服务器集群系统。
实验系统:CentOS 6.6_x86_64
实验前提:提前准备好编译环境,防火墙和 selinux 都关闭
实验说明:本实验共有 4 台主机,其中 keep1 和 keep2 为 2 台前端的 keepalived 服务器,real1 和 real2 为 LVS 中的 realserver,IP 地址对应如拓扑图。
实验软件:httpd-2.2.15 keepalived-1.2.19
实验拓扑:
一、配置 realserver
1. 安装 httpd:
yum -y install httpd
2. 配置内核参数:
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore // 仅在请求的地址配置在请求报文的接口进行响应
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce // 表示仅通告网络直连的接口的地址
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
3. 增加测试页面并配置 VIP:
real1 上:
ifconfig lo:0 192.168.19.150 netmask 255.255.255.255 broadcast 192.168.19.150 up // 配置 VIP
ifconfig lo:1 192.168.19.151 netmask 255.255.255.255 broadcast 192.168.19.151 up
route add -host 192.168.19.150 dev lo:0 // 配置路由
route add -host 192.168.19.151 dev lo:1
vim /var/www/html/index.html
———————————————
<h1>realserver1</h1>
———————————————
service httpd start
real2 上 :
ifconfig lo:0 192.168.19.150 netmask 255.255.255.255 broadcast 192.168.19.150 up
ifconfig lo:1 192.168.19.151 netmask 255.255.255.255 broadcast 192.168.19.151 up
route add -host 192.168.19.150 dev lo:0
route add -host 192.168.19.151 dev lo:1
vim /var/www/html/index.html
———————————————
<h1>realserver2</h1>
———————————————
service httpd start
二、安装并配置 keepalived
1. 编译安装 keepalived,在 keep1 和 keep2 上操作:
wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
tar xf keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure –prefix=/usr/local/keepalived
make && make install
cp -p /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
chkconfig –add keepalived
chkconfig keepalived on
ln -sv /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
cp -p /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
ln -sv /usr/local/keepalived/etc/keepalived/ /etc/keepalived
yum -y install ipvsadm // 安装 LVS 工具
复制代码
2. 配置 keepalived:
keep1 上:
vim /etc/keepalived/keepalived.conf
—————————————————-
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 31
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass abcd
}
virtual_ipaddress {
192.168.19.150
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 41
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass abcd
}
virtual_ipaddress {
192.168.19.151
}
}
virtual_server 192.168.19.150 80 {
delay_loop 6
lb_algo rr //LVS 算法
lb_kind DR // 调度类型
protocol TCP
real_server 192.168.19.29 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
real_server 192.168.19.34 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
virtual_server 192.168.19.151 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.19.29 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
real_server 192.168.19.34 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
keep2 上:
vim /etc/keepalived/keepalived.conf
———————————————-
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 31
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass abcd
}
virtual_ipaddress {
192.168.19.150
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 41
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass abcd
}
virtual_ipaddress {
192.168.19.151
}
}
virtual_server 192.168.19.150 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.19.29 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
real_server 192.168.19.34 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
virtual_server 192.168.19.151 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.19.29 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
real_server 192.168.19.34 {
weight 1
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
3. 两台机器启动 keepalived:
service keepalived start
ipvsadm -L -n // 用 LVS 工具查看 keepalived 运行
ip addr show // 查看 VIP
两台机器上 LVS 规则都已经生效,且 2 个 VIP 分别运行在 2 个节点:
keep1 上:
keep2 上:
分别打开测试页面进行测试:
停掉任何一个节点,资源都会自动转移:
至此,演示完毕,谢谢!
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
Keepalived+Haproxy 配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm
Haproxy+Keepalived 构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm
CentOS 7 上配置 LVS + Keepalived + ipvsadm http://www.linuxidc.com/Linux/2014-11/109237.htm
Keepalived 高可用集群搭建 http://www.linuxidc.com/Linux/2014-09/106965.htm
LVS/DR + Keepalived 搭建负载均衡集群 http://www.linuxidc.com/Linux/2015-06/118647.htm
LVS+Keepalived 实现四层负载及高可用 http://www.linuxidc.com/Linux/2015-02/112695.htm
LVS+Keepalived 高可用负载均衡集群架构实验 http://www.linuxidc.com/Linux/2015-01/112560.htm
Heartbeat+LVS 构建高可用负载均衡集群 http://www.linuxidc.com/Linux/2014-09/106964.htm
搭建 LVS 负载均衡测试环境 http://www.linuxidc.com/Linux/2014-09/106636.htm
一个针对 LVS 的压力测试报告 http://www.linuxidc.com/Linux/2015-03/114422.htm
Keepalived 构建高可用 LVS 集群 http://www.linuxidc.com/Linux/2015-06/118967.htm
Keepalived 的详细介绍 :请点这里
Keepalived 的下载地址 :请点这里
本文永久更新链接地址 :http://www.linuxidc.com/Linux/2015-08/120883.htm