当前位置:网站首页>keepalived安装使用与快速入门
keepalived安装使用与快速入门
2022-07-02 05:53:00 【进击的程序猿~】
文章目录
常用中间件相关链接:
nginx安装使用与快速入门: https://blog.csdn.net/qq_41822345/article/details/121973966
zookeeper安装使用与快速入门: https://blog.csdn.net/qq_41822345/article/details/118862232
kafka安装使用与快速入门: https://blog.csdn.net/qq_41822345/article/details/118874681
rocketmq安装使用与快速入门: https://blog.csdn.net/qq_41822345/article/details/118636458
一、keepalived的前世今生
Keepalived起初是为LVS设计的——它的作用是检测服务器的状态【根据TCP/IP参考模型的第三、第四层、第五层交换机制检测每个服务节点的状态】,如果有一台web服务器宕机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
Keepalived后来又加入了VRRP的功能,VRRP(Vritrual Router Redundancy Protocol,虚拟路由冗余协议)出现的目的是解决静态路由出现的单点故障问题,通过VRRP可以实现网络不间断稳定运行,因此Keepalvied一方面具有服务器状态检测和故障隔离功能,另外一方面也有HAcluster功能。
总结:健康检查和失败切换是keepalived的两大核心功能。所谓的健康检查,就是采用tcp三次握手,icmp请求,http请求,udp echo请求等方式对负载均衡器后面的实际的服务器(通常是承载真实业务的服务器)进行保活;而失败切换主要是应用于配置了主备模式的负载均衡器,利用VRRP维持主备负载均衡器的心跳,当主负载均衡器出现问题时,由备负载均衡器承载对应的业务,从而在最大限度上减少流量损失,并提供服务的稳定性。
二、keepalived功能
单点故障:keepalived服务简单来说,就是用来防止单点故障的。所谓的单点故障就是,主服务器挂了之后从服务器充当主服务器,原来的主服务器恢复后,当从服务器来使用,保证服务的高可用性。
1、实现对LVS集群节点健康检查功能(healthcheck)
通常情况下,keepalived和LVS负载均衡软件一起出现,用来管理和监控整个集群的节点状态。
Keepalived可以通过在自身的keepalived.conf
文件里配置LVS的节点IP和相关参数实现对LVS的直接管理;除此之外,当LVS集群中的某一个甚至是几个节点服务器同时发生故障无法提供服务时,Keepalived服务会自动将失效的节点服务器从LVS的正常转发队列中清除出去,并将请求调度到别的正常节点服务器上,从而保证最终用户的访问不受影响;当故障的节点服务器被修复以后,Keepalived服务又会自动地把它们加入到正常转发队列中,对客户提供服务。
2、为系统网络服务的高可用功能
Keepalived 高可用功能实现的简单原理为,两台主机同时安装好Keepalived软件并启动服务,开始正常工作时,由角色为Master的主机获得所有资源并对用户提供服务,角色为Backup的主机作为Master主机的热备;当角色为Master的主机失效或出现故障时,角色为Backup的主机将自动接管Master主机的所有工作,包括接管VIP资源及相应资源服务;而当角色为Master的主机故障修复后,又会自动接管回它原来处理的工作,角色为Backup的主机则同时释放Master主机失效时它接管的工作,此时,两台主机将恢复到最初启动时各自的原始角色及工作状态。
3、Keepalived高可用故障切换转移原理
Keepalived高可用服务对之间的故障切换转移,是通过VRRP(Virtual Router Redundancy Protocol,虚拟路由器冗余协议)来实现的。
原理:在Keepalived服务正常工作时,主Master节点会不断地向备节点发送(多播的方式)心跳消息,用以告诉备Backup节点自己还活着,当主Master节点发生故障时,就无法发送心跳消息,备节点也就因此无法继续检测到来自主Master节点的心跳了,于是调用自身的接管程序,接管主Master节点的IP资源及服务。而当主Master节点恢复时,备Backup节点又会释放主节点故障时自身接管的IP资源及服务,恢复到原来的备用角色。
三、keepalived通信原理
1、在网络中,主机之间的通信都是通过配置静态路由或者(默认网关)来完成的,而主机之间的路由器一旦发生故障,服务就会中断,因此这种通信模式当中,路由器就成了一个单点瓶颈,为了解决这个问题,就引入了VRRP协议。
2、VRRP协议是一种容错的主备模式的协议,保证当主机的下一跳路由出现故障时,由另一台路由器来代替出现故障的路由器进行工作,通过VRRP可以在网络发生故障时透明的进行设备切换而不影响主机之间的数据通信。
3、VRRP是通过一种竞选(election)协议机制来将路由任务交给某台VRRP路由器的。
4、工作时主节点发包,备节点接包,当备节点接收不到主节点发的数据包的时候,就启动接管程序接管主节点的资源。备节点可以有多个,通过优先级竞选。
5、VRRP路由器在运行过程中有三种状态:
a、Initialize状态
:系统启动后就进入Initialize,此状态下路由器不对VRRP报文做任何处理;
b、Master状态
;一般主路由器处于Master状态。
c、 Backup状态
;备份路由器处于Backup状态。
6、VRRP选举机制
- a、VRRP组中匹配IP拥有者。如果虚拟IP地址与VRRP组中的某台VRRP路由器IP地址相同,则此路由器为IP地址拥有者,这台路由器将被定位主路由器。
- b、比较优先级。如果没有IP地址拥有者,则比较路由器的优先级,优先级的范围是0~255,优先级大的作为主路由器。
- c、比较IP地址。在没有Ip地址拥有者和优先级相同的情况下,IP地址大的作为主路由器。
四、实战:搭建Keepalived+Nginx 高可用集群
- 安装nginx
step1:先安装好nginx:https://blog.csdn.net/qq_41822345/article/details/121973966
step2:再编辑好nginx的index.html文件
# 在机器1上192.168.168.101
vim /usr/local/nginx/html/index.html
# 输入内容如下:
hello,i am k8s101,my ip is 192.168.168.101.
# 在机器2上192.168.168.102
vim /usr/local/nginx/html/index.html
# 输入内容如下:
hello,i am k8s102,my ip is 192.168.168.102.
step3:启动nginx
# 两个机器上都启动
cd /usr/local/nginx/sbin
./nginx
ps -ef|grep nginx
# 输出如下,说明启动成功
root 24447 1 0 20:26 ? 00:00:00 nginx: master process ./nginx
nobody 24448 24447 0 20:26 ? 00:00:00 nginx: worker process
step4:验证,访问http://192.168.168.101/和http://192.168.168.102/
step5:添加检测nginx健康脚本
vim /usr/local/nginx/nginx_check.sh
## 脚本内容如下
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
## 给脚本加上执行权限
chmod +x /usr/local/nginx/nginx_check.sh
- 安装keepalived
step1:两台机器上都执行
yum install keepalived -y
step2:配置机器1 [192.168.168.101]
[[email protected] ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
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
}
vrrp_instance VI_1 {
state MASTER ## 主,VIP会先绑定到主上
interface ens33 ## 绑定的网卡
virtual_router_id 51 ## 路由id,主备必须一致
priority 150 ## 优先级[0~255],主必须大于备
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.168.110/24 dev ens33 label ens33:1 ## VIP
}
}
... ...
step3:配置机器2 [192.168.168.102]
[[email protected] ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
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
}
vrrp_instance VI_2 {
state BACKUP ## 备,当主挂了之后,VIP会绑定到备上
interface ens33 ## 绑定的网卡
virtual_router_id 51 ## 路由id,主备必须一致
priority 100 ## 优先级[0~255],备必须小于主
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.168.110/24 dev ens33 label ens33:1 ## VIP
}
}
......
step4:启动机器1和机器2上keepalived服务
[[email protected] ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[[email protected] ~]# systemctl start keepalived
[[email protected] ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2022-06-15 21:05:46 CST; 1s ago
Process: 118831 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 118832 (keepalived)
Tasks: 3
Memory: 6.1M
CGroup: /system.slice/keepalived.service
├─118832 /usr/sbin/keepalived -D
├─118833 /usr/sbin/keepalived -D
└─118834 /usr/sbin/keepalived -D
6月 15 21:05:46 k8s101 Keepalived_healthcheckers[118833]: Activating healthchecker for service [192.168.200.100]:443
6月 15 21:05:46 k8s101 Keepalived_healthcheckers[118833]: Activating healthchecker for service [10.10.10.2]:1358
6月 15 21:05:46 k8s101 Keepalived_healthcheckers[118833]: Activating healthchecker for service [10.10.10.2]:1358
6月 15 21:05:46 k8s101 Keepalived_healthcheckers[118833]: Activating healthchecker for service [10.10.10.3]:1358
6月 15 21:05:46 k8s101 Keepalived_healthcheckers[118833]: Activating healthchecker for service [10.10.10.3]:1358
6月 15 21:05:46 k8s101 Keepalived_vrrp[118834]: VRRP_Instance(VI_1) removing protocol VIPs.
6月 15 21:05:46 k8s101 Keepalived_vrrp[118834]: VRRP_Instance(VI_1) removing protocol iptable drop rule
6月 15 21:05:46 k8s101 Keepalived_vrrp[118834]: Using LinkWatch kernel netlink reflector...
6月 15 21:05:46 k8s101 Keepalived_vrrp[118834]: VRRP_Instance(VI_1) Entering BACKUP STATE
6月 15 21:05:46 k8s101 Keepalived_vrrp[118834]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
[[email protected] ~]# ip a s
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:79:7a:ea brd ff:ff:ff:ff:ff:ff
inet 192.168.168.101/24 brd 192.168.168.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.168.110/24 scope global secondary ens33:1 ## VIP 192.168.168.110【要稍等一会才会显示出来,说明为网卡ens33绑定VIP成功,但是在机器2上观察不到VIP】
valid_lft forever preferred_lft forever
inet6 fe80::4b54:3c90:6b87:162e/64 scope link noprefixroute
valid_lft forever preferred_lft forever
step5:访问VIP http://192.168.168.110/
step6:关闭机器1上的keepalived,继续访问http://192.168.168.110/
[[email protected] ~]# systemctl stop keepalived
[[email protected] sbin]# ip a s
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:34:21:6f brd ff:ff:ff:ff:ff:ff
inet 192.168.168.102/24 brd 192.168.168.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.168.110/24 scope global secondary ens33:1 ## VIP 192.168.168.110【VIP从机器1飘到了机器2,说明VIP没有问题】
valid_lft forever preferred_lft forever
inet6 fe80::a5ca:1450:a012:aa1f/64 scope link noprefixroute
valid_lft forever preferred_lft forever
附注,对于主从模式的keepalived,可以通过关闭一个keepalived服务,模仿机器挂了,只能访问到一个机器的服务。
但是,对于主主模式[即两台机器互为主备]的keepalived,关闭一个keepalived服务,还是可以访问两个机器的服务。
边栏推荐
- Several keywords in C language
- Taskbar explicit / implicit toggle function
- php继承(extends)
- Zzuli:1066 character classification statistics
- Generate QR code
- PHP read file (read the specified line containing a string in the file)
- PHP inner class name is the same as the inner class method name
- Vite打包后的dist不能直接在浏览器打开吗
- JS determines whether the mobile terminal or the PC terminal
- Some experience of exercise and fitness
猜你喜欢
all3dp. All Arduino projects in com website (2022.7.1)
idea開發工具常用的插件合集匯總
深度学习分类网络--Network in Network
Regular expression summary
2022-2-14 learning xiangniuke project - Section 7 account setting
Technologists talk about open source: This is not just using love to generate electricity
Appnuim environment configuration and basic knowledge
[paper translation] gcnet: non local networks meet squeeze exception networks and beyond
ThreadLocal memory leak
ESP8266与STC8H8K单片机联动——天气时钟
随机推荐
STC8H8K系列汇编和C51实战——按键允许按键计数(利用下降沿中断控制)
Balsamiq wireframes free installation
[paper translation] gcnet: non local networks meet squeeze exception networks and beyond
Software testing - concept
all3dp. All Arduino projects in com website (2022.7.1)
PHP extensions
TI毫米波雷达学习(一)
PHP obtains some values in the string according to the specified characters, and reorganizes the remaining strings into a new array
1036 Boys vs Girls
JS determines whether the mobile terminal or the PC terminal
Reading notes of cgnf: conditional graph neural fields
PHP parent
Test case
1036 Boys vs Girls
页面打印插件print.js
How vite is compatible with lower version browsers
Mathematical statistics and machine learning
memcached安装
5g market trend in 2020
1035 Password