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

Source: Internet
Author: User

Nginx + Keepalived load balancing high availability (dual-machine Hot Backup), nginxkeepalived
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 $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" "$ http_x_forwarded_for "';
Access_log/usr/local/nginx/logs/access. log main;
Sendfile on;
Keepalive_timeout 65;
Client_header_buffer_size 32 k;
Large_client_header_buffers 432 k;
Client_max_body_size 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 during testing
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;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
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.io
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
Router_idLVS_DEVEL
}
Vrrp_instanceVI_1 {
State BACKUP
Interface eth0
Virtual_router_id 100
Priority200
Advert_int 1
Nopreempt
Authentication {
Auth_type PASS
Auth_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.io
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
Router_id LVS_DEVEL
}
Vrrp_instanceVI_1 {
State BACKUP
Interface eth0
Virtual_router_id 100
Priority200
Advert_int 1
Nopreempt
Authentication {
Auth_type PASS
Auth_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/bash
While:
Do
Nginxcheck = 'ps-cngcheck -- no-header | wc-l'
Keepalivedcheck = 'ps-Ckeepalived -- no-header | wc-l'
If [$ nginxcheck-eq0]; then
If [$ keepalivedcheck-ne0]; then
/Etc/init. d/keepalivedstop
Else
Echo "keepalivedisstoped"
Fi
Elif [$ keepalivedcheck-eq0]; then
/Etc/init. d/keepalivedstart
Fi
Sleep 5
Done

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 "[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 "[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.