nginx+keepalived script installation Master-slave dual-machine hot standby automatic switching solution

Source: Internet
Author: User
Tags curl nginx server
nginx+keepalived script installation Master-slave dual-machine hot standby automatic switching solution 2013-07-02 19:24:13 Tags: files Nginx keepalived original works, allow reprint, reprint, please be sure to hyperlink form to indicate the original source of the article, Author information and this statement. Otherwise, legal liability will be held. http://kling.blog.51cto.com/3320545/1240359

Nginx+keepalived automatic switching solution for master-slave dual-machine hot standby


The test environment is as follows:

System: Ceentos 6.4 64-bit

Primary nginx server: 192.168.122.5

Standby Nginx Server: 192.168.122.6

vip:192.168.122.15
First, nginx+keepalived installation-Script installation 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 The #!/bin/bash # author:kuangl # mail:kuangl@orient-media.com # description:the installation of N Ginx files. #--------------------------------------------------------# # # # # Nginx_install #------------------------------------ --------------------# # Nginx installation #CURRENT_PATH =$ (PWD) for I in $ (rpm-q gcc gcc-c++ kernel-devel openssl-devel Zlib-devel popt-devel popt-static libnl-devel wget make | grep ' not installed ' | awk ' {print $} ' does yum-y install $i done [-d/root/software] ["$?"!= 0] && mkdir/root/software cd/root/ Software [!-e pcre-8.33. Tar. gz] && wget ftp://ftp. csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33. Tar. GZ tar-zxvf pcre-8.33. Tar. GZ CD pcre-8.33. /configure make && make install echo $ | | [$!= 0] | | echo "Installation Pcrefailed" | | Exit 1 Cd/root/software [!-E nginx-1.2.9. Tar. gz] && wget http://nginx. org/download/nginx-1.2.9. Tar. GZ tar-zxvf nginx-1.2.9. Tar. GZ CD nginx-1.2.9. /configure--prefix =/usr/local/nginx--with-http_ssl_module--with-http_sub_module--with-http_stub_status_ Module--with-http_gzip_static_module make && make install echo $ | | [$!= 0] | | echo "Installationnginxfailed" | | Exit 1 # # # # # # # # # # # keepalived_intsall # #-------------------------- ------------------------------# # keepalived installation cd/root/softwarae [!-e keepalived-1.2.4. Tar. gz] &&amp wget http: www. keepalived.org/software/keepalived-1.2.4. Tar. GZ tar-zxvf keepalived-1.2.4. Tar. GZ CD keepalived-1.2.4 ln-s/usr/src/kernels/$ (uname-r)/usr/src/kernels/linux. /configure--prefix=/usr--bindir =/usr/bin--sbindir =/usr/bin--libexecdir =/usr/libexec--localstatedir=/var--libdi r=/lib64--infodir =/usr/share/info--sysconfdir =/etc--mandir=/usr/locaL/share/man--with-kernel-dir =/usr/src/kernels/linux make && make install echo $ | | [$!= 0] | | Print "Installation keepalivedfailed" | | Exit 1 chkconfig--add keepalived chkconfig--level 345 keepalived on


Second, the main nginx configuration 1 2 3 [ROOT@NODE5 conf] # mkdir-p/var/www/html [ROOT@NODE5 conf] # cat "192.168.122.5" >/var/www/html/in dex.html [ROOT@NODE5 conf] # vim NGINX.CONF

1 2 3 [ROOT@NODE5 conf] # ... /sbin/nginx-s Reload [ROOT@NODE5 conf] # Curl http://192.168.122.5 192.168.122.5


Third, the main keepalived configuration 1 2 3 4 5 6 7 8 + 9, at the same (Root@node6 C), at the same ONF] # vim/etc/keepalived/keepalived.conf! Configuration File for keepalived global_defs {notification_email {404060@qq.com 138162@139.com} notification_email_fr Om 404060@qq.com smtp_server 127.0.0.1 smtp_connect_timeout router_id lvs_devel} vrrp_script chk_nginx {script/etc/ Keepalived/chk_nginx.keepalived.sh "Interval 2 weight 2} vrrp_instance vi_1 {state MASTER interface eth0 ID Wuyi Priority Advert_int 1 authentication {Auth_type pass Auth_pass kuangling} track_script {chk_nginx.keepalived } virtual_ipaddress {192.168.122.15}}


Four, Standby Nginx configuration 1 2 3 [root@node6 conf] # mkdir-p/var/www/html [Root@node6 conf] # cat "192.168.122.6" >/var/www/html/ind ex.html [Root@node6 conf] # vim NGINX.CONF

1 2 3 [root@node6 conf] # ... /sbin/nginx-s Reload [root@node6 conf] # Curl http://192.168.122.6 192.168.122.6


Five, Standby keepalived configuration 1 2 3 4 5 6 7 8-A-I (Root@node6 c) in all of the * * * ONF] # vim/etc/keepalived/keepalived.conf! Configuration File for keepalived global_defs {notification_email {404060@qq.com 138162@139.com} notification_email_fr Om 404060@qq.com smtp_server 127.0.0.1 smtp_connect_timeout router_id lvs_devel} vrrp_script chk_nginx {script/etc/ Keepalived/chk_nginx.keepalived.sh "Interval 2 weight 2} vrrp_instance vi_1 {state BACKUP interface eth0 ID Wuyi Priority Advert_int 1 authentication {Auth_type pass Auth_pass kuangling} track_script {chk_nginx.keepalived } virtual_ipaddress {192.168.122.15}}


Vi. Add detection scripts on 2 nginx servers: 1 2 3 4 5 6 7 8 9 [Root@node6 conf] # vim/etc/keepalived/chk_nginx.keepalived.sh # !/bin/bash # Description: # timed to see if nginx exists, start nginx if it does not exist keepalived status= ' ps-c nginx--no-header | Wc-l ' If [$status-eq 0]; Then/usr/local/nginx/sbin/nginx Sleep 3 If [' Ps-c nginx--no-header | wc-l '-eq 0]; Then Killall keepalived fi fi [root@node6 conf] #chmod +x/etc/keepalived/chk_nginx.keepalived.sh


Seven, test on 2 nginx on the start Nginx and keepalived services, and then use IP A to view IP respectively


This article comes from the "& Dawn" blog, please be sure to keep this source http://kling.blog.51cto.com/3320545/1240359



Note:

1. This article is keepalived single master highly available configuration

2.keepalived does not solve the problem of its own failure, more for the operating system and network failure to carry out VIP migration

3.keepalived does not and nginx linkage, that is, nginx failure, keepalived will not know, will not migrate VIP, the solution to this problem: will monitor the pace of long-term in the background continuous operation (while Infinite loop, sleep 1)

The steps were transformed as follows:

#!/bin/bash
# Description:
# timed to see if nginx exists, start nginx if it does not exist
# If startup fails, stop keepalived
While [A==a]
Todo
Status= ' ps-c nginx--no-header |wc-l '
If [$status-eq 0];then
/usr/local/nginx/sbin/nginx >/dev/null 2>&1
Sleep 3
If [' Ps-c nginx--no-header |wc-l '-eq 0];then
Killall keepalived >/dev/null 2>&1
Fi
Fi
Sleep 1
Done


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.