search

How does keepalived work?

Jun 24, 2021 am 11:08 AM
keepalivedworking principle

The working principle of keepalived is to detect the status of each service node based on the third, fourth and fifth layer switching mechanisms of the TCP/IP reference model. If a server node is abnormal or fails, Keepalived will The failed server node is detected and removed from the cluster system.

How does keepalived work?

The operating environment of this tutorial: centos7 system, thinkpad t480 computer.

Keepalived introduction

Keepalived is the next lightweight level high-availability solution for Linux. High availability: In a broad sense, it refers to the high availability of the entire system; in a narrow sense, it refers to the redundancy and takeover of the host.

It implements similar functions to HeartBeat, both of which can achieve high availability of services or networks, but there are differences. HeartBeat is a professional, fully functional high-availability software that provides the basic requirements for HA software. Functions, such as: heartbeat detection, resource takeover, detection of services in the cluster, transfer of owners of shared IP addresses on cluster nodes, etc.

HeartBeat is powerful, but deployment and use are relatively troublesome. Compared with HeartBeat, Keepalived mainly implements high availability functions through virtual routing redundancy. Although it is not as powerful as HeartBeat, Keepalived is very easy to deploy and use. Extremely simple, all configurations only require one configuration file.

What is Keepalived?

Keepalived was originally designed for LVS and is specifically used to monitor the status of each service node in the cluster system. It is based on the third, fourth and fifth layer switching mechanisms of the TCP/IP reference model. Detect the status of each service node. If a server node is abnormal or the work fails, Keepalived will detect it and remove the failed server node from the cluster system. All these tasks are completed automatically and do not require Manual intervention requires manual work only to repair the failed service node.

Later Keepalived added the VRRP function. The purpose of VRRP (VritrualRouterRedundancyProtocol, virtual routing redundancy protocol) is to solve the single point of failure problem in static routing. VRRP can achieve uninterrupted and stable operation of the network, so On the one hand, Keepalvied has server status detection and fault isolation functions, and on the other hand, it also has the HAcluster function.

Health check and failover are the two core functions of keepalived. The so-called health check uses TCP three-way handshake, ICMP request, HTTP request, UDP echo request, etc. to keep alive the actual server behind the load balancer (usually the server that carries the real business); while failed switching is mainly the application For load balancers configured in active and standby mode, VRRP is used to maintain the heartbeat of the active and standby load balancers. When a problem occurs with the active load balancer, the standby load balancer carries the corresponding services, thereby minimizing traffic loss and Provide stability of services.

VRRP protocol and working principle

In a real network environment. Communication between hosts is completed by configuring static routing or (default gateway). Once the router between hosts fails, communication will fail. Therefore, in this communication mode, the router becomes a single point bottleneck. ,In order to solve this problem, the VRRP protocol was ,introduced.

The VRRP protocol is a fault-tolerant master-backup mode protocol, which ensures that when the host's next-hop route fails, another router will replace the failed router. VRRP can be used on the network When a failure occurs, device switching is performed transparently without affecting data communication between hosts.

How does keepalived work?

Virtual router: A virtual router is a collection of all routers in a VRRP backup group. It is a logical concept and does not really exist. Looking at the routers in the backup group from outside the backup group, it feels like all the routers in the group are the same, which can be understood as being in a group: main router all backup routers = virtual routers.

A virtual router has a virtual IP address and MAC address. The host uses the virtual router as the default gateway. The format of the virtual MAC address is 00-00-5E-00-01-{VRID}. Normally, the virtual router uses the virtual MAC address in response to the ARP request. Only when the virtual router is specially configured, will it respond to the real MAC address of the interface.

Main router (MASTER): The virtual router provides services to the outside world through the virtual IP, and within the virtual router, there is only one physical router that provides services to the outside world at the same time. This physical router that provides services is called the master router. Generally, the Master is generated by an election algorithm. It has a virtual IP for external services and provides various network functions, such as ARP requests, ICMP data forwarding, etc.

Backup router (BACKUP): Other physical routers in the virtual router do not have external virtual IPs, nor do they provide external network functions. They only accept the VRRP status advertisement information of the MASTER. These routers are called backup routers. When the main router fails, the backup router in the BACKUP role will be re-elected to generate a new main router to enter the MASTER role and continue to provide external services. The entire switch is completely transparent to users.

VRRP election mechanism

The VRRP router has three states during operation:

1. Initialize state: After the system starts, it enters Initialize. In this state, the router does not process VRRP packets;

2. Master state;

3. Backup state;

Generally, the main router is in the Master state and the backup router is in the Backup state. state.

VRRP uses an election mechanism to determine the status of the router. Priority election:

1. IP owner in the VRRP group. If the virtual IP address is the same as the IP address of a VRRP router in the VRRP group, then this router is the owner of the IP address and this router will be positioned as the master router.

2. Compare priorities. If there is no IP address owner, compare the priorities of the routers. The priority range is 0~255. The one with the higher priority will be used as the main router

3. Compare the IP addresses. In the case where there is no IP address owner and the priority is the same, the one with the larger IP address acts as the main router.

As shown in the figure below, the virtual IP is 10.1.1.254. There is no IP address owner in the VRRP group. Then compare the priorities. Obviously the priorities of RB and RA are greater than RC. Then compare RA and RB. The IP address of RB is larger. So RB is the main router in the group.

How does keepalived work?

Working process

After the router uses the VRRP function, it will determine its role in the backup group based on priority. The router with higher priority becomes the Master router, and the router with lower priority becomes the Backup router. The Master has a virtual IP for external services, provides various network functions, and regularly sends VRRP messages to notify other devices in the backup group that it is working normally; the Backup router only receives message information sent by the Master to monitor the operation of the Master. state. When the Master fails, the Backup router will elect, and the Backup with higher priority will become the new Master.

In preemption mode, when the Backup router receives the VRRP message, it will compare its own priority with the priority in the message. If it is greater than the priority in the advertisement message, it will become the Master router; otherwise it will remain in the Backup state;

In non-preemption mode, as long as the Master router does not fail, the routers in the backup group will always remain Master or Backup. status, the Backup router will not become the Master router even if it is subsequently configured with a higher priority;

If the Backup router does not receive the VRRP message sent by the Master router after its timer expires, it is considered The Master router is no longer working properly. At this time, the Backup router will think that it is the Master router and send VRRP packets to the outside world. The routers in the backup group elect a master router based on priority to assume the packet forwarding function.

Keepalvied’s working principle

Keepalived’s working principle for server running status and fault isolation:

Keepalived works in the third phase of the TCP/IP reference model Layer, four, and five (physical layer, link layer):

Network layer (3): Keepalived sends an ICMP packet to each node in the server cluster through the ICMP protocol (somewhat similar to Ping function), if a node does not return a response packet, it is considered that the node has failed. Keepalived will report the node failure and remove the failed node from the server cluster.

Transport layer (4): Keepalived uses the port connection and scanning technology of the TCP protocol in the transport layer to determine whether the port of the cluster node is normal, such as port 80 of the common WEB server. Or SSH service port 22. Once Keepalived detects that these port numbers have no data response or data return at the transport layer, it will consider these ports to be abnormal, and then forcibly remove the nodes corresponding to these ports from the server cluster.

Application layer (5): The operation mode of Keepalived is also more comprehensive and complex. Users can customize the working mode of Keepalived. For example, you can run Keepalived by writing a program or script, and Keepalived will be run according to the The user's set parameters detect whether various programs or services are allowed to function normally. If Keepalived's detection results are inconsistent with the user's settings, Keepalived will remove the corresponding server from the server cluster.

Keepalived Architecture

Keepalived was originally designed for LVS. Since Keepalived can detect the status of cluster nodes, and IPVS can realize the load balancing function, Keepalived relies on The third-party module IPVS can easily build a load balancing system. The IPVS module in Keepalived is configurable. If you need the load balancing function, you can turn on the load balancing function when compiling Keepalived, or turn it off through compilation parameters.

How does keepalived work?

SchedulerI/OMultiplexer is an I/O multiplexing distribution scheduler, which loads all internal task requests of Keepalived;

Memory Mngt is a memory Management mechanism, this framework provides some common methods of accessing memory;

Control Plane is the control plane of keepalived, which can compile and parse configuration files;

Core componets This part mainly includes 5 parts;

  • Watchdog: It is an extremely simple and very effective detection tool in the field of computer reliability. Keepalived monitors Checkers and VRRP processes through it.

  • Checkers: This is the most basic function of Keepalived and the most important function. It can detect the running status of the server and isolate faults.

  • VRRP Stack: This is the VRRP function that keepalived later quoted, which can realize the failover function in the HA cluster. Responsible for failover switching between load balancers FailOver;

  • IPVS wrapper: This is an implementation of the IPVS function. The IPVSwarrper module sends the kernel space that can be set up and provides IPVS rules to IPVS module finally realizes the load function of IPVS module.

  • Netlink Reflector: Used to implement the setting and switching of virtual IP (VIP) during high-availability cluster Failover.

When keepalived is running, it will Start 3 processes, namely: core (core process), check and vrrp

- core: responsible for the startup, maintenance and loading of the global configuration file of the main process;

- check: responsible for Health check

- vrrp: Used to implement the vrrp protocol

Compare with heartbeat/corosync

The three cluster components of Heartbeat, Corosync, and Keepalived Which one should we choose? Heartbeat and Corosync belong to the same type. Keepalived, Heartbeat and Corosync are not the same type at all.

Keepalived uses the vrrp virtual routing redundancy protocol method; Heartbeat or Corosync is a high-availability method based on host or network services; simply put, the purpose of Keepalived is to simulate the high availability of the router, Heartbeat or Corosync The purpose is to achieve high availability of Service.

So generally Keepalived is used to achieve front-end high availability. Commonly used combinations of front-end high availability include our common LVS Keepalived, Nginx Keepalived, and HAproxy Keepalived. Heartbeat or Corosync is used to achieve high availability of services. Common combinations include Heartbeat v3 (Corosync) Pacemaker NFS Httpd to achieve high availability of Web servers, and Heartbeat v3 (Corosync) Pacemaker NFS MySQL to achieve high availability of MySQL servers.

To summarize, Keepalived implements lightweight high availability, which is generally used for front-end high availability and does not require shared storage. It is generally used for high availability of two nodes. Heartbeat (or Corosync) is generally used for high availability of services and requires shared storage, and is generally used for high availability of multiple nodes.

(Recommended learning: linux tutorial)

The above is the detailed content of How does keepalived work?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Linux's Essential Components: Explained for BeginnersLinux's Essential Components: Explained for BeginnersApr 17, 2025 am 12:08 AM

The core components of Linux include the kernel, file system, shell and common tools. 1. The kernel manages hardware resources and provides basic services. 2. The file system organizes and stores data. 3. Shell is the interface for users to interact with the system. 4. Common tools help complete daily tasks.

Linux: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Understanding Linux's Maintenance Mode: The EssentialsUnderstanding Linux's Maintenance Mode: The EssentialsApr 14, 2025 am 12:04 AM

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

How Debian improves Hadoop data processing speedHow Debian improves Hadoop data processing speedApr 13, 2025 am 11:54 AM

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

How to learn Debian syslogHow to learn Debian syslogApr 13, 2025 am 11:51 AM

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

How to choose Hadoop version in DebianHow to choose Hadoop version in DebianApr 13, 2025 am 11:48 AM

When choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and

TigerVNC share file method on DebianTigerVNC share file method on DebianApr 13, 2025 am 11:45 AM

This article describes how to use TigerVNC to share files on Debian systems. You need to install the TigerVNC server first and then configure it. 1. Install the TigerVNC server and open the terminal. Update the software package list: sudoaptupdate to install TigerVNC server: sudoaptinstalltigervnc-standalone-servertigervnc-common 2. Configure TigerVNC server to set VNC server password: vncpasswd Start VNC server: vncserver:1-localhostno

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment