1 什么是keepalived
keepalived是解决单点故障的一种解决方案,通过
VRRP 协议实现高可用性
VRRP 协议将两台或多台路由器设备虚拟成一个设备,对外提供虚拟路由器 IP(一个或多个),而在路由器组内部,如果实际拥有这个对外 IP 的路由器如果工作正常的话就是 MASTER,或者是通过算法选举产生, MASTER 实现针对虚拟路由器 IP 的各种网络功能,其他设备不拥有该虚拟 IP,状态是 BACKUP。
2ubuntu 安装keepalived
2.1 依赖下载
apt-get install libssl-dev
apt-get install openssl
apt-get install libpopt-dev
2.2 安装
tar -zxvf keepalived-1.2.15.tar.gz
./configure
make
Sudo make install
2.3 配环境
在/usr
find -name keepalived
cp /usr/local/sbin/keepalived /usr/sbin
拷贝可执行文件,没有这一步将会在执行service keepalived start的时候报错:keepalived: command not found
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
需要在/etc目录下创建keepalived目录
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
创建入口脚本文件,可以接收start| stop | status | restart
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
拷贝系统配置文件(上面提到的日志就是在这里配置的
2.4 改配置文件
sudo vi /etc/init.d/keepalived
# Source function library
#. /etc/rc.d/init.d/functions
./lib/lsb/init-functions
# Source configuration file (we set KEEPALIVED_OPTIONS there)
#. /etc/sysconfig/keepalived
. /etc/sysconfig/keepalived
start() {
echo -n $"Starting $prog: "
#daemon keepalived ${KEEPALIVED_OPTIONS}
daemon keepalived start
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}
2.5 更新启动服务
mkdir -p /var/lock/subsys
update-rc.d keepalived defaults
apt-get install daemon
apt-get install daemon
3 环境搭建
Vip |
Ip |
Nginx |
状态 |
|
http://192.168.137.135/ |
http://192.168.137.138/ |
端口80代理了Tomcat |
主 |
|
http://192.168.137.135/ |
http://192.168.137.139/ |
端口80代理了Tomcat |
备 |
|
外部统一访问 vip
Keep vip
192.168.137.135
判断同一个keepalived.conf下配置
为主节点的服务器是否
存活
Nginx服务器
Ip 192.168.137.138
BACKUP
否
是
Nginx服务器
ip 192.168.137.139
MASTER
选举一个主节点
Nginx服务器
Ip 192.168.137.138
MASTER
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 60
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(调用检测脚本)
}
virtual_ipaddress {
192.168.137.135
}
}
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/check_nginx_pid.sh"
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port #(调用检测脚本)
}
virtual_ipaddress {
192.168.137.135
}
}