简介
什么是Keepalived
keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于heartbeat,用来防止单点故障。
Keepalived软件主要是通过VRRP协议实现高可用功能的。VRRP是Virtual Router RedundancyProtocol(虚拟路由器冗余协议)的缩写,VRRP出现的目的就是为了解决静态路由单点故障问题的,它能够保证当个别节点宕机时,整个网络可以不间断地运行。
Keepalived提供了负载均衡、健康检查和高可用的功能,高可用功能是由VRRP协议来实现的。
功能和模块
keepalived主要功能
- 管理LVS负载均衡软件
- 实现LVS集群节点的健康检查
- 作为系统网络服务的高可用性(failover)
keepalived三大模块
core模块:为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。
check模块:负责健康检查,包括常见的各种检查方式。
vrrp模块:是来实现VRRP协议的。
keepalived核心组件
用户空间核心组件:
- vrrp stack:VIP消息通告
- checkers:监测real server
- system call:实现 vrrp 协议状态转换时调用脚本的功能
- SMTP:邮件组件
- IPVS wrapper:生成IPVS规则
- Netlink Reflector:网络接口
- WatchDog:监控进程
控制组件:
- 提供keepalived.conf 的解析器,完成Keepalived配置
IO复用组件:
- 针对网络目的而优化的自己的线程抽象
内存管理组件:
- 为某些通用的内存管理功能(例如分配,重新分配,发布等)提供访问权限
架构和原理
keepalived架构图
keepalived工作原理
工作原理图:
工作原理:
keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议。
虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组。
这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip)master会发组播,当backup收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。
这样的话就可以保证路由器的高可用了。
Keepalived官网
keepalived官网首页:https://www.keepalived.org/
keepalived官网下载:https://www.keepalived.org/download.html
keepalived官网文档:https://www.keepalived.org/manpage.html
安装&管理
在线安装
#安装keepalived依赖
yum -y install gcc openssl openssl-devel curl libnl3-devel net-snmp-devel libssl-dev libpopt-dev daemon build-essential
yum -y install keepalived
离线安装
#安装keepalived依赖
yum -y install gcc openssl openssl-devel curl libnl3-devel net-snmp-devel libssl-dev libpopt-dev daemon build-essential
#安装keepalived
tar -zxvf keepalived-2.2.7.tar.gz -C /usr/local/
cd /usr/local/keepalived-2.2.7/
./configure --prefix=/usr/local/keepalived
make & make install
管理服务
查看版本
keepalived -v
/usr/local/keepalived/keepalived -v
启动服务
systemctl start keepalived
/usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf
关闭服务
systemctl stop keepalived
开机启动
systemctl enable keepalived
echo "/usr/local/keepalived/sbin/keepalived -f /usr/local/keepalived/etc/keepalived/keepalived.conf" >> /etc/rc.d/rc.local
chmod 755 /etc/rc.d/rc.local
查看进程
ps -ef | grep keepalived
配置&应用
配置服务
global_defs
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_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
#global_defs:声明此处配置为全局配置
#notification_email:声明此处为邮件配置(是可选配置,不建议使用)
#notification_email_from:指定邮件配置的发件人(是可选配置)
#smtp_server:指定邮件配置的邮件服务器地址(是可选配置)
#smtp_connect_timeout:指定邮件配置的邮件发送超时时间(是可选配置)
#router_id LVS_DEVEL:用于用户标识本节点的名称,一般配置为主机名,建议配置为IP地址标识
vrrp_instance
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16
192.168.200.17
192.168.200.18
}
}
#vrrp_instance VI_1:声明此处配置为VRRP配置
#state MASTER:声明本机的角色状态,有MASTER和BACKUP两种主备状态
#interface eth0:指定本机对外提供服务的网络接口,一般为主机网卡名称
#virtual_router_id 51:指定虚拟路由的ID标识,只有ID号相同的,才属于同一个虚拟路由器中
#priority 100:指定本机的优先级,数值越大,优先级越高,默认数值为100(此数值也是绝对谁是主备的唯一因素)
#advert_int 1:指定主备直接通信检查的时间间隔,默认为1,单位秒
#authentication:除了虚拟路由的ID表示确定是否为同一虚拟路由器外,还需要相同虚拟路由器中的权限认证,权限认证一致后才表明为同一虚拟路由总
#auth_type PASS:虚拟路由的认证方式,默认是密码认证,即PASS
#auth_pass 1111:虚拟路由的认证密码
#virtual_ipaddress:指定虚拟IP,可以配置多个虚拟IP。此处的虚拟IP是在工作时在主备直接漂移的IP
#192.168.200.16:可以指定多个虚拟IP,每个虚拟IP单独一行
virtual_server
virtual_server 192.168.200.100 443 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.201.100 443 {
weight 1
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
#virtual_server 10.10.10.3 1358:声明定义的一个虚拟服务器,语法:virtual_server+空格+IP+空格+端口
#delay_loop 3:在虚拟服务器中,健康检查的时间间隔,默认为3,单位秒
#lb_algo rr:虚拟服务器调度真实服务器的算法方式:默认为rr(轮询),还有wlc(权重)
#lb_kind NAT:虚拟服务器转发真实服务器的负载方式:默认为NAT,还有DR和TUN
配置参考
参考配置-master
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
cat >> /etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.147.136
}
}
EOF
#router_id Kubernetes_keepalived:用于用户标识本节点的名称
#state MASTER:注意主节点使用MASTER,备节点使用BACKUP;
#interface ens33:确定该节点的网卡名称名称;
#virtual_router_id 51:同一组节点使用相同ID;
#priority 100:指定本机的优先级,数值越大,优先级越高,默认数值为100;
#advert_int 1:指定主备直接通信检查的时间间隔,默认为1,单位秒;
#authentication:确定认证方式;
#virtual_ipaddress:指定虚拟IP,可以配置多个虚拟IP
配置参考-backup
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
cat >> /etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.147.136
}
}
EOF
应用举例
主机规划
主机名 | IP地址 | 主机规划 | 备注 |
---|---|---|---|
Kubernetes-01-master01 | 192.168.147.130 | keepalived主机-master | |
Kubernetes-02-master02 | 192.168.147.131 | keepalived主机-backup01 | |
Kubernetes-03-master03 | 192.168.147.132 | keepalived主机-backup02 | |
192.168.147.136 | 虚拟VIP |
安装keepalived
三个节点都要安装
yum -y install keepalived
配置keepalived
配置Kubernetes-01-master01
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
cat > /etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
router_id Kubernetes_keepalived
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.147.136
}
}
EOF
配置Kubernetes-02-master02
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
cat > /etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
router_id Kubernetes_keepalived
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.147.136
}
}
EOF
配置Kubernetes-03-master03
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
cat > /etc/keepalived/keepalived.conf <<EOF
! Configuration File for keepalived
global_defs {
router_id Kubernetes_keepalived
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.147.136
}
}
EOF
管理keepalived
keepalived启动
systemctl start keepalived
systemctl enable keepalived
keepalived状态
ps -ef | grep keepalived
systemctl status keepalived
查看VIP地址
Kubernets-01-master01
ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:6b:30:18 brd ff:ff:ff:ff:ff:ff
inet 192.168.147.130/24 brd 192.168.147.255 scope global noprefixroute dynamic ens33
valid_lft 1764sec preferred_lft 1764sec
inet 192.168.147.136/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::2cb6:fe24:e72c:ae77/64 scope link noprefixroute
valid_lft forever preferred_lft forever
Kubernets-02-master02
ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:18:e8:ff brd ff:ff:ff:ff:ff:ff
inet 192.168.147.131/24 brd 192.168.147.255 scope global noprefixroute dynamic ens33
valid_lft 1036sec preferred_lft 1036sec
inet 192.168.147.136/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::cbd5:cf51:bcd9:716c/64 scope link noprefixroute
valid_lft forever preferred_lft forever
Kubernetes-03-master03
ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:01:cd:ec brd ff:ff:ff:ff:ff:ff
inet 192.168.147.132/24 brd 192.168.147.255 scope global noprefixroute dynamic ens33
valid_lft 1610sec preferred_lft 1610sec
inet 192.168.147.136/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::b57d:bcaa:bfad:1546/64 scope link noprefixroute
valid_lft forever preferred_lft forever