{"id":153476,"date":"2019-08-24T08:12:46","date_gmt":"2019-08-24T00:12:46","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=153476"},"modified":"2019-08-12T17:14:33","modified_gmt":"2019-08-12T09:14:33","slug":"nginx-efficient-load-balancing","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/nginx-efficient-load-balancing.html","title":{"rendered":"Nginx\u8d1f\u8f7d\u5747\u8861\u9ad8\u53ef\u7528"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nnginx\u4f5c\u4e3a\u8d1f\u8f7d\u5747\u8861\u5668\uff0c\u6240\u6709\u8bf7\u6c42\u90fd\u5230\u4e86nginx\uff0c\u53ef\u89c1nginx\u5904\u4e8e\u975e\u5e38\u91cd\u70b9\u7684\u4f4d\u7f6e\uff0c\u5982\u679cnginx\u670d\u52a1\u5668\u5b95\u673a\u540e\u7aefweb\u670d\u52a1\u5c06\u65e0\u6cd5\u63d0\u4f9b\u670d\u52a1\uff0c\u5f71\u54cd\u4e25\u91cd\u3002\u8981\u5b9e\u73b0nginx\u7684\u9ad8\u53ef\u7528\uff0c\u9700\u8981\u5b9e\u73b0\u5907\u4efd\u673a\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1.1. \u4ec0\u4e48\u662f\u8d1f\u8f7d\u5747\u8861\u9ad8\u53ef\u7528<\/strong><\/div>\n

nginx\u4f5c\u4e3a\u8d1f\u8f7d\u5747\u8861\u5668\uff0c\u6240\u6709\u8bf7\u6c42\u90fd\u5230\u4e86nginx\uff0c\u53ef\u89c1nginx\u5904\u4e8e\u975e\u5e38\u91cd\u70b9\u7684\u4f4d\u7f6e\uff0c\u5982\u679cnginx\u670d\u52a1\u5668\u5b95\u673a\u540e\u7aefweb\u670d\u52a1\u5c06\u65e0\u6cd5\u63d0\u4f9b\u670d\u52a1\uff0c\u5f71\u54cd\u4e25\u91cd\u3002
\n\u4e3a\u4e86\u5c4f\u853d\u8d1f\u8f7d\u5747\u8861\u670d\u52a1\u5668\u7684\u5b95\u673a\uff0c\u9700\u8981\u5efa\u7acb\u4e00\u4e2a\u5907\u4efd\u673a\u3002\u4e3b\u670d\u52a1\u5668\u548c\u5907\u4efd\u673a\u4e0a\u90fd\u8fd0\u884c\u9ad8\u53ef\u7528(High Availability)\u76d1\u63a7\u7a0b\u5e8f\uff0c\u901a\u8fc7\u4f20\u9001\u8bf8\u5982\u201cI am alive\u201d\u8fd9\u6837\u7684\u4fe1\u606f\u6765\u76d1\u63a7\u5bf9\u65b9\u7684\u8fd0\u884c\u72b6\u51b5\u3002\u5f53\u5907\u4efd\u673a\u4e0d\u80fd\u5728\u4e00\u5b9a\u7684\u65f6\u95f4\u5185\u6536\u5230\u8fd9\u6837\u7684\u4fe1\u606f\u65f6\uff0c\u5b83\u5c31\u63a5\u7ba1\u4e3b\u670d\u52a1\u5668\u7684\u670d\u52a1IP\u5e76\u7ee7\u7eed\u63d0\u4f9b\u8d1f\u8f7d\u5747\u8861\u670d\u52a1;\u5f53\u5907\u4efd\u7ba1\u7406\u5668\u53c8\u4ece\u4e3b\u7ba1\u7406\u5668\u6536\u5230\u201cI am alive\u201d\u8fd9\u6837\u7684\u4fe1\u606f\u65f6\uff0c\u5b83\u5c31\u91ca\u653e\u670d\u52a1IP\u5730\u5740\uff0c\u8fd9\u6837\u7684\u4e3b\u670d\u52a1\u5668\u5c31\u5f00\u59cb\u518d\u6b21\u63d0\u4f9b\u8d1f\u8f7d\u5747\u8861\u670d\u52a1\u3002
\n\"\"<\/p>\n

1.2. keepalived+nginx\u5b9e\u73b0\u4e3b\u5907<\/strong><\/div>\n
1.2.1. \u4ec0\u4e48\u662fkeepalived<\/strong><\/span><\/div>\n

keepalived\u662f\u96c6\u7fa4\u7ba1\u7406\u4e2d\u4fdd\u8bc1\u96c6\u7fa4\u9ad8\u53ef\u7528\u7684\u4e00\u4e2a\u670d\u52a1\u8f6f\u4ef6\uff0c\u7528\u6765\u9632\u6b62\u5355\u70b9\u6545\u969c\u3002
\nKeepalived\u7684\u4f5c\u7528\u662f\u68c0\u6d4bweb\u670d\u52a1\u5668\u7684\u72b6\u6001\uff0c\u5982\u679c\u6709\u4e00\u53f0web\u670d\u52a1\u5668\u6b7b\u673a\uff0c\u6216\u5de5\u4f5c\u51fa\u73b0\u6545\u969c\uff0cKeepalived\u5c06\u68c0\u6d4b\u5230\uff0c\u5e76\u5c06\u6709\u6545\u969c\u7684web\u670d\u52a1\u5668\u4ece\u7cfb\u7edf\u4e2d\u5254\u9664\uff0c\u5f53web\u670d\u52a1\u5668\u5de5\u4f5c\u6b63\u5e38\u540eKeepalived\u81ea\u52a8\u5c06web\u670d\u52a1\u5668\u52a0\u5165\u5230\u670d\u52a1\u5668\u7fa4\u4e2d\uff0c\u8fd9\u4e9b\u5de5\u4f5c\u5168\u90e8\u81ea\u52a8\u5b8c\u6210\uff0c\u4e0d\u9700\u8981\u4eba\u5de5\u5e72\u6d89\uff0c\u9700\u8981\u4eba\u5de5\u505a\u7684\u53ea\u662f\u4fee\u590d\u6545\u969c\u7684web\u670d\u52a1\u5668\u3002<\/p>\n

1.2.2. keepalived\u5de5\u4f5c\u539f\u7406<\/strong><\/span><\/div>\n

keepalived\u662f\u4ee5VRRP\u534f\u8bae\u4e3a\u5b9e\u73b0\u57fa\u7840\u7684\uff0cVRRP\u5168\u79f0Virtual Router Redundancy Protocol\uff0c\u5373\u865a\u62df\u8def\u7531\u5197\u4f59\u534f\u8bae\u3002
\n\u865a\u62df\u8def\u7531\u5197\u4f59\u534f\u8bae\uff0c\u53ef\u4ee5\u8ba4\u4e3a\u662f\u5b9e\u73b0\u8def\u7531\u5668\u9ad8\u53ef\u7528\u7684\u534f\u8bae\uff0c\u5373\u5c06N\u53f0\u63d0\u4f9b\u76f8\u540c\u529f\u80fd\u7684\u8def\u7531\u5668\u7ec4\u6210\u4e00\u4e2a\u8def\u7531\u5668\u7ec4\uff0c\u8fd9\u4e2a\u7ec4\u91cc\u9762\u6709\u4e00\u4e2amaster\u548c\u591a\u4e2abackup\uff0cmaster\u4e0a\u9762\u6709\u4e00\u4e2a\u5bf9\u5916\u63d0\u4f9b\u670d\u52a1\u7684vip(VIP = Virtual IP Address\uff0c\u865a\u62dfIP\u5730\u5740\uff0c\u8be5\u8def\u7531\u5668\u6240\u5728\u5c40\u57df\u7f51\u5185\u5176\u4ed6\u673a\u5668\u7684\u9ed8\u8ba4\u8def\u7531\u4e3a\u8be5vip)\uff0cmaster\u4f1a\u53d1\u7ec4\u64ad\uff0c\u5f53backup\u6536\u4e0d\u5230VRRP\u5305\u65f6\u5c31\u8ba4\u4e3amaster\u5b95\u6389\u4e86\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u6839\u636eVRRP\u7684\u4f18\u5148\u7ea7\u6765\u9009\u4e3e\u4e00\u4e2abackup\u5f53master\u3002\u8fd9\u6837\u7684\u8bdd\u5c31\u53ef\u4ee5\u4fdd\u8bc1\u8def\u7531\u5668\u7684\u9ad8\u53ef\u7528\u4e86\u3002
\nkeepalived\u4e3b\u8981\u6709\u4e09\u4e2a\u6a21\u5757\uff0c\u5206\u522b\u662fcore\u3001check\u548cVRRP\u3002core\u6a21\u5757\u4e3akeepalived\u7684\u6838\u5fc3\uff0c\u8d1f\u8d23\u4e3b\u8fdb\u7a0b\u7684\u542f\u52a8\u3001\u7ef4\u62a4\u4ee5\u53ca\u5168\u5c40\u914d\u7f6e\u6587\u4ef6\u7684\u52a0\u8f7d\u548c\u89e3\u6790\u3002check\u8d1f\u8d23\u5065\u5eb7\u68c0\u67e5\uff0c\u5305\u62ec\u5e38\u89c1\u7684\u5404\u79cd\u68c0\u67e5\u65b9\u5f0f\u3002VRRP\u6a21\u5757\u662f\u6765\u5b9e\u73b0VRRP\u534f\u8bae\u7684\u3002<\/p>\n

1.2.3. keepalived+nginx\u5b9e\u73b0\u4e3b\u5907\u8fc7\u7a0b<\/strong><\/span><\/div>\n

\u521d\u59cb\u72b6\u6001
\n\"\"
\n\u4e3b\u673a\u5b95\u673a
\n\"\"
\n\u4e3b\u673a\u6062\u590d
\n\"\"
\n\u9ad8\u53ef\u7528\u73af\u5883<\/p>\n

\u4e24\u53f0nginx\uff0c\u4e00\u4e3b\u4e00\u5907\uff1a192.168.101.3\u548c192.168.101.4\r\n\u4e24\u53f0tomcat\u670d\u52a1\u5668\uff1a192.168.101.5\u3001192.168.101.6\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

nginx\u4f5c\u4e3a\u8d1f\u8f7d\u5747\u8861\u5668\uff0c\u6240\u6709\u8bf7\u6c42\u90fd\u5230\u4e86nginx\uff0c\u53ef\u89c1nginx\u5904\u4e8e\u975e\u5e38\u91cd\u70b9\u7684\u4f4d\u7f6e\uff0c\u5982\u679cnginx\u670d\u52a1\u5668\u5b95\u673a […]<\/p>\n","protected":false},"author":1329,"featured_media":153483,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-153476","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/153476","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1329"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=153476"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/153476\/revisions"}],"predecessor-version":[{"id":153484,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/153476\/revisions\/153484"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/153483"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=153476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=153476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=153476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}