Summary of complete configuration of nginx Load Balancing + keepalived High Availability

Source: Internet
Author: User
Tags nginx load balancing

Load Balancing with nginx (no high availability)

General steps.

1. Front-end

Nginx installation and PCRE installation. The detailed steps are not explained.

2. load configuration

A. Default round robin

Add a line to nginx. conf

Include upstream. conf, and then all the Server Load balancer configurations are directly configured in upstream. conf.

[[Email protected] conf] # Cat upstream. conf

Upstream httpservers {

Server 192.168.137.10: 80 Weight = 5;

Server 192.168.137.20: 80 Weight = 5;

}



Server {

Listen 80;

SERVER_NAME 192.168.137.100;

Location /{

Proxy_pass http: // httpservers;

}


}



A. ip_hash each request is allocated according to the hash result of the access IP address. In this way, each visitor accesses a backend server in a fixed manner and can solve the session problem.



[[Email protected] conf] # Cat upstream. conf

Upstream httpservers {

Ip_hash;

Server 192.168.137.10: 80 Weight = 5;

Server 192.168.137.20: 80 Weight = 5;

}

Server {

Listen 80;

SERVER_NAME 192.168.137.100;

Location /{

Proxy_pass http: // httpservers;

}

}

Nginx performs Load Balancing (keepalived high availability)

Topology

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/43/F0/wKioL1Pe6TPwIllVAAFxc-5xmvQ713.jpg "Title =" 70fc6d50-eaaf-47fb-8615-8236cd40dbdb.png "alt =" wKioL1Pe6TPwIllVAAFxc-5xmvQ713.jpg "/>

650) This. width = 650; "src ="/e/u261/themes/default/images/spacer.gif "style =" Background: URL ("/e/u261/lang/ZH-CN/images/localimage.png") No-repeat center; Border: 1px solid # DDD; "alt =" spacer.gif "/>


1. nginx configuration (configured on the Master/Slave server)


Upstream httpservers {

Ip_hash;

Server 192.168.137.10: 80 Weight = 5;

Server 192.168.137.20: 80 Weight = 5;

}

Server {

Listen 80;

SERVER_NAME 192.168.137.201;

Location /{

Proxy_pass http: // httpservers;

}

}

2. install and configure keepalived on the Master/Slave server)

Download: wget http://www.keepalived.org/software/keepalived-1.2.8.tar.gz

Unzip: Tar zxvf keepalived-1.2.8.tar.gz

Compile and install:./configure -- prefix =/usr/local/keepalived

Make

Make install




CP/usr/local/keepalived/etc/sysconfig/

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

CP/usr/local/keepalived/sbin/


Mkdir-P/etc/keepalived/

CP/usr/local/keepalived/etc/keepalived. CONF/etc/keepalived)


Modify/etc/keepalived. conf. The following lists the configurations of master and backup respectively.


++ Master ++ ++ +

! Configuration file for keepalived

Global_defs {

Router_id nginx-proxy-ha

}

Vrrp_script chk_http_port {

Script "/usr/bin/check_nginx.sh"

Interval 2

Weight 2

}

Vrrp_instance vi_1 {

State master

Interface eth0

Virtual_router_id 51

Priority200

Advert_int 1

Authentication {

Auth_type pass

Auth_pass 1234

}

Track_interface {

Eth0

}

Track_script {

Chk_http_port

}

Virtual_ipaddress {

192.168.137.201

}

}

+++ ++ Backup ++ ++

! Configuration file for keepalived

Global_defs {

Router_id nginx-proxy-ha

}

Vrrp_script chk_http_port {

Script "/usr/bin/check_nginx.sh"

Interval 2

Weight 2

}

Vrrp_instance vi_1 {

State backup

Interface eth0

Virtual_router_id 51

Priority180

Advert_int 1

Authentication {

Auth_type pass

Auth_pass 1234

}

Track_interface {

Eth0

}

Track_script {

Chk_http_port

}

Virtual_ipaddress {

192.168.137.201

}

}

After the preceding configuration is complete, start nginx and keepalived. You must first start nginx and then start keepalived.

Test Result: After keepalived and nginx are enabled for both master and backup, the default virtual IP address is on the master node. After nginx is killed on the master node, the floating IP address is bound to the backup node.

Configurations:

Global_defs {

 

Notification_email {

[Email protected] (multiple alarm mailboxes can be defined here)

 

}

Notification_email_from [email protected] (Alert contact)

Smtp_server 127.0.0.1

Smtp_connect_timeout 30

Router_id lvs_devel

}

 

Vrrp_script chk_http_port {

Script "/opt/tools/bin/check_ng.sh"

Interval 2 (interval between script execution detection)

Weight 2

}

 

Vrrp_instance vi_1 {

State backup (display defined as slave server)

Interface eth1 (bound network port, which is the interface of the two IP addresses mentioned above)

Virtual_router_id 51 (defined ID, which is officially 51 and must be the Master/Slave server)

Mcast_src_ip 211.151.138.3 (slave server IP)

Priority 50 (priority, defined at will, but must be lower than the master server)

Advert_int 1

Authentication {

Auth_type pass

Auth_pass 1111 (default)

}

 

Track_script {

Chk_http_port (call the detection script)

}

 

Virtual_ipaddress {

211.151.137.5 (bound virtual IP address)

}

}

++ Check_nginx.sh ++ ++

This script is used to check the nginx status. Once nginx exits, the master keepalived is disabled. At this time, the backup automatically switches to the master node and binds the virtual IP address. Put the script in/usr/bin.

#! /Bin/bash

If ["$ (PS-Ef | grep" nginx: Master process "| grep-V grep)" = ""]

Then

Killall-9 keepalived

Fi


This article from the "flying dream" blog, please be sure to keep this source http://flyingdreams.blog.51cto.com/802109/1535155

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.