62.Linux cluster, high availability with keepalived configuration

Source: Internet
Author: User

First, Linux cluster overview
  • 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
  • Here we use keepalived to implement a highly available cluster, because heartbeat has some problems on the CENTOS6 that affect the experimental results
  • 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. Iii. Configuring high Availability with keepalived
  • Prepare two machines 133 and 134,133 as master,134 as backup
  • Both machines perform Yum install-y keepalived
    Two machines are installed Nginx, of which 133 have been compiled installed on the nginx,134 need yum installation Nginx:

    yum install -y epel-release yum install -y nginx
  • Set VIP to 100
  • Edit 133 on keepalived configuration file
      >/etc/keepalived/keepalived.conf vim/etc/keepalived/keepalived.conf  
      global_defs {notification_email {[email protected]//mailbox receiving notification mail}notification_email_ From [email protected]//Email notification mail smtp_server 127.0.0.1smtp_connect_timeout 30router_id lvs_devel}vrrp_script            Chk_nginx {Script "/usr/local/sbin/check_ng.sh"//check nginx sh path interval 3}vrrp_instance vi_1 {State MASTER Master, the backup on the Backupinterface ENS33//with which network card communication virtual_router_id 51//Primary and Standby as priority 100//standby than the main Low Advert_int 1authentication {auth_type PASS auth_pass chinantfy>com}virtual_ipaddress {192.168.127.100/ /Set a public Ip}track_script {Chk_nginx}}  
  • 133 Edit Monitoring Script
      vim/usr/local/sbin/check_ng.sh  
      #!/bin/bash# time variable for logging d= ' date-- Date Today +%y%m%d_%h:%m:%s ' #计算nginx进程数量n = ' ps-c nginx--no-heading|wc-l ' #如果进程为0, the Nginx is started, and the number of nginx processes is detected again, #如果还为0, Description Nginx cannot start, need to close keepalivedif [$n-eq "0"]; Then/etc/init.d/nginx start n2= ' ps-c nginx--no-heading|wc-l ' If [$n 2-eq "0"]; Then echo "$d Nginx down,keepalived'll Stop" >>/var/log/check_ng.log systemctl Stop Keepali VED Fifi  

    Give script 755 permissions

      chmod 755/usr/local/sbin/check_ng.shsystemctl start keepalived//13 3 Start service  

    Nginx default profile can be found in
    /usr/local/nginx/conf/vhost/default.conf files

  • 134 Edit configuration file
      >/etc/keepalived/keepalived.conf vim/etc/keepalived/keepalived.conf  
      Global_defs {notification_email {[email protected]//mailbox receiving notification e-mail}notification_email_from [email  protected]//mailbox Smtp_server 127.0.0.1smtp_connect_timeout 30router_id lvs_devel}vrrp_script chk_nginx to send notification messages { Script "/usr/local/sbin/check_ng.sh"//check nginx sh path interval 3}vrrp_instance vi_1 {state BACKUP//Lord for Mas TER, Standby for Backupinterface ENS33//with which network card communication virtual_router_id 51//Master and Standby as priority 90//standby is lower than the main Advert_int 1 Authentication {Auth_type PASS auth_pass chinantfy>com}virtual_ipaddress {192.168.127.100//set a public Ip}tra Ck_script {Chk_nginx}}  
  • 134 Editing the Monitoring 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    systemctl start nginx    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
    chmod 755 /usr/local/sbin/check_ng.sh> /usr/share/nginx/html/index.html     //由于nginx是yum安装,所以默认配置文件路径与133不同vim /usr/share/nginx/html/index.htmlBACKUP BACKUPsystemctl start  keepalived    




    When Master is on, the VIP can be seen in 133 with the IP addr command, and the VIP can be found in 134 when backup is turned on.
    If the switch service fails, check the firewall, iptables or SELinux

Iv. Test High Availability
    • First determine the two machine on the nginx differences, such as can be curl-i to view the Nginx version
    • Test 1: Turn off Nginx service on Master
    • Test 2: Add the Iptabls rule on master
      Iptables-i output-p vrrp-j DROP
    • Test 3: Turn off the keepalived service on Master
    • Test 4: Start the keepalived service on Master

62.Linux cluster, high availability with keepalived configuration

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.