1
0

Kubernetes Flannel工作原理、探针与Helm

2026-11-01
Kubernetes Flannel工作原理、探针与Helm

一、Flannel 工作原理

1. cni0 实现同节点不同 Pod 通信

同一个节点上的 Pod 都插在 cni0 网桥上,通过二层直接互通,无需走 flannel.1 隧道。

2. Flannel 的 DaemonSet 做了什么

Flannel 以 DaemonSet 部署,每个节点一个 Pod,包含两部分:

  1. 初始化容器:拷贝 flannel 程序、拷贝配置文件;
  2. 业务容器:启动 flannel 程序。

flannel 启动后做三件事:

  1. 创建 cni0 和 flannel.1 虚拟设备;
  2. 维护路由表;
  3. 为各节点的 Pod 提供网络。

二、Overlay 与 Underlay 网络

类型含义特点
Underlay(底层网络)物理基础设施看得见摸得着,如服务器
Overlay(覆盖网络)基于底层网络构建的虚拟网络看不见摸不着,如虚拟机

Flannel 属于 Overlay 网络,可抓包验证封装的报文。

三、Flannel 的四种工作模式

模式说明
udp早期模式,性能差,官方已弃用
vxlan把源报文封装为二层报文(借物理网卡转发)做跨主机转发
host-gw把容器网络路由写进宿主机路由表,效率高,但不支持跨网段
Directroutingvxlan 与 host-gw 结合,推荐

1. 切换 vxlan

kube-flannel.yml 的 net-conf.json:

{
  "Network": "10.100.0.0/16",
  "Backend": { "Type": "vxlan" }
}
kubectl delete -f kube-flannel.yml
kubectl apply -f kube-flannel.yml
ip route
# 10.100.0.0/24 via 10.100.0.0 dev flannel.1 onlink

2. 切换 host-gw

{
  "Network": "10.100.0.0/16",
  "Backend": { "Type": "host-gw" }
}
ip route
# 10.100.0.0/24 via 10.0.0.231 dev ens33   (直接走物理网卡)

3. 切换 Directrouting(推荐)

{
  "Network": "10.100.0.0/16",
  "Backend": { "Type": "vxlan", "Directrouting": true }
}

同网段走 host-gw,跨网段走 vxlan。

四、探针概述

1. 三类探针

探针作用失败处理未配置时
livenessProbe周期性检查服务是否存活重启容器(删除旧容器重建)默认 Success
readinessProbe周期性检查服务是否可用/就绪从 svc 的 Endpoints 移除;恢复后重新加入默认 Success
startupProbe启动时一次性检查(1.16+)失败杀死容器,按重启策略重启默认 Success

注意:只要配置了 startupProbe,在它成功之前,liveness 和 readiness 都会被禁用;startupProbe 成功后不再生效。

2. 四种检测方式

方式说明
exec执行命令,按返回值(0 成功,非 0 失败)判断
httpGet发 HTTP 请求,按状态码判断(200 成功,3xx 跳转,4xx/5xx 失败等)
tcpSocket测试 TCP 端口能否连接,类似 telnet / nc
grpc对 grpc 端口发起调用(1.19 引入,1.23.17 仍 alpha,1.24+ beta)

3. 通用参数

参数默认含义
periodSeconds10检测频率,最小 1
failureThreshold3失败次数阈值,成功后重置
successThreshold1成功次数阈值,最小 1
initialDelaySeconds0启动后延迟多久开始检测(期间失败不计数)
timeoutSeconds1单次检测超时,最小 1

五、livenessProbe 案例

1. exec

spec:
  containers:
  - name: c1
    image: registry.cn-hangzhou.aliyuncs.com/demo-k8s/apps:v1
    command: ["/bin/sh", "-c", "touch /tmp/demo-linux-healthy; sleep 20; rm -f /tmp/demo-linux-healthy; sleep 600"]
    livenessProbe:
      exec:
        command: ["cat", "/tmp/demo-linux-healthy"]
      periodSeconds: 1
      failureThreshold: 3
      initialDelaySeconds: 30
      timeoutSeconds: 1

2. httpGet

    livenessProbe:
      httpGet:
        port: 80
        path: /index.html
      failureThreshold: 3
      initialDelaySeconds: 10
      periodSeconds: 1
      timeoutSeconds: 1

3. tcpSocket

    command: ["/bin/sh", "-c", "nginx; sleep 10; nginx -s stop; sleep 600"]
    livenessProbe:
      tcpSocket:
        port: 80
      failureThreshold: 3
      initialDelaySeconds: 20
      periodSeconds: 2
      timeoutSeconds: 1

六、readinessProbe 案例

readiness 失败时 Pod 从 svc 的 Endpoints 移除,恢复后重新加入,因此 curl svc 的 VIP 时会看到流量被摘除。

1. exec

    livenessProbe:
      exec:
        command: ["cat", "/tmp/demo-linux-healthy"]
      failureThreshold: 3
      initialDelaySeconds: 65
      periodSeconds: 1
      timeoutSeconds: 1
    readinessProbe:
      exec:
        command: ["cat", "/tmp/demo-linux-healthy"]
      failureThreshold: 3
      initialDelaySeconds: 5
      periodSeconds: 1
      timeoutSeconds: 1

2. httpGet

    readinessProbe:
      httpGet:
        port: 80
        path: /index.html
      failureThreshold: 3
      initialDelaySeconds: 15
      periodSeconds: 1
      timeoutSeconds: 1

3. tcpSocket

    readinessProbe:
      tcpSocket:
        port: 80
      failureThreshold: 3
      initialDelaySeconds: 0
      periodSeconds: 1
      timeoutSeconds: 1

测试:while true; do curl <svc-VIP>; sleep 0.5; done。

七、startupProbe 案例

用 initContainers 分别准备三个测试页面,三个探针各检测一个页面:

spec:
  initContainers:
  - name: init01
    image: registry.cn-hangzhou.aliyuncs.com/demo-k8s/apps:v1
    volumeMounts:
    - name: data
      mountPath: /demo
    command: ["/bin/sh", "-c", "echo 'liveness probe test page' >> /demo/huozhe.html"]
  - name: init02
    image: registry.cn-hangzhou.aliyuncs.com/demo-k8s/apps:v2
    volumeMounts:
    - name: data
      mountPath: /demo
    command: ["/bin/sh", "-c", "echo 'readiness probe test page' >> /demo/demo.html"]
  - name: init03
    image: registry.cn-hangzhou.aliyuncs.com/demo-k8s/apps:v3
    volumeMounts:
    - name: data
      mountPath: /demo
    command: ["/bin/sh", "-c", "echo 'startup probe test page' >> /demo/start.html"]
  containers:
  - name: c1
    image: registry.cn-hangzhou.aliyuncs.com/demo-k8s/apps:v1
    volumeMounts:
    - name: data
      mountPath: /usr/share/nginx/html
    livenessProbe:
      httpGet: { port: 80, path: /huozhe.html }
    readinessProbe:
      httpGet: { port: 80, path: /demo.html }
    startupProbe:
      httpGet: { port: 80, path: /start.html }
      initialDelaySeconds: 35
      periodSeconds: 3

startupProbe 成功后,readiness 和 liveness 才会开始执行。

八、grpc 探针(新特性)

livenessProbe:
  grpc:
    port: 2379
    # service: health
  failureThreshold: 3
  initialDelaySeconds: 10
  periodSeconds: 1
  timeoutSeconds: 1

在 1.23.17 中仍属 alpha,检测失败只会触发警告事件、不会重启容器;正式使用建议 1.24+(beta)。

九、Pod 优雅终止(lifecycle)

lifecycle 定义容器的生命周期钩子:

  • postStart:容器启动之后执行;
  • preStop:容器停止之前执行;
  • terminationGracePeriodSeconds:优雅终止等待时间,默认 30s,preStop 必须在此时间内结束。
spec:
  terminationGracePeriodSeconds: 3
  containers:
  - name: c1
    image: registry.cn-hangzhou.aliyuncs.com/demo-k8s/apps:v1
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "sleep 60; echo \"postStart at $(date +%F_%T)\" >> /data/postStart.log"]
      preStop:
        exec:
          command: ["/bin/sh", "-c", "sleep 20; echo \"preStop at $(date +%F_%T)\" >> /data/preStop.log"]

十、kubelet 管理 Pod 生命周期全流程

  1. kubelet 调用 CRI(cri-o / containerd / cri-docker,1.24 移除 docker-shim);
  2. 容器运行时调用底层 runc 创建容器;
  3. 先创建 pause 基础架构容器,初始化网络命名空间;
  4. 有初始化容器就先执行初始化容器(创建 Pod 时一次性);
  5. 启动业务容器后调用 postStart(一次性);
  6. 调用 startupProbe(一次性);
  7. 周期性调用 livenessProbe 和 readinessProbe;
  8. 容器退出时调用 preStop,且必须在优雅终止时间内结束。

十一、基于 kuboard 管理 K8s 集群

docker run -d \
  --restart=unless-stopped \
  --name=kuboard \
  -p 8080:80/tcp \
  -p 10081:10081/tcp \
  -e KUBOARD_ENDPOINT="http://10.0.0.233:8080" \
  -e KUBOARD_AGENT_SERVER_TCP_PORT="10081" \
  -v /root/kuboard-data:/data \
  swr.cn-east-2.myhuaweicloud.com/kuboard/kuboard:v3

浏览器访问 http://<主机IP>:8080,默认账号 admin / Kuboard123。

十二、Helm 概述与安装

1. 什么是 Helm

Helm 是 K8s 资源清单的管理工具,类似 Linux 的包管理器(yum / apt)。

核心术语:

术语含义
helm命令行工具,负责 chart 的创建、打包、发布、管理
chart应用描述,一组描述 K8s 资源的文件集合
release基于 chart 的部署实体,chart 运行一次生成一个 release

2. 为什么需要 Helm

  1. 资源清单过多不易整体管理(deploy/ds/rs、cm/secret、pv/pvc/sc 等);
  2. 需要应用的版本管理(发布、回滚到指定版本);
  3. 需要资源清单的高效复用。

3. 版本选择

Helm 有 v2 和 v3。2019 年 11 月发布 v3,相比 v2 删除了 Tiller,重构了大部分代码;v3 允许不同名称空间同名资源。生产推荐使用 v3。

4. 安装

wget https://get.helm.sh/helm-v3.15.3-linux-amd64.tar.gz
tar xf helm-v3.15.3-linux-amd64.tar.gz \
  -C /usr/local/bin/ linux-amd64/helm --strip-components=1

helm version

5. 常用子命令

install(安装)、upgrade(升级)、rollback(回滚)、uninstall(卸载)、list(列出 release)、history(历史)、status(状态)、create(创建 chart)、package(打包)、repo(仓库管理)、search(搜索)、template(本地渲染)、lint(检查)、test(测试)。

自动补全:

helm completion bash > /etc/bash_completion.d/helm
source <(helm completion bash)

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或者给予支持!