# linux 高可用

## keepalived 的介绍

在这里我们使用keepalived来实现高可用集群，因为heartbeat在centos6上有一些问题。

keepalived通过VRRP（Virtual Router Redundancy Protocl 虚拟路由冗余协议）来实现高可用。\
在这个协议里会将多台功能相同的路由器组成一个小组，这个小组里会有1个master角色和N（N>=1）个backup角色。\
master会通过组播的形式向各个backup发送VRRP协议的数据包，当backup收不到master发来的VRRP数据包时，就会认为master宕机了。此时就需要根据各个backup的优先级来决定谁成为新的mater。

Keepalived要有三个模块，分别是core、check和vrrp。其中core模块为keepalived的核心，负责主进程的启动、维护以及全局配置文件的加载和解析，check模块负责健康检查，vrrp模块是来实现VRRP协议的。

## 使用keepalived配置高可用

准备：

* master 192.168.127.128
* slave 192.168.127.129

### master 上

**安装keepalived**

```bash
yum install -y keepalived
```

**安装nginx(作为高可用对象)**

```bash
yum install -y nginx
```

**修改配置文件**

```bash
vim /etc/keepalived/keepalived.conf 

global_defs {
   notification_email {
     xx@qq.com  # 告警邮件
   }
   notification_email_from root@qq.com  # 告警邮件服务器
   smtp_server 127.0.0.1       # smtp 服务器器
   smtp_connect_timeout 30     # 超时时间
   router_id LVS_DEVEL
}

vrrp_script chk_nginx {
    script "/usr/local/sbin/check_ng.sh" # 心跳检测脚本
    interval 3    # 检测的时间间隔，单位s
}

vrrp_instance VI_1 {
    state MASTER      # 角色，MASTER | BACKUP        
    interface ens33   # 使用的网卡
    virtual_router_id 51  # 路由id
    priority 100       # 权重，权重越高优先级越高
    advert_int 1       # 
    authentication {   # 认证
        auth_type PASS # 密码认证
        auth_pass 123456 # 密码
    }
    virtual_ipaddress {
        192.168.127.100   # 共有ip
    }

    track_script {
        chk_nginx      # 检测的脚本
    }
}
```

**配置监控脚本**

```bash
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
        # 这里是nginx服务的启动命令
        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
```

**修改权限**

```bash
chmod 755 /usr/local/sbin/check_ng.sh
```

**启动keepalived**

```bash
systemctl start keepalived
```

查看vip地址

```bash
# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00: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 forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:66:07:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.127.128/24 brd 192.168.127.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.127.100/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::d44c:c57b:11b7:9bd5/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
```

### slave 上

**安装keepalived和nginx**

```bash
yum install -y keepalived
yum install -y nginx
```

**修改配置文件**

```bash
vim /etc/keepalived/keepalived.conf 

global_defs {
   notification_email {
     xx@qq.com
   }
   notification_email_from root@qq.com
   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 123456
    }
    virtual_ipaddress {
        192.168.127.100   # vip 要和master上一致
    }

    track_script {
        chk_nginx
    }
}
```

**配置监控脚本**

```bash
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
        # 这里是nginx服务的启动命令
        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
```

**修改权限**

```bash
chmod 755 /usr/local/sbin/check_ng.sh
```

**启动keepalived**

```bash
systemctl start keepalived
```

### 测试高可用

> 注意：主从上关闭防火墙

修改主从上nginx首页内容

```bash
# 192.168.127.128 上
echo "<h1>192.168.127.128</h1>" > /usr/share/nginx/html/index.html

# 192.168.127.129 上
echo "<h1>192.168.127.129</h1>" > /usr/share/nginx/html/index.html
```

使用curl访问192.168.127.100

```bash
[root@localhost ~]# curl 192.168.127.100
<h1>192.168.127.128</h1>
```

停掉主上的keepalived服务，再访问

```bash
[root@localhost ~]# systemctl stop keepalived
[root@localhost ~]# curl 192.168.127.100
<h1>192.168.127.129</h1>
```

> 请求的数据发生变化，说明从启动了

重新再主上开启keepalived

```bash
[root@localhost ~]# systemctl start keepalived
[root@localhost ~]# curl 192.168.127.100
<h1>192.168.127.128</h1>
```

> 请求返回的数据又变回来了
