实时警报通知:微信告警通知的重要性解析
1034
2023-03-10
Keepalived+Nginx架构详解
Keepalived+Nginx架构
keepalived是一个类似于layer3、4、7交换机制的软件,也就是我们平时说的第3层、第4层和第7层交换。Keepalived的作用是检测web服务器的状态,如果有一台web服务器、Mysql服务器宕机,或工作出现故障,Keepalived将检测到后,会将有故障的web服务器或者Mysql服务器从系统中剔除,当服务器工作正常后Keepalived自动将web、Mysql服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的WEB和Mysql服务器。
keepalived的工作方式
keepalived在Layer3、4、7工作在IP/TCP协议栈的IP层,TCP层,及应用层,原理分别如下:
layer3:Keepalived使用Layer3的方式工作式时,Keepalived会定期向服务器群中的服务器发送一个ICMP的数据包(既我们平时用的Ping程序),如果发现某台服务的IP地址没有激活,Keepalived便报告这台服务器失效,并将它从服务器群中剔除,这种情况的典型例子是某台服务器被非法关机。Layer3的方式是以服务器的IP地址是否有效作为服务器工作正常与否的标准。
Layer4: Layer4主要以TCP端口的状态来决定服务器工作正常与否。如web server的服务端口一般是80,如果Keepalived检测到80端口没有启动,则Keepalived将把这台服务器从服务器群中剔除。
Layer7:Layer7就是工作在具体的应用层了,比Layer3,Layer4要复杂一点,在网络上占用的带宽也要大一些。Keepalived将根据用户的设定检查服务器程序的URL运行是否正常,如果与用户的设定不相符,则Keepalived将把服务器从服务器群中剔除。
keepalived的工作原理
keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议。
虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip),master会发组播给backup,当backup收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。这样的话就可以保证路由器的高可用了。
keepalived主要有三个模块,分别是core、check和vrrp。
core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。vrrp模块是来实现VRRP协议的。
keepalived配置文件详解:
完整的keepalived的配置文件,其配置文件keepalived.conf可以包含三个文本块:全局定义块、VRRP实例定义块及虚拟服务器定义块。全局定义块和虚拟服务器定义块是必须的,如果在只有一个负载均衡器的场合,就不须VRRP实例定义块。
keepalived+Nginx架构实例:
环境准备:
操作系统 | 2 台centos6.6 |
---|---|
nginx-master | 192.168.1.21 |
nginx-backup | 192.168.1.22 |
vip | 192.168.1.190 |
操作系统2 台centos6.6nginx-master192.168.1.21nginx-backup192.168.1.22vip192.168.1.190
安装keepalived:
在Nginx-master和Nginx-backup上同时进行:
修改Nginx-master配置文件:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc } notification_email_from localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL} vrrp_script chk_nginx {script "/usr/local/keepalived/sbin/check_nginx.sh"interval 2weight 2}vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 #设置成非抢占 authentication { auth_type PASS auth_pass linux123}virtual_ipaddress { 192.168.1.190}track_script {chk_nginx}}
! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc } notification_email_from localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL} vrrp_script chk_nginx {script "/usr/local/keepalived/sbin/check_nginx.sh"interval 2weight 2}vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 #设置成非抢占 authentication { auth_type PASS auth_pass linux123}virtual_ipaddress { 192.168.1.190}track_script {chk_nginx}}
修改Nginx-backup的配置文件:
! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc } notification_email_from localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL} vrrp_script chk_nginx { script "/usr/local/keepalived/sbin/check_nginx.sh" interval 2 weight 2}vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 #和master相同的优先级 priority 100 advert_int 1 authentication { auth_type PASS auth_pass linux123}virtual_ipaddress { 192.168.1.190}track_script {chk_nginx}}
! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc } notification_email_from localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL} vrrp_script chk_nginx { script "/usr/local/keepalived/sbin/check_nginx.sh" interval 2 weight 2}vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 #和master相同的优先级 priority 100 advert_int 1 authentication { auth_type PASS auth_pass linux123}virtual_ipaddress { 192.168.1.190}track_script {chk_nginx}}
检测脚本:
vim /usr/local/keepalived/sbin/check_nginx.sh #!/bin/bash#num=$(ps -ef|grep nginx|grep -v grep|grep -v check_nginx.sh)if [ "$(ps -ef|grep nginx|grep -v grep|grep -v check_nginx.sh)" == "" ];then /usr/local/nginx/sbin/nginx sleep 5 if [ "$(ps -ef|grep nginx|grep -v grep|grep -v check_nginx.sh)" == "" ];then /usr/local/nginx/sbin/nginx -s stop fifi
vim /usr/local/keepalived/sbin/check_nginx.sh #!/bin/bash#num=$(ps -ef|grep nginx|grep -v grep|grep -v check_nginx.sh)if [ "$(ps -ef|grep nginx|grep -v grep|grep -v check_nginx.sh)" == "" ];then /usr/local/nginx/sbin/nginx sleep 5 if [ "$(ps -ef|grep nginx|grep -v grep|grep -v check_nginx.sh)" == "" ];then /usr/local/nginx/sbin/nginx -s stop fifi
脚本加上可执行权限,不加会报错:
chmod +x /usr/local/keepalived/sbin/check_nginx.sh
chmod +x /usr/local/keepalived/sbin/check_nginx.sh
验证结果:
上述实验结果,如果nginx-master挂掉之后,keepalived会尝试启动Nginx,如果5秒后没有起来就会关闭Nginx,发生vip漂移到backup上。如果突然ngixn-master起来了,但是只能Nginx-master自动切换成backup。因为我们设置了Nginx-master为nopreempt。
keepalived几种设置状态总结:
如果状态相同(都是master或者backup),优先级高的为master。如果优先级一样,状态是master的就是master。两台master,优先级相同,后起的为master。两台backup,优先级相同,先起的为master。一台master,一台backup,优先级相同,但是将master设置成nopreempt,谁先起谁是master。(生产环境运用)
报错总结:
错误1.
Keepalived_vrrp[8204]: pid 8445 exited with status 32256解决:该脚本没有给权限script "/usr/local/keepalived/sbin/check_nginx.sh"
Keepalived_vrrp[8204]: pid 8445 exited with status 32256解决:该脚本没有给权限script "/usr/local/keepalived/sbin/check_nginx.sh"
错误2.
(VI_1): unknown state 'backup', defaulting to BACKUP解决:BACKUP必须要大写
(VI_1): unknown state 'backup', defaulting to BACKUP解决:BACKUP必须要大写
错误3.
Unknown keyword 'track_script{'解决:在track_script和{之间留个空格就好了
Unknown keyword 'track_script{'解决:在track_script和{之间留个空格就好了
发表评论
暂时没有评论,来抢沙发吧~