||
在集群当中离不开的一个词就是是高可用,用本文来简单聊聊Keepalived是什么、Keepalived如何实现高可用、Keepalived的使用场景。
因此,实现的效果应是一主一备,主机宕机备机上线提供服务,不可能同时提供服务。访问业务通过的入口为vip,因此我们引入Keepalived,让其跑在两台nginx上,达到Keepalived之间的相互通信,检测心跳,如果主机宕机,Kpalived就将主机的vip漂移到备机上,这样就可以实现不需要关注这两台nginx的具体ip地址是多少就可以实现来者之间高可用的切换。至于宕机的服务器恢复以后重新上线是否作为主机就要根据配置文件里面的优先级进行竞选。
Keepalived是一款保证集群高可用的一个服务软件,用来防止单点故障。Kepalived是以VRRP(Virtual Router Redundancy Protocol)协议(虚拟路由冗余协议)为实现基础的。
虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。这样的话就可以保证路由器的高可用了。
假设nginx服务器为192.168.1.1、192.168.1.2,vip为192.168.1.200:
为机子安装环境,采用yum源安装:
[root@localhost ~]# yum -y install kernel-devel*
[root@localhost ~]# yum -y install openssl-*
[root@localhost ~]# yum -y install popt-devel
[root@localhost ~]# yum -y install lrzsz
[root@localhost ~]# yum -y install openssh-clients
[root@localhost ~]# yum -y install libnl libnl-devel popt
[root@localhost ~]# yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
/usr/local/
目录:[root@localhost ~]# cd /usr/local/
[root@localhost local]# tar zxvf keepalived-1.2.15.tar.gz
[root@localhost local]# cd keepalived-1.2.15/
[root@localhost keepalived-1.2.15]#
[root@localhost keepalived-1.2.15]# ./configure --prefix=/usr/local/keepalived
[root@localhost keepalived-1.2.15]# make
[root@localhost keepalived-1.2.15]# make install
[root@localhost keepalived-1.2.15]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@localhost keepalived-1.2.15]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/keepalived
[root@localhost keepalived-1.2.15]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost keepalived-1.2.15]# mkdir -p /etc/keepalived
[root@localhost keepalived-1.2.15]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
[root@localhost keepalived-1.2.15]# chmod +x /etc/init.d/keepalived
# 添加时必须保证/etc/init.d/keepalived存在
[root@localhost keepalived-1.2.15]# chkconfig --add keepalived
[root@localhost keepalived-1.2.15]# chkconfig keepalived on
# 添加完可查询系统服务是否存在
[root@localhost keepalived-1.2.15]# chkconfig --list
启动:service keepalived start
停止:service keepalived stop
重启:service keepalived restart
[root@localhost keepalived-1.2.15]# vi /etc/sysconfig/keepalived
# 添加:
KEEPALIVED_OPTIONS="-D -d -S 0"
[root@localhost keepalived-1.2.15]# vi /etc/rsyslog.conf
# 内容:
local0.* /var/log/keepalived.log
[root@localhost keepalived-1.2.15]# service rsyslog restart
[root@localhost keepalived-1.2.15]# service keepalived restart
! Configuration File for keepalived
global_defs {
router_id NG_DEVEL1
}
vrrp_instance NG {
# 标示为主nginx
state MASTER
# HA检测端口,查看主机服务器的网卡
interface eth33
# 主备上的id必须相同
virtual_router_id 51
# 配置优先级,本示例备机比主机优先级小
priority 100
# VRRP Multicast 广播周期秒数
advert_int 1
# 定义认证
authentication {
# 认证方式为口令认证
auth_type PASS
# 定义口令
auth_pass 1111
}
# 定义vip,可以设置多个
virtual_ipaddress {
192.168.1.200
}
}
! Configuration File for keepalived
global_defs {
router_id NG_DEVEL2
}
vrrp_instance NG {
# 标示为备nginx
state BACKUP
# HA检测端口,查看主机服务器的网卡
interface eth33
# 主备上的id必须相同
virtual_router_id 51
# 配置优先级,本示例备机比主机优先级小
priority 50
# VRRP Multicast 广播周期秒数
advert_int 1
# 定义认证
authentication {
# 认证方式为口令认证
auth_type PASS
# 定义口令
auth_pass 1111
}
# 定义vip,可以设置多个
virtual_ipaddress {
192.168.1.200
}
}
[root@localhost keepalived-1.2.15]# service keepalived restart
Keepalived的使用不止局限于nginx,他的作用是作用于服务器,去检测服务器来实现单点访问的高可用,因此可以使用keepalived的场景就十分丰富了,lvs可以使用,也可以通过keepalived来实现MySQL的双主模式切换,也可以设置多个vip绑定在不同的机器上,通过dns轮询的方式访问vip,如果发生宕机就把宕机的主机vip漂移到其他主机上。在由文章中的模拟实验环境搭建可以参考上一篇文章《搭建基于lvs+nginx的负载均衡服务集群》。
keepalived的原理其实很简单就是通过服务器上的keepalived进程进行相互通信,这个时候需要注意,假设服务器没有宕机但是服务器的业务报错了,这个时候没有实现切换,可以写相应的运维脚本,通过脚本检测当前的业务是否出现错误,出现错误就将keepalived进程kill掉来实现切换。这里就不继续展开了。* GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源。
合作电话:010-64087828
社区邮箱:greatsql@greatdb.com