# 高可用-Lvs+Keepalived



## 主从模式

### 环境

| IP            | 用途     |
| ------------- | ------ |
| 10.10.181.17  | Master |
| 10.10.181.17  | Slave  |
| 10.10.181.100 | VIP    |

### 初始化

```bash
sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux
setenforce 0
systemctl stop firewalld && systemctl disable firewalld
yum install ntpdate -y
ntpdate time.windows.com
```

### 安装依赖

```bash
yum -y install gcc openssl-devel popt-devel libnl* kernel-devel ipvsadm libnfnetlink libnfnetlink-devel net-snmp-agent-libs wget
```

### 安装keepalived

```bash
wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz
tar xvf keepalived-1.3.5.tar.gz
cd keepalived-1.3.5
./configure --prefix=/usr/local/keepalived/
make && make install


ln -s /usr/local/keepalived/etc/keepalived/ /etc/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/sbin/keepalived /usr/sbin/

/usr/sbin/keepalived
echo '/usr/sbin/keepalived' >> /etc/rc.local

# 查看主是否存在 vrrp ip 地址 
```

### 配置 keepalived

```bash
# 备主机： 修改一下 state、优先级

cat > /etc/keepalived/keepalived.conf << EOF
global_defs {
   notification_email {
     xxxx@xxxxxx
   }
   notification_email_from xxxx@xxxxxx
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}


# 定义脚本
vrrp_script check_nginx {
    script "/usr/local/keepalived/sbin/check_nginx.sh"
    interval 2
    weight 2
 }

vrrp_instance VI_1 {
    # 设置主机状态，MASTER|BACKUP
    state MASTER

    # 对外提供服务的网络接口
    interface ens192

    # 主备服务器要保持一致，VRID标记 ,路由ID，可通过 tcpdump vrrp 查看
    virtual_router_id 100

    # 优先级，高优先级竞选为master
    priority 100

    # 心跳线检验时间
    advert_int 1

    authentication {
        auth_type PASS
        auth_pass 1111
    }

    virtual_ipaddress {
       # VIP地址, 可多个
        10.10.181.100
    }

    # 使用自定义脚本函数
    track_script {
        check_nginx
    }
}


# LVS服务器地址，使用VIP地址
virtual_server 10.10.181.100 80 {
    # 健康检查时间间隔
    delay_loop 6

    # LVS调度算法，rr|wrr|lc|wlc|lblc|sh|dh
    lb_algo rr

    # LVS工作模式，负载均衡转发规则NAT|DR|RUN
    lb_kind DR
    
    # 保持客户端的请求在这个时间段内全部发到同一个真实服务器，单位为秒; 会话保持
    #persistence_timeout 50

    # 使用协议
    protocol TCP

    
    # 后端真实WEB服务器1
    real_server 10.10.181.17 80 {
        weight 1
        TCP_CHECK {
            # 连接超时时间
            connect_timeout 3

            # 健康检查的端口的端口;
            connect_port 80

            # 重连次数
            nb_get_retry 3

            # 重连间隔时间
            delay_before_retry 3
        }
    }

    # 后端真实WEB服务器2
    real_server 10.10.181.18 80 {
        weight 1
        TCP_CHECK   {
            connect_timeout 3
            connect_port 80
            nb_get_retry 3
            delay_before_retry 3
        }
    }      
}

EOF
```

### 检查脚本

```bash
#!/bin/bash

run=$(ps -C nginx --no-header | wc -l)
if [[ $run -eq 0 ]];then
    /etc/rc.d/init.d/nginx start
    sleep 3
fi


# 执行权限
chmod +x /usr/local/keepalived/sbin/check_nginx.sh
```

## 主主模式

> 与主从模式类似，只需要再复制： vrrp\_instance 、virtual\_server 即可，再添加 一个vip,让新添加的vip漂移到主从的从服务器上，然后反过来定义master，slave， 主主ok
