Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

High availability with keepalived+nginx

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Install keepalived

You can install it directly in yum, which automatically installs dependencies:

Copy

Yum-y install keepalived

Modify the host (192.168.30.21) keepalived configuration file

The configuration files installed in yum mode will be produced under / etc/keepalived:

Copy

Vi keepalived.conf

Keepalived.conf:

Copy

# Test script

Vrrp_script chk_http_port {

Script "/ usr/local/src/check_nginx_pid.sh" # script executed by heartbeat to detect whether nginx is started

Interval 2 # (detect the interval between script execution, in seconds)

Weight 2 # weight

}

# vrrp instance definition section

Vrrp_instance VI_1 {

State MASTER # specifies the role of keepalived, with MASTER as the primary and BACKUP as the backup

Interface eth0 # current network interface card for vrrp communication (current centos network card) use ifconfig to view your specific network card

Virtual_router_id 66 # virtual route number, master and slave must be consistent

Priority 100 # priority. The higher the number, the higher the priority of obtaining and processing requests.

Advert_int 1 # check interval. Default is 1s (number of seconds of vrrp Multicast cycles)

# authorizing access

Authentication {

Auth_type PASS # sets the authentication type and password. MASTER and BACKUP must use the same password to communicate normally.

Auth_pass 1111

}

Track_script {

Chk_http_port # (call detection script)

}

Virtual_ipaddress {

192.168.30.20 # defines a virtual ip (VIP), which can be configured with one per line.

}

}

Vip can be configured in virtual_ipaddress, and the service can be accessed online through vip.

Interface needs to be set according to the server network card, and the usual viewing method ip addr

The same configuration is required for authentication configuration to authorize access to the backup.

Modify the standby (192.168.30.22) keepalived configuration file

Keepalived.conf:

Copy

# Test script

Vrrp_script chk_http_port {

Script "/ usr/local/src/check_nginx_pid.sh" # script executed by heartbeat to detect whether nginx is started

Interval 2 # (detect the interval between script execution)

Weight 2 # weight

}

# vrrp instance definition section

Vrrp_instance VI_1 {

State BACKUP # specifies the role of keepalived, with MASTER as the primary and BACKUP as the backup

Interface eth0 # current network interface card for vrrp communication (current centos network card) use ifconfig to view your specific network card

Virtual_router_id 66 # virtual route number, master and slave all the time

Priority 99 # priority. The higher the value, the higher the priority of obtaining and processing requests.

Advert_int 1 # check interval. Default is 1s (number of seconds of vrrp Multicast cycles)

# authorizing access

Authentication {

Auth_type PASS # sets the authentication type and password. MASTER and BACKUP must use the same password to communicate normally.

Auth_pass 1111

}

Track_script {

Chk_http_port # (call detection script)

}

Virtual_ipaddress {

192.168.30.20 # defines a virtual ip (VIP), which can be configured with one per line.

}

}

Test script:

Copy

#! / bin/bash

# check whether nginx is started

A=ps-C nginx-- no-header | wc-l

If [$A-eq 0]; then # start nginx if nginx is not started

Service nginx start # launch nginx

If [ps-C nginx-- no-header | wc-l-eq 0]; if the restart of then # nginx fails, stop the keepalived service and transfer VIP

Killall keepalived

Fi

Fi

Script authorization: chmod 775 check_nginx_pid.sh

Note: the script must be authorized, otherwise there is no permission to access, ah, here we have two servers to execute, VIP (virtual_ipaddress:192.168.30.20), we access the service directly through vip in the production environment.

Simulate a nginx failure:

Modify the html page of the Nginx accessed by the two servers by default as a difference.

First visit 192.168.30.20 and access it through vip. The page shows 192.168.30.21, indicating that the service is currently provided by the master server.

At this time, the 192.168.30.21 master server executes the command:

Copy

Systemctl stop nginx; # stop nginx

Visit vip again (192.168.30.20) and find that the page still shows 192.168.30.21 at this time, which is automatically restarted in the script.

Now shut down the 192.168.30.21 server directly, visit the vip (192.168.30.20) and now find that the page shows 192.168.30.22 when keepalived automatically fails over, and a high-availability solution for an enterprise production environment is set up.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report