Linux集群,高可用keepalived

18.1- 18.5 集群介绍,高可用,keepalived配置

服务器设置集群的目的分为冗余高可用,以及负载均衡两种作用。高可用指的是high availablity,即一个服务器宕机后另一台服务器可以接手继续为用户提供服务。

负载均衡是load balance,普通的小网站用户量少时可以直接用两台服务器,一台主一台备用。但是当用户访问量很大后,例如腾讯和淘宝京东,单台服务器没法承受这样的访问量,所以就需要有多台服务器分散用户的访问。其中在服务器的最前端就需要有负载均衡服务器去分配用户的访问。

实现高可用的软件有keepalived以及heartbeat,实现负载均衡的软件有LVS,nginx,haproxy,keepalived,商业的有F5以及netscaler。但是商业产品价格昂贵。

keepalived高可用

做高可用时,也可能会有负载均衡的需求。而nginx无论做http server还是负载均衡服务器,都是很好的选择。所以我们以keepalived+nginx为例。

实现这个目的的核心是创建一个虚拟ip,virtual IP,简称vip。使用的协议是VRRP,虚拟路由冗余协议。

环境准备:
master:192.168.27.128 keepalived+nginx
backup:192.168.27.129 keepalived+nginx
VIP: 192.168.27.100

以下为两个主机的返回值
[root@cent03 ~]# curl 192.168.27.128
default_server
[root@cent03 ~]# curl 192.168.27.129
rs 02

master的操作

yum install -y keepalived

[root@cent01 ~]# 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 MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass aminglinux>com
    }
    virtual_ipaddress {
        192.168.27.100
    }
    track_script {
        chk_nginx
    }
}


[root@cent01 ~]# 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无法启动,此时需要关闭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


[root@cent01 ~]# systemctl start keepalived  //启动keepalived会自动启动nginx

backup的操作

yum install -y keepalived

[root@cent02 ~]# 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
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass aminglinux>com
    }
    virtual_ipaddress {
        192.168.27.100
    }
    track_script {
        chk_nginx
    }
}


[root@cent02 ~]# 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无法启动,此时需要关闭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


[root@cent02 ~]# systemctl start keepalived  //启动keepalived

测试failover
因为刚刚配置了虚拟ip,可以先重启两台主机的网络服务。

systemctl restart network

[root@cent03 ~]# curl 192.168.27.100  //说明此时连接的是master
default_server


//关闭主机的keepalived模拟主机宕机
systemctl stop keepalived

[root@cent03 ~]# curl 192.168.27.100  //已经切换到backup
rs 02


[root@cent01 ~]# systemctl start keepalived  //开启master的keepalived

[root@cent03 ~]# curl 192.168.27.100  //又重新显示回到了master机上
default_server

mysql高可用

mysql也可以用keepalived做高可用,但是需要注意的是,需要保证主从的数据一致。才能做高可用。

你可能感兴趣的:(Linux集群,高可用keepalived)