Linux Learning Summary (52) keepalived Configuration High Availability

Source: Internet
Author: User
Tags system log

Introduction of a group
  • Divided into two categories according to function: High availability and load balancing
  • A highly available cluster is typically two servers, one working, the other being redundant, and redundancy will take over as the service goes down.
  • Open source software for high availability: heartbeat, keepalived
  • A load Balancer cluster requires a server as a dispatcher, which distributes the user's requests to the backend server, where, in addition to the Distributor, is the server that serves the user, at least 2 of these servers
  • The open source software for load Balancing has LVS, keepalived, Haproxy, Nginx, commercial F5, Netscaler two keepalived introduction
  • Keepalived is highly available through VRRP (Virtual Router redundancy protocl).
  • In this protocol, multiple routers of the same function will be formed into a group that will have 1 master roles and N (n>=1) backup roles.
  • Master sends the VRRP protocol packets to each backup through multicast, and when backup does not receive the VRRP packets from master, it is considered master down. At this point, you need to decide who will be the new Mater based on the priority of each backup.
  • The keepalived will have three modules, core, check, and VRRP, respectively. The core module is keepalived, responsible for the start of the main process, maintenance and global configuration file loading and parsing, check module is responsible for health check, VRRP module is to implement VRRP protocol. Three keepalived configuration High availability

    Prepare two machines, Ens33 NIC IP for 192.168.226.131 and 192.168.226.132,131 as master,132 as backup
    Both machines are installed keepalived
    Performyum install -y keepalived
    Both machines are pre-installed Nginx, using the source package installation, of course, can be installed directly yum

    yum install -y epel-release yum install -y nginx

    1) operation on Master
    Edit the keepalived configuration file and modify it to the following content

    vim /etc/keepalived/keepalived.confglobal_defs {notification_email { [email protected]   //接收通知邮件的邮箱}notification_email_from [email protected]   //发送通知邮件的邮箱smtp_server 127.0.0.1smtp_connect_timeout 30router_id LVS_DEVEL}vrrp_script chk_nginx {script "/usr/local/sbin/check_ng.sh"  // 定义检查nginx运行状况的脚本interval 3}vrrp_instance VI_1 {state MASTERinterface ens33    // 定义vip 绑定的网卡virtual_router_id 51 priority 100  // vrrp 协议中各角色的权重advert_int 1authentication {    auth_type PASS    auth_pass lvlinux}virtual_ipaddress {    192.168.226.100   // 定义vip}track_script {    chk_nginx}}

    Create Nginx Check Script

    vim /usr/local/sbin/check_ng.sh#!/bin/bash#时间变量,用于记录日志d=`date --date today +%Y%m%d_%H:%M:%S`#计算nginx进程数量n=`ps -C nginx --no-heading|wc -l`#如果进程为0,则启动nginx,并且再次检测nginx进程数量,#如果还为0,说明nginx无法启动,此时需要关闭keepalivedif [ $n -eq "0" ]; then    /etc/init.d/nginx start    n2=`ps -C nginx --no-heading|wc -l`    if [ $n2 -eq "0"  ]; then            echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log            systemctl stop keepalived    fifi

    Note If it is a Yum installation, the Nginx Start command in the script changes tosystemctl start nginx
    Change Script Permissions
    chmod 755 /usr/local/sbin/check_ng.sh
    When the above operation is complete, start the keepalived service
    systemctl start keepalived
    2) operation on backup
    Basically consistent with the Lord's operation, edit the keepalived configuration file and create an nginx check script.
    The difference is that the keepalived configuration file to make the corresponding changes, the state by the master should be prepared, the weight of a bit. Only part of the code is attached

    state BACKUPinterface ens33virtual_router_id 51priority 90

    Also create Nginx check script after giving permission and then start the keepalived service
    Quad Test High Availability
    1 First look at the VIP tied to where, the two machines on the implementation of IP addr, found VIP tied to the Lord

    2 Turn off Nginx service on Master

    After turning off Nginx, PS viewing process found that Nginx is still in, this is because keepalived called Nginx Check script, and it started up, if the nginx problem, unable to start, the error message will be output to the script defined in the log file, while the main backup switch occurs.
    3 intentionally edit the configuration file of the master Nginx error, and then close the Nginx service. See what's going to change again
    Found the VIP bound to the standby machine

View the script error log of the Lord, and the system log from the
192.168.226.131:cat /var/log/check_ng.log

192.168.226.132:less /var/log/messages

Can clearly see the Lord Nginx failed to boot, the standby machine takes over the master state.
4 Last access test, editor of the Lord's Nginx default page content is Hello World, I'm from Master
From top to Hello World, I'm from backup
In Lord Curl Access VIP
Curl 192.168.226.100, turn off keepalived access again

Note, in the check script, we set, when the discovery Nginx can not start, output error message to the log, and then will stop the downtime of the keepalived service, to avoid it continue to control master.

Linux Learning Summary (52) keepalived Configuration High Availability

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.