search
HomeOperation and MaintenanceNginxHow to achieve keepalived+nginx high availability

How to achieve keepalived+nginx high availability

May 11, 2023 pm 09:28 PM
nginxkeepalived

1.Keepalived introduction

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

keepalived implements high availability function through vrrp protocol. vrrp (virtual router redundancy protocol) virtual router redundancy protocol. The purpose of vrrp is to solve the single point of failure problem of static routing. It can ensure that when individual nodes go down, the entire network can run uninterrupted.

2. Keepalived high-availability failover principle

Failover between keepalived high-availability services is achieved through vrrp. When the keepalived service is working, the master node will continuously send (multicast) heartbeat messages to the backup node to tell the backup node that it is still alive.

When the master node fails, heartbeat messages cannot be sent to the backup node. If the backup node cannot continue to detect the heartbeat from the master node. It will call its own takeover program to take over the IP resources and services of the master node. When the primary node recovers, the standby node will release the IP resources and services it took over when the primary node failed, and return to its original standby role

3. Install nginx

3.1. Master 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

#进入目录
cd /usr/local/develop/anginx

#上传安装文件并解压
tar -zxvf pcre-8.38.tar.gz

#进入安装目录
cd pcre-8.38

#检查配置
./configure

#编译、安装
make && make install

#查看pcre版本
pcre-config --version

3.1.3. Install nginx

#进入目录
cd /usr/local/develop/anginx

#上传安装文件,并解压
tar -zxvf nginx-1.8.1.tar.gz

#进入安装目录
cd nginx-1.8.1

#检查配置
./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

#编译安装
make && make install

#查看nginx版本
 /usr/local/develop/anginx/webserver/nginx/sbin/nginx -v
--------------------------------------------------------
[root@hadoop02 webserver]# /usr/local/develop/anginx/webserver/nginx/sbin/nginx -v
nginx version: nginx/1.8.1

#配置nginx(检查)
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -t

#nginx管理命令
/usr/local/develop/anginx/webserver/nginx/sbin/nginx       # 启动 nginx
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s stop       # 停止 nginx
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reload      # 重新载入配置文件
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s reopen      # 重启 nginx

3.1.4.nginx basic configuration

vi nginx.conf

#user nobody;
worker_processes 1;

#error_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;
  
  #添加tomcat列表,真实应用服务器都放在这
  upstream tomcat_pool{
    #server tomcat地址:端口号 weight表示权值,权值越大,被分配的几率越大;
    server 192.168.80.22:8080 weight=4 max_fails=2 fail_timeout=30s;
    server 192.168.80.22: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;  #转向tomcat处理
      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  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }


}

3.2. Standby node (192.168.80.21)

Instructions : The installation method is the same as the nginx master node.

4. Install keepalived

4.1. Master node (192.168.80.22)

#安装keepalived
yum install keepalived -y

#启动keepalived服务
/etc/init.d/keepalived start
-------------------------------------------
[root@hadoop02 anginx]# /etc/init.d/keepalived start
正在启动 keepalived:                   [确定]
[root@hadoop02 anginx]# ps -ef |grep keepalived
root   15723   1 0 00:59 ?    00:00:00 /usr/sbin/keepalived -d
root   15724 15723 0 00:59 ?    00:00:00 /usr/sbin/keepalived -d
root   15725 15723 0 00:59 ?    00:00:00 /usr/sbin/keepalived -d
root   15731 15622 0 00:59 pts/1  00:00:00 grep keepalived
[root@hadoop02 anginx]#

#设置开机自启动
echo "/etc/init.d/keepalived start" >>/etc/rc.local

#关闭keepalived服务
/etc/init.d/keepalived stop

#编辑keepalived配置文件
vi /etc/keepalived/keepalived.conf

-----------------------------------------------------------
! configuration file for keepalived

global_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 eth1
  virtual_router_id 55
  priority 150
  advert_int 1
  authentication {
    auth_type pass
    auth_pass server123
  }
  virtual_ipaddress {
    192.168.80.100 dev eth1 label eth1:1
  }
}
...........................................................

About configuration instructions:

  • [router_id] is the routing identifier, which should be unique in a LAN

    • [vrrp_instance vi_1]{...}This It is a vrrp instance, which defines the active and backup status, interface, priority, authentication and IP information of keepalived

    • [state] defines the role of vrrp

    • [interface] Define the interface used. The network cards used by my server here are all eth1

    • [virtual_router_id] is the virtual routing id identifier, which is in a set of keepalived configurations The active and standby settings are consistent

    • [priority] is the priority. The larger the number, the greater the priority.

    • [auth_type] is Authentication method

    • [auth_pass] is the password for authentication

  • [virtual_ipaddress] {...} Define the virtual ip address, you can Configure multiple IP addresses, here I define it as 192.168.80.100, bound to the network interface of eth1, virtual interface eth1:1

4.2. Standby node (192.168.80.21 )

#安装keepalived
yum install keepalived -y

#启动keepalived服务
/etc/init.d/keepalived start
-------------------------------------------
[root@hadoop02 anginx]# /etc/init.d/keepalived start
正在启动 keepalived:                   [确定]
[root@hadoop02 anginx]# ps -ef |grep keepalived
root   15723   1 0 00:59 ?    00:00:00 /usr/sbin/keepalived -d
root   15724 15723 0 00:59 ?    00:00:00 /usr/sbin/keepalived -d
root   15725 15723 0 00:59 ?    00:00:00 /usr/sbin/keepalived -d
root   15731 15622 0 00:59 pts/1  00:00:00 grep keepalived
[root@hadoop02 anginx]#

#设置开机自启动
echo "/etc/init.d/keepalived start" >>/etc/rc.local

#关闭keepalived服务
/etc/init.d/keepalived stop

#编辑keepalived配置文件
vi /etc/keepalived/keepalived.conf

-----------------------------------------------------------------
! configuration file for keepalived

global_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 eth1
  virtual_router_id 55
  priority 100
  advert_int 1
  authentication {
    auth_type pass
    auth_pass server123
  }
  virtual_ipaddress {
    192.168.80.100 dev eth1 label eth1:1
  }
}
.............................................................

5. Test

5.1. Start the keepalived service of the active and backup nodes

#在节点一执行(192.168.80.22)
/etc/init.d/keepalived start
-------------------------------------
[root@hadoop02 anginx]# ps -ef |grep keepalived
root   15788   1 0 01:09 ?    00:00:00 /usr/sbin/keepalived -d
root   15790 15788 0 01:09 ?    00:00:00 /usr/sbin/keepalived -d
root   15791 15788 0 01:09 ?    00:00:00 /usr/sbin/keepalived -d
root   15807 15622 0 01:33 pts/1  00:00:00 grep keepalived
[root@hadoop02 anginx]#


#在节点二执行(192.168.80.21)
/etc/init.d/keepalived start
---------------------------------------
[root@hadoop01 ~]# ps -ef |grep keepalived
root   11542   1 0 01:30 ?    00:00:00 /usr/sbin/keepalived -d
root   11544 11542 0 01:30 ?    00:00:00 /usr/sbin/keepalived -d
root   11545 11542 0 01:30 ?    00:00:00 /usr/sbin/keepalived -d
root   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/

How to achieve keepalived+nginx high availability

5.3. Stop the host Node keepalived service

#在节点一执行(192.168.80.22)
/etc/init.d/keepalived stop

#观察备节点变化
ip addr
-------------------------------------------
[root@hadoop01 ~]# ip addr
1: lo: <loopback,up,lower_up> 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 forever
2: eth1: <broadcast,multicast,up,lower_up> 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 eth1
  inet 192.168.80.100/32 scope global eth1:1
  inet6 fe80::250:56ff:fe38:e546/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/

How to achieve keepalived+nginx high availability

6.keepalived nginx integration

Instructions: Write an nginx daemon script. If the nginx service fails, stop the keepalived service of the current node. Automatically switches to the backup node.

6.1. Write nginx daemon script

vi nginx_check.sh

--------------------------------------
#!/bin/bash
while true
do
if [ $(netstat -tlnp|grep nginx|wc -l) -ne 1 ]
then
  /etc/init.d/keepalived stop
fi
sleep 2
done

#给脚本授权
chmod u+x nginx_check.sh

#执行脚本
nohup /usr/local/develop/anginx/shell/nginx_check.sh &

6.2. Stop the main node nginx service

#停止主节点nginx服务
/usr/local/develop/anginx/webserver/nginx/sbin/nginx -s stop

#查找进程
[root@hadoop02 ~]# ps -ef |grep nginx
root   15915   1 0 01:51 ?    00:00:00 /bin/bash /usr/local/develop/anginx/shell/nginx_check.sh
root   16516 15753 0 01:54 pts/5  00:00:00 grep nginx
[root@hadoop02 ~]#

#观察备用节点变化【服务正常】
ip addr
--------------------------------------
[root@hadoop01 shell]# ip addr
1: lo: <loopback,up,lower_up> 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 forever
2: eth1: <broadcast,multicast,up,lower_up> 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 eth1
  inet 192.168.80.100/32 scope global eth1:1
  inet6 fe80::250:56ff:fe38:e546/64 scope link 
    valid_lft forever preferred_lft forever
[root@hadoop01 shell]#

#再次重新启动主节点nginx和keepalived服务
/usr/local/develop/anginx/webserver/nginx/sbin/nginx

/etc/init.d/keepalived start

The above is the detailed content of How to achieve keepalived+nginx high availability. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
The Advantages of NGINX: Speed, Efficiency, and ControlThe Advantages of NGINX: Speed, Efficiency, and ControlMay 12, 2025 am 12:13 AM

The reason why NGINX is popular is its advantages in speed, efficiency and control. 1) Speed: Adopt asynchronous and non-blocking processing, supports high concurrent connections, and has strong static file service capabilities. 2) Efficiency: Low memory usage and powerful load balancing function. 3) Control: Through flexible configuration file management behavior, modular design facilitates expansion.

NGINX vs. Apache: Community, Support, and ResourcesNGINX vs. Apache: Community, Support, and ResourcesMay 11, 2025 am 12:19 AM

The differences between NGINX and Apache in terms of community, support and resources are as follows: 1. Although the NGINX community is small, it is active and professional, and official support provides advanced features and professional services through NGINXPlus. 2.Apache has a huge and active community, and official support is mainly provided through rich documentation and community resources.

NGINX Unit: An Introduction to the Application ServerNGINX Unit: An Introduction to the Application ServerMay 10, 2025 am 12:17 AM

NGINXUnit is an open source application server that supports a variety of programming languages ​​and frameworks, such as Python, PHP, Java, Go, etc. 1. It supports dynamic configuration and can adjust application configuration without restarting the server. 2.NGINXUnit supports multi-language applications, simplifying the management of multi-language environments. 3. With configuration files, you can easily deploy and manage applications, such as running Python and PHP applications. 4. It also supports advanced configurations such as routing and load balancing to help manage and scale applications.

Using NGINX: Optimizing Website Performance and ReliabilityUsing NGINX: Optimizing Website Performance and ReliabilityMay 09, 2025 am 12:19 AM

NGINX can improve website performance and reliability by: 1. Process static content as a web server; 2. forward requests as a reverse proxy server; 3. allocate requests as a load balancer; 4. Reduce backend pressure as a cache server. NGINX can significantly improve website performance through configuration optimizations such as enabling Gzip compression and adjusting connection pooling.

NGINX's Purpose: Serving Web Content and MoreNGINX's Purpose: Serving Web Content and MoreMay 08, 2025 am 12:07 AM

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

NGINX Unit: Streamlining Application DeploymentNGINX Unit: Streamlining Application DeploymentMay 07, 2025 am 12:08 AM

NGINXUnit simplifies application deployment with dynamic configuration and multilingual support. 1) Dynamic configuration can be modified without restarting the server. 2) Supports multiple programming languages, such as Python, PHP, and Java. 3) Adopt asynchronous non-blocking I/O model to improve high concurrency processing performance.

NGINX's Impact: Web Servers and BeyondNGINX's Impact: Web Servers and BeyondMay 06, 2025 am 12:05 AM

NGINX initially solved the C10K problem and has now developed into an all-rounder who handles load balancing, reverse proxying and API gateways. 1) It is well-known for event-driven and non-blocking architectures and is suitable for high concurrency. 2) NGINX can be used as an HTTP and reverse proxy server, supporting IMAP/POP3. 3) Its working principle is based on event-driven and asynchronous I/O models, improving performance. 4) Basic usage includes configuring virtual hosts and load balancing, and advanced usage involves complex load balancing and caching strategies. 5) Common errors include configuration syntax errors and permission issues, and debugging skills include using nginx-t command and stub_status module. 6) Performance optimization suggestions include adjusting worker parameters, using gzip compression and

Nginx Troubleshooting: Diagnosing and Resolving Common ErrorsNginx Troubleshooting: Diagnosing and Resolving Common ErrorsMay 05, 2025 am 12:09 AM

Diagnosis and solutions for common errors of Nginx include: 1. View log files, 2. Adjust configuration files, 3. Optimize performance. By analyzing logs, adjusting timeout settings and optimizing cache and load balancing, errors such as 404, 502, 504 can be effectively resolved to improve website stability and performance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.