{"id":337,"date":"2021-07-28T15:25:24","date_gmt":"2021-07-28T07:25:24","guid":{"rendered":"http:\/\/egonlin.com\/?p=337"},"modified":"2024-04-01T15:16:46","modified_gmt":"2024-04-01T07:16:46","slug":"%e7%ac%ac%e4%b8%80%e8%8a%82%ef%bc%9akeepalive%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=337","title":{"rendered":"\u7b2c\u4e00\u8282\uff1aKeepalive\u5165\u95e8"},"content":{"rendered":"<h1>Keepalived\u9ad8\u53ef\u7528<\/h1>\n<h2>\u4e00\u3001\u9ad8\u53ef\u7528\u4ecb\u7ecd<\/h2>\n<h3>1.\u4ec0\u4e48\u662f\u9ad8\u53ef\u7528<\/h3>\n<pre><code class=\"language-bash\">\u4e00\u822c\u662f\u63072\u53f0\u673a\u5668\u542f\u52a8\u7740\u5b8c\u5168\u76f8\u540c\u7684\u4e1a\u52a1\u7cfb\u7edf\uff0c\u5f53\u6709\u4e00\u53f0\u673a\u5668down\u673a\u4e86\uff0c\u53e6\u5916\u4e00\u53f0\u670d\u52a1\u5668\u5c31\u80fd\u5feb\u901f\u7684\u63a5\u7ba1\uff0c\u5bf9\u4e8e\u8bbf\u95ee\u7684\u7528\u6237\u662f\u65e0\u611f\u77e5\u7684\u3002<\/code><\/pre>\n<h3>2.\u5e38\u7528\u7684\u5de5\u5177<\/h3>\n<pre><code class=\"language-bash\">1.\u786c\u4ef6\u901a\u5e38\u4f7f\u7528 F5\n2.\u8f6f\u4ef6\u901a\u5e38\u4f7f\u7528 keepalived<\/code><\/pre>\n<h3>3.keepalived\u662f\u5982\u4f55\u5b9e\u73b0\u9ad8\u53ef\u7528\u7684\uff1f<\/h3>\n<h4>1\uff09\u6d89\u53ca\u540d\u8bcd<\/h4>\n<pre><code class=\"language-bash\">keepalived\u8f6f\u4ef6\u662f\u57fa\u4e8eVRRP\u534f\u8bae\u5b9e\u73b0\u7684\uff0cVRRP\u865a\u62df\u8def\u7531\u5197\u4f59\u534f\u8bae\uff0c\u4e3b\u8981\u7528\u4e8e\u89e3\u51b3\u5355\u70b9\u6545\u969c\u95ee\u9898\n\nARP\u5e7f\u64ad\nVRRP\u534f\u8bae\nvip\u8d1f\u8d23IP\u6f02\u79fb\nvmac\u8d1f\u8d23\u901a\u77e5ARP\u5e7f\u64ad\u4fee\u6539mac\u5730\u5740<\/code><\/pre>\n<h4>2\uff09\u4f8b\u5b50<\/h4>\n<pre><code class=\"language-bash\">\u6bd4\u5982\u516c\u53f8\u7684\u7f51\u7edc\u662f\u901a\u8fc7\u7f51\u5173\u8fdb\u884c\u4e0a\u7f51\u7684\uff0c\u90a3\u4e48\u5982\u679c\u8be5\u8def\u7531\u5668\u6545\u969c\u4e86\uff0c\u7f51\u5173\u65e0\u6cd5\u8f6c\u53d1\u62a5\u6587\u4e86\uff0c\u6b64\u65f6\u6240\u6709\u4eba\u90fd\u65e0\u6cd5\u4e0a\u7f51\u4e86\uff0c\u600e\u4e48\u529e\uff1f\n\n\u901a\u5e38\u505a\u6cd5\u662f\u7ed9\u8def\u7531\u5668\u589e\u52a0\u4e00\u53f0\u5907\u8282\u70b9\uff0c\u4f46\u662f\u95ee\u9898\u662f\uff0c\u5982\u679c\u6211\u4eec\u7684\u4e3b\u7f51\u5173master\u6545\u969c\u4e86\uff0c\u7528\u6237\u662f\u9700\u8981\u624b\u52a8\u6307\u5411backup\u7684\uff0c\u5982\u679c\u7528\u6237\u8fc7\u591a\u4fee\u6539\u8d77\u6765\u4f1a\u975e\u5e38\u9ebb\u70e6\u3002\n\n\u95ee\u9898\u4e00\uff1a\u5047\u8bbe\u7528\u6237\u5c06\u6307\u5411\u90fd\u4fee\u6539\u4e3abackup\u8def\u7531\u5668\uff0c\u90a3\u4e48master\u8def\u7531\u5668\u4fee\u597d\u4e86\u600e\u4e48\u529e\uff1f\n\u95ee\u9898\u4e8c\uff1a\u5047\u8bbeMaster\u7f51\u5173\u6545\u969c\uff0c\u6211\u4eec\u5c06backup\u7f51\u5173\u914d\u7f6e\u4e3amaster\u7f51\u5173\u7684ip\u662f\u5426\u53ef\u4ee5\uff1f\n\n\u5176\u5b9e\u662f\u4e0d\u884c\u7684\uff0c\u56e0\u4e3aPC\u7b2c\u4e00\u6b21\u901a\u8fc7ARP\u5e7f\u64ad\u5bfb\u627e\u5230Master\u7f51\u5173\u7684MAC\u5730\u5740\u4e0eIP\u5730\u5740\u540e\uff0c\u4f1a\u5c06\u4fe1\u606f\u5199\u5230ARP\u7684\u7f13\u5b58\u8868\u4e2d\uff0c\u90a3\u4e48PC\u4e4b\u540e\u8fde\u63a5\u90fd\u662f\u901a\u8fc7\u90a3\u4e2a\u7f13\u5b58\u8868\u7684\u4fe1\u606f\u53bb\u8fde\u63a5\uff0c\u7136\u540e\u8fdb\u884c\u6570\u636e\u5305\u7684\u8f6c\u53d1\uff0c\u5373\u4f7f\u6211\u4eec\u4fee\u6539\u4e86IP\u4f46\u662fMac\u5730\u5740\u662f\u552f\u4e00\u7684\uff0cpc\u7684\u6570\u636e\u5305\u4f9d\u7136\u4f1a\u53d1\u9001\u7ed9master\u3002\uff08\u9664\u975e\u662fPC\u7684ARP\u7f13\u5b58\u8868\u8fc7\u671f\uff0c\u518d\u6b21\u53d1\u8d77ARP\u5e7f\u64ad\u7684\u65f6\u5019\u624d\u80fd\u83b7\u53d6\u65b0\u7684backup\u5bf9\u5e94\u7684Mac\u5730\u5740\u4e0eIP\u5730\u5740\uff09\n\n\u5982\u4f55\u624d\u80fd\u505a\u5230\u51fa\u73b0\u6545\u969c\u81ea\u52a8\u8f6c\u79fb\uff0c\u6b64\u65f6VRRP\u5c31\u51fa\u73b0\u4e86\uff0c\u6211\u4eec\u7684VRRP\u5176\u5b9e\u662f\u901a\u8fc7\u8f6f\u4ef6\u6216\u8005\u786c\u4ef6\u7684\u5f62\u5f0f\u5728Master\u548cBackup\u5916\u9762\u589e\u52a0\u4e00\u4e2a\u865a\u62df\u7684MAC\u5730\u5740\uff08VMAC\uff09\u4e0e\u865a\u62dfIP\u5730\u5740\uff08VIP\uff09\uff0c\u90a3\u4e48\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cPC\u8bf7\u6c42VIP\u7684\u65f6\u5019\uff0c\u65e0\u8bba\u662fMaster\u5904\u7406\u8fd8\u662fBackup\u5904\u7406\uff0cPC\u4ec5\u4f1a\u5728ARP\u7f13\u5b58\u8868\u4e2d\u8bb0\u5f55VMAC\u4e0eVIP\u7684\u4fe1\u606f\u3002<\/code><\/pre>\n<h3>4.\u9ad8\u53ef\u7528keepalived\u6838\u5fc3\u6982\u5ff5<\/h3>\n<pre><code class=\"language-bash\">1.\u5982\u4f55\u786e\u5b9a\u8c01\u662f\u4e3b\u8282\u70b9\u8c01\u662f\u5907\u8282\u70b9\uff08\u9009\u4e3e\u6295\u7968\uff0c\u4f18\u5148\u7ea7\uff09\n2.\u5982\u679cMaster\u6545\u969c\uff0cBackup\u81ea\u52a8\u63a5\u7ba1\uff0c\u90a3\u4e48Master\u6062\u590d\u540e\u4f1a\u593a\u6743\u5417\uff08\u62a2\u5360\u8bd5\u3001\u975e\u62a2\u5360\u5f0f\uff09\n3.\u5982\u679c\u4e24\u53f0\u670d\u52a1\u5668\u90fd\u8ba4\u4e3a\u81ea\u5df1\u662fMaster\u4f1a\u51fa\u73b0\u4ec0\u4e48\u95ee\u9898\uff08\u8111\u88c2\uff09<\/code><\/pre>\n<h2>\u4e8c\u3001keepalived\u642d\u5efa<\/h2>\n<h3>1.\u73af\u5883\u51c6\u5907<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u4e3b\u673a<\/th>\n<th>IP<\/th>\n<th>\u8eab\u4efd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>lb01<\/td>\n<td>10.0.0.4<\/td>\n<td>keepalived master<\/td>\n<\/tr>\n<tr>\n<td>lb02<\/td>\n<td>10.0.0.5<\/td>\n<td>keepalived backup<\/td>\n<\/tr>\n<tr>\n<td>web01<\/td>\n<td>172.16.1.7<\/td>\n<td>web\u7aef<\/td>\n<\/tr>\n<tr>\n<td>web02<\/td>\n<td>172.16.1.8<\/td>\n<td>web\u7aef<\/td>\n<\/tr>\n<tr>\n<td>db01<\/td>\n<td>172.16.1.51<\/td>\n<td>\u6570\u636e\u5e93<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td>10.0.0.3<\/td>\n<td>VIP<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>2.\u4fdd\u8bc1\u4e03\u5c42\u8d1f\u8f7d\u5747\u8861\u5b8c\u5168\u4e00\u81f4<\/h3>\n<pre><code class=\"language-bash\">[root@lb01 conf.d]# scp linux.wp.com.conf 172.16.1.5:\/etc\/nginx\/conf.d\/\n[root@lb01 conf.d]# scp -r \/etc\/nginx\/ssl_key 172.16.1.5:\/etc\/nginx\/<\/code><\/pre>\n<h3>3.\u5b89\u88c5keepalived<\/h3>\n<pre><code class=\"language-bash\">[root@lb01 ~]# yum install -y keepalived\n[root@lb02 ~]# yum install -y keepalived<\/code><\/pre>\n<h3>4.\u914d\u7f6ekeepalived<\/h3>\n<h4>1\uff09\u67e5\u627e\u914d\u7f6e\u6587\u4ef6<\/h4>\n<pre><code class=\"language-bash\">[root@lb01 ~]# rpm -qc keepalived\n\/etc\/keepalived\/keepalived.conf<\/code><\/pre>\n<h4>2\uff09\u914d\u7f6e\u4e3b\u8282\u70b9\u7684\u914d\u7f6e\u6587\u4ef6<\/h4>\n<pre><code class=\"language-bash\">[root@lb01 ~]# vim \/etc\/keepalived\/keepalived.conf\nglobal_defs {\n    router_id lb01\n}\n\nvrrp_instance VI_1 {\n    state MASTER\n    interface eth0\n    virtual_router_id 50\n    priority 100\n    advert_int 1\n    authentication {\n        auth_type PASS\n        auth_pass 1111\n    }\n    virtual_ipaddress {\n        10.0.0.3\n    }\n}\n\n[root@lb01 ~]# vim \/etc\/keepalived\/keepalived.conf\n#\u5168\u5c40\u914d\u7f6e\nglobal_defs {\n    #\u8eab\u4efd\u8bc6\u522b\n    router_id lb01\n}\n\n#\u914d\u7f6eVRRP\u534f\u8bae\nvrrp_instance VI_1 {\n    #\u72b6\u6001\uff0cMASTER\u548cBACKUP\n    state MASTER\n    #\u7ed1\u5b9a\u7f51\u5361\n    interface eth0\n    #\u865a\u62df\u8def\u7531\u6807\u793a\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u5206\u7ec4\n    virtual_router_id 50\n    #\u4f18\u5148\u7ea7\n    priority 100\n    #\u76d1\u6d4b\u5fc3\u8df3\u95f4\u9694\u65f6\u95f4\n    advert_int 1\n    #\u914d\u7f6e\u8ba4\u8bc1\n    authentication {\n        #\u8ba4\u8bc1\u7c7b\u578b\n        auth_type PASS\n        #\u8ba4\u8bc1\u7684\u5bc6\u7801\n        auth_pass 1111\n    }\n    #\u8bbe\u7f6eVIP\n    virtual_ipaddress {\n        #\u865a\u62df\u7684VIP\u5730\u5740\n        10.0.0.3\n    }\n}<\/code><\/pre>\n<h4>3\uff09\u914d\u7f6e\u5907\u8282\u70b9<\/h4>\n<pre><code class=\"language-bash\">[root@lb02 ~]# vim \/etc\/keepalived\/keepalived.conf\nglobal_defs {\n    router_id lb02\n}\n\nvrrp_instance VI_1 {\n    state BACKUP\n    interface eth0\n    virtual_router_id 50\n    priority 90\n    advert_int 1\n    authentication {\n        auth_type PASS\n        auth_pass 1111\n    }\n    virtual_ipaddress {\n        10.0.0.3\n    }\n}<\/code><\/pre>\n<h3>5.\u542f\u52a8\u670d\u52a1<\/h3>\n<pre><code class=\"language-bash\">[root@lb01 ~]# tail -f \/var\/log\/messages\n[root@lb01 ~]# systemctl start keepalived.service\n\n[root@lb02 ~]# tail -f \/var\/log\/messages\n[root@lb02 ~]# systemctl start keepalived.service<\/code><\/pre>\n<h3>6.keepalived\u5f00\u542f\u65e5\u5fd7<\/h3>\n<pre><code class=\"language-bash\">#\u914d\u7f6ekeepalived\n[root@lb01 ~]# vim \/etc\/sysconfig\/keepalived\nKEEPALIVED_OPTIONS=&quot;-D -d -S 0&quot;\n\n#\u914d\u7f6ersyslog\u6293\u53d6\u65e5\u5fd7\n[root@lb01 ~]# vim \/etc\/rsyslog.conf\nlocal0.*        \/var\/log\/keepalived.log\n\n#\u91cd\u542f\u670d\u52a1\n[root@lb01 ~]# systemctl restart keepalived rsyslog<\/code><\/pre>\n<h2>\u4e09\u3001keepalived\u7684\u62a2\u5360\u5f0f\u4e0e\u975e\u62a2\u5360\u5f0f<\/h2>\n<h3>1.\u4e24\u4e2a\u8282\u70b9\u90fd\u542f\u52a8\u7684\u60c5\u51b5<\/h3>\n<pre><code class=\"language-bash\">#\u4e24\u4e2a\u8282\u70b9\u90fd\u542f\u52a8\u65f6\uff0c\u7531\u4e8e\u8282\u70b91\u4f18\u5148\u7ea7\u9ad8\u4e8e\u8282\u70b92\uff0c\u6240\u4ee5\u53ea\u6709\u8282\u70b91\u4e0a\u6709VIP\n[root@lb01 ~]# ip addr | grep 10.0.0.3\n    inet 10.0.0.3\/32 scope global eth0\n\n[root@lb02 ~]# ip addr | grep 10.0.0.3<\/code><\/pre>\n<h3>2.\u505c\u6b62\u4e3b\u8282\u70b9<\/h3>\n<pre><code class=\"language-bash\">[root@lb01 ~]# systemctl stop keepalived.service \n[root@lb01 ~]# ip addr | grep 10.0.0.3\n\n#\u7531\u4e8e\u8282\u70b91keepalived\u6302\u6389\uff0c\u8282\u70b92\u4f1a\u81ea\u52a8\u63a5\u7ba1\u8282\u70b91\u7684\u5de5\u4f5c\uff0c\u5373VIP\n[root@lb02 ~]# ip addr | grep 10.0.0.3\n    inet 10.0.0.3\/32 scope global eth0<\/code><\/pre>\n<h3>3.\u91cd\u65b0\u542f\u52a8\u4e3b\u8282\u70b9<\/h3>\n<pre><code class=\"language-bash\">#\u542f\u52a8\u4e3b\u8282\u70b9\n[root@lb01 ~]# systemctl start keepalived\n[root@lb01 ~]# ip addr | grep 10.0.0.3\n    inet 10.0.0.3\/32 scope global eth0\n\n#\u7531\u4e8e\u8282\u70b91\u4f18\u5148\u7ea7\u9ad8\u4e8e\u8282\u70b92\uff0c\u6240\u4ee5\u5f53\u8282\u70b91\u6062\u590d\u65f6\uff0c\u4f1a\u5c06VIP\u62a2\u5360\u56de\u6765<\/code><\/pre>\n<h3>4.\u914d\u7f6e\u975e\u62a2\u5360\u5f0f<\/h3>\n<h4>1\uff09\u4e3b\u8282\u70b9\u914d\u7f6e<\/h4>\n<pre><code class=\"language-bash\">[root@lb01 ~]# vim \/etc\/keepalived\/keepalived.conf \n... ...\nvrrp_instance VI_1 {\n    state BACKUP\n    #\u5f00\u542f\u975e\u62a2\u5360\u5f0f\n    nopreempt\n    priority 100\n... ...\n}\n\n[root@lb01 ~]# systemctl restart keepalived<\/code><\/pre>\n<h4>2\uff09\u5907\u8282\u70b9\u914d\u7f6e<\/h4>\n<pre><code class=\"language-bash\">[root@lb02 ~]# vim \/etc\/keepalived\/keepalived.conf \n... ...\nvrrp_instance VI_1 {\n    state BACKUP\n    nopreempt\n    priority 90\n... ...\n}\n\n[root@lb02 ~]# systemctl restart keepalived.service<\/code><\/pre>\n<h4>3\uff09\u914d\u7f6e<\/h4>\n<pre><code class=\"language-bash\">1.\u4e24\u4e2a\u8282\u70b9\u7684state\u90fd\u5fc5\u987b\u914d\u7f6e\u4e3aBACKUP\n2.\u4e24\u4e2a\u8282\u70b9\u90fd\u5fc5\u987b\u52a0\u4e0a\u914d\u7f6e nopreempt\n3.\u5176\u4e2d\u4e00\u4e2a\u8282\u70b9\u7684\u4f18\u5148\u7ea7\u5fc5\u987b\u8981\u9ad8\u4e8e\u53e6\u5916\u4e00\u4e2a\u8282\u70b9\u7684\u4f18\u5148\u7ea7\u3002\n\n\u4e24\u53f0\u670d\u52a1\u5668\u90fd\u89d2\u8272\u72b6\u6001\u542f\u7528nopreempt\u540e\uff0c\u5fc5\u987b\u4fee\u6539\u89d2\u8272\u72b6\u6001\u7edf\u4e00\u4e3aBACKUP\uff0c\u552f\u4e00\u7684\u533a\u5206\u5c31\u662f\u4f18\u5148\u7ea7\u3002<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Keepalived\u9ad8\u53ef\u7528 \u4e00\u3001\u9ad8\u53ef\u7528\u4ecb\u7ecd 1.\u4ec0\u4e48\u662f\u9ad8\u53ef\u7528 \u4e00\u822c\u662f\u63072\u53f0\u673a\u5668\u542f\u52a8\u7740\u5b8c\u5168\u76f8\u540c\u7684\u4e1a\u52a1\u7cfb\u7edf\uff0c\u5f53\u6709\u4e00 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[21,19,22,57,20],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/337"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=337"}],"version-history":[{"count":1,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/337\/revisions"}],"predecessor-version":[{"id":9560,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/337\/revisions\/9560"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}