Keepalived
Lvs Active 172.25.23.1 server1.example.com
Lvs Backup 172.25.23.2 server2.example.com
Lvs VIP 172.25.23.100
Real Server 172.25.23.3server3.example.com
172.25.23.4server4.example.com
Iptables selinux 关掉,加解析及时间同步
一.安装软件
Server1
下载安装包 keepalived-1.2.20.tar.gz
编译时需要的软件有ipvsadm(需要修改yum源,在yum源中添加高可用,负载均衡等) kernel-devel(基于内核转发的) openssl-devel popt-devel gcc
[root@server1 ~]# tar zxf keepalived-1.2.20.tar.gz
[root@server1 ~]# cd keepalived-1.2.20
[root@server1 keepalived-1.2.20]# ./configure --prefix=/usr/local/keepalived
[root@server1 keepalived-1.2.20]# make && make install
[root@server1 keepalived-1.2.20]# cd /usr/local/keepalived
[root@server1 keepalived]# scp -r /usr/local/keepalived/ root@172.25.23.2:/usr/local
Server1 Server2中 文件会保存/usr/local/keepalived/etc/rc.d/init.d,文件存放的路径不对,建立链接,操作会比较方便
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/keepalived/ /etc
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
/etc/init.d/keepalived status
二.Keepalived + lvs + http
1.编写配置文件并启动服务
[root@server1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost#配置管理员邮箱
}
notification_email_from #配置发件人
smtp_server 127.0.0.1#配置邮件服务器
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #配置模式
interface eth0
virtual_router_id 51 # 两台机器的虚拟路由ID一样,同一个广播域不同
priority 100 # 配置优先级 MASTER的优先级高于BACKUP
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.23.100#配置虚拟IP地址
}
}
virtual_server 172.25.23.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 50
protocol TCP
real_server 172.25.23.3 80 { #配置realaserver
weight 1
TCP_CHECK { #监控配置
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.25.23.4 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@server1 ~]# /etc/init.d/keepalived start
[root@server1 ~]# scp /etc/keepalived/keepalived.conf root@172.25.23.2:/etc/keepalived
# 将配置文件同步到slave
[root@server2 ~]# vim /etc/keepalived/keepalived.conf
# 简单修改一下slave配置文件 修改server1的绿色部分,state BACKUP priority 50
[root@server2 ~]# /etc/init.d/keepalived start
注意:Server3 server4 (real server)上需要添加DR模式,keepalived只负责lvs,而real server上的DR模式仍然需要自己配置
[root@server4 ~]# /etc/init.d/httpd start
2.测试
[root@server3 ~]# echo server3 > /var/www/html/index.html
[root@server4 ~]# echo server4 > /var/www/html/index.html
(1) 高可用测试:停止 master 上的 keepalived 服务,看 backup 是否接管。
(2) 负载均衡测试:访问 http://172.25.23.100,看到页面在两个 real server 上切换表示成功!也可以通过查看一下LVS状态 ipvsadm -Lnc 查看详细连接情况!
(3)故障切换测试(健康监测):任意关闭 realserver 上的 httpd 服务,Keepalived 监控模块是否能及时发现,然后屏蔽故障节点,同时将服务转移到正常节点来执行。
三.Keppalived + lvs + ftp
将httpd 服务换成ftp服务即可,即将80 --> 21。
注意:
ftp 的端口号为21,另外添加persistence_timeout 50,保持一个会话50S。因为ftp服务的数据流是21 -> 20的,若不保存一个会话,则会显示一直在进行ftp连接。
四.当所有的real server都宕机时
在集群中如果所有real server全部宕机了,客户端访问时就会出现错误页面,这样是很不友好的,我们得提供一个维护页面来提醒用户,服务器正在维护,什么时间可以访问等,下面我们就来解决一下这个问题。解决方案有两种,一种是提供一台备用的real server当所有的服务器宕机时,提供维护页面,但这样做有点浪费服务器。另一种就是在负载均衡器上提供维护页面,这样是比较靠谱的,也比较常用。下面我们就来具体操作一下。
(1)在master和slave端安装httpd及编写测试页
yum install -y httpd
echo “系统正在维护,请稍后访问“ > /var/www/html/index.html
/etc/init.d/httpd start
(2) 修改配置文件 master slave
Vim /etc/keepalived/keepalived.conf
---->
......
sorry_server 127.0.0.1 80 #增加一行sorry_server
}
#一定要将语句加在{}内部