{"id":303,"date":"2013-03-28T12:22:54","date_gmt":"2013-03-28T04:22:54","guid":{"rendered":"http:\/\/www.shuran.cn\/?p=303"},"modified":"2013-03-28T12:22:54","modified_gmt":"2013-03-28T04:22:54","slug":"nginxkeepalived%e5%8f%8c%e6%9c%ba%e7%83%ad%e5%a4%87","status":"publish","type":"post","link":"https:\/\/www.shuran.cn\/?p=303","title":{"rendered":"Nginx+keepalived\u53cc\u673a\u70ed\u5907"},"content":{"rendered":"<p>[plain] view plaincopy<br \/>\nNginx Master: 192.168.128.134<br \/>\nNginx Backup: 192.168.128.138<br \/>\nVIP:192.168.128.177<br \/>\nNginx\u5b89\u88c5\u89c1nginx\u5b89\u88c5\u914d\u7f6e\u6587\u6863<br \/>\nMaster\u4e0eBackup\u7684Nginx\u914d\u7f6e\u4e00\u81f4.<br \/>\n\u5728MASTER\u8ddfBACKUP\u4e0a\u5b89\u88c5Keepalived:<br \/>\n[plain] view plaincopy<br \/>\n#tar zxvf keepalived-1.1.15.tar.gz -C ..\/software<br \/>\n#cd ..\/software\/keepalived-1.1.15<br \/>\n#.\/configure \u2013prefix=\/usr\/local\/keepalived &amp;&amp; make &amp;&amp; make install<br \/>\n#cp \/usr\/local\/keepalived\/etc\/rc.d\/init.d\/keepalived \/etc\/rc.d\/init.d\/<br \/>\n#cp \/usr\/local\/keepalived\/etc\/sysconfig\/keepalived \/etc\/sysconfig\/<br \/>\n#mkdir \/etc\/keepalived<br \/>\n#cp \/usr\/local\/keepalived\/etc\/keepalived\/keepalived.conf \/etc\/keepalived\/<br \/>\n#cp \/usr\/local\/keepalived\/sbin\/keepalived \/usr\/sbin\/<br \/>\n#service keepalived start|stop #\u505a\u6210\u7cfb\u7edf\u542f\u52a8\u670d\u52a1\u65b9\u4fbf\u7ba1\u7406.<br \/>\n\u4fee\u6539MASTER \u7684keepalived.conf<br \/>\n[plain] view plaincopy<br \/>\n#vim \/etc\/keepalived\/keepalived.conf<br \/>\nvrrp_script chk_http_port {<br \/>\nscript \"\/usr\/local\/scripts\/nginx_pid.sh\" ###\u76d1\u63a7\u811a\u672c<br \/>\ninterval 2 ###\u76d1\u63a7\u65f6\u95f4<br \/>\nweight 2<br \/>\n}<br \/>\nvrrp_instance VI_1 {<br \/>\nstate MASTER ### \u8bbe\u7f6e\u4e3a\u4e3b<br \/>\ninterface eth0 ### \u76d1\u63a7\u7f51\u5361<br \/>\nvirtual_router_id 51 ### \u8fd9\u4e2a\u4e24\u53f0\u670d\u52a1\u5668\u5fc5\u987b\u4e00\u6837<br \/>\npriority 101 ### \u6743\u91cd\u503cMASTRE\u4e00\u5b9a\u8981\u9ad8\u4e8eBAUCKUP<br \/>\nauthentication {<br \/>\nauth_type PASS ### \u52a0\u5bc6<br \/>\nauth_pass test ### \u52a0\u5bc6\u7684\u5bc6\u7801\uff0c\u4e24\u53f0\u670d\u52a1\u5668\u4e00\u5b9a\u8981\u4e00\u6837\uff0c\u4e0d\u7136\u4f1a\u51fa\u9519<br \/>\n}<br \/>\ntrack_script {<br \/>\nchk_http_port ### \u6267\u884c\u76d1\u63a7\u7684\u670d\u52a1<br \/>\n}<br \/>\nvirtual_ipaddress {<br \/>\n192.168.128.177 ### VIP \u5730\u5740<br \/>\n}<br \/>\n}<\/p>\n<p>\u4fee\u6539BACKUP keepalived.conf<br \/>\n[plain] view plaincopy<br \/>\n#vim \/etc\/keepalived\/keepalived.conf<br \/>\nvrrp_script chk_http_port {<br \/>\nscript \"\/opt\/nginx_pid.sh\"<br \/>\ninterval 2<br \/>\nweight 2<br \/>\n}<br \/>\nvrrp_instance VI_1 {<br \/>\nstate BACKUP ### \u8bbe\u7f6e\u4e3a\u5907\u4efd\u673a<br \/>\ninterface eth0<br \/>\nvirtual_router_id 51 ### \u4e0e MASTRE \u8bbe\u7f6e\u503c\u4e00\u6837<br \/>\npriority 80 ### \u6bd4 MASTRE\u6743\u91cd\u503c\u4f4e<\/p>\n<p>authentication {<br \/>\nauth_type PASS<br \/>\nauth_pass test ### \u5bc6\u7801 \u4e0e MASTRE \u4e00\u6837<br \/>\n}<br \/>\ntrack_script {<br \/>\nchk_http_port<br \/>\n}<br \/>\nvirtual_ipaddress {<br \/>\n192.168.128.177<br \/>\n}<br \/>\n}<br \/>\n\u7f16\u5199\u76d1\u63a7nginx\u76d1\u63a7\u811a\u672c<br \/>\n[plain] view plaincopy<br \/>\n#vim \/usr\/local\/scripts\/nginx_pid.sh<br \/>\n#!\/bin\/bash<br \/>\nA=`ps -C nginx --no-header |wc -l` ## \u67e5\u770b\u662f\u5426\u6709 nginx\u8fdb\u7a0b \u628a\u503c\u8d4b\u7ed9\u53d8\u91cfA<br \/>\nif [ $A -eq 0 ];then ## \u5982\u679c\u6ca1\u6709\u8fdb\u7a0b\u503c\u5f97\u4e3a \u96f6<br \/>\n\/usr\/local\/nginx\/sbin\/nginx<br \/>\nsleep 3<br \/>\nif [ `ps -C nginx --no-header |wc -l` -eq 0 ];then<br \/>\nkillall keepalived ## \u5219\u7ed3\u675f keepalived \u8fdb\u7a0b<br \/>\nfi<br \/>\nfi<\/p>\n<p>\u6d4b\u8bd5,\u5206\u522b\u5728\u4e24\u4e2a\u670d\u52a1\u5668 \u542f\u52a8 nginx \u548c keepalived<br \/>\n[plain] view plaincopy<br \/>\n#\/usr\/local\/nginx\/sbin\/nginx<br \/>\n#\/etc\/init.d\/keepalived start<br \/>\n\u76d1\u63a7 Nginx Mastaer \u7684\u65e5\u5fd7<br \/>\n[plain] view plaincopy<br \/>\nMay 12 17:33:44 localhost Keepalived_vrrp: Configuration is using : 35676 Bytes<br \/>\nMay 12 17:33:44 localhost Keepalived: Starting VRRP child process, pid=1245<br \/>\nMay 12 17:33:44 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(8,9)]<br \/>\nMay 12 17:33:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE<br \/>\nMay 12 17:33:46 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE<br \/>\nMay 12 17:33:46 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.<br \/>\nMay 12 17:33:46 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.128.177<br \/>\nMay 12 17:33:46 localhost avahi-daemon[2344]: Registering new address record for 192.168.128.177 on eth0.<br \/>\nMay 12 17:33:46 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.128.177 added<br \/>\n\u76d1\u63a7 Nginx Backup \u7684\u65e5\u5fd7<br \/>\n[plain] view plaincopy<br \/>\nMay 11 22:28:21 localhost Keepalived: Starting Keepalived v1.1.15 (05\/11,2010)<br \/>\nMay 11 22:28:21 localhost Keepalived_vrrp: Using MII-BMSR NIC polling thread...<br \/>\nMay 11 22:28:21 localhost Keepalived_vrrp: Registering Kernel netlink reflector<br \/>\nMay 11 22:28:21 localhost Keepalived_vrrp: Registering Kernel netlink command channel<br \/>\nMay 11 22:28:21 localhost Keepalived_vrrp: Registering gratutious ARP shared channel<br \/>\nMay 11 22:28:21 localhost Keepalived: Starting VRRP child process, pid=27040<br \/>\nMay 11 22:28:21 localhost Keepalived_vrrp: Opening file '\/etc\/keepalived\/keepalived.conf'.<br \/>\nMay 11 22:28:21 localhost Keepalived_vrrp: Configuration is using : 35538 Bytes<br \/>\nMay 11 22:28:21 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE<br \/>\nMay 11 22:28:21 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]<br \/>\nMay 11 22:28:23 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded<br \/>\n\u770b\u65e5\u5fd7\u53ef\u4ee5\u770b\u51fa\uff0c\u4e24\u53f0\u670d\u52a1\u5668\u7684 MASTRE \u548c BACUKUP \u5df2\u7ecf\u90fd\u6b63\u5e38\u4e86<br \/>\n\u73b0\u5728\u6211\u4eec\u628aMaster\u7684Nginx\u505c\u6389.\u67e5\u770bBackup\u7684\u65e5\u5fd7<br \/>\n[plain] view plaincopy<br \/>\nMay 11 22:28:21 localhost Keepalived: Starting VRRP child process, pid=27040<br \/>\nMay 11 22:28:21 localhost Keepalived_vrrp: Opening file '\/etc\/keepalived\/keepalived.conf'.<br \/>\nMay 11 22:28:21 localhost Keepalived_vrrp: Configuration is using : 35538 Bytes<br \/>\nMay 11 22:28:21 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE<br \/>\nMay 11 22:28:21 localhost Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(7,8)]<br \/>\nMay 11 22:28:23 localhost Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded<br \/>\nMay 11 22:29:25 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE<br \/>\nMay 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE<br \/>\nMay 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.<br \/>\nMay 11 22:29:26 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.128.177<br \/>\n\u53ef\u4ee5\u770b\u51fabackup\u5df2\u7ecf\u53d8\u6210master<br \/>\n\u73b0\u5728\u518d\u542f\u52a8\u539f\u6765master\u7684nginx,\u518d\u67e5\u770bbackup\u7684\u65e5\u5fd7<\/p>\n<p><em id=\"__mceDel\"> [plain] view plaincopy<br \/>\nMay 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert<br \/>\nMay 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE<br \/>\nMay 11 22:30:32 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.<br \/>\nMay 11 22:30:32 localhost avahi-daemon[2409]: Withdrawing address record for 192.168.128.177 on eth0.<br \/>\n\u53ef\u4ee5\u770b\u51fa\u53c8\u53d8\u56de\u4e86backup.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>[plain] view plaincopy Nginx Master: 192.168.128.134 Ng\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.shuran.cn\/?p=303\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-303","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/303","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=303"}],"version-history":[{"count":2,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/303\/revisions"}],"predecessor-version":[{"id":306,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=\/wp\/v2\/posts\/303\/revisions\/306"}],"wp:attachment":[{"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuran.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}