nginx+keepalived high availability for load balancing

Source: Internet
Author: User

nginx+keepalived high availability for load balancing



First, the environment

5 virtual machines, respectively:

1 sets of test machines (192.168.2.83);

2 sets of nginx/keepalived (192.168.2.235/192.168.2.236);

2 Web Servers (192.168.2.237/192.168.2.238);

Note: The VIP setting is 192.168.2.229;


Second, install the configuration Web Server

Because the installation and configuration of Web server is very simple, according to their own preferences, install one, such as: Apache, Nginx, Tomcat and so on. No further details are to be described here;


Three, installation configuration Nginx

Yum-y install gcc vim lrzsz pcre-devel kernel-devel openssl-devel wget

wget http://nginx.org/download/nginx-1.9.0.tar.gz

Tar xzvf nginx-1.9.0.tar.gz

CD nginx-1.9.0

./configure--prefix=/usr/local/nginx--with-debug--with-http_stub_status_module--with-stream--with-http_ssl_ Module

Make;make Install

Ln-s/usr/local/nginx/sbin/nginx/sbin/

Cd/usr/local/nginx/conf

CP nginx.conf Nginx.conf.bak

Edit the Nginx Master Profile nginx.conf, which is roughly as follows:

Worker_processes 10;
Events {
Worker_connections 1024;
}
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";
Sendfile on;
Keepalive_timeout 65;
Upstream Html_pool {
Server 192.168.2.237:80;
Server 192.168.2.238:80;
}
server {
Listen 80;
server_name localhost;
Location/{
Proxy_pass Http://html_pool;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
Error_page 502 503 504/50x.html;
Location =/50x.html {
root HTML;
}
}
}


Note: The above operation, both need to operate, to this, our Nginx installed and configured.


Iv. Installation Configuration keepalived

wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
Tar xzvf keepalived-1.2.15.tar.gz
CD keepalived-1.2.15
./configure--sysconf=/etc/--with-kernel-dir=/usr/src/kernels/2.6.32-504.23.4.el6.x86_64/

#内核参数, depending on the operating system, will be different, notice changes to the kernel path on your server;

Make
Make install
Ln-s/usr/local/sbin/keepalived/sbin/
cd/etc/keepalived/
CP keepalived.conf Keepalived.conf.bak

Edit the keepalive.conf master configuration file, as follows:

! Configuration File for Keepalived

Global_defs {
router_id Lvs_devel
}

Vrrp_script Chk_nginx {
Script "/etc/keepalived/check_nginx.sh"
Interval 2
Weight 2
}

Vrrp_instance Vi_1 {
State Master/backup #前主后备
Interface eth0
VIRTUAL_ROUTER_ID 60
Priority 100/80 #前主后备
Advert_int 3
Authentication {
Auth_type PASS
Auth_pass 33333
}
virtual_ipaddress {
192.168.2.229
}
Track_script {
Chk_nginx
}
}


Because the main configuration file is useful to a script to detect nginx state, so another need to create a script, the content is as follows:

# Cat Check_ngix.sh

#!/bin/bash
Nginx_start= ' ps-c nginx--no-header |wc-l '
if [$nginx _start = 0]; Then
#/etc/init.d/nginx start
/usr/local/nginx/sbin/nginx
Sleep 3
Nginx_status= ' ps-c nginx--no-header |wc-l '
if [$nginx _status = 0]; Then
/etc/init.d/keepalived stop
Fi
Fi


chmod +x/etc/keepalived/check_nginx.sh

Note: Similarly, all of the above operations, need two servers on the same operation, pay attention to change the main standby and priority, to this, the relevant installation and configuration of keepalive is OK;


Five, calibration and testing

    1. Start Keepalived (two units):/etc/init.d/keepalived start

    2. At the main server, the Nginx or keepalived service is closed to observe the availability of the service;

    3. If floating to the standby server, the same, on the standby server at random to the Nginx or keepalived services shut down, and then observe;

    4. Finally, it should be noted that if the server has security software enabled, such as Ipatalbes, it is necessary to let the server access to each other, not only ping pass, such as: Iptables-i input-s 192.168.2.0/24-j ACCEPT



This article is from the "Square Hill" blog, please be sure to keep this source http://63638790.blog.51cto.com/513514/1664137

nginx+keepalived high availability for load balancing

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.