Nginx + Keepalived load balancing high availability (dual-machine Hot Backup)

Source: Internet
Author: User

Nginx + Keepalived load balancing high availability (dual-machine Hot Backup)
Nginx + Keepalived load balancing high availability (dual-machine Hot Backup)
1.1 Nginx installation and configuration
1.2 install and configure Keepalived
1.3 install WebServer
1.4 test Nginx + Keepalived

The environment is as follows:
CentOS 6.4 _ 64 K
Eepalived-1.2.12
Nginx-1.4.4
Vip: 192.168.10.50
Master: 192.168.10.11
Backup: 192.168.10.12
Webserver1: 192.168.10.13
Webserver2: 192.168.10.14

1.1 Nginx installation and configuration
A. Install Nginx on 192.168.10.11 and 192.168.10.12 respectively (refer to the Nginx installation Documentation)
B. Modify the Nginx configuration files on 192.168.10.11 and 192.168.10.12 as follows:

User wwwwww; worker_processes 8; pid/usr/local/nginx/logs/nginx. pid; worker_rlimit_nofile 51200; events {use epoll; worker_connections 51200;} http {include mime. types; default_type application/octet-stream; log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request" ''$ status $ response" $ http_referer "'' "$ http_user_agent" "$ http_x_forwarded_for "'; access_log/usr/local/nginx/logs/access. log main; sendfile on; keepalive_timeout 65; bytes 32 k; bytes 432 k; bytes 10 m; gzip on; gzip_min_length 1 k; gzip_buffers 416 k; gzip_http_version 1.1; gzip_comp_level 5; gzip_disable "MSIE [1-6] \. "; gzip_vary on; gzip_types text/plainapplication/x-javascripttext/cssapplication/xml; upstreambackend {# ip_hash; # comment out server192.168.10.13: 80; server192.168.10.14: 80 ;} server {listen 80; server_name localhost; location/{proxy_redirect off; proxy_set_header Host $ host; using X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ scheme; proxy_pass http: // backend ;}}}


1.2 install and configure Keepalived
A. Install Keepalived on 192.168.10.11 and 192.168.10.12 respectively (refer to the Keepalived installation documentation ).
B. Modify the/etc/keepalived. conf file on 192.168.10.11 as follows:
global_defs{notification_email{1244918797@qq.com}notification_email_fromtest@baba.iosmtp_server 127.0.0.1smtp_connect_timeout 30router_idLVS_DEVEL}vrrp_instanceVI_1{state BACKUPinterface eth0virtual_router_id 100priority 200advert_int 1nopreemptauthentication{auth_type PASSauth_pass 123456}virtual_ipaddress{192.168.10.50}}

C. Modify the/etc/keepalived. conf file on 192.168.10.12, as shown below:
global_defs{notification_email{1244918797@qq.com}notification_email_fromtest@baba.iosmtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL}vrrp_instanceVI_1{state BACKUPinterface eth0virtual_router_id 100priority 200advert_int 1nopreemptauthentication{auth_type PASSauth_pass 123456}virtual_ipaddress{192.168.10.50}}

D. Write a SHELL script on 192.168.10.11 and 192.168.10.12 to check whether Nginx works normally.
Stop Keepalived.
[root@localhost~]#vim/root/shell/nginx_check.sh#!/bin/bashwhile :donginxcheck=`ps-Cnginx--no-header|wc-l`keepalivedcheck=`ps-Ckeepalived--no-header|wc-l`if[$nginxcheck-eq0];thenif[$keepalivedcheck-ne0];then/etc/init.d/keepalivedstopelseecho "keepalivedisstoped"fielif[$keepalivedcheck-eq0];then/etc/init.d/keepalivedstartfisleep 5done


E. Set automatic execution upon startup
[Root @ localhost ~] # Echo "nohupsh/root/shell/nginx_check.sh &">/etc/rc. local

1.3 install WebServer
For ease of demonstration, only Apache (Tomcat and Resin can be installed) is installed on WebServer to provide Web
Service.
A. install Apache on 192.168.10.13 and set the homepage content as your IP address.
[Root @ localhost ~] # Yum install httpd
[Root @ localhost ~] # Echo "192.168.10.13">/var/www/html/index.html
[Root @ localhost ~] # Service httpd start
B. install Apache on 192.168.10.14 and set the homepage content as your IP address.
[Root @ localhost ~] # Yum install httpd
[Root @ localhost ~] # Echo "192.168.10.14">/var/www/html/index.html
[Root @ localhost ~] # Service httpd start


1.4 test Nginx + Keepalived
After completing the preceding steps, the cluster environment has been set up. Next, we will test whether the cluster environment is normal.
A. Start Nginx and enable monitoring on 192.168.10.11 and 192.168.10.12 respectively.
[Root @ localhost ~] #/Usr/local/nginx/sbin/nginx
[Root @ localhost ~] # Nohupsh/root/shell/nginx_check.sh &
B. the test method is as follows:
1) Open the browser, enter 192.168.10.50, and check whether "192.163.10.13" or "192.163.10.14" is displayed again"
2) Disable Nginx on 192.168.10.11 and check whether the Web Service is normal.
3) Disable Nginx on 192.168.10.12 and start Nginx on 192.168.10.11. Is the Web Service normal?
4) Disable Apahce on 192.168.10.13 and check whether the Web Service is normal.

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.