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

How to use nginx to achieve High availability in keepalived

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

Share

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

In this issue, the editor will bring you about how to use nginx in keepalived to achieve high availability. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.

1.keepalived introduction

Keepalived was originally designed for LVS load balancing software to manage and monitor the status of each service node in the LVS cluster system, and later added the highly available VRRP function. In addition to managing LVS software, keepalived can also support highly available solutions for other services.

Keepalived implements high availability functions through the VRRP protocol. VRRP (Virtual Router Redundancy Protocol) virtual routing redundancy protocol. The purpose of VRRP is to solve the problem of single point of failure of static routes. it can ensure that the whole network can run uninterrupted when individual nodes are down.

2.keepalived High availability failover principle

Failover between keepalived highly available services is achieved through VRRP. When the keepalived service is working, the primary Master node will constantly send heartbeat messages to the standby node to tell the standby Backup node that it is still alive.

When the primary node fails, the heartbeat message cannot be sent to the standby node, if the standby node can no longer detect the heartbeat from the primary node. It invokes its own receiver to take over the IP resources and services of the master node. When the primary node is restored, the standby node will release the IP resources and services that it takes over when the primary node fails, and return to the original standby role.

3. Install nginx

3.1. Primary node (192.168.80.22)

3.1.1. Install compilation tools and library files

Yum-y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel

3.1.2. Install pcre

# enter the directory cd / usr/local/develop/anginx# to upload the installation file and decompress tar-zxvf pcre-8.38.tar.gz# into the installation directory cd pcre-8.38# to check the configuration. / configure# compile, install make & & make install# to view the pcre version pcre-config-- version

3.1.3. Install nginx

# enter the directory cd / usr/local/develop/anginx# to upload installation files And extract tar-zxvf nginx-1.8.1.tar.gz# into the installation directory cd nginx-1.8.1# to check the configuration. / configure-- prefix=/usr/local/develop/anginx/webserver/nginx-- with-http_stub_status_module-- with-http_ssl_module-- with-pcre=/usr/local/develop/anginx/pcre-8.38# compilation installation make & & make install# View nginx version / usr/local/develop/anginx/ Webserver/nginx/sbin/nginx-root@hadoop02 webserver-[root@hadoop02 webserver] # / usr/local/develop/anginx/webserver/nginx/sbin/nginx-vnginx version: nginx/1.8.1# configuration nginx (check) / usr/local/develop/anginx/webserver/nginx / sbin/nginx-t#nginx management command / usr/local/develop/anginx/webserver/nginx/sbin/nginx # start Nginx/usr/local/develop/anginx/webserver/nginx/sbin/nginx-s stop # stop Nginx/usr/local/develop/anginx/webserver/nginx/sbin/nginx-s reload # reload configuration file / usr/local/develop/anginx/webserver/nginx/sbin/nginx-s reopen # restart Nginx

3.1.4.nginx basic configuration

Vi nginx.conf#user nobody;worker_processes 1 is responsible for errorship log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;pid logs/nginx.pid;events {worker_connections 1024;} http {include mime.types; default_type application/octet-stream Log_format main'$remote_addr-$remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_agent"$http_x_forwarded_for"; access_log logs/access.log main; sendfile on; # tcp_nopush on; # keepalive_timeout 0; keepalive_timeout 65; # gzip on # add tomcat list, real application servers are placed here upstream tomcat_pool {# server tomcat address: Port number weight indicates weight, the greater the weight, the greater the probability of being assigned; server 192.168.80.22 server 8080 weight=4 max_fails=2 fail_timeout=30s; server 192.168.80.22 server 8081 weight=4 max_fails=2 fail_timeout=30s;} server {listen 80; server_name tomcat_pool; # charset koi8-r # access_log logs/host.access.log main; location / {# root html; # index index.html index.htm; proxy_pass http://tomcat_pool; # switch to tomcat processing proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for } # error_page 404 / 404.html; # redirect server error pages to the static page / 50x.html # error_page 500502503504 / 50x.html; location = / 50x.html {root html;}}

3.2. Standby node (192.168.80.21)

Description: the installation method is the same as the nginx master node.

4. Install keepalived

4.1. Primary node (192.168.80.22)

# install keepalivedyum install keepalived-y # start keepalived service / etc/init.d/keepalived start--- [root@hadoop02 anginx] # / etc/init.d/keepalived start is starting keepalived: [OK] [root@hadoop02 anginx] # ps-ef | grep keepalivedroot 15723 1 0 00:59? 00:00:00 / usr/sbin/keepalived-Droot 15724 15723 0 00:59? 00:00:00 / usr/sbin/keepalived-Droot 15725 15723 0 00:59? 00:00:00 / usr/sbin/keepalived-Droot 15731 15622 0 00:59 pts/1 00:00:00 grep keepalived [root@hadoop02 anginx] # # Settings Power on echo "/ etc/init.d/keepalived start" > > / etc/rc.local# shuts down keepalived The service / etc/init.d/keepalived stop# edits the keepalived configuration file vi / etcUnitablement keepalivedUnitabalived.confusMurray! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc} notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id lb01} vrrp_instance VI_1 {state MASTER interface eth2 virtual_router_id 55 priority 150 advert_int 1 authentication {auth_type PASS auth_pass server123} virtual_ipaddress {192.168.80.100 dev Eth2 label eth2:1}}...

For configuration instructions:

[router_id] is a routing identity and should be unique in a local area network

[vrrp_instance VI_1] {.} this is a VRRP instance that defines the master / slave status, interface, priority, authentication and IP information of keepalived.

[state] defines the role of VRRP

[interface] defines the interface to be used. Here, the network cards used by my server are all eth2.

[virtual_router_id] is the virtual routing ID identity. In the keepalived configuration of a group, the master / slave settings are consistent.

[priority] is priority. The higher the number, the higher the priority.

[auth_type] is the authentication method

[auth_pass] is the password for authentication

[virtual_ipaddress] {...} defines a virtual IP address, which can be configured with multiple IP addresses. Here I define it as 192.168.80.100, which is bound to the network interface of eth2 and the virtual interface eth2:1.

4.2. Standby node (192.168.80.21)

# install keepalivedyum install keepalived-y # start keepalived service / etc/init.d/keepalived start--- [root@hadoop02 anginx] # / etc/init.d/keepalived start is starting keepalived: [OK] [root@hadoop02 anginx] # ps-ef | grep keepalivedroot 15723 1 0 00:59? 00:00:00 / usr/sbin/keepalived-Droot 15724 15723 0 00:59? 00:00:00 / usr/sbin/keepalived-Droot 15725 15723 0 00:59? 00:00:00 / usr/sbin/keepalived-Droot 15731 15622 0 00:59 pts/1 00:00:00 grep keepalived [root@hadoop02 anginx] # # Settings Power on echo "/ etc/init.d/keepalived start" > > / etc/rc.local# shuts down keepalived The service / etc/init.d/keepalived stop# edits the keepalived configuration file vi / etcserver keepalivedUniverse keepalived.confmer! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc} notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id lb02} vrrp_instance VI_1 {state BACKUP interface eth2 virtual_router_id 55 priority 100 advert_int 1 authentication {auth_type PASS auth_pass server123} virtual_ipaddress {192.168.80.100 dev Eth2 label eth2:1}}...

5. test

5.1. Start the keepalived service of the master / slave node

# execute on Node 1 (192.168.80.22) / etc/init.d/keepalived start-- [root@hadoop02 anginx] # ps-ef | grep keepalivedroot 15788 10 01:09? 00:00:00 / usr/sbin/keepalived-Droot 15790 15788 0 01:09? 00:00:00 / usr/sbin/keepalived- Droot 15791 15788 0 01:09? 00:00:00 / usr/sbin/keepalived-Droot 15807 15622 0 01:33 pts/1 00:00:00 grep keepalived [root@hadoop02 anginx] # # execute on Node 2 (192.168.80.21) / etc/init.d/keepalived start-- [root@hadoop01 ~] # ps -ef | grep keepalivedroot 11542 10 01:30? 00:00:00 / usr/sbin/keepalived-Droot 11544 11542 0 01:30? 00:00:00 / usr/sbin/keepalived-Droot 11545 11542 0 01:30? 00:00:00 / usr/sbin/keepalived-Droot 11550 11512 0 01:33 pts/1 00:00:00 grep keepalived [root@hadoop01 ~] #

5.2. Access the service through virtual ip

Http://192.168.80.100/session-redis-demo/

5.3. Stop the keepalived service of the primary node

# observe the changes of the backup node at Node 1 (192.168.80.22) / etc/init.d/keepalived stop# ip addr--- [root@hadoop01 ~] # ip addr1: mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00lv 0000lv 0000lv 00lv 00lv 00 brd 00lv 00lv 00lv 00 : 00:00 inet 127.0.0.1/8 scope host lo inet6:: 1/128 scope host valid_lft forever preferred_lft forever2: eth2: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:38:e5:46 brd ff:ff:ff:ff:ff:ff inet 192.168.80.21/24 brd 192.168.80.255 scope global eth2 inet 192.168.80.100/32 scope global eth2:1 inet6 fe80::250:56ff:fe38 : e546Compact 64 scope link valid_lft forever preferred_lft forever [root@hadoop01 ~] #

5.4. Continue to access the service through virtual ip

Http://192.168.80.100/session-redis-demo/

6.keepalived+nginx integration

Description: write a nginx daemon script to stop the nginx service of the current node if the keepalived service fails. Automatically switch to the backup node.

6.1. Write nginx daemon scripts

Vi nginxcoat check.shkimachi chmod while truedoif [$(nginx_check.sh#-tlnp | grep nginx | wc-l)-ne 1] then / etc/init.d/keepalived stopfisleep 2done# authorizes the script to execute the script nohup / usr/local/develop/anginx/shell/nginx_check.sh &

6.2. Stop the nginx service of the primary node

# stop nginx service on primary node / usr/local/develop/anginx/webserver/nginx/sbin/nginx-s stop# lookup process [root@hadoop02 ~] # ps-ef | grep nginxroot 15915 10 01:51? 00:00:00 / bin/bash / usr/local/develop/anginx/shell/nginx_check.shroot 16516 15753 0 01:54 pts/5 00:00:00 grep nginx [root@hadoop02 ~] # # observe the change of standby node [service is normal] Ip addr-- [root@hadoop01 shell] # 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: eth2: Mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:38:e5:46 brd ff:ff:ff:ff:ff:ff inet 192.168.80.21 scope global eth2:1 inet6 fe80::250:56ff:fe38:e546/64 scope link valid_lft forever preferred_lft forever 24 brd 192.168.80.255 scope global eth2 inet 192.168.80.100 Charpy 32 scope global eth2:1 inet6 fe80::250:56ff:fe38:e546/64 scope link valid_lft forever preferred_lft forever [root@hadoop01 shell] # # restart the primary node nginx and keepalived services again / usr/local/develop/anginx/webserver/nginx/sbin/nginx/etc/init.d/keepalived start above is what the editor shares with you on how to use nginx in keepalived to achieve high availability. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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