Keepalived集群软件高级使用

Keepalived集群软件高级使用,第1张

Keepalived集群软件高级使用(工作原理和状态通知)

1。导言

keepalived的应用场景主要有两种。一种是通过用ipv配置Keepalived来实现负载均衡(LVS+Keepalived)。有这方面需求的可以参考之前的博文:http://lizhenliang.blog.51cto.com/7876557/1343734.另一种是通过自健康检查和资源接管功能做高可用(双机热备)实现故障转移。

以下内容主要基于keepalived+MySQL双主实现双机热备,主要讲解keepalived的状态转换通知功能,可以有效加强对MySQL数据库的监控。本文将不描述Keepalived+MySQL的双主部署过程。有需要的可以参考之前的博文:http://lizhenliang.blog.51cto.com/7876557/1362313.

2。keepalived的主要功能

Keepalived采用VRRP(VirtualRouterRedundancyProtocol,虚拟路由冗余协议),以软件的形式实现服务器热备功能。通常,两台linux服务器构成一个主备份。同时,主备份中只有一个主服务器提供服务。同时,主机虚拟一个共享IP地址(VIP),只存在于主机上,对外提供服务。如果keepalived检测到主服务器宕机或服务失败,备份服务器会自动接管VIP成为主服务器,keepalived会将主服务器从热备组中移除,主服务器恢复后会自动加入热备组,默认情况下会抢占成为主服务器,起到故障转移的作用。

3。在三层、四层和七层工作的原则

第三层:在第三层工作时,keepalived会定期向热备用组中的服务器发送互联网控制消息协议,以确定某个服务器是否停机,如果停机,则将该服务器从热备用组中移除。

第四层:在第四层工作时,keepalived根据TCP端口的状态判断服务器是否出现故障,比如检测mysql3306端口,如果出现故障,则将该服务器从热备组中移除。

示例: ! Configuration File for keepalived global_defs {    notification_email {      [email protected]    }    notification_email_from  [email protected]    smtp_server 127.0.0.1    smtp_connect_timeout 30    router_id MYSQL_HA } vrrp_instance VI_1 {     state BACKUP     interface eth1     virtual_router_id 50     nopreempt                   #当主down时,备接管,主恢复,不自动接管     priority 100     advert_int 1     authentication {         auth_type PASS         ahth_pass 123     }     virtual_ipaddress {         192.168.1.200          #虚拟IP地址     } } virtual_server 192.168.1.200 3306 {             delay_loop 6 #    lb_algo rr  #    lb_kind NAT     persistence_timeout 50     protocol TCP     real_server 192.168.1.201 3306 {       #监控本机3306端口         weight 1         notify_down /etc/keepalived/kill_keepalived.sh   #检测3306端口为down状态就执行此脚本(只有keepalived关闭,VIP才漂移 )          TCP_CHECK {         #健康状态检测方式,可针对业务需求调整(TTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK)             connect_timeout 3             nb_get_retry 3             delay_before_retry 3         }     } }

第七层:在7层工作时,keepalived根据用户设置的策略判断服务器上的程序是否正常运行,如果失败就将该服务器从热备组中移除。

示例: ! Configuration File for keepalived global_defs {    notification_email {      [email protected]    }    notification_email_from  [email protected]    smtp_server 127.0.0.1    smtp_connect_timeout 30    router_id MYSQL_HA } vrrp_script check_nginx {     script /etc/keepalived/check_nginx.sh    #检测脚本     interval 2   #执行间隔时间 } vrrp_instance VI_1 {     state BACKUP     interface eth1     virtual_router_id 50     nopreempt                   #当主down时,备接管,主恢复,不自动接管     priority 100     advert_int 1     authentication {         auth_type PASS         ahth_pass 123     }     virtual_ipaddress {         192.168.1.200          #虚拟IP地址     }     track_script {          #在实例中引用脚本         check_nginx     } } 脚本内容如下: # cat /etc/keepalived/check_nginx.sh Count1=`netstat -antp |grep -v grep |grep nginx |wc -l` if [ $Count1 -eq 0 ]; then     /usr/local/nginx/sbin/nginx     sleep 2     Count2=`netstat -antp |grep -v grep |grep nginx |wc -l`     if [ $Count2 -eq 0 ]; then         service keepalived stop     else         exit 0     fi  else     exit 0 fi

4。健康状态检测方法

4.1HTTP服务状态检测

  HTTP_GET或SSL_GET {           url {           path /index.html        #检测url,可写多个           digest  24326582a86bee478bac72d5af25089e    #检测效验码           #digest效验码获取方法:genhash -s IP -p 80 -u http://IP/index.html            status_code 200         #检测返回http状态码       }       connect_port 80 #连接端口       connect_timeout 3  #连接超时时间       nb_get_retry 3  #重试次数       delay_before_retry 2 #连接间隔时间    }

4.2TCP端口状态检测(基本上所有的TCP端口服务都可以使用)

  TCP_CHECK {           connect_port 80     #健康检测端口,默认为real_server后跟端口       connect_timeout 5       nb_get_retry 3       delay_before_retry 3   }

4.3邮件服务器的SMTP检测

  SMTP_CHECK {            #健康检测邮件服务器smtp           host {           connect_ip           connect_port       }       connect_timeout 5       retry 2       delay_before_retry 3       hello_name "mail.domain.com"   }

4.4用户定义脚本检测real_server服务的状态

  MISC_CHECK {           misc_path /script.sh    #指定外部程序或脚本位置       misc_timeout 3      #执行脚本超时时间       !misc_dynamic       #不动态调整服务器权重(weight),如果启用将通过退出状态码动态调整real_server权重值   }

5。状态转换通知功能

Keepalived主要配置了邮件通知功能。默认情况下,当real_server关闭或恢复时,将发送电子邮件。有时候我们会想知道keepalived的主服务器故障转移后,VIP是否平滑漂移到备用服务器,MySQL服务器是否正常?编写一个监控脚本。可以,但没必要。因为keepalived有状态检测功能,我们直接用就可以了。

主配置默认邮件通知配置模板如下: global_defs           # Block id     {     notification_email    # To:         {         [email protected]         ...          }     # From: from address that will be in header     notification_email_from [email protected]     smtp_server 127.0.0.1   # IP     smtp_connect_timeout 30 # integer, seconds     router_id my_hostname   # string identifying the machine,                             # (doesn't have to be hostname).     enable_traps            # enable SNMP traps         }

5.1实例状态通知

A)notify_master:当节点成为主节点时执行。

B)notify_backup:当节点成为备份时执行

C)notify_fault:当一个节点成为故障时执行。

5.2虚拟服务器检测通知

A)notify_up:当虚拟服务器启动时执行。

B)notify_down:当虚拟服务器关闭时执行

示例:     ! Configuration File for keepalived     global_defs {        notification_email {          [email protected]        }        notification_email_from [email protected]         smtp_server 127.0.0.1        smtp_connect_timeout 30        router_id MYSQL_HA     }     vrrp_instance VI_1 {         state BACKUP         interface eth1         virtual_router_id 50         nopreempt           #当主down时,备接管,主恢复,不自动接管         priority 100         advert_int 1         authentication {             auth_type PASS             ahth_pass 123         }         virtual_ipaddress {             192.168.1.200         }             notify_master /etc/keepalived/to_master.sh             notify_backup /etc/keepalived/to_backup.sh             notify_fault /etc/keepalived/to_fault.sh     }     virtual_server 192.168.1.200 3306 {         delay_loop 6         persistence_timeout 50         protocol TCP         real_server 192.168.1.201 3306 {             weight 1             notify_up /etc/keepalived/mysql_up.sh             notify_down /etc/keepalived/mysql_down.sh                 TCP_CHECK {                 connect_timeout 3                 nb_get_retry 3                 delay_before_retry 3             }         }     }

status参数后面可以跟bash命令或者shell脚本,具体内容根据自己的需要定义。上例中涉及的状态脚本如下:

1)当服务器变为主服务器时,执行此脚本。

# cat to_master.sh  #!/bin/bash Date=$(date +%F" "%T) IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}') Mail="[email protected]" echo "$Date $IP change to master." |mail -s "Master-Backup Change Status" $Mail

2)当服务器变为备用时,执行此脚本

# cat to_backup.sh #!/bin/bash Date=$(date +%F" "%T) IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}') Mail="[email protected]" echo "$Date $IP change to backup." |mail -s "Master-Backup Change Status" $Mail

3)当服务器变为故障时,执行此脚本。

# cat to_fault.sh #!/bin/bash Date=$(date +%F" "%T) IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}') Mail="[email protected]" echo "$Date $IP change to fault." |mail -s "Master-Backup Change Status" $Mail

4)当检测到TCP端口3306不可用时,执行该脚本,杀死keepalived,实现切换。

# cat mysql_down.sh #!/bin/bash Date=$(date +%F" "%T) IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}') Mail="[email protected]" pkill keepalived echo "$Date $IP The mysql service failure,kill keepalived." |mail -s "Master-Backup MySQL Monitor" $Mail

5)当检测到TCP端口3306可用时,执行此脚本。

# cat mysql_up.sh #!/bin/bash Date=$(date +%F" "%T) IP=$(ifconfig eth0 |grep "inet addr" |cut -d":" -f2 |awk '{print $1}') Mail="[email protected]" echo "$Date $IP The mysql service is recovery." |mail -s "Master-Backup MySQL Monitor" $Mail

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/782476.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-04
下一篇 2022-05-04

发表评论

登录后才能评论

评论列表(0条)

保存