Nginx+keepalived Load Balancing high availability (dual standby)

Source: Internet
Author: User

Nginx+keepalived Load Balancing high availability (dual standby)
1.1 Nginx Installation and configuration
1.2 Keepalived Installation and configuration
1.3 Webserver Installation
1.4 Test Nginx+keepalived

The environment is as follows:
CentOS 6.4_64k
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. On 192.168.10.11, 192.168.10.12, respectively, install Nginx (can refer to the Nginx installation documentation)
B. Modify the Nginx configuration file on the 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 32k;
Large_client_header_buffers 432k;
Client_max_body_size 10m;
gzip on;
Gzip_min_length 1k;
Gzip_buffers 416k;
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 first.
server192.168.10.13:80;
server192.168.10.14:80;
}
server{
Listen;
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 Keepalived Installation and configuration
A. On 192.168.10.11, 192.168.10.12, install the keepalived separately (refer to the Keepalived installation documentation).
B. Modify the/etc/keepalived/keepalived.conf file on the 192.168.10.11 as follows:
global_defs{
notification_email{
[email protected]
}
[Email protected]
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
Router_idlvs_devel
}
vrrp_instancevi_1{
State BACKUP
Interface eth0
VIRTUAL_ROUTER_ID 100
Priority 200
Advert_int 1
Nopreempt
authentication{
auth_type PASS
auth_pass 123456
}
virtual_ipaddress{
192.168.10.50
}
}
C. Modify the/etc/keepalived/keepalived.conf file on the 192.168.10.12 as follows:
global_defs{
notification_email{
[email protected]
}
[Email protected]
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
Priority 200
Advert_int 1
Nopreempt
authentication{
auth_type PASS
auth_pass 123456
}
virtual_ipaddress{
192.168.10.50
}
}
D. On 192.168.10.11, 192.168.10.12, write the shell script, monitor nginx is normal, if nginx
Hang it off, then stop keepalived.
[Email protected]~] #vim/root/shell/nginx_check.sh
#!/bin/bash
While:
Do
nginxcheck= ' Ps-cnginx--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. Setting up power-on automatic execution
[Email protected]~]# echo "nohupsh/root/shell/nginx_check.sh&" >>/etc/rc.local

1.3 Webserver Installation
For demonstration purposes, only Apache is installed on webserver (you can also install Tomcat, resin, etc.) to provide web
Service.
A. Install Apache on the 192.168.10.13 and set the home page content to its own IP address
[Email protected]~]# yum Install httpd
[Email protected]~]# echo [[Email protected]~]# service httpd start
B. Install Apache on the 192.168.10.14 and set the home page content to its own IP address
[Email protected]~]# yum Install httpd
[Email protected]~]# echo [[Email protected]~]# service httpd start


1.4 Test Nginx+keepalived
After completing the above steps, the cluster environment is set up, and the following tests the cluster environment is OK.
A. On 192.168.10.11 and 192.168.10.12, start the nginx separately and turn on the monitoring
[[Email protected]~]#/usr/local/nginx/sbin/nginx
[[Email protected]~]# nohupsh/root/shell/nginx_check.sh&
B. The test method is as follows:
1) Open the browser, enter 192.168.10.50, whether reincarnation display "192.163.10.13" or "192.163.10.14"
2) Close the Nginx,web service on 192.168.10.11 is still normal
3) Close the Nginx on 192.168.10.12 to start the Nginx,web service on the 192.168.10.11 is still normal
4) Close the Apahce,web service on 192.168.10.13 is still normal

Nginx+keepalived Load Balancing high availability (dual standby)

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.