ÔõÑùÔÚLinuxÉÏÉèÖø߿ÉÓõĸºÔØƽºâÆ÷¼à¿Ø
ÔõÑùÔÚlinuxÉÏÉèÖø߿ÉÓõĸºÔØƽºâÆ÷¼à¿Ø
ÔÚÏÖ´ú»¥ÁªÍøÓ¦Óüܹ¹ÖУ¬¸ºÔØƽºâÆ÷ÊÇÒ»¸öÒªº¦µÄ×é¼þ£¬Ëü¿ÉÒÔ½«»á¼ûÇëÇó·ÖÅɵ½¶à¸öЧÀÍÆ÷ÉÏ£¬ÒÔÌá¸ßÕûÌåÐÔÄܺͿÉÓÃÐÔ¡£È»¶ø£¬¸ºÔØƽºâÆ÷×Ô¼ºÒ²¿ÉÄܳÉΪϵͳÖеĵ¥µã¹ÊÕÏ¡£ÎªÏàʶ¾öÕâ¸öÎÊÌ⣬ÎÒÃÇ¿ÉÒÔʹÓø߿ÉÓÃÐÔÊÖÒÕÀ´È·±£¸ºÔØƽºâÆ÷µÄ¿ÉÓÃÐÔ¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚlinuxÉÏÉèÖø߿ÉÓõĸºÔØƽºâÆ÷¼à¿Ø£¬²¢ÌṩÏà¹ØµÄ´úÂëʾÀý¡£
×°ÖúÍÉèÖøºÔØƽºâÆ÷
Ê×ÏÈ£¬ÎÒÃÇÐèҪѡÔñÒ»¸öºÏÊʵĸºÔØƽºâÆ÷Èí¼þ¡£ÔÚLinuxÉϳ£ÓõĸºÔØƽºâÆ÷Èí¼þÓÐNginx¡¢HAProxyºÍKeepalivedµÈ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÒÔKeepalivedΪÀý¾ÙÐÐÉèÖá£
ʹÓÃÒÔÏÂÏÂÁîÔÚLinuxÉÏ×°ÖÃKeepalived£º
$ sudo apt-get install keepalived
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÎÒÃÇÐèÒªÉèÖÃKeepalivedÒÔÆôÓø߿ÉÓÃÐÔ¡£ÔÚ/etc/keepalived/Ŀ¼Ï½¨ÉèÒ»¸öÃûΪkeepalived.confµÄÎļþ£¬²¢½«ÒÔÏÂÉèÖÃÄÚÈÝÌí¼Óµ½ÎļþÖУº
# Ö»ÔÚÖ÷½ÚµãÉÏÔËÐÐ vrrp_script check_http { script "/usr/local/bin/check_http.sh" interval 2 } vrrp_instance VI_1 { state MASTER virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass example } virtual_ipaddress { 10.0.0.1/24 } track_script { check_http } }
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬check_http.shÊÇÒ»¸ö×Ô½ç˵µÄ¾ç±¾£¬ÓÃÓÚ¼ì²é¸ºÔØƽºâÆ÷ËùÔڵĺó¶ËЧÀÍÆ÷µÄ¿ÉÓÃÐÔ¡£¾ç±¾¿ÉÒÔƾ֤ÏÖʵÐèÇó±àд¡£
ÉèÖøºÔØƽºâÆ÷¼à¿Ø
Íê³ÉKeepalivedµÄÉèÖúó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÆäËû¹¤¾ß¼à¿Ø¸ºÔØƽºâÆ÷µÄÔËÐÐ״̬¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ê¹ÓÃÒ»¸ö¼òÆÓµÄPython½ÅÔÀ´¼à¿Ø¸ºÔØƽºâÆ÷µÄ¿ÉÓÃÐÔ¡£
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃPythonµÄrequestsÄ£¿é£º
$ sudo pip install requests
µÇ¼ºó¸´ÖÆ
È»ºó£¬½¨ÉèÒ»¸öÃûΪcheck_lb.pyµÄPython¾ç±¾£¬²¢½«ÒÔÏ´úÂëÌí¼Óµ½ÎļþÖУº
import requests def check_lb(): try: response = requests.get('http://10.0.0.1') if response.status_code == 200: print('¸ºÔØƽºâÆ÷Õý³£ÔËÐÐ') else: print('¸ºÔØƽºâÆ÷¹ÊÕÏ') except requests.exceptions.RequestException as e: print('¸ºÔØƽºâÆ÷¹ÊÕÏ') if __name__ == '__main__': check_lb()
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃrequests¿âÀ´·¢ËÍHTTPÇëÇó²¢¼ì²é·µ»ØµÄ״̬Âë¡£ÈôÊÇ״̬ÂëΪ200£¬ÔòÌåÏÖ¸ºÔØƽºâÆ÷Õý³£ÔËÐУ»²»È»£¬ÌåÏÖ¸ºÔØƽºâÆ÷¹ÊÕÏ¡£
ÉèÖÃ×Ô¶¯ÔËÐмà¿Ø¾ç±¾
ΪÁËÈüà¿Ø¾ç±¾°´ÆÚÔËÐУ¬ÎÒÃÇ¿ÉÒÔʹÓÃcronʹÃüÀ´×¼Ê±ÔËÐо籾¡£·¿ªÖն˲¢ÊäÈëÒÔÏÂÏÂÁ
$ crontab -e
µÇ¼ºó¸´ÖÆ
ÔÚ·¿ªµÄcronʹÃü±à¼Æ÷ÖУ¬Ìí¼ÓÒÔÏÂÄÚÈÝ£º
*/5 * * * * /usr/bin/python /path/to/check_lb.py
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄÉèÖý«Ã¿¸ô5·ÖÖÓÔËÐÐÒ»´Î¼à¿Ø¾ç±¾¡£¿ÉÒÔƾ֤ÏÖʵÐèÇóÐÞ¸ÄÕâ¸öʱ¼ä¾àÀë¡£
¼à¿Ø±¨¾¯
ÔÚ¼à¿Ø¾ç±¾ÖУ¬ÎÒÃÇ¿ÉÒÔÐÞ¸Ächeck_lb()º¯Êý£¬Ìí¼Ó±¨¾¯Âß¼¡£ÀýÈ磬µ±¸ºÔØƽºâÆ÷¹ÊÕÏʱ·¢Ë͵ç×ÓÓʼþ»ò·¢ËÍÐÂÎŵ½ÊÖ»úÓ¦ÓóÌÐò¡£
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý£¬·¢Ë͵ç×ÓÓʼþµÄ´úÂëƬ¶Ï£º
import smtplib from email.mime.text import MIMEText def send_email(): sender = 'your_email@example.com' receiver = 'recipient@example.com' subject = '¸ºÔØƽºâÆ÷¹ÊÕÏ' message = '¸ºÔØƽºâÆ÷ÎÞ·¨»á¼û' msg = MIMEText(message) msg['Subject'] = subject msg['From'] = sender msg['To'] = receiver try: smtp_obj = smtplib.SMTP('localhost') smtp_obj.sendmail(sender, receiver, msg.as_string()) smtp_obj.quit() print('ÓʼþÒÑ·¢ËÍ') except smtplib.SMTPException: print('Óʼþ·¢ËÍʧ°Ü')
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÎÒÃÇʹÓÃsmtplib¿âÀ´·¢Ë͵ç×ÓÓʼþ¡£Ê×ÏÈ£¬ÐèÒªÉèÖ÷¢¼þÈ˺ÍÊÕ¼þÈ˵ÄÓÊÏäµØµã£¬È»ºó½¨ÉèÒ»¸öMIMEText¹¤¾ß£¬½«·¢Ë͵ÄÐÂÎźÍÖ÷ÌâÌí¼Óµ½¹¤¾ßÖУ¬²¢Ê¹ÓÃSMTPЧÀÍÆ÷·¢ËÍÓʼþ¡£
ÔÚcheck_lb()º¯ÊýÖУ¬µ±¸ºÔØƽºâÆ÷¹ÊÕÏʱ£¬¿ÉÒÔŲÓÃsend_email()º¯Êý·¢Ëͱ¨¾¯Óʼþ¡£
×ܽ᣺
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑùÔÚlinuxÉÏÉèÖø߿ÉÓõĸºÔØƽºâÆ÷¼à¿Ø¡£ÎÒÃÇʹÓÃKeepalivedÀ´Ìṩ¸ºÔØƽºâÆ÷µÄ¸ß¿ÉÓÃÐÔ£¬²¢Ê¹ÓÃPython¾ç±¾ºÍcronʹÃüÀ´°´ÆÚ¼à¿Ø¸ºÔØƽºâÆ÷µÄÔËÐÐ״̬¡£±ðµÄ£¬ÎÒÃÇ»¹ÌṩÁËÒ»¸ö¼òÆÓµÄʾÀý£¬ÑÝʾÔõÑùÔÚ¸ºÔØƽºâÆ÷¹ÊÕÏʱ·¢Ë͵ç×ÓÓʼþ±¨¾¯¡£Í¨¹ýÕâЩ°ì·¨£¬Äú¿ÉÒÔÇáËɵØÉèÖøºÔØƽºâÆ÷µÄ¼à¿ØºÍ±¨¾¯ÏµÍ³£¬²¢È·±£ÄúµÄÓ¦ÓþßÓи߿ÉÓÃÐÔ¡£
ÒÔÉϾÍÊÇÔõÑùÔÚLinuxÉÏÉèÖø߿ÉÓõĸºÔØƽºâÆ÷¼à¿ØµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡