Topic: Distributed architecture high-availability architecture _keepalived+nginx high-availability web load Balancing

Source: Internet
Author: User
Tags iptables

First, the scene needs

Ii. Brief introduction of Keepalived
Keepalived is a high-performance server high availability or hot standby solution, keepalived can be used to prevent the occurrence of single point of failure of the server, by matching Nginx can achieve the high availability of Web front-end services.
Keepalived is based on the VRRP protocol and uses the VRRP protocol to achieve high availability (HA). VRRP (Virtualrouter
Redundancy Protocol) protocol is used to implement routers redundancy protocol, VRRP protocol will two or more router devices virtual into a device, the external provision of virtual router IP (one or more), and within the router group, if the actual external IP If the router is working correctly is master, or through the algorithm election, master implementation of the virtual router IP network functions,
such as ARP request, ICMP, and data forwarding, etc. other devices do not own the virtual IP, the state is BACKUP, in addition to receiving MASTER's VRRP status notification information, do not perform external network functions. When the host fails, BACKUP takes over the network capabilities of the original master. The VRRP protocol uses multicast data to transmit VRRP data, VRRP data uses a special virtual source MAC address to send data instead of its own network card's MAC address, VRRP runtime only the master router sends VRRP notification information at regular intervals, indicating that master is working properly and that the virtual road By the IP (group), backup only receives VRRP data, does not send data, if a certain period of time did not receive Master's notification information, each backup will declare itself to become master, send notification information, restart master election status.

III. Programme Planning

Iv. installation of Nginx

1, the installation of Nginx required to build the dependent package

Connecting to 192.168.1.121:22 ...
Connection established.
To an escape to the local shell, press ' ctrl+alt+] '.


Last Login:sat APR 17:55:20 2016 from 192.168.1.61
[root@edu-proxy--01 ~]# yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel OpenSSL ope Nssl-devel

2, Upload Nginx (nginx-1.6.2.tar.gz) to/usr/local/src directory

3. Compile and install Nginx

[Root@edu-proxy--01 ~]# cd/usr/local/src/
[Root@edu-proxy--01 src]# ls
Keepalived-1.2.18.tar.gz nginx-1.6.2.tar.gz
[Root@edu-proxy--01 src]# TAR-ZXVF nginx-1.6.2.tar.gz

[Root@edu-proxy--01 src]# CD nginx-1.6.2
[root@edu-proxy--01 nginx-1.6.2]#./configure--prefix=/usr/local/nginx

[root@edu-proxy--01 nginx-1.6.2]# make && make install

[Root@edu-proxy--01 nginx-1.6.2]# vi/usr/local/nginx/conf/nginx.conf

User root;
Worker_processes 1;


#error_log Logs/error.log;
#error_log Logs/error.log Notice;
#error_log Logs/error.log Info;
#pid Logs/nginx.pid;


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 ';


#access_log Logs/access.log Main;


Sendfile on;
#tcp_nopush on;


#keepalive_timeout 0;
Keepalive_timeout 65;


#gzip on;


server {
Listen 88;
server_name localhost;
#charset Koi8-r;
#access_log Logs/host.access.log Main;
Location/{
root HTML;
Index index.html index.htm;
}
#error_page 404/404.html;
# REDIRECT Server error pages to the static page/50x.html
Error_page 502 503 504/50x.html;
Location =/50x.html {
root HTML;
}
}
}


Modify Nginx Welcome Home content (for later testing, to differentiate two nodes of Nginx):

Vi/usr/local/nginx/html/index.html

192.168.1.121 in the title plus 1

Welcome to nginx! 1

192.168.1.122 in the title plus 2

Welcome to nginx! 2

5, the system firewall to open the corresponding port 88

Vi/etc/sysconfig/iptables

-A input-m state--state new-m tcp-p TCP--dport 88-j ACCEPT

Service Iptables Restart

6. Test Nginx Whether the installation is successful

/usr/local/nginx/sbin/nginx-t

Nginx:the configuration file/usr/local/nginx/conf/nginx.conf syntax is OK
Nginx:configuration file/usr/local/nginx/conf/nginx.conf Test is successful

7, Start Nginx

/usr/local/nginx/sbin/nginx

Reboot

/usr/local/nginx/sbin/nginx-s Reload

8, set up Nginx boot

Vi/etc/rc.local

Join

/usr/local/nginx/sbin/nginx

Access two Nginx respectively

Nginx1

2.nginx2

V. Installation of keepalived (http://www.keepalived.org/download.html)

1, upload or download keepalived (keepalived-1.2.18.tar.gz) to/usr/local/src directory

2, decompression installation

Cd/usr/local/src

TAR-ZXVF keepalived-1.2.18.tar.gz

CD keepalived-1.2.18

./configure--prefix=/usr/local/keepalived

Make && make install

3, install the keepalived into Linux system services:

Because it is not installed using the default path of keepalived (default is/usr/local), some work needs to be done after the installation is complete

Copy default profile to default path

Mkdir/etc/keepalived

cp/usr/local/keepalived/etc/keepalived/keepalived.conf/etc/keepalived/

Copy the Keepalived service script to the default address

cp/usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/

cp/usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/

Ln-s/usr/local/sbin/keepalived/usr/sbin/

Ln-s/usr/local/keepalived/sbin/keepalived/sbin/

Set up the keepalived service to boot

Chkconfig keepalived on

4, modify the keepalived configuration file

Note that if Linux uses the NIC is eth0,interface to write eth1, with the eth1 will be written eth1

(1) MASTER node configuration file (192.168.1.121)

Vi/etc/keepalived/keepalived.conf


! Configuration File for Keepalived


Global_defs {
router_id edu-proxy-01
}


Vrrp_script Chk_nginx {
Script "/etc/keepalived/nginx_check.sh"
Interval 2
Weight-20
}


Vrrp_instance Vi_1 {
State MASTER
Interface eth0
VIRTUAL_ROUTER_ID 51
Mcast_src_ip 192.168.1.121
Priority 100
Nopreempt
Advert_int 1
Authentication {
Auth_type Pass
Auth_pass 1111
}


Track_script {
Chk_nginx
}


virtual_ipaddress {
192.168.1.120
}

Backup node configuration (192.168.1.122)

! Configuration File for keepalived


global_defs {
   router_id edu-proxy-02
}


Vrrp_script Chk_nginx {
    script "/etc/keepalived/nginx_check.sh"
    interval 2
    WEIGHT-20
}


vrrp_instance vi_1 {
    State BACKUP
    interface Eth0
    virtual_router_id Wuyi
    mcast_src_ip 192.168.1.122
    Priority
    Advert_int 1
    Authentication {
        Auth_type pass
&N Bsp       Auth_pass 1111
   }


    Track_script {
        Chk_nginx
   }


    virtual_ipaddress {
        19 2.168.1.120
   }
}

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.