April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Keepalived Nginx Dual Primary High Availability Load Balancing Cluster

Keepalived Nginx Dual Primary High Availability Load Balancing Cluster

Purpose of the experiment: Use keepalived to implement Nginx’s dual main high availability load balancing cluster.

Experimental environment: two Nginx proxy (two main Nginx, each need two NIC, eth0 connection network, eth1 connected to the external network), two web server (request load balancing), a client used to verify the results.

Note: In order not to affect the experimental results, before the start of the experiment to close iptables and selinux

Steps:

First, configure IP

1. Configure the IP of the A host

# Ip addr add dev eth0 192.168.1.2/24

2. Configure the IP of the B host

# Ip addr add dev eth0 192.168.1.23/24

3. Configure the IP of the C host

# Ip addr add dev eth0 192.168.1.3/24

4. Configure the IP of the D host

# Ip addr add dev eth0 192.168.1.33/24

Second, the configuration of web services (C and D hosts have done the same configuration, just modify the default home page IP address for the machine can be IP to distinguish)

1. install apache

# Yum -y install apache

2. Create a default home page

# Vim /var/www/html/index.html

192.168.1.3

3. Start apache

# Service httpd start

Third, the configuration sorry_server (This service is configured on the Nginx proxy host, two Nginx proxy do the same configuration, just modify the default home page IP address for the machine can be IP to distinguish)

1. install apache

# Yum -y install apache

2. Create a default home page

# Vim /var/www/html/index.html

sorry_server: 192.168.1.2

3. Modify the listening port to 8080 to avoid conflicts with the ports that nginx listens on

# Vim /etc/httpd/conf/httpd.conf

Listen 8080

4. Start apache service

Fourth, the configuration agent (two Nginx proxy do the same configuration)

1. Install nginx

# Yum -y install nginx

2. Define the upstream cluster group, defined in the http {} section;

# Vim /etc/nginx/nginx.conf

Http {

Upstream websrvs {

Server 192.168.1.3:80;

Server 192.168.1.33:80;

Server 127.0.0.1:8080 backup;

}

}

3. Call the defined cluster group and call it in the location {} section of the server {} section;

# Vim /etc/nginx/conf.d/default.conf

Server {

Location / {

Proxy_pass http: // wersrvs ;

Index index.html;

}

}

4. Start the service

# Service nginx start

Fifth, configure keepalived

A on the host operation

1. Install keepalived

# Yum -y install keepalived

2. Edit the A host’s configuration file /etc/keepalived/keepalived.conf, as follows:

! Configuration File for keepalived

Global_defs {

Notification_email {

Root @ localhost

}

Notification_email_from keepalived @ localhost

Smtp_server 127.0.0.1

Smtp_connect_timeout 30

Router_id CentOS 6

Vrrp_mcast_group4 224.0.100.39

}

Vrrp_script chk_down {

Script “[[-f / etc / keepalived / down]] && exit 1 || exit 0”

Interval 1

Weight -5

}

Vrrp_script chk_nginx {

Script “killall -0 nginx && exit 0 || exit 1”

Interval 1

Weight -5

Fall 2

Rise

}

Vrrp_instance ngx {

State MASTER

Interface eth1

Virtual_router_id 14

Priority 100

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass MDQ41fTp

}

Virtual_ipaddress {

192.168.20.100/24 ??dev eth1

}

Track_script {

Chk_down

Chk_nginx

}

}

Vrrp_instance ngx2 {

State BACKUP

Interface eth1

Virtual_router_id 15

Priority 98

Advert_int 1

Authentication {

Auth_type PASS

Auth_pass XYZ41fTp

}

Virtual_ipaddress {

192.168.20.200/24 ??dev eth1

}

Track_script {

Chk_down

Chk_nginx

}

}

B host also for the same configuration, a little change can be, need to modify the following places:

Vrrp_instance ngx {

State BACKUP

Priority 98

}

Vrrp_instance ngx2 {

State MASTER

Priority 100

}

6, simulation failure, verify the results

1. Start the two Nginx proxy keepalived services

# Service keepalived start

2. Visit 192.168.20.100, the result should be the back-end web server polling response request

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>