{"id":3142,"date":"2018-09-06T19:47:32","date_gmt":"2018-09-06T11:47:32","guid":{"rendered":"http:\/\/www.menglanglang.cn\/?p=3142"},"modified":"2022-09-16T05:54:09","modified_gmt":"2022-09-15T21:54:09","slug":"nginx-%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%ae%e9%ab%98%e5%8f%af%e7%94%a8","status":"publish","type":"post","link":"http:\/\/www.menglanglang.cn\/?p=3142","title":{"rendered":"Nginx \u5982\u4f55\u914d\u7f6e\u9ad8\u53ef\u7528"},"content":{"rendered":"<p>\u8fd9\u4e00\u7bc7\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e0b\u00a0Nginx + KeepAlived \u5982\u4f55\u914d\u7f6e HA\uff08\u9ad8\u53ef\u7528\u6027\u201d\uff08High Availability\uff09\u901a\u5e38\u6765\u63cf\u8ff0\u4e00\u4e2a\u7cfb\u7edf\u7ecf\u8fc7\u4e13\u95e8\u7684\u8bbe\u8ba1\uff0c\u4ece\u800c\u51cf\u5c11\u505c\u5de5\u65f6\u95f4\uff0c\u800c\u4fdd\u6301\u5176\u670d\u52a1\u7684\u9ad8\u5ea6\u53ef\u7528\u6027\uff09\u3002<\/p>\n<p>\u5728\u9879\u76ee\u90e8\u7f72\u8fc7\u7a0b\u4e2d\uff0c\u4ee3\u7406\u670d\u52a1\u5668 Nginx \u8981\u505a\u5230\u7ebf\u4e0a\u9ad8\u53ef\u7528\uff0c\u907f\u514d\u51fa\u73b0\u5355\u70b9\u6545\u969c\uff0c\u56e0\u6b64\u9700\u8981\u81f3\u5c11 2 \u53f0\u670d\u52a1\u5668\u624d\u884c\u3002\u6240\u4ee5\u8ba1\u5212\u4f7f\u7528 2 \u53f0 nginx \u4ee3\u7406\u670d\u52a1\u5668\uff0c\u901a\u8fc7 Keepalived \u6765\u5b9e\u73b0 nginx \u7684 HA\uff0c\u5b9e\u73b0\u4e00\u53f0 nginx \u4ee3\u7406\u670d\u52a1\u5668\u5b95\u673a\uff0c\u53e6\u4e00\u53f0\u5907\u673a\u81ea\u52a8\u63a5\u7ba1\u670d\u52a1\u7684\u6548\u679c\u3002\u5728\u8fd9\u91cc nginx \u505a\u53cd\u5411\u4ee3\u7406\uff0c\u5b9e\u73b0\u540e\u7aef\u5e94\u7528\u670d\u52a1\u5668\uff08\u5982 Tomcat \u7b49\uff09\u7684\u8d1f\u8f7d\u5747\u8861\u3002<\/p>\n<h3>1 KeepAlived\u4ecb\u7ecd<\/h3>\n<p>Keepalived \u662f\u4e00\u4e2a\u57fa\u4e8e VRRP \u534f\u8bae\u6765\u5b9e\u73b0\u7684\u670d\u52a1\u9ad8\u53ef\u7528\u65b9\u6848\uff0c\u53ef\u4ee5\u5229\u7528\u5176\u6765\u907f\u514d IP \u5355\u70b9\u6545\u969c\uff0c\u7c7b\u4f3c\u7684\u5de5\u5177\u8fd8\u6709 heartbeat\u3001corosync\u3001pacemaker\u3002\u4f46\u662f\u5b83\u4e00\u822c\u4e0d\u4f1a\u5355\u72ec\u51fa\u73b0\uff0c\u800c\u662f\u4e0e\u5176\u5b83\u8d1f\u8f7d\u5747\u8861\u6280\u672f\uff08\u5982lvs\u3001haproxy\u3001nginx\uff09\u4e00\u8d77\u5de5\u4f5c\u6765\u8fbe\u5230\u96c6\u7fa4\u7684\u9ad8\u53ef\u7528\u3002<\/p>\n<h4>1.1 VRRP \u534f\u8bae<\/h4>\n<p>VRRP \u5168\u79f0 Virtual Router Redundancy Protocol\uff0c\u5373\u865a\u62df\u8def\u7531\u5197\u4f59\u534f\u8bae\u3002\u53ef\u4ee5\u8ba4\u4e3a\u5b83\u662f\u5b9e\u73b0\u8def\u7531\u5668\u9ad8\u53ef\u7528\u7684\u5bb9\u9519\u534f\u8bae\uff0c\u5373\u5c06 N \u53f0\u63d0\u4f9b\u76f8\u540c\u529f\u80fd\u7684\u8def\u7531\u5668\u7ec4\u6210\u4e00\u4e2a\u8def\u7531\u5668\u7ec4(Router Group)\uff0c\u8fd9\u4e2a\u7ec4\u91cc\u9762\u6709\u4e00\u4e2a master \u548c\u591a\u4e2a backup\uff0c\u4f46\u5728\u5916\u754c\u770b\u6765\u5c31\u50cf\u4e00\u53f0\u4e00\u6837\uff0c\u6784\u6210\u865a\u62df\u8def\u7531\u5668\uff0c\u62e5\u6709\u4e00\u4e2a\u865a\u62df IP\uff08vip\uff0c\u4e5f\u5c31\u662f\u8def\u7531\u5668\u6240\u5728\u5c40\u57df\u7f51\u5185\u5176\u4ed6\u673a\u5668\u7684\u9ed8\u8ba4\u8def\u7531\uff09\uff0c\u5360\u6709\u8fd9\u4e2a IP \u7684 master \u5b9e\u9645\u8d1f\u8d23 ARP \u76f8\u5e94\u548c\u8f6c\u53d1 IP \u6570\u636e\u5305\uff0c\u7ec4\u4e2d\u7684\u5176\u5b83\u8def\u7531\u5668\u4f5c\u4e3a\u5907\u4efd\u7684\u89d2\u8272\u5904\u4e8e\u5f85\u547d\u72b6\u6001\u3002master \u4f1a\u53d1\u7ec4\u64ad\u6d88\u606f\uff0c\u5f53 backup \u5728\u8d85\u65f6\u65f6\u95f4\u5185\u6536\u4e0d\u5230 vrrp \u5305\u65f6\u5c31\u8ba4\u4e3a master \u5b95\u6389\u4e86\uff0c\u8fd9\u65f6\u5c31\u9700\u8981\u6839\u636e VRRP \u7684\u4f18\u5148\u7ea7\u6765\u9009\u4e3e\u4e00\u4e2a backup \u5f53 master\uff0c\u4fdd\u8bc1\u8def\u7531\u5668\u7684\u9ad8\u53ef\u7528\u3002<\/p>\n<p>\u5728 VRRP \u534f\u8bae\u5b9e\u73b0\u91cc\uff0c\u865a\u62df\u8def\u7531\u5668\u4f7f\u7528 00-00-5E-00-01-XX \u4f5c\u4e3a\u865a\u62df MAC \u5730\u5740\uff0cXX \u5c31\u662f\u552f\u4e00\u7684 VRID \uff08Virtual Router IDentifier\uff09\uff0c\u8fd9\u4e2a\u5730\u5740\u540c\u4e00\u65f6\u95f4\u53ea\u6709\u4e00\u4e2a\u7269\u7406\u8def\u7531\u5668\u5360\u7528\u3002\u5728\u865a\u62df\u8def\u7531\u5668\u91cc\u9762\u7684\u7269\u7406\u8def\u7531\u5668\u7ec4\u91cc\u9762\u901a\u8fc7\u591a\u64ad IP \u5730\u5740 224.0.0.18 \u6765\u5b9a\u65f6\u53d1\u9001\u901a\u544a\u6d88\u606f\u3002\u6bcf\u4e2a Router \u90fd\u6709\u4e00\u4e2a 1-255 \u4e4b\u95f4\u7684\u4f18\u5148\u7ea7\u522b\uff0c\u7ea7\u522b\u6700\u9ad8\u7684\uff08highest priority\uff09\u5c06\u6210\u4e3a\u4e3b\u63a7\uff08master\uff09\u8def\u7531\u5668\u3002\u901a\u8fc7\u964d\u4f4e master \u7684\u4f18\u5148\u6743\u53ef\u4ee5\u8ba9\u5904\u4e8e backup \u72b6\u6001\u7684\u8def\u7531\u5668\u62a2\u5360\uff08pro-empt\uff09\u4e3b\u8def\u7531\u5668\u7684\u72b6\u6001\uff0c\u4e24\u4e2a backup \u4f18\u5148\u7ea7\u76f8\u540c\u7684 IP \u5730\u5740\u8f83\u5927\u8005\u4e3a master\uff0c\u63a5\u7ba1\u865a\u62df IP\u3002<\/p>\n<h4><strong>1.2 \u4e0e heartbeat\/corosync \u7b49\u6bd4\u8f83<\/strong><\/h4>\n<p>Heartbeat\u3001Corosync\u3001Keepalived \u8fd9\u4e09\u4e2a\u96c6\u7fa4\u7ec4\u4ef6\u6211\u4eec\u5230\u5e95\u9009\u54ea\u4e2a\u597d\uff0c\u9996\u5148\u6211\u60f3\u8bf4\u660e\u7684\u662f\uff0cHeartbeat\u3001Corosync \u662f\u5c5e\u4e8e\u540c\u4e00\u7c7b\u578b\uff0cKeepalived\u4e0eHeartbeat\u3001Corosync\uff0c\u6839\u672c\u4e0d\u662f\u540c\u4e00\u7c7b\u578b\u7684\u3002Keepalived \u4f7f\u7528\u7684 vrrp \u534f\u8bae\u65b9\u5f0f\uff0c\u865a\u62df\u8def\u7531\u5197\u4f59\u534f\u8bae (Virtual Router Redundancy Protocol\uff0c\u7b80\u79f0 VRRP)\uff1bHeartbeat \u6216 Corosync \u662f\u57fa\u4e8e\u4e3b\u673a\u6216\u7f51\u7edc\u670d\u52a1\u7684\u9ad8\u53ef\u7528\u65b9\u5f0f\uff1b\u7b80\u5355\u7684\u8bf4\u5c31\u662f\uff0cKeepalived \u7684\u76ee\u7684\u662f\u6a21\u62df\u8def\u7531\u5668\u7684\u9ad8\u53ef\u7528\uff0cHeartbeat \u6216 Corosync \u7684\u76ee\u7684\u662f\u5b9e\u73b0 Service \u7684\u9ad8\u53ef\u7528\u3002<\/p>\n<p>\u6240\u4ee5\u4e00\u822c Keepalived \u662f\u5b9e\u73b0\u524d\u7aef\u9ad8\u53ef\u7528\uff0c\u5e38\u7528\u7684\u524d\u7aef\u9ad8\u53ef\u7528\u7684\u7ec4\u5408\u6709\uff0c\u5c31\u662f\u6211\u4eec\u5e38\u89c1\u7684 LVS+Keepalived\u3001Nginx+Keepalived\u3001HAproxy+Keepalived\u3002\u800c Heartbeat \u6216 Corosync \u662f\u5b9e\u73b0\u670d\u52a1\u7684\u9ad8\u53ef\u7528\uff0c\u5e38\u89c1\u7684\u7ec4\u5408\u6709Heartbeat v3(Corosync)+Pacemaker+NFS+Httpd \u5b9e\u73b0 Web \u670d\u52a1\u5668\u7684\u9ad8\u53ef\u7528\u3001Heartbeat v3(Corosync)+Pacemaker+NFS+MySQL \u5b9e\u73b0 MySQL \u670d\u52a1\u5668\u7684\u9ad8\u53ef\u7528\u3002\u603b\u7ed3\u4e00\u4e0b\uff0cKeepalived \u4e2d\u5b9e\u73b0\u8f7b\u91cf\u7ea7\u7684\u9ad8\u53ef\u7528\uff0c\u4e00\u822c\u7528\u4e8e\u524d\u7aef\u9ad8\u53ef\u7528\uff0c\u4e14\u4e0d\u9700\u8981\u5171\u4eab\u5b58\u50a8\uff0c\u4e00\u822c\u5e38\u7528\u4e8e\u4e24\u4e2a\u8282\u70b9\u7684\u9ad8\u53ef\u7528\u3002\u800c Heartbeat(\u6216Corosync)\u4e00\u822c\u7528\u4e8e\u670d\u52a1\u7684\u9ad8\u53ef\u7528\uff0c\u4e14\u9700\u8981\u5171\u4eab\u5b58\u50a8\uff0c\u4e00\u822c\u7528\u4e8e\u591a\u8282\u70b9\u7684\u9ad8\u53ef\u7528\u3002\u8fd9\u4e2a\u95ee\u9898\u6211\u4eec\u8bf4\u660e\u767d\u4e86\u3002<\/p>\n<p>\u53c8\u6709\u535a\u53cb\u4f1a\u95ee\u4e86\uff0c\u90a3 heartbaet \u4e0e corosync \u6211\u4eec\u53c8\u5e94\u8be5\u9009\u62e9\u54ea\u4e2a\u597d\u554a\uff0c\u6211\u60f3\u8bf4\u6211\u4eec\u4e00\u822c\u7528 corosync\uff0c\u56e0\u4e3acorosync \u7684\u8fd0\u884c\u673a\u5236\u66f4\u4f18\u4e8e heartbeat\uff0c\u5c31\u8fde\u4ece heartbeat \u5206\u79bb\u51fa\u6765\u7684 pacemaker \u90fd\u8bf4\u5728\u4ee5\u540e\u7684\u5f00\u53d1\u5f53\u4e2d\u66f4\u503e\u5411\u4e8ecorosync\uff0c\u6240\u4ee5\u73b0\u5728 corosync+pacemaker \u662f\u6700\u4f73\u7ec4\u5408\u3002<\/p>\n<h4>1.3 KeepAlived + Nginx<\/h4>\n<p>keepalived \u53ef\u4ee5\u8ba4\u4e3a\u662f VRRP \u534f\u8bae\u5728 Linux \u4e0a\u7684\u5b9e\u73b0\uff0c\u4e3b\u8981\u6709\u4e09\u4e2a\u6a21\u5757\uff0c\u5206\u522b\u662f core\u3001check \u548c vrrp\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\u73b0 VRRP \u534f\u8bae\u7684\u3002<\/p>\n<h3>2. \u5b89\u88c5\u8f6f\u4ef6<\/h3>\n<h4>2.0 \u6d4b\u8bd5\u73af\u5883<\/h4>\n<p>\u4ee3\u7406\u673a\u5668 2 \u53f0\uff1aCentOS release 6.9 (Final)\u00a0 2.6.32-696.el6.x86_64<\/p>\n<p>\u673a\u5668 IP\uff1a192.168.220.58\uff08web58\uff0c\u505aBACKUP\uff09\u3001192.168.220.59\uff08web59\uff0c\u505aMASTER\uff09<\/p>\n<p>\u865a\u62df IP\uff1a192.168.220.60<\/p>\n<h4>2.1 \u5b89\u88c5 Nginx<\/h4>\n<p>\u5728 2 \u53f0\u673a\u5668\u4e0a\uff0c\u5b89\u88c5 Nginx\u3002<\/p>\n<p><strong>\u5b89\u88c5\u547d\u4ee4\uff1a<\/strong>yum install -y nginx<\/p>\n<p><strong>\u67e5\u770b\u5b89\u88c5\u7684 Nginx \u7248\u672c\uff1a<\/strong>nginx -v<\/p>\n<p>\u7248\u672c\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n<p>nginx version: nginx\/1.10.2<\/p>\n<p>\u53e6\uff1a\u4e00\u4e9b\u8f6f\u4ef6\u5e38\u89c4\u914d\u7f6e\u547d\u4ee4\uff1a<\/p>\n<p>chkconfig nginx --list<\/p>\n<p>chkconfig nginx on<\/p>\n<p>service nginx status<\/p>\n<p>service nginx start<\/p>\n<h4>2.2 \u6d4b\u8bd5 Nginx<\/h4>\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\uff0c\u76f4\u63a5\u8f93\u5165\u5730\u5740 http:\/\/192.168.220.58 \u548c 192.168.220.59\uff0c\u770b\u5230\u5982\u4e0b\u754c\u9762\uff0c\u8bf4\u660e Nginx \u5b89\u88c5\u5e76\u542f\u52a8\u6210\u529f\u3002<\/p>\n<p><a href=\"http:\/\/www.menglanglang.cn\/wp-content\/uploads\/2018\/09\/nginx_start.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-3149\" src=\"http:\/\/www.menglanglang.cn\/wp-content\/uploads\/2018\/09\/nginx_start-1024x194.png\" alt=\"\" width=\"1024\" height=\"194\" srcset=\"http:\/\/www.menglanglang.cn\/wp-content\/uploads\/2018\/09\/nginx_start-1024x194.png 1024w, http:\/\/www.menglanglang.cn\/wp-content\/uploads\/2018\/09\/nginx_start-300x57.png 300w, http:\/\/www.menglanglang.cn\/wp-content\/uploads\/2018\/09\/nginx_start-768x146.png 768w, http:\/\/www.menglanglang.cn\/wp-content\/uploads\/2018\/09\/nginx_start.png 1917w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n<h4>2.3 \u5b89\u88c5 Keepalived<\/h4>\n<p>\u5728 2 \u53f0\u673a\u5668\u4e0a\uff0c\u5b89\u88c5 Keepalived\u3002<\/p>\n<p><strong>\u5b89\u88c5\u547d\u4ee4\uff1a<\/strong>yum install -y keepalived<\/p>\n<p><strong>\u67e5\u770b\u5b89\u88c5\u7684 Keepalived \u7248\u672c\uff1a<\/strong>keepalived -v<\/p>\n<p>\u7248\u672c\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n<p>Keepalived v1.2.13 (03\/19,2015)<\/p>\n<p>\u53e6\uff1a\u4e00\u4e9b\u8f6f\u4ef6\u5e38\u89c4\u914d\u7f6e\u547d\u4ee4\uff1a<\/p>\n<p>chkconfig keepalived --list<\/p>\n<p>chkconfig keepalived on<\/p>\n<p>service keepalived status<\/p>\n<p>service keepalived start<\/p>\n<h3>3\u00a0Keepalived \u5b9e\u73b0 nginx \u9ad8\u53ef\u7528<\/h3>\n<h4>3.1 nginx \u76d1\u63a7\u811a\u672c<\/h4>\n<p>\u8be5\u811a\u672c\u68c0\u6d4b ngnix \u7684\u8fd0\u884c\u72b6\u6001\uff0c\u5e76\u5728 nginx \u8fdb\u7a0b\u4e0d\u5b58\u5728\u65f6\u5c1d\u8bd5\u91cd\u65b0\u542f\u52a8 ngnix\uff0c\u5982\u679c\u542f\u52a8\u5931\u8d25\u5219\u505c\u6b62 keepalived\uff0c\u51c6\u5907\u8ba9\u5176\u5b83\u673a\u5668\u63a5\u7ba1\u3002<\/p>\n<p>\u8def\u5f84\uff1a\/etc\/keepalived\/check_nginx.sh\uff1a<\/p>\n<pre>#! \/bin\/bash\ncounter=$(ps -C nginx --no-heading|wc -l)\nif [ \"${counter}\" = \"0\" ]; then\n    \/etc\/rc.d\/init.d\/nginx start\n    sleep 2\n    counter=$(ps -C nginx --no-heading|wc -l)\n    if [ \"${counter}\" = \"0\" ]; then\n        \/etc\/rc.d\/init.d\/keepalived stop\n    fi\nfi<\/pre>\n<p>\u53e6\uff1a\u4e5f\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u603b\u7ed3\u51fa\u5728\u4ec0\u4e48\u60c5\u5f62\u4e0b\u5173\u95edkeepalived\uff0c\u5982 curl \u4e3b\u9875\u8fde\u7eed 2 \u4e2a 5s \u6ca1\u6709\u54cd\u5e94\u5219\u5207\u6362\uff1a<\/p>\n<pre>#!\/bin\/bash\n# curl -IL http:\/\/localhost\/member\/login.htm\n# curl --data \"memberName=fengkan&amp;password=22\" http:\/\/localhost\/member\/login.htm\n \ncount = 0\nfor (( k=0; k&lt;2; k++ )) \ndo \n    check_code=$( curl --connect-timeout 3 -sL -w \"%{http_code}\\\\n\" http:\/\/localhost\/login.html -o \/dev\/null )\n    if [ \"$check_code\" != \"200\" ]; then\n        count = count +1\n        continue\n    else\n        count = 0\n        break\n    fi\ndone\nif [ \"$count\" != \"0\" ]; then\n#   \/etc\/init.d\/keepalived stop\n    exit 1\nelse\n    exit 0\nfi<\/pre>\n<h4>3.2 keepalived.conf \u914d\u7f6e<\/h4>\n<pre>! Configuration File for keepalived\n\nglobal_defs {\n   notification_email {\n     acassen@firewall.loc\n     failover@firewall.loc\n     sysadmin@firewall.loc\n   }\n   notification_email_from Alexandre.Cassen@firewall.loc\n   smtp_server 192.168.200.1\n   smtp_connect_timeout 30\n   router_id LVS_DEVEL\n}\n\nvrrp_script chk_nginx {\n#    script \"killall -0 nginx\"\n    script \"\/etc\/keepalived\/check_nginx.sh\" \n    interval 2 \n    weight -5 \n    fall 3  \n    rise 2 \n}\n\nvrrp_instance VI_1 {\n    state MASTER\n    interface eth0\n    mcast_src_ip 192.168.220.59\n    virtual_router_id 51\n    priority 101\n    advert_int 2\n    authentication {\n        auth_type PASS\n        auth_pass 1111\n    }\n    virtual_ipaddress {\n        192.168.220.60\n    }\n    track_script {\n       chk_nginx \n    }\n}<\/pre>\n<p>\u5728\u53e6\u5916\u4e00\u53f0\u4ee3\u7406\u670d\u52a1\u5668\u4e0a\uff0c\u53ea\u9700\u8981\u6539\u53d8 state MASTER -&gt; state BACKUP\uff0cpriority 101 -&gt; priority 100\uff0cmcast_src_ip 192.168.220.59 -&gt; mcast_src_ip 192.168.220.58 \u5373\u53ef\u3002<\/p>\n<p><strong>\u914d\u7f6e\u9009\u9879\u8bf4\u660e\uff1a<\/strong><\/p>\n<p><span style=\"color: #ff0000;\">global_defs<\/span><\/p>\n<p>notification_email \uff1a keepalived \u5728\u53d1\u751f\u8bf8\u5982\u5207\u6362\u64cd\u4f5c\u65f6\u9700\u8981\u53d1\u9001 email \u901a\u77e5\u5730\u5740\uff0c\u540e\u9762\u7684 smtp_server \u76f8\u6bd4\u4e5f\u90fd\u77e5\u9053\u662f\u90ae\u4ef6\u670d\u52a1\u5668\u5730\u5740\u3002\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5176\u5b83\u65b9\u5f0f\u62a5\u8b66\uff0c\u6bd5\u7adf\u90ae\u4ef6\u4e0d\u662f\u5b9e\u65f6\u901a\u77e5\u7684\u3002<\/p>\n<p>router_id \uff1a \u673a\u5668\u6807\u8bc6\uff0c\u901a\u5e38\u53ef\u8bbe\u4e3a hostname\u3002\u6545\u969c\u53d1\u751f\u65f6\uff0c\u90ae\u4ef6\u901a\u77e5\u4f1a\u7528\u5230\u3002<\/p>\n<p><span style=\"color: #ff0000;\">vrrp_instance<\/span><\/p>\n<p>state \uff1a \u6307\u5b9a instance(Initial) \u7684\u521d\u59cb\u72b6\u6001\uff0c\u5c31\u662f\u8bf4\u5728\u914d\u7f6e\u597d\u540e\uff0c\u8fd9\u53f0\u670d\u52a1\u5668\u7684\u521d\u59cb\u72b6\u6001\u5c31\u662f\u8fd9\u91cc\u6307\u5b9a\u7684\uff0c\u4f46\u8fd9\u91cc\u6307\u5b9a\u7684\u4e0d\u7b97\uff0c\u8fd8\u662f\u5f97\u8981\u901a\u8fc7\u7ade\u9009\u901a\u8fc7\u4f18\u5148\u7ea7\u6765\u786e\u5b9a\u3002\u5982\u679c\u8fd9\u91cc\u8bbe\u7f6e\u4e3a MASTER\uff0c\u4f46\u5982\u82e5\u4ed6\u7684\u4f18\u5148\u7ea7\u4e0d\u53ca\u53e6\u5916\u4e00\u53f0\uff0c\u90a3\u4e48\u8fd9\u53f0\u5728\u53d1\u9001\u901a\u544a\u65f6\uff0c\u4f1a\u53d1\u9001\u81ea\u5df1\u7684\u4f18\u5148\u7ea7\uff0c\u53e6\u5916\u4e00\u53f0\u53d1\u73b0\u4f18\u5148\u7ea7\u4e0d\u5982\u81ea\u5df1\u7684\u9ad8\uff0c\u90a3\u4e48\u4ed6\u4f1a\u5c31\u56de\u62a2\u5360\u4e3a MASTER\u3002<\/p>\n<p>interface \uff1a \u5b9e\u4f8b\u7ed1\u5b9a\u7684\u7f51\u5361\uff0c\u56e0\u4e3a\u5728\u914d\u7f6e\u865a\u62df IP \u7684\u65f6\u5019\u5fc5\u987b\u662f\u5728\u5df2\u6709\u7684\u7f51\u5361\u4e0a\u6dfb\u52a0\u7684\u3002<\/p>\n<p>mcast_src_ip \uff1a \u53d1\u9001\u591a\u64ad\u6570\u636e\u5305\u65f6\u7684\u6e90 IP \u5730\u5740\uff0c\u8fd9\u91cc\u6ce8\u610f\u4e86\uff0c\u8fd9\u91cc\u5b9e\u9645\u4e0a\u5c31\u662f\u5728\u90a3\u4e2a\u5730\u5740\u4e0a\u53d1\u9001 VRRP \u901a\u544a\uff0c\u8fd9\u4e2a\u975e\u5e38\u91cd\u8981\uff0c\u4e00\u5b9a\u8981\u9009\u62e9\u7a33\u5b9a\u7684\u7f51\u5361\u7aef\u53e3\u6765\u53d1\u9001\uff0c\u8fd9\u91cc\u76f8\u5f53\u4e8e heartbeat \u7684\u5fc3\u8df3\u7aef\u53e3\uff0c\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u90a3\u4e48\u5c31\u7528\u9ed8\u8ba4\u7684\u7ed1\u5b9a\u7684\u7f51\u5361\u7684 IP\uff0c\u4e5f\u5c31\u662f interface \u6307\u5b9a\u7684 IP \u5730\u5740\u3002<\/p>\n<p>virtual_router_id \uff1a \u8fd9\u91cc\u8bbe\u7f6e VRID\uff0c\u8fd9\u91cc\u975e\u5e38\u91cd\u8981\uff0c\u76f8\u540c\u7684 VRID \u4e3a\u4e00\u4e2a\u7ec4\uff0c\u4ed6\u5c06\u51b3\u5b9a\u591a\u64ad\u7684 MAC \u5730\u5740\u3002<\/p>\n<p>priority \uff1a \u8bbe\u7f6e\u672c\u8282\u70b9\u7684\u4f18\u5148\u7ea7\uff0c\u4f18\u5148\u7ea7\u9ad8\u7684\u4e3a master\u3002<\/p>\n<p>advert_int \uff1a \u68c0\u67e5\u95f4\u9694\uff0c\u9ed8\u8ba4\u4e3a 1 \u79d2\u3002\u8fd9\u5c31\u662f VRRP \u7684\u5b9a\u65f6\u5668\uff0cMASTER \u6bcf\u9694\u8fd9\u6837\u4e00\u4e2a\u65f6\u95f4\u95f4\u9694\uff0c\u5c31\u4f1a\u53d1\u9001\u4e00\u4e2aadvertisement \u62a5\u6587\u4ee5\u901a\u77e5\u7ec4\u5185\u5176\u4ed6\u8def\u7531\u5668\u81ea\u5df1\u5de5\u4f5c\u6b63\u5e38\u3002<\/p>\n<p>authentication \uff1a \u5b9a\u4e49\u8ba4\u8bc1\u65b9\u5f0f\u548c\u5bc6\u7801\uff0c\u4e3b\u4ece\u5fc5\u987b\u4e00\u6837\u3002<\/p>\n<p>virtual_ipaddress \uff1a \u8fd9\u91cc\u8bbe\u7f6e\u7684\u5c31\u662f VIP\uff0c\u4e5f\u5c31\u662f\u865a\u62df IP \u5730\u5740\uff0c\u4ed6\u968f\u7740 state \u7684\u53d8\u5316\u800c\u589e\u52a0\u5220\u9664\uff0c\u5f53 state \u4e3a master \u7684\u65f6\u5019\u5c31\u6dfb\u52a0\uff0c\u5f53 state \u4e3a backup \u7684\u65f6\u5019\u5220\u9664\uff0c\u8fd9\u91cc\u4e3b\u8981\u662f\u6709\u4f18\u5148\u7ea7\u6765\u51b3\u5b9a\u7684\uff0c\u548c state \u8bbe\u7f6e\u7684\u503c\u6ca1\u6709\u591a\u5927\u5173\u7cfb\uff0c\u8fd9\u91cc\u53ef\u4ee5\u8bbe\u7f6e\u591a\u4e2a IP \u5730\u5740\u3002<\/p>\n<p>track_script \uff1a \u5f15\u7528 VRRP \u811a\u672c\uff0c\u5373\u5728 vrrp_script \u90e8\u5206\u6307\u5b9a\u7684\u540d\u5b57\u3002\u5b9a\u671f\u8fd0\u884c\u5b83\u4eec\u6765\u6539\u53d8\u4f18\u5148\u7ea7\uff0c\u5e76\u6700\u7ec8\u5f15\u53d1\u4e3b\u5907\u5207\u6362\u3002<\/p>\n<p><span style=\"color: #ff0000;\">vrrp_script<\/span><\/p>\n<p>\u544a\u8bc9 keepalived \u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u5207\u6362\uff0c\u6240\u4ee5\u5c24\u4e3a\u91cd\u8981\u3002\u53ef\u4ee5\u6709\u591a\u4e2a vrrp_script\u3002<\/p>\n<p>script \uff1a \u81ea\u5df1\u5199\u7684\u68c0\u6d4b\u811a\u672c\u3002\u4e5f\u53ef\u4ee5\u662f\u4e00\u884c\u547d\u4ee4\u5982 killall -0 nginx\u3002<\/p>\n<p>interval 2 \uff1a \u6bcf 2s \u68c0\u6d4b\u4e00\u6b21\u3002<\/p>\n<p>weight -5 \uff1a \u68c0\u6d4b\u5931\u8d25\uff08\u811a\u672c\u8fd4\u56de\u975e0\uff09\u5219\u4f18\u5148\u7ea7 -5\u3002<\/p>\n<p>fall 2 \uff1a \u68c0\u6d4b\u8fde\u7eed 2 \u6b21\u5931\u8d25\u624d\u7b97\u786e\u5b9a\u662f\u771f\u5931\u8d25\u3002\u4f1a\u7528 weight \u51cf\u5c11\u4f18\u5148\u7ea7\uff081-255\u4e4b\u95f4\uff09\u3002<\/p>\n<p>rise 1 \uff1a \u68c0\u6d4b 1 \u6b21\u6210\u529f\u5c31\u7b97\u6210\u529f\u3002\u4f46\u4e0d\u4fee\u6539\u4f18\u5148\u7ea7\u3002<\/p>\n<p>\u8fd9\u91cc\u8981\u63d0\u793a\u4e00\u4e0b script \u4e00\u822c\u6709 2 \u79cd\u5199\u6cd5\uff1a<\/p>\n<p>1\uff09\u901a\u8fc7\u811a\u672c\u6267\u884c\u7684\u8fd4\u56de\u7ed3\u679c\uff0c\u6539\u53d8\u4f18\u5148\u7ea7\uff0ckeepalived \u7ee7\u7eed\u53d1\u9001\u901a\u544a\u6d88\u606f\uff0cbackup \u6bd4\u8f83\u4f18\u5148\u7ea7\u518d\u51b3\u5b9a\uff1b<\/p>\n<p>2\uff09\u811a\u672c\u91cc\u9762\u68c0\u6d4b\u5230\u5f02\u5e38\uff0c\u76f4\u63a5\u5173\u95edkeepalived\u8fdb\u7a0b\uff0cbackup \u673a\u5668\u63a5\u6536\u4e0d\u5230 advertisement \u4f1a\u62a2\u5360 IP\u3002<\/p>\n<p>\u4e0a\u6587 vrrp_script \u914d\u7f6e\u90e8\u5206\uff0ckillall -0 nginx \u5c5e\u4e8e\u7b2c 1 \u79cd\u60c5\u51b5\uff0c\/etc\/keepalived\/check_nginx.sh \u5c5e\u4e8e\u7b2c 2 \u79cd\u60c5\u51b5\uff08\u811a\u672c\u4e2d\u5173\u95ed keepalived\uff09\u3002\u4e2a\u4eba\u66f4\u503e\u5411\u4e8e\u901a\u8fc7 shell \u811a\u672c\u5224\u65ad\uff0c\u4f46\u6709\u5f02\u5e38\u65f6 exit 1\uff0c\u6b63\u5e38\u9000\u51fa exit 0\uff0c\u7136\u540e keepalived\u6839\u636e\u52a8\u6001\u8c03\u6574\u7684 vrrp_instance \u4f18\u5148\u7ea7\u9009\u4e3e\u51b3\u5b9a\u662f\u5426\u62a2\u5360VIP\uff1a<\/p>\n<p>1\uff09\u5982\u679c\u811a\u672c\u6267\u884c\u7ed3\u679c\u4e3a 0\uff0c\u5e76\u4e14 weight \u914d\u7f6e\u7684\u503c\u5927\u4e8e 0\uff0c\u5219\u4f18\u5148\u7ea7\u76f8\u5e94\u7684\u589e\u52a0\uff1b<\/p>\n<p>2\uff09\u5982\u679c\u811a\u672c\u6267\u884c\u7ed3\u679c\u975e 0\uff0c\u5e76\u4e14 weight \u914d\u7f6e\u7684\u503c\u5c0f\u4e8e 0\uff0c\u5219\u4f18\u5148\u7ea7\u76f8\u5e94\u7684\u51cf\u5c11\uff1b<\/p>\n<p>3\uff09\u5176\u4ed6\u60c5\u51b5\uff0c\u539f\u672c\u914d\u7f6e\u7684\u4f18\u5148\u7ea7\u4e0d\u53d8\uff0c\u5373\u914d\u7f6e\u6587\u4ef6\u4e2d priority \u5bf9\u5e94\u7684\u503c\u3002<\/p>\n<p>\u63d0\u793a\uff1a<\/p>\n<p>1\uff09\u4f18\u5148\u7ea7\u4e0d\u4f1a\u4e0d\u65ad\u7684\u63d0\u9ad8\u6216\u8005\u964d\u4f4e\uff1b<\/p>\n<p>2\uff09\u53ef\u4ee5\u7f16\u5199\u591a\u4e2a\u68c0\u6d4b\u811a\u672c\u5e76\u4e3a\u6bcf\u4e2a\u68c0\u6d4b\u811a\u672c\u8bbe\u7f6e\u4e0d\u540c\u7684 weight\uff08\u5728\u914d\u7f6e\u4e2d\u5217\u51fa\u5c31\u884c\uff09\uff1b<\/p>\n<p>3\uff09\u4e0d\u7ba1\u63d0\u9ad8\u4f18\u5148\u7ea7\u8fd8\u662f\u964d\u4f4e\u4f18\u5148\u7ea7\uff0c\u6700\u7ec8\u4f18\u5148\u7ea7\u7684\u8303\u56f4\u662f\u5728[1,254]\uff0c\u4e0d\u4f1a\u51fa\u73b0\u4f18\u5148\u7ea7\u5c0f\u4e8e\u7b49\u4e8e 0 \u6216\u8005\u4f18\u5148\u7ea7\u5927\u4e8e\u7b49\u4e8e 255\u7684\u60c5\u51b5\uff1b<\/p>\n<p>4\uff09\u5728 MASTER \u8282\u70b9\u7684 vrrp_instance \u4e2d\u914d\u7f6e nopreempt \uff0c\u5f53\u5b83\u5f02\u5e38\u6062\u590d\u540e\uff0c\u5373\u4f7f\u5b83 prio \u66f4\u9ad8\u4e5f\u4e0d\u4f1a\u62a2\u5360\uff0c\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u6b63\u5e38\u60c5\u51b5\u4e0b\u505a\u65e0\u8c13\u7684\u5207\u6362\uff1b<\/p>\n<p>\u4ee5\u4e0a\u53ef\u4ee5\u505a\u5230\u5229\u7528\u811a\u672c\u68c0\u6d4b\u4e1a\u52a1\u8fdb\u7a0b\u7684\u72b6\u6001\uff0c\u5e76\u52a8\u6001\u8c03\u6574\u4f18\u5148\u7ea7\u4ece\u800c\u5b9e\u73b0\u4e3b\u5907\u5207\u6362\u3002<\/p>\n<p><strong>\u914d\u7f6e\u7ed3\u675f<\/strong><\/p>\n<p>\u5728\u9ed8\u8ba4\u7684 keepalive.conf \u91cc\u9762\u8fd8\u6709 virtual_server,real_server \u8fd9\u6837\u7684\u914d\u7f6e\uff0c\u8fd9\u91cc\u7528\u4e0d\u5230\uff0c\u5b83\u662f\u4e3a lvs \u51c6\u5907\u7684\u3002 notify \u53ef\u4ee5\u5b9a\u4e49\u5728\u5207\u6362\u6210 MASTER \u6216 BACKUP \u65f6\u6267\u884c\u7684\u811a\u672c\uff0c\u5982\u6709\u9700\u6c42\u8bf7\u81ea\u884c\u914d\u7f6e\u3002<\/p>\n<h4>3.3 Nginx \u914d\u7f6e<\/h4>\n<p>\u5f53\u7136 nginx \u6ca1\u6709\u4ec0\u4e48\u53ef\u914d\u7f6e\u7684\uff0c\u56e0\u4e3a\u5b83\u4e0e keepalived \u5e76\u6ca1\u6709\u8054\u7cfb\u3002\u4f46\u8bb0\u4f4f\uff0c2 \u53f0 nginx \u670d\u52a1\u5668\u4e0a\u7684\u914d\u7f6e\u5e94\u8be5\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff08rsync\u540c\u6b65\uff09\uff0c\u8fd9\u6837\u624d\u80fd\u505a\u5230\u5bf9\u7528\u6237\u900f\u660e\uff0cnginx.conf \u91cc\u9762\u7684\u00a0server_name\u00a0\u00a0\u5c3d\u91cf\u4f7f\u7528\u57df\u540d\u6765\u4ee3\u66ff\uff0c\u7136\u540edns\u89e3\u6790\u8fd9\u4e2a\u57df\u540d\u5230\u865a\u62df IP 192.168.220.60\u3002<\/p>\n<h3>4 \u6d4b\u8bd5<\/h3>\n<h4>4.1 \u67e5\u770b\u7f51\u7edc<\/h4>\n<p>\u5728\u4e24\u53f0\u673a\u5668\u4e0a\u8f93\u5165\u547d\u4ee4\u00a0ip a |grep eth0\uff0c\u5219\u770b\u5230\u5982\u4e0b\u7ed3\u679c\uff1a<\/p>\n<pre>[root@web59 html]# ip a |grep eth0\n2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UP qlen 1000\n    inet 192.168.220.59\/24 brd 192.168.220.255 scope global eth0\n    inet 192.168.220.60\/32 scope global eth0\n\n[root@web58 html]# ip a |grep eth0\n2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UP qlen 1000\n    inet 192.168.220.58\/24 brd 192.168.220.255 scope global eth0<\/pre>\n<p>\u6d4f\u89c8\u5668\u8bbf\u95ee 192.168.220.60\uff0c\u6b63\u5e38\u4e14\u8d70\u7684\u662f 59 \u673a\u5668\u4e0a\u7684 nginx\u3002<\/p>\n<h4>4.2 \u505c\u6389 Keepalived MASTER<\/h4>\n<p>\u505c\u6389 59 \u673a\u5668\u4e0a\u7684 Keepalived \u670d\u52a1\uff0c\u5c31\u76f8\u5f53\u4e8e\u673a\u5668\u76f4\u63a5\u6302\u6389\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u770b\u5230 58 \u4f1a\u81ea\u52a8\u63a5\u7ba1 MASTER\uff0c\u8fd9\u662f\u8bbf\u95ee\u00a0192.168.220.60\uff0c\u6b63\u5e38\u4e14\u8d70\u7684\u662f 58 \u673a\u5668\u4e0a\u7684 nginx\u3002<\/p>\n<p>\u518d\u6b21\u542f\u52a8 59 \u673a\u5668\u7684 Keepalived\uff0c\u53ef\u4ee5\u770b\u5230\u865a\u62df IP \u53c8\u6f02\u79fb\u5230\u4e86 59 \u673a\u5668\u4e0a\u3002<\/p>\n<h4>4.3 \u505c\u6389 MASTER \u673a\u5668\u4e0a\u7684 Nginx<\/h4>\n<p>\u5982\u679c\u505c\u6389 59 \u673a\u5668\u4e0a\u7684 Nginx\uff0c\u76f8\u5f53\u4e8e\u53ea\u662f\u4ee3\u7406\u670d\u52a1\u5668\u6302\u6389\u4e86\uff0c\u53ef\u4ee5\u770b\u5230 Keepalived \u4f1a\u81ea\u52a8\u6267\u884c\u81ea\u5b9a\u4e49\u7684\u68c0\u67e5\u811a\u672c\uff0c\u4f1a\u5c1d\u8bd5\u91cd\u542f Nginx \u670d\u52a1\uff0c\u5982\u679c\u542f\u52a8\u6210\u529f\uff0c\u5219\u865a\u62df IP \u4e0d\u4f1a\u6f02\u79fb\uff1b\u5982\u679c\u5c1d\u8bd5\u542f\u52a8\u5931\u8d25\uff0c\u5219\u865a\u62df IP \u5c06\u6f02\u79fb\u5230 58 \u673a\u5668\u4e0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e00\u7bc7\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e0b\u00a0Nginx + KeepAlived \u5982\u4f55\u914d\u7f6e HA\uff08\u9ad8\u53ef\u7528\u6027\u201d\uff08High Availab [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[705],"tags":[721,720,719,877,704,722,214,199,781],"class_list":["post-3142","post","type-post","status-publish","format-standard","hentry","category-it-frame","tag-ha","tag-keepalived","tag-nginx","tag-877","tag-704","tag-722","tag-214","tag-199","tag-781"],"_links":{"self":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/3142","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3142"}],"version-history":[{"count":11,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/3142\/revisions"}],"predecessor-version":[{"id":3165,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/3142\/revisions\/3165"}],"wp:attachment":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3142"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}