Linux集群

分类(按功能)

1. 高可用:通常为两台服务器,一台工作,另一台做冗余;当主机宕机,冗余接替提供服务;实现软件有:heartbeat、keepalived;
2. 负载均衡:一台做分发器,将任务分发给其他机器;可以当成一台做调度,1+N服务;软件有:LVS、keepalived、haproxy、nginx;

keepalived介绍

keepalived通过VRRP协议来实现高可用,在这个协议里面会将多台功能相同的路由器组成一个组,其中一个Master,N个backup角色;
master会通过组播的形式向各个backup发送VRRP协议的数据包,当backup收不到master发来的VRRP数据包时,就会认为master宕机了。此时就需要根据各个backup的优先级来决定谁成为新的master。

三个模块

1. core:核心模块,主要负责主进程的启动、维护以及全局配置文件的加载与解析;
2. check:负责健康体检;
3. vrrp:实现VRRP协议;

keepalived配置

master机器:192.168.188.2
backup机器:192.168.188.3
vip公共ip:192.168.188.188
组id:51

安装keepalived

yum install -y keepalived

master机器设置

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 {                                    //定义健康检查shell,这里指定的shell要手动创建
    script "/usr/local/sbin/check_ng.sh"
    interval 3
}
vrrp_instance VI_1 {
    state MASTER                                            //设置为master
    interface ens33                                        //指定网卡,这里试ens33,有的是eth0(根据自身情况改变)
    virtual_router_id 51                                //定义组id,必须一致
    priority 100                                            //优先级100,优先级越大越优先
    advert_int 1
    authentication {                                                //定义认证形式
        auth_type PASS                                               //认证形式为pass密码认证
        auth_pass testlinux123                            //定义密码为testlinux123
    }
    virtual_ipaddress {
        192.168.188.188                                //定义vip的ip,也就是所有服务器共用的一个ip
    }
    track_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无法启动,此时需要关闭keepalived
if [ $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
fi
fi
chmod 755 /usr/local/sbin/check_ng.sh        //给shell文件755权限
systemctl start keepalived                //启动keepalived服务

backup设置

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无法启动,此时需要关闭keepalived
if [ $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
fi
fi
chmod 755 /usr/local/sbin/check_ng.sh        //给shell文件755权限
systemctl start keepalived                //启动keepalived服务

日志查看

less /var/log/messages

vip的ip查看

ip add

测试
master关闭keepalived,访问vip的ip192.168.188.188为buckup的内容

systemctl stop keepalived
systemctl start keepalived