Linux clusters: Building high-availability clusters

Source: Internet
Author: User
Tags ranges

First, the introduction of the cluster
  • Divided into two categories according to the function: high availability and load balancing;
  • A highly available cluster is typically two servers, one work, and the other as redundancy, and redundancy will take over as the service continues to provide services when the machine goes down;
  • To achieve high availability of open source software are: heartbeat, keepalived;
  • Load Balancer cluster, need to have a server as a dispatcher, it is responsible for the user's request distributed to the backend server processing, in this cluster, in addition to the Distributor, is to provide services to the user server, the number of these servers is at least 2;
  • The open source software for load Balancing has LVS, keepalived, Haproxy, Nginx, commercial F5, Netscaler.
Ii. Building high-availability clusters with keepalived
  • Here we use keepalived to implement a highly available cluster, because heartbeat has a problem on the CENTOS6 that affects 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.
1, installation keepalived
  • Prepare two machines 128 and 129,130 as master,132 as backup
  • Both machines are executed.yum install -y keepalived
  • Two machines are installed Nginx, of which 128 have been compiled installed on the nginx,129 need yum installation Nginx:yum install -y nginx
  • Set VIP to 100 (virtual IP or floating IP), the server relies on this VIP to provide services
2. Configure Mastera) Edit keepalived Configuration
[[email protected] ~]# > /etc/keepalived/keepalived.conf          //清空配置文件内容[[email protected] ~]# vim /etc/keepalived/keepalived.conf      //添加以下内容global_defs {   notification_email {   [email protected]   } #出现问题时发送邮件   notification_email_from [email protected]   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id LVS_DEVEL}  #上述自定义参数vrrp_script chk_nginx {    script "/usr/local/sbin/check_ng.sh"#   自定义脚本,该脚本为监控Nginx服务的脚本    interval 3}   #检测脚本,检测时间3s一次vrrp_instance VI_1 {    state MASTER    interface ens33    virtual_router_id 51    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass zlinux>com    }       virtual_ipaddress {        192.168.242.100    }#   定义VIP    track_script {        chk_nginx    }   #   定义监控chk_nginx}
b) Define the Monitoring Nginx service script
[[email protected] ~]# 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        
c) Start keepalived service
[[email protected] ~]# systemctl start keepalived[[email protected] ~]# ps aux |        grep keeproot 2739 0.0 0.0 120720 1400?        Ss 13:12 0:00/usr/sbin/keepalived-droot 2740 0.0 0.1 127460 3260?        S 13:12 0:00/usr/sbin/keepalived-droot 2748 0.0 0.1 122792 2380? S 13:12 0:00/usr/sbin/keepalived-droot 2750 0.0 0.0 112680 976 pts/0 r+ 13:12 0:00 grep--color=au To keep[[email protected] ~]# PS aux |        grep nginxroot 865 0.0 0.1 46764 2872?   Ss 10:49 0:00 nginx:master process/usr/local/ngin/sbin/nginx-c/usr/local/nginx/conf/nginx.confnobody 2606 0.0        0.2 48688 4608?        S 12:15 0:00 nginx:worker processnobody 2607 0.0 0.2 48688 4104?  S 12:15 0:00 nginx:worker processroot 2752 0.0 0.0 112680 976 pts/0 r+ 13:12 0:00 grep--color=auto nginx[[email protected] ~]# IP Add//If no 100 is present for this IP, it is possible that SELinux is not turned off 1:lo: <LOOPBACK,UP,LOWER_UP> MTU 65536 qdisc noqueue State UNKNOWN Qlen 1 link/loopback 00:00:00:00:00:00 BRD 00:00:0        0:00:00:00 inet 127.0.0.1/8 Scope host lo valid_lft forever Preferred_lft Forever Inet6:: 1/128 Scope Host Valid_lft Forever preferred_lft forever2:ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> MTU Qdisc pfifo_fast STA  Te up Qlen link/ether 00:0c:29:0c:74:aa brd ff:ff:ff:ff:ff:ff inet 192.168.242.128/24 BRD 192.168.242.255 Scope Global Ens33 Valid_lft Forever Preferred_lft Forever inet 192.168.242.100/32 scope Global ENS33 Valid_lft Forever Preferred_lft Forever Inet6 fe80::e335:85d4:68d:9b01/64 scope link Valid_lft forever preferred_lft forev Er

Here's how the master is done.

2, configuration Backupa) modify the keepalived configuration
[[email protected] ~]# > /etc/keepalived/keepalived.conf          //清空配置文件内容[[email protected] ~]# vim /etc/keepalived/keepalived.conf      //添加以下内容,内容与master基本一直,state和priority有变化global_defs {   notification_email {   [email protected]   } #出现问题时发送邮件   notification_email_from [email protected]   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id LVS_DEVEL}#上述自定义参数vrrp_script chk_nginx {    script "/usr/local/sbin/check_ng.sh"#   自定义脚本,该脚本为监控Nginx服务的脚本    interval 3}#检测脚本,检测时间3s一次vrrp_instance VI_1 {    state BACKUP    interface ens33    virtual_router_id 51    priority 90    advert_int 1    authentication {        auth_type PASS        auth_pass zlinux>com    }    virtual_ipaddress {        192.168.242.100    }#   定义VIP    track_script {        chk_nginx    }#   定义监控chk_nginx}
b) Add Nginx service Monitoring script
[[email protected] ~]# 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[[email protected] ~]# chmod 755 /usr/local/sbin/check_ng.sh [[email protected] ~]# systemctl start keepalived
3. Testing
[[email protected] html]# curl  -I 192.168.242.100HTTP/1.1 200 OKServer: nginx/1.12.2Date: Mon, 09 Apr 2018 07:00:49 GMTContent-Type: text/htmlContent-Length: 13Last-Modified: Mon, 09 Apr 2018 06:16:24 GMTConnection: keep-aliveETag: "5acb0538-d"Accept-Ranges: bytes[[email protected] html]# systemctl stop keepalived              [[email protected] html]# curl  -I 192.168.242.100                //这个就到slave上了HTTP/1.1 200 OKServer: nginx/1.12.2Date: Mon, 09 Apr 2018 07:01:08 GMTContent-Type: text/htmlContent-Length: 15Last-Modified: Mon, 09 Apr 2018 06:21:29 GMTConnection: keep-aliveETag: "5acb0669-f"Accept-Ranges: bytes

Master Normal Case:

The problem with Master occurs:

Linux clusters: Building high-availability clusters

Related Article

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.