{"id":18568,"date":"2024-11-19T20:05:51","date_gmt":"2024-11-19T12:05:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/?p=18568"},"modified":"2024-11-19T20:05:51","modified_gmt":"2024-11-19T12:05:51","slug":"keepalived%e7%9a%84%e9%85%8d%e7%bd%ae%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/18568\/","title":{"rendered":"Keepalived\u7684\u914d\u7f6e\u8be6\u89e3"},"content":{"rendered":"<p>\u5728\u521d\u6b21\u5b89\u88c5\u540e\uff0c\u5728\u5b89\u88c5\u76ee\u5f55\u6709\u4e2a\u78e8\u4eba\u7684\u914d\u7f6e\u6587\u4ef6 .\/etc\/keepalived\/keepalived.conf\uff0c\u5c06\u5176\u590d\u5236\u5230.\/etc\/keepalived.conf\uff0c\u5982\u679c\u9700\u8981\u66f4\u591a\u7684\u914d\u7f6e\u793a\u4f8b\uff0c\u5728.\/etc\/keepalived\/sample\u4e0b\u6709\u5f88\u591a\uff0c\u53ef\u4ee5\u501f\u9274\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e00\u4e2a\u529f\u80fd\u6bd4\u8f83\u5b8c\u6574\u7684\u5e38\u7528\u7684keepalived\u914d\u7f6e\u6587\u4ef6\uff0c\u4e3b\u8981\u5305\u542b\u4e09\u5757\uff1a\u5168\u5c40\u5b9a\u4e49\u5757\u3001VRRP\u5b9e\u4f8b\u5b9a\u4e49\u5757\u548c\u865a\u62df\u670d\u52a1\u5668\u5b9a\u4e49\u5757\u3002\u5168\u5c40\u5b9a\u4e49\u5757\u662f\u5fc5\u987b\u7684\uff0c\u5982\u679ckeepalived\u53ea\u7528\u6765\u505aha\uff0c\u865a\u62df\u670d\u52a1\u5668\u662f\u53ef\u9009\u7684\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u529f\u80fd\u6bd4\u8f83\u5b8c\u6574\u7684\u914d\u7f6e\u6587\u4ef6\u6a21\u677f\uff1a<\/p>\n<h1>\u4e00\u3001\u5185\u6838\u53c2\u6570<\/h1>\n<p>\u68c0\u67e5\u76f8\u5173\u7684\u5185\u6838\u53c2\u6570<\/p>\n<pre>net.ipv4.conf.all.arp_announce = 2\r\nnet.ipv4.conf.default.arp_announce = 2\r\nnet.ipv4.conf.lo.arp_announce = 2\r\nnet.ipv4.conf.enp0s3.arp_announce = 2\r\nnet.ipv4.conf.enp0s8.arp_announce = 2\r\n\r\nnet.ipv4.conf.all.arp_ignore = 1\r\nnet.ipv4.conf.default.arp_ignore = 1\r\nnet.ipv4.conf.lo.arp_ignore = 1\r\nnet.ipv4.conf.enp0s3.arp_ignore = 1\r\nnet.ipv4.conf.enp0s8.arp_ignore = 1<\/pre>\n<p><strong>Tip 1 \uff1a<\/strong>\u6ca1\u6709\u4e0a\u8ff0\u7684\u8bbe\u7f6e\uff0c\u51fa\u73b0\u65e0\u6cd5ping \u901a vip\u3002<\/p>\n<h1>\u4e8c\u3001\u914d\u7f6e\u5b9e\u4f8b<\/h1>\n<pre># \u5168\u5c40\u5b9a\u4e49\u5757\r\nglobal_defs\r\n{\r\n   #\u90ae\u4ef6\u901a\u77e5 notification: \u82f1\u6587\u793a\u610f:\u901a\u77e5,\u544a\u793a\r\n   #\u6307\u5b9akeepalived\u5728\u53d1\u751f\u5207\u6362\u65f6\u9700\u8981\u53d1\u9001email\u5230\u7684\u5bf9\u8c61\uff0c\u4e00\u884c\u4e00\u4e2a\r\n   notification_email\r\n   {\r\n     acassen@firewall.loc;\r\n     failover@firewall.loc;\r\n     sysadmin@firewall.loc;\r\n   }\r\n   notification_email_from Alexandre.Cassen@firewall.loc\r\n   smtp_server 192.168.200.1\r\n   smtp_connect_timeout 30\r\n   #\u6807\u8bc6\u672c\u8282\u70b9\u7684\u5b57\u6761\u4e32\uff0c\u901a\u5e38\u4e3ahostname\uff0c\u4f46\u4e0d\u4e00\u5b9a\u975e\u5f97\u662fhostname\u3002\u6545\u969c\u53d1\u751f\u65f6\uff0c\u90ae\u4ef6\u901a\u77e5\u4f1a\u7528\u5230\u3002\r\n   #\u5728\u5c40\u57df\u7f51\u5185\u5e94\u8be5\u662f\u552f\u4e00\u7684,\u6ce8\u610f\u548cbackup\u8282\u70b9\u533a\u5206\u5f00\u3002\r\n   router_id KEEPALIVED.192.168.80.110\r\n\r\n   #\u68c0\u67e5vrrp\u62a5\u6587\u4e2d\u7684\u6240\u6709\u5730\u5740\u6bd4\u8f83\u8017\u65f6\uff0c\u8bbe\u7f6e\u6b64\u6807\u5fd7\u7684\u610f\u601d\u662f\u5982\u679c\u63a5\u6536\u7684\u5230\u62a5\u6587\u548c\u4e0a\u4e00\u4e2a\u62a5\u6587\u6765\u81f3\u540c\u4e00\u4e2a\u8def\u7531\u5668\r\n   #\u5219\u4e0d\u6267\u884c\u68c0\u67e5\u3002\u9ed8\u8ba4\u662f\u8df3\u8fc7\u68c0\u67e5\r\n   #vrrp_skip_check_adv_addr\r\n\r\n   #\u4e25\u683c\u9075\u5b88vrrp\u534f\u8bae\uff0c\u4e0b\u9762\u8fd9\u4e9b\u529f\u80fd\u5c06\u4f1a\u7981\u6b62\uff1a\r\n   # 1.   0 VIP   2. unicast(\u5355\u64ad) peers    3. vrrp \u7248\u672c2\u7684ipv6\u529f\u80fd\r\n   #vrrp_strict\r\n\r\n   #\u5c0f\u6570\u7c7b\u578b\uff0c\u5355\u4f4d\u79d2\uff0c\u5728\u4e00\u4e2a\u7f51\u5361\u4e0a\u6bcf\u7ec4gratuitous arp\u6d88\u606f\u4e4b\u95f4\u7684\u5ef6\u8fdf\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4e3a0\uff0c\u4e00\u4e2a\u53d1\u9001\u7684\u6d88\u606f=n\u7ec4 \r\n  \uff03arp\u62a5\u6587 vrrp_garp_interval 0.001\r\n\r\n   #\u5c0f\u6570\u7c7b\u578b\uff0c\u5355\u4f4d\u79d2\uff0c \u5728\u4e00\u4e2a\u7f51\u5361\u4e0a\u6bcf\u7ec4na\u6d88\u606f\u4e4b\u95f4\u7684\u5ef6\u8fdf\u65f6\u95f4\uff0c\u9ed8\u8ba4\u4e3a0\r\n   vrrp_gna_interval 0.000001\r\n}\r\n#############################################################################\r\n#\u5e94\u7528\u5c42\u70ed\u5907\u9700\u8981\u6dfb\u52a0\u7684\u5185\u5bb9\r\nvrrp_script chk_nginx\r\n{\r\n        #\u6267\u884c\u811a\u672c\u7684\u8def\u5f84\r\n        script \"\/etc\/nginx\/check_nginx.sh\"\r\n        #\u8c03\u7528\u811a\u672c\u4e24\u6b21\u4e4b\u95f4\u7684\u95f4\u9694\uff0c\u9ed8\u8ba4\u4e3a1\u79d2\r\n        interval 1\r\n        #\u7b2c\u4e8c\u6b21\u8c03\u7528\u811a\u672c\u540e\u591a\u957f\u65f6\u95f4\u6ca1\u6709\u56de\u5e94\u7684\u8d85\u65f6\u65f6\u95f4\r\n        timeout 10\r\n        weight 10 #weight\u53ef\u4ee5\u53c2\u8003\uff1a<a title=\"keepalived\u4e4bvrrp_script\u603b\u7ed3\" href=\"http:\/\/www.hangdaowangluo.com\/archives\/1828\" target=\"_blank\" rel=\"noopener\">keepalived\u4e4bvrrp_script\u603b\u7ed3<\/a>\r\n}\r\n#############################################################################\r\n#vrrp\u5b9e\u4f8b\u5b9a\u4e49\u90e8\u5206\r\nvrrp_instance VI_1\r\n{\r\n    #\u72b6\u6001\u53ea\u6709MASTER\u548cBACKUP\u4e24\u79cd\uff0c\u5e76\u4e14\u8981\u5927\u5199\uff0cMASTER\u4e3a\u5de5\u4f5c\u72b6\u6001\uff0cBACKUP\u662f\u5907\u7528\u72b6\u6001\u3002\r\n    state BACKUP\r\n    nopreempt\r\n    interface enp0s8\r\n    #\u8fd9\u4e2a\u9ed8\u8ba4\u6ca1\u6709\uff0c\u76f8\u5f53\u4e8e\u5fc3\u8df3\u7ebf\u63a5\u53e3\uff0cDR\u6a21\u5f0f\u7528\u7684\u548c\u4e0a\u9762\u7684\u63a5\u53e3\u4e00\u6837\uff0c\u4e5f\u53ef\u4ee5\u7528\u673a\u5668\u4e0a\u7684\u5176\u4ed6\u7f51\u5361eth1\uff0c\r\n    #\u7528\u6765\u9632\u6b62\u8111\u88c2\u3002\r\n    lvs_sync_daemon_inteface enp0s8\r\n    #\u865a\u62df\u8def\u7531\u6807\u8bc6\uff0c\u540c\u4e00\u4e2avrrp_instance\u7684MASTER\u548cBACKUP\u7684vitrual_router_id \u662f\u4e00\u81f4\u7684\u3002\r\n    virtual_router_id 110\r\n    #\u4f18\u5148\u7ea7\uff0c\u540c\u4e00\u4e2avrrp_instance\u7684MASTER\u4f18\u5148\u7ea7\u5fc5\u987b\u6bd4BACKUP\u9ad8\u3002\r\n    priority 101 \/\/backup\u6bd4\u8be5\u503c\u5c0f\r\n    #MASTER \u4e0eBACKUP \u8d1f\u8f7d\u5747\u8861\u5668\u4e4b\u95f4\u540c\u6b65\u68c0\u67e5\u7684\u65f6\u95f4\u95f4\u9694\uff0c\u5355\u4f4d\u4e3a\u79d2\u3002\r\n    advert_int 1\r\n    #\u9a8c\u8bc1authentication\u3002\u5305\u542b\u9a8c\u8bc1\u7c7b\u578b\u548c\u9a8c\u8bc1\u5bc6\u7801\u3002\u7c7b\u578b\u4e3b\u8981\u6709PASS\u3001AH \u4e24\u79cd\uff0c\u901a\u5e38\u4f7f\u7528\u7684\u7c7b\u578b\u4e3aPASS,\r\n    #auth_pass 1111   \u636e\u8bf4AH \u4f7f\u7528\u65f6\u6709\u95ee\u9898\u3002\u9a8c\u8bc1\u5bc6\u7801\u4e3a\u660e\u6587\uff0c\u540c\u4e00vrrp \u5b9e\u4f8bMASTER \u4e0eBACKUP \r\n    #\u4f7f\u7528\u76f8\u540c\u7684\u5bc6\u7801\u624d\u80fd\u6b63\u5e38\u901a\u4fe1\u3002\r\n    #mcast_src_ip 192.168.80.110\r\n    authentication\r\n    {\r\n        auth_type PASS\r\n        auth_pass 1111\r\n    }\r\n    #\u865a\u62dfip\u5730\u5740,\u53ef\u4ee5\u6709\u591a\u4e2a\u5730\u5740\uff0c\u6bcf\u4e2a\u5730\u5740\u5360\u4e00\u884c\uff0c\u4e0d\u9700\u8981\u5b50\u7f51\u63a9\u7801\uff0c\u540c\u65f6\u8fd9\u4e2aip \u5fc5\u987b\u4e0e\u6211\u4eec\u5728lvs \r\n    #\u5ba2\u6237\u7aef\u8bbe\u5b9a\u7684vip \u76f8\u4e00\u81f4\uff01\r\n    virtual_ipaddress\r\n    {\r\n        192.168.80.200\/24 dev enp0s8\r\n    }\r\n    track_script\r\n    {\r\n        #\u8c03\u7528\u68c0\u6d4b\u811a\u672c\r\n        chk_nginx\r\n        #chk_http_port\r\n    }\r\n}<\/pre>\n<p>\u4ee5\u4e0a\u662fmaster\u7684\u914d\u7f6e\uff0cbackup\u7684\u914d\u7f6e\u6ce8\u610f\u4e0a\u9762\u52a0\u7ea2\u7684\u90e8\u5206\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u521d\u6b21\u5b89\u88c5\u540e\uff0c\u5728\u5b89\u88c5\u76ee\u5f55\u6709\u4e2a\u78e8\u4eba\u7684\u914d\u7f6e\u6587\u4ef6 .\/etc\/keepalived\/keepalived.conf\uff0c\u5c06\u5176\u590d\u5236\u5230.\/etc\/keepalived.conf\uff0c\u5982\u679c\u9700\u8981\u66f4\u591a\u7684\u914d\u7f6e\u793a\u4f8b\uff0c\u5728.\/etc\/keepalived\/sample\u4e0b\u6709\u5f88\u591a\uff0c\u53ef\u4ee5\u501f\u9274\u3002 &nbsp; \u4e00\u4e2a\u529f\u80fd\u6bd4\u8f83\u5b8c\u6574\u7684\u5e38\u7528\u7684keepalived\u914d\u7f6e\u6587\u4ef6\uff0c\u4e3b\u8981\u5305\u542b\u4e09\u5757\uff1a\u5168\u5c40\u5b9a\u4e49\u5757\u3001VRRP\u5b9e\u4f8b\u5b9a\u4e49\u5757\u548c\u865a\u62df\u670d\u52a1\u5668\u5b9a\u4e49\u5757\u3002\u5168\u5c40\u5b9a\u4e49\u5757\u662f\u5fc5\u987b\u7684\uff0c\u5982\u679ckeepalived\u53ea\u7528\u6765\u505aha\uff0c\u865a\u62df\u670d\u52a1\u5668\u662f\u53ef\u9009\u7684\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u529f\u80fd\u6bd4\u8f83\u5b8c\u6574\u7684\u914d\u7f6e\u6587\u4ef6\u6a21\u677f\uff1a \u4e00\u3001\u5185\u6838\u53c2\u6570 \u68c0\u67e5\u76f8\u5173\u7684\u5185\u6838\u53c2\u6570 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.lo.arp_announce = 2 net.ipv4.conf.enp0s3.arp_announce = 2 net.ipv4.conf.enp0s8.arp_announce = 2 net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.default.arp_ignore = 1 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.enp0s3.arp_ignore = 1 net.ipv4.conf.enp0s8.arp_ignore = 1 Tip 1 \uff1a\u6ca1\u6709\u4e0a\u8ff0\u7684\u8bbe\u7f6e\uff0c\u51fa\u73b0\u65e0\u6cd5ping \u901a vip\u3002 \u4e8c\u3001\u914d\u7f6e\u5b9e\u4f8b # \u5168\u5c40\u5b9a\u4e49\u5757 global_defs { #\u90ae\u4ef6\u901a\u77e5 notification: \u82f1\u6587\u793a\u610f:\u901a\u77e5,\u544a\u793a #\u6307\u5b9akeepalived\u5728\u53d1\u751f\u5207\u6362\u65f6\u9700\u8981\u53d1\u9001email\u5230\u7684\u5bf9\u8c61\uff0c\u4e00\u884c\u4e00\u4e2a notification_email { acassen@firewall.loc; failover@firewall.loc; sysadmin@firewall.loc; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-18568","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=18568"}],"version-history":[{"count":1,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18568\/revisions"}],"predecessor-version":[{"id":18569,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18568\/revisions\/18569"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=18568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=18568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=18568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}