In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use Keepalived to achieve automatic restart of Nginx", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Keepalived to automatically restart Nginx.
Catalogue
1. Overview
2. Using Keepalived to realize the automatic restart of Nginx
2.1 complete Nginx restart with the help of Shell script
2.2 give the script permission to run
2.3 add configuration to the Keepalived configuration file
2.4 restart the Keepalived service
3. The construction of double main hot standby
3.1 Overview of dual main Hot standby
3.2 scenario description
3.3 modify A server configuration
3.4 modify B server configuration
3.5 restart Keepalived for both servers
3.6 set up DNS polling
4. Load balancing of CVM
1. Overview
Previously, we used Keepalived to achieve high availability of dual-server active and standby Nginx services, but there are several problems that have not been solved. Let's discuss them today.
1) in the dual-server master / slave mechanism, if the Keepalived service goes down, the slave server will automatically be enabled for service, but if the load of the Nginx service goes down too much, the virtual ip will not point to the slave.
2) the characteristic of dual master / slave is that only one machine is providing service, and the standby machine will provide service only when the Keepalived service of the host is down, which greatly results in a waste of resources.
3) it is quite popular to rent a CVM to operate the company's products. Does the CVM support virtual IP?
Today we will explain the above three questions.
2. Using Keepalived to realize the automatic restart of Nginx 2.1 complete the restart of Nginx with the help of Shell script
Keepalived cannot start Nginx directly, but it can execute shell scripts, so here we need to start Nginx with the help of Shell scripts.
Under the / etc/keepalived directory, create a new script check_nginx.sh. Here are the details of the script:
#! / bin/bash# executes the command to view the Nginx process, and places it in the variable A = `ps-C nginx-- no-header | wc-l` # to determine whether it is down. If reboot is attempted, stop Keepalivedif [$A-eq 0] if the restart is unsuccessful; then / usr/local/nginx/sbin/nginx sleep 3 if [`ps-C nginx-- no-header | wc-l`-eq 0]; then killall keepalived fifi
The script comes from the network.
2.2 give script permission to run # chmod + x / etc/keepalived/check_nginx.sh2.3 add configuration to Keepalived configuration file
Open the Keepalived configuration file, # vi / etc/keepalived/keepalived.conf
Modify the configuration file to add the configuration of vrrp_script and track_script, as follows:
Global_defs {# globally unique host ID router_id server_a} vrrp_script check_nginx {script "/ etc/keepalived/check_nginx.sh" interval 3 # runs the Shell script every 3 seconds weight 10 # script runs successfully, weight plus 10} vrrp_instance VI_1 {# identifies whether it is a primary node or a standby node The network card interface ens33 # virtual routing id bound with MASTER or BACKUP state MASTER # ensures that the master / slave nodes have the same virtual_router_id 51 # weight priority 100 # synchronization check time, and the default interval is 1 second advert_int 1 # authentication authorization password. All master and slave needs the same authentication {auth_type PASS auth_pass 1111} track_script {check_nginx} # Virtual IP virtual_ipaddress {192.168.1.88}} 2.4 restart Keepalived service
After rebooting, it is found that Nginx will start automatically soon after it is stopped.
3. Construction of dual main hot standby 3.1 Overview of dual main hot standby
Due to the dual-machine active and standby mechanism, only one server at a time will provide services, and the configuration of the active and standby machines is the same, which greatly results in a waste of resources.
Dual master hot backup solves this problem. The principle of dual master hot backup is that the two servers use Keepalived as the master / slave for each other, so two virtual IP are needed. Then, using the configuration of DNS polling, a domain name is routed to the two virtual IP by polling, thus achieving high availability.
3.2 scenario description
Virtual IP1:192.168.1.88
Virtual IP2:192.168.1.66
A server IP (master): 192.168.1.144
B server IP (standby): 192.168.1.22
3.3 modify A server configuration
The configuration is as follows:
! Configuration File for keepalivedglobal_defs {# globally unique host ID router_id server_a} vrrp_instance VI_1 {# identifies whether it is a master node or a standby node. The value is MASTER or BACKUP state MASTER # bound network card interface ens33 # virtual routing id to ensure that the primary and standby nodes are consistent virtual_router_id 51 # weight priority 100 # synchronous check time The default interval is 1 second for the password of advert_int 1 # authentication authorization. All masters and slaves need the same authentication {auth_type PASS auth_pass 1111} # virtual IP virtual_ipaddress {192.168.1.88}} vrrp_instance VI_2 {# to identify whether it is a master node or a standby node. The network card interface ens33 # virtual routing id bound with MASTER or BACKUP state BACKUP # ensures that the master / slave nodes have the same virtual_router_id 52 # weight priority 80 # synchronization check time, and the default interval is 1 second advert_int 1 # authentication authorization password. All master and slave need the same authentication {auth_type PASS auth_pass 1111} # virtual IP virtual_ipaddress {192.168.1.66}} 3.4 modify B server configuration
The configuration is as follows:
! Configuration File for keepalivedglobal_defs {router_id server_b} vrrp_instance VI_1 {# set to standby state BACKUP interface ens33 virtual_router_id 51 # weight setting is lower than host priority 90 advert_int 1 authentication {auth_type PASS auth_pass 1111} # virtual IP needs to be set to the same virtual_ipaddress { 192.168.1.88} vrrp_instance VI_2 {# set to host state MASTER interface ens33 virtual_router_id 52 # weight setting is lower than host priority 100 advert_int 1 authentication {auth_type PASS auth_pass 1111} # virtual IP needs to be set as the same virtual_ipaddress {192.168.1.66 }} 3.5 restart the Keepalived of both servers
Just restart Keepalived.
3.6 set up DNS polling
Contact the network operator to solve.
4. Load balancing of CVM
Now, many companies will choose to rent cloud servers to run their own products, because it is more cost-effective and stable, and there are professionals responsible for operation and maintenance.
However, if you use a cloud server, it will be limited by the cloud service provider.
Take virtual IP, for example, some cloud service operators simply do not support it.
If virtual IP is not supported, the Keepalived solution will have to be abandoned.
However, even if Keepalived is not available, there are solutions for the high availability of Nginx. Many cloud service providers have launched their own cloud load balancer services (such as Aliyun's SLB and Tencent Cloud's CLB). We just rent it directly. We don't need to configure it ourselves.
At this point, I believe you have a deeper understanding of "how to use Keepalived to automatically restart Nginx". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.