keepalived Introduction, high-availability configuration, Linux cluster introduction;

Source: Internet
Author: User

Linux Cluster

Classification (by Function)

1. High availability: Usually two servers, one work, the other is redundant; when the host is down, redundancy provides service; The software is: Heartbeat, keepalived;
2. Load balancer: One makes the dispatcher, distributes the task to other machines, can be used as a dispatch, 1+n service, Software: LVS, keepalived, Haproxy, Nginx;

keepalived Introduction

Keepalived is highly available through the VRRP protocol, where multiple routers of the same function are formed into a group, one of which master,n a backup role;
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 master based on the priority of each backup.

Three modules

1. Core: Kernel module, mainly responsible for the main process startup, maintenance and global configuration file loading and parsing;
2. Check: Responsible for health checkup;
3. VRRP: Implementation of the VRRP Agreement;

keepalived Configuration

Master machine: 192.168.188.2
Backup machine: 192.168.188.3
VIP Public ip:192.168.188.188
Group id:51

Installing keepalived
yum install -y keepalived
Master machine settings
vim /etc/keepalived/keepalived.conf            //编辑配置
global_defs {//Definition error sent to specified message notification_email {[email protected ]} Notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout router_id LVS_D Evel}vrrp_script Chk_nginx {//define Health Check shell, the shell specified here is to create script manually "/usr/local/sbin/c heck_ng.sh "Interval 3}vrrp_instance vi_1 {state master//set to Master I                                Nterface ENS33//Specify network card, here try Ens33, some eth0 (according to their own situation change) virtual_router_id 51 Define the group ID, must be consistent with priority 100//precedence 100, the greater the priority the higher the priority advert_i                                               NT 1 Authentication {//define authentication form Auth_type PASS    Authentication form is pass password Authentication auth_pass testlinux123//define password as testlinux123 } virtual_ipaddress{192.168.188.188//define the IP of the VIP, that is, an IP that is common to all servers} track_script { Load health Check script Chk_nginx}}
vim /usr/local/sbin/check_ng.sh            //创建健康检查的shell
#!/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 startn2=`ps -C nginx --no-heading|wc -l`if [ $n2 -eq "0" ]; thenecho "$d nginx down,keepalived will stop" >> /var/log/check_ng.logsystemctl stop keepalivedfifi
chmod 755 /usr/local/sbin/check_ng.sh        //给shell文件755权限systemctl start keepalived                //启动keepalived服务
Backup settings
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"    interval 3}vrrp_instance VI_1 {    state BACKUP                                                //定义backup    interface ens33                                            //定义网卡    virtual_router_id 51                                    //和master一样的id    priority 90                                                    //定义优先级,要比master的小,越小越不优先    advert_int 1    authentication {        auth_type PASS        auth_pass testlinux123    }    virtual_ipaddress {        192.168.188.188    }    track_script {        chk_nginx    }}
vim /usr/local/sbin/check_ng.sh            //创建健康检查的shell
#时间变量,用于记录日志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" ]; thensystemctl start nginxn2=`ps -C nginx --no-heading|wc -l`if [ $n2 -eq "0" ]; thenecho "$d nginx down,keepalived will stop" >> /var/log/check_ng.logsystemctl stop keepalivedfifi
chmod 755 /usr/local/sbin/check_ng.sh        //给shell文件755权限systemctl start keepalived                //启动keepalived服务

Log view

less /var/log/messages

IP view of VIP

ip add

Test
Master close keepalived, Access VIP ip192.168.188.188 for buckup content

systemctl stop keepalivedsystemctl start keepalived

Keepalived Introduction, High availability configuration, Linux cluster introduction;

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.