Keepalived Server installation and configuration, keepalived Server

Source: Internet
Author: User
Tags server installation and configuration haproxy

Keepalived Server installation and configuration, keepalived Server
0. Keepalived Introduction

Keepalived is a high-availability service solution based on VRRP protocol. It can be used to avoid IP spof. Similar Tools include heartbeat, corosync, and pacemaker. However, it generally does not appear separately, but works with other load balancing technologies (such as lvs, haproxy, and nginx) to achieve high cluster availability.
  
Keepalived is used to detect the status of the server. If a web server crashes or fails to work, Keepalived will detect it and remove the faulty server from the system, at the same time, other servers are used to replace the work of the server. When the server is working normally, Keepalived automatically adds the server to the server group. All these work is completed automatically without manual interference, only the faulty server needs to be manually repaired.

Health Check and Failover are two core functions of keepalived. The so-called health check is to use tcp three-way handshake, icmp request, http request, udp echo request, and other methods to the actual server behind the Load balancer (usually the server that carries real services) failover is mainly used to configure the Load balancer in the active/standby mode, using VRRP (Virtual routing redundancy protocol, refer to RFC document http://tools.ietf.org/html/rfc5798) to maintain the heartbeat of the active/standby Load balancer, when a primary Server Load balancer encounters a problem, the Standby server Load balancer carries the corresponding services to minimize traffic loss and provide service stability.

1. VRRP Protocol

VRRP is short for Virtual Router Redundancy Protocol, that is, Virtual routing Redundancy Protocol. It can be considered as a fault tolerance protocol for High Availability of routers. N routers that provide the same functions form a Router Group, which contains one master node and multiple backups, however, in the outside world, like a vro, A vro has a virtual IP address (vip, which is the default route of other machines in the LAN where the router is located ), the master who occupies this IP address is actually responsible for ARP and forwarding IP data packets. Other routers in the group are on standby as backup roles. The master node sends a multicast message. When the backup node fails to receive the vrrp packet within the timeout period, the master node is deemed to be down. In this case, a backup node needs to be elected as the master Based on the VRRP priority, ensures high availability of routers.

In VRRP, The vro uses 00-00-5E-00-01-XX as the Virtual MAC address, and XX is the unique VRID (Virtual Router IDentifier), which is occupied by only one physical Router at a time. In the physical router group in the vro, the multicast IP address 224.0.0.18 is used to regularly send the notification message. Each Router has a priority between 1 and 255, and the highest-level (highest priority) will become the master Router. By reducing the master priority, you can make the vro in the backup state preemptible (pro-empt) the status of the master router. The master takes over the virtual IP addresses with the same backup priority.

2. Comparison with heartbeat/corosync

Which of the three cluster components are Heartbeat, Corosync, and Keepalived? First, I want to explain that Heartbeat and Corosync belong to the same type. Keepalived, Heartbeat, and Corosync, it is not of the same type. Keepalived uses the vrrp Protocol, vrouter Redundancy Protocol (VRRP), and Heartbeat or Corosync is a high-availability mode based on host or network services. Simply put, the purpose of Keepalived is to simulate High Availability of the vro. The goal of Heartbeat or Corosync is to achieve high availability of the Service.

Therefore, Keepalived is usually used to achieve high front-end availability. Common combinations of front-end high availability include LVS + Keepalived, Nginx + Keepalived, HAproxy + Keepalived. Heartbeat or Corosync is used to achieve high service availability. Common combinations include Heartbeat v3 (Corosync) + Pacemaker + NFS + Httpd to achieve high availability of Web servers and Heartbeat v3 (Corosync) + Pacemaker + NFS + MySQL enables High Availability of MySQL servers. To sum up, Keepalived implements lightweight high availability, which is generally used for front-end high availability without shared storage. It is generally used for high availability of two nodes. Heartbeat (or Corosync) is generally used for high availability of services and shared storage. It is generally used for high availability of multiple nodes. We have explained this problem.

Some bloggers may ask, which one should we choose for heartbaet and corosync? I 'd like to say we generally use corosync, because corosync is better than heartbeat, even the pacemaker separated from heartbeat said that in future development, corosync + pacemaker is the best combination.

3. Keepalived + nginx

Keepalived can be considered as the implementation of VRRP in Linux. It mainly consists of three modules: core, check, and vrrp. The core module is the core of keepalived and is responsible for starting and maintaining the main process and loading and parsing global configuration files. Check is responsible for health checks, including common health check methods. The vrrp module is used to implement the VRRP protocol.

I. Installation 1. First install the dependency package
[root@bogon /]# yum install -y libnl*[root@bogon /]# yum install -y libnfnetlink-devel zlib zlib-devel gcc gcc-c++ openssl openssl-devel openssh
2. Download and decompress Keepalived
[root@bogon src]# pwd/usr/local/src[root@bogon src]# wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz[root@bogon src]# tar xvf keepalived-1.3.5.tar.gz
3. Compile and install
[root@bogon src]# cd keepalived-1.3.5[root@bogon keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived[root@bogon keepalived-1.3.5]# make[root@bogon keepalived-1.3.5]# make install
Ii. Configuration

After keepalived is installed, it is not registered as a system service by default. Therefore, you must manually add a system service script. In the/etc/init. d directory, create a keepalived file and change its permissions.

1. Copy the startup script under the keepalived source code directory to/etc/init. d /.
[Root @ bogon keepalived-1.3.5] # pwd/usr/local/src/keepalived-1.3.5 [root @ bogon keepalived-1.3.5] # cp/usr/local/src/keepalived-1.3.5/keepalived/etc/init. d/keepalived/etc/init. d/keepalived [root @ bogon keepalived-1.3.5] # ll/etc/init. d/| grep keepalived-rwxr-xr-x 1 root 1308 August 2 14:15 keepalived
2. Modify the/etc/init. d/keepalived STARTUP script.

The data to be modified has the following items:

# Config: the location of the/etc/keepalived. conf file; # the location of the Source configuration file (we set KEEPALIVED_OPTIONS there) file .. /Etc/sysconfig/keepalived

Change to the actual installation file path. My installation path is as follows:

# Config:/usr/local/keepalived/etc/keepalived. conf./usr/local/keepalived/etc/sysconfig/keepalived

Add the following variables and modify the corresponding part of the file:

Keepalived =/usr/local/keepalived/sbin/keepalived keepalived_config =/usr/local/keepalived/etc/keepalived. conf keepalived_pid =/usr/local/keepalived/run/keepalived. pid
[root@bogon keepalived-1.3.5]# vim /etc/init.d/keepalived#!/bin/sh## Startup script for the Keepalived daemon## processname: keepalived# pidfile: /usr/local/keepalived/run/keepalived.pid# config: /usr/local/keepalived/etc/keepalived/keepalived.conf# chkconfig: - 21 79# description: Start and stop Keepalived# Source function library. /etc/rc.d/init.d/functions# Source configuration file (we set KEEPALIVED_OPTIONS there). /usr/local/keepalived/etc/sysconfig/keepalivedkeepalived=/usr/local/keepalived/sbin/keepalivedkeepalived_config=/usr/local/keepalived/etc/keepalived/keepalived.confkeepalived_pid=/usr/local/keepalived/run/keepalived.pidRETVAL=0prog="keepalived"start() {    echo -n $"Starting $prog: "    daemon $keepalived -f ${keepalived_config} ${KEEPALIVED_OPTIONS}    RETVAL=$?    echo    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog}stop() {    echo -n $"Stopping $prog: "    killproc $keepalived    RETVAL=$?    echo    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog}reload() {    echo -n $"Reloading $prog: "    killproc $keepalived -1    RETVAL=$?    echo}# See how we were called.case "$1" in    start)        start        ;;    stop)        stop        ;;    reload)        reload        ;;    restart)        stop        start        ;;    condrestart)        if [ -f /var/lock/subsys/$prog ]; then            stop            start        fi        ;;    status)        status $keepalived        RETVAL=$?        ;;    *)        echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"        RETVAL=1esacexit $RETVAL
2. Modify the permission and start the instance upon startup.Modify the permission: chmod 755/etc/init. d/keepalived and add it to the System Service: chkconfig-add keepalived boot start: chkconfig keepalived on to view the startup service: chkconfig-list 3. Remarks: keepalived Service ControlSystemctl enable keepalived. service # enable automatic start of systemctl disable keepalived. service # disable automatic start of systemctl start keepalived. service # Start the systemctl restart keepalived service. service # restart the systemctl stop keepalived service. service # Stop the systemctl status keepalived service. service # view service status Appendix: another method for adding a service to the System 1. Copy the corresponding file to the corresponding directory
# Keepalived the startup script variable references the file. The default file path is/etc/sysconfig/. You can also directly modify the file path in the startup script without using soft links (under the installation directory) [root @ bogon/] # cp/usr/local/keepalived/etc/sysconfig/keepalived # Add the keepalived main program to the environment variable (under the installation directory) [root @ bogon/] # cp/usr/local/keepalived/sbin/keepalived/usr/sbin/keepalived # keepalived STARTUP script (under the source code directory), put it in/etc/init. d/directory, you can use the service command to conveniently call [root @ bogon/] # cp/usr/local/src/keepalived-1.3.5/keepalived/etc/init. d/keepalived/etc/init. d/keepalived # Put the configuration file in the default path [root @ bogon/] # mkdir/etc/keepalived [root @ bogon/] # cp/usr/local/keepalived/etc/ keepalived/keepalived. conf/etc/keepalived. conf
2. Set startup
# Set boot start [root @ bogon/] # chkconfig keepalived on [root @ bogon/] # service keepalived start | stop | restart
Appendix. Problems During Installation 1. Modification of the startup script/etc/init. d/keepalived does not take effect

To solve this problem, run the following command to reload the script.

[root@bogon sbin]# systemctl daemon-reload

Most of the above methods will take effect, but there are exceptions. In this case, try the following method:

Delete the corresponding STARTUP script in the/usr/lib/systemd/system/directory.

[Root @ bogon/] # chkconfig -- del keepalived [root @ bogon/] # cd/usr/lib/systemd/system/[root @ bogon system] # ls | grep keepalivedkeepalived. service [root @ bogon system] # rm keepalived. servicerm: whether to delete the common file "keepalived. service "? Y

Then register as a system service again:

[root@bogon system]# chkconfig --add keepalived

After you register the instance again, the modified configuration file takes effect.

2. a configured resource limit was exceeded.

The following error occurs after you run systemctl start keepalived. service:

[root@bogon sbin]# systemctl start keepalived.serviceJob for keepalived.service failed because a configured resource limit was exceeded. See "systemctl status keepalived.service" and "journalctl -xe" for details.

This problem has not been solved for a long time on the Internet. However, after problem 1 is solved, problem 2 does not appear again.

3. the following problems occur during installation:

If the dependency package is missing, install it:

[root@bogon /]# yum install -y libnfnetlink-devel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.