众所周知,在我们实际部署应用服务的时候,高可用是一个必须满足的条件,这篇文档说下,比较常用的一种手段,通过keepalived来实现高可用。
keepalived实现了VRRP协议,VRRP(Virtual RouterRedundancy Protocol)协议是用于实现路由器冗余的协议,在 Keepalived服务正常工作时,主 Master节点会不断地向备节点发送(多播的方式)心跳消息,用以告诉备Backup节点自己还活看,当主 Master节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自主 Master节点的心跳了,于是调用自身的接管程序,接管主Master节点的 IP资源及服务。而当主 Master节点恢复时,备Backup节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。
本章中,采用的是keepalived-2.0.19 进行配置的;
- 首先下载安装包,可以在官网 https://www.keepalived.org/download.html 或者 keepalived安装包 下载即可。
- 安装
tar -zxvf keepalived-2.0.19.tar.gz
cd keepalived-2.0.19
./configure --prefix=/usr/local/keepalived
make
make install
- 配置
安装好后,默认情况下,keepalived会读取/etc/keepalived/keepalived.conf
的配置,我们将安装好后的/usr/local/keepalived/etc/keepalived/keepalived.conf
连接到/etc/keepalived
目录:
mkdir /etc/keepalived
cp keepalived-2.0.19/keepalived/etc/keepalived/keepalived.conf /etc/keepalived
将keepalvied加入系统服务:
#安装包目录下
#这是一个shell脚本
cp keepalived-2.0.19/keepalived/etc//init.d/keepalived /etc/init.d/keepalived
cp keepalived-2.0.19/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
这样就完成了keepalived 的安装。
如果报:
Starting keepalived: /bin/bash: keepalived: command not found
配置
这里用两台搭建,
- 192.168.199.6
- 192.168.199.8
192.168.199.6配置如下
:
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/chk_nginx.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 50
priority 90
advert_int 1
unicast_src_ip 192.168.199.6
unicast_peer {
192.168.199.8
}
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.199.100
}
}
192.168.199.8配置如下
:
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/chk_nginx.sh"
interval 5
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 50
priority 100
advert_int 1
unicast_src_ip 192.168.199.8
unicast_peer {
192.168.199.6
}
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.199.100
}
}
两台配置上:
virtual_router_id
保持一致,priority
不一致
这时候,通过192.168.199.100
虚IP就能够访问了。
启动keepalived:
/etc/init.d/keepalived start
开机自启动:
chkconfig keepalived on