Keepalived高可用

Keepalived 高可用

配置文件存放位置:/usr/share/doc/keepalived/samples
VVRP 虚拟路由冗余协议

组成

LB 集群:Load Balancing,负载均衡集群,平均分配给多个节点
HA 集群:High Availability,高可用集群,保证服务可用
HPC 集群:High Performance Computing,高性能集群

配置

keepalived+LVS+nginx

  1. 各节点时间必须同步:ntp, chrony
  2. 关闭防火墙及 SELinux

同步各节点时间

1#安装ntpdate
2yum -y install ntpdate
3#更改时区
4timedatectl set-timezone 'Asia/Shanghai'
5#查看时间
6timedatectl
7datetime

安装 keepalived

1#安装
2yum -y install keepalived

创建 check_apiserver.sh

1# 创建检测脚本
2vim /etc/keepalived/check_apiserver.sh 
 1#!/bin/bash
 2
 3# VIP 地址(你的 Kubernetes apiserver 将通过这个 VIP 暴露)
 4APISERVER_VIP="10.10.10.68" # 设定你自己的vip
 5APISERVER_PORT="6443"
 6
 7errorExit() {
 8  echo "*** $*" 1>&2
 9  exit 1
10}
11
12# 检查 apiserver 的 /healthz
13curl -sf --max-time 3 https://${APISERVER_VIP}:${APISERVER_PORT}/healthz \
14  -k -o /dev/null || errorExit "API Server Unhealthy"

配置master

1vim /etc/keepalived/keepalived.conf
 1vrrp_script chk_apiserver {
 2    script "/etc/keepalived/check_apiserver.sh"
 3    interval 3
 4    weight -10
 5    fall 3
 6    rise 2
 7}
 8
 9vrrp_instance VI_1 {
10    state MASTER
11    interface eth0 # 改为你实际网关
12    virtual_router_id 51
13    priority 120 #master改为最大值
14    advert_int 1
15
16    authentication {
17        auth_type PASS
18        auth_pass 123456 #改为实际的密码
19    }
20
21    virtual_ipaddress {
22        10.10.10.68/24  #改为vip地址
23    }
24
25    track_script {
26        chk_apiserver
27    }
28}

启动keepalived

1systemctl enable keepalived --now
2systemctl status keepalived
3# 检测vip是否正常
4ip a | grep eth0
5ping 10.10.10.68
6curl -k https://10.10.10.68:6443/healthz

配置 backup

1vim /etc/keepalived/keepalived.conf
 1vrrp_script chk_apiserver {
 2    script "/etc/keepalived/check_apiserver.sh"
 3    interval 3
 4    weight -10
 5    fall 3
 6    rise 2
 7}
 8
 9vrrp_instance VI_1 {
10    state BACKUP                   # master2/master3 上改为 BACKUP
11    interface eth0
12    virtual_router_id 51
13    priority 100                   # master2: 100, master3: 90
14    advert_int 1
15
16    authentication {
17        auth_type PASS
18        auth_pass 123456
19    }
20
21    virtual_ipaddress {
22        10.10.10.68/24
23    }
24
25    track_script {
26        chk_apiserver
27    }
28}

启动keepalived

1systemctl enable keepalived --now
2systemctl status keepalived
3# 检测vip是否正常
4ping 10.10.10.68
5curl -k https://10.10.10.68:6443/healthz