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

Complete steps to achieve high availability with nginx combined with keepalived

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Preface

In order to meet the high availability of the system, it is generally necessary to build clusters. When the host hangs up, our system can continue to provide services. The same is true when we use nginx as a reverse proxy and static separation server. Realizing the high availability of the system is the focus of our programmers. This paper introduces nginx combined with keepalived to build a high availability cluster in master-slave mode.

Premise knowledge

This article does not give too much introduction to nginx-related configuration, and the default reader already knows about nginx.

Introduction of keepalived

The function of Keepalived is to detect the status of the server. If a web server goes down or fails, Keepalived will detect and remove the faulty server from the system, and use other servers to replace the server's work. When the server is working normally, Keepalived will automatically add the server to the server farm, and all these tasks will be done automatically without human intervention. All that needs to be done manually is to repair the failed server.

System architecture diagram

Implementation steps

1. Environment configuration

1.1 install nginx

Installation related dependencies

Yum install pcre-devel zlib zlib-devel openssl openssl-devel

Upload the decompressed package

Tar zxvf nginx-1.12.2.tar.gz

Create a directory and detect the configuration

Mkdir-p / usr/local/nginx./configure-- prefix=/usr/local/nginx

Precompile and install

Make & & make install

Start and stop related commands

Cd / usr/local/nginx/sbin/./nginx./nginx-s stop./nginx-s start

1.2 install keepalived

Yum install-y keepalived

two。 Host configuration

Modify keepalived configuration file

Vim / etc/keepalived/keepalived.conf

The changes are as follows

# New configuration vrrp_script chk_http_port {script "/ shell/nginx_check.sh" # script address interval 2 # check script execution interval weight 2 # specific gravity} vrrp_instance VI_1 {state MASTER # master server is MASTER, slave server is BACKUP interface eth0 # network card virtual_router_id 51 # the virtual_router_id of master and standby must be the same priority 100# different priority of master and standby, the priority of host is higher, and the value of standby is smaller The default is 100 advert_int 1 authentication {auth_type PASS auth_pass 1111} # Virtual IP address virtual_ipaddress {192.168.126.88}}

Create a scripting file

Mkdir / shell/nginx_check.shvim / shell/nginx_check.sh

The script file is as follows

#! / bin/bashA= `ps-C nginx-no-header | wc-l`if [$A-eq 0]; then / usr/local/nginx/sbin/nginx sleep 2 if [`ps-C nginx-- no-header | wc-l`-eq 0]; then killall keepalived fifi

3. Slave configuration

Modify keepalived configuration file

Vrrp_instance VI_1 {state BACKUP # master server is MASTER, slave server is BACKUP interface eth0 # network card virtual_router_id 51 # master and standby virtual_router_id must be the same priority 50 # different priority between master and standby, host priority is higher, standby value is lower advert_int 1 authentication {auth_type PASS auth_pass 1111} # virtual IP address virtual_ipaddress {192.168.126.88}}

4. Start the service

Start

Service keepalived start

View virtual IP address

Ip addr1: lo: mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6:: 1/128 scope host valid_lft forever preferred_lft forever2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:4f:31:ce brd ff:ff:ff:ff:ff:ff inet 192.168.126.100/24 Brd 192.168.126.255 scope global eth0 inet 192.168.126.88/32 scope global eth0 inet6 fe80::20c:29ff:fe4f:31ce/64 scope link valid_lft forever preferred_lft forever

5. Turn off the firewall test

Turn off the firewall

Service iptables stop# to check whether the firewall boots chkconfig-- list | grep iptableschkconfig iptables off

Log in to the virtual IP address to view

Http://192.168.126.88/

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support.

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