Kubernetes 扩展点全景指南:30 个你应该知道的扩展机制
Kubernetes 的设计哲学是"一切皆可扩展”。它本身只是一个核心控制面,真正强大的能力都来自周边的扩展点。本文系统梳理 30 个常见扩展机制,每个都配一个简单可上手的例子,方便对照查阅。
一、节点 / 运行时层(7 个)
1. CRI(Container Runtime Interface)
作用:让 kubelet 对接不同的容器运行时,是 Pod 真正"跑起来"的底层接口。
常见实现:containerd、CRI-O、Kata Containers、gVisor。
简单例子:在 kubelet 启动参数中指定运行时 socket:
1
|
--container-runtime-endpoint=unix:///run/containerd/containerd.sock
|
更换运行时对上层 K8s API 完全透明,应用无感知。
2. CNI(Container Network Interface)
作用:负责 Pod 网络——IP 分配、跨节点通信、NetworkPolicy 实现。
常见实现:Calico、Cilium、Flannel、Multus(多网卡)。
简单例子:一行命令安装 Calico,集群所有 Pod 自动获得跨节点可达的 IP:
1
|
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
|
3. CSI(Container Storage Interface)
作用:把任意存储系统对接进 K8s,实现动态卷供应、快照、扩容等。
常见实现:AWS EBS CSI、Ceph-CSI、Longhorn、JuiceFS。
简单例子:定义 StorageClass,应用申请 PVC 时会自动创建底层卷:
1
2
3
4
5
6
7
|
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: ebs.csi.aws.com
parameters:
type: gp3
|
4. Device Plugin
作用:把节点上的特殊硬件(GPU、FPGA、SR-IOV 网卡等)暴露成 K8s 可调度的资源。
简单例子:部署 device-plugin 后,Pod 即可像申请 CPU 一样申请这些资源:
1
2
3
|
resources:
limits:
example.com/fpga: 1
|
调度器会自动把 Pod 分配到拥有该资源的节点上。
5. CDI(Container Device Interface)
作用:CRI 之上的设备注入标准,统一描述如何把设备暴露给容器,逐步替代 device-plugin 中的 hostPath 挂载逻辑。
简单例子:CDI spec 文件描述一个设备:
1
2
3
4
5
6
7
8
9
|
cdiVersion: 0.6.0
kind: vendor.com/device
devices:
- name: dev0
containerEdits:
deviceNodes:
- path: /dev/dri/card0
env:
- VENDOR_DEVICE=0
|
containerd / CRI-O 读到这个文件后会自动注入对应的设备和环境变量。
6. NRI(Node Resource Interface)
作用:在容器创建/启动前后挂钩子,对 OCI spec 做精细化调整(NUMA 绑核、cgroup 微调等)。
简单例子:写一个 NRI 插件,在容器创建前自动设置 CPU 亲和性、内存策略,实现 NUMA 拓扑感知。比 device-plugin 更轻量,适合做底层资源精细化管理。
7. Image Credential Provider
作用:kubelet 拉镜像时动态获取凭证,告别静态 imagePullSecrets。
简单例子:配置 ECR Credential Provider,kubelet 自动用节点 IAM Role 拉私有镜像:
1
2
3
4
5
6
7
8
|
apiVersion: kubelet.config.k8s.io/v1
kind: CredentialProviderConfig
providers:
- name: ecr-credential-provider
matchImages:
- "*.dkr.ecr.*.amazonaws.com"
defaultCacheDuration: "12h"
apiVersion: credentialprovider.kubelet.k8s.io/v1
|
二、API / 调度层(7 个)
8. CRD(Custom Resource Definition)
作用:扩展 K8s API,定义自定义资源类型。是整个扩展生态的基石。
简单例子:定义一个 Database 资源类型:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: databases.example.com
spec:
group: example.com
names:
kind: Database
plural: databases
scope: Namespaced
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
engine: { type: string }
size: { type: string }
|
注册后,kubectl get databases 就成为合法命令。
9. Operator / Controller
作用:把运维知识编码成"控制循环”,监听 CRD 变化并把实际状态调谐到期望状态。
简单例子:cert-manager 监听 Certificate 资源,自动从 Let's Encrypt 申请并续期证书:
1
2
3
4
5
6
7
8
9
10
11
|
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: example-com
spec:
secretName: example-com-tls
issuerRef:
name: letsencrypt-prod
kind: ClusterIssuer
dnsNames:
- example.com
|
提交这个 YAML,几分钟后就会有一个完整的 TLS Secret 出现在集群里。
10. Admission Webhook
作用:在对象写入 etcd 之前拦截、修改或拒绝它。分两种:
- Mutating Webhook:修改对象(如自动注入 sidecar、添加默认 label)
- Validating Webhook:拒绝不合规的对象
简单例子:自动给所有创建的 Pod 加上一个 team label:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: add-team-label
webhooks:
- name: team.example.com
rules:
- operations: ["CREATE"]
apiGroups: [""]
apiVersions: ["v1"]
resources: ["pods"]
clientConfig:
service:
name: webhook-svc
namespace: default
path: "/mutate"
admissionReviewVersions: ["v1"]
sideEffects: None
|
11. Aggregated API Server
作用:把外部 API 注册到 kube-apiserver 下,对用户来说就像原生 K8s API。
简单例子:metrics-server 通过 APIService 注册了 /apis/metrics.k8s.io,于是 kubectl top node 才能正常工作:
1
2
3
4
5
6
7
8
9
10
11
12
|
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
name: v1beta1.metrics.k8s.io
spec:
service:
name: metrics-server
namespace: kube-system
group: metrics.k8s.io
version: v1beta1
groupPriorityMinimum: 100
versionPriority: 100
|
12. Scheduler Framework Plugin
作用:通过插件方式扩展默认调度器的过滤、打分、绑定逻辑。
简单例子:写一个 PreFilter 插件,过滤掉磁盘剩余空间小于 20GB 的节点。如果完全自定义,也可以跑多个调度器实例(见 #28),Pod 通过 spec.schedulerName 指定使用哪个。
13. Authentication / Authorization Webhook
作用:把认证或授权决策外包给外部系统。
简单例子:对接 OIDC,让用户用公司 SSO 登录 kubectl:
1
2
3
4
5
|
# kube-apiserver 启动参数
--oidc-issuer-url=https://sso.example.com/realms/k8s
--oidc-client-id=kubernetes
--oidc-username-claim=email
--oidc-groups-claim=groups
|
用户登录拿到 OIDC token 后,kubectl 用 token 访问集群,权限再由 RBAC 控制。
14. Audit Webhook
作用:把 API server 的审计日志推送到外部系统集中存储和分析。
简单例子:审计 policy 文件指定哪些操作要记录,webhook 配置指定推送地址:
1
2
3
4
5
6
7
8
9
|
# audit-policy.yaml
apiVersion: audit.k8s.io/v1
kind: Policy
rules:
- level: RequestResponse
resources:
- group: ""
resources: ["secrets", "configmaps"]
- level: Metadata
|
apiserver 启动参数加上 --audit-webhook-config-file=...,所有审计事件直送 SIEM。
三、应用 / 流量层(4 个)
15. Ingress Controller
作用:把外部 HTTP/HTTPS 流量路由到集群内 Service。
常见实现:Nginx、Traefik、HAProxy、Envoy。
简单例子:用 Nginx Ingress 暴露一个网站:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: blog
spec:
ingressClassName: nginx
rules:
- host: blog.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: blog-svc
port:
number: 80
|
16. Gateway API
作用:Ingress 的下一代标准,支持 L4/L7、多租户、流量切分等高级特性。
简单例子:HTTPRoute 把流量按 90/10 切到两个版本,做灰度发布:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: blog-route
spec:
parentRefs:
- name: my-gateway
hostnames:
- blog.example.com
rules:
- backendRefs:
- name: blog-v1
port: 80
weight: 90
- name: blog-v2
port: 80
weight: 10
|
新项目建议直接上 Gateway API,不用再绕 Ingress 的各种 annotation 黑魔法。
17. Service Mesh
作用:在 Pod 间注入代理(Sidecar 或 eBPF),统一管理流量、安全、可观测性。
常见实现:Istio、Linkerd、Cilium Service Mesh。
简单例子:给 namespace 打个标签,所有 Pod 自动注入 Envoy sidecar,立即获得 mTLS 加密和细粒度流量控制:
1
|
kubectl label namespace prod istio-injection=enabled
|
18. NetworkPolicy
作用:Pod 间网络访问控制,类似集群内的防火墙规则。由 CNI 实现。
简单例子:只允许带 role=frontend 的 Pod 访问数据库 Pod:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: db-only-from-frontend
namespace: prod
spec:
podSelector:
matchLabels:
app: db
policyTypes: ["Ingress"]
ingress:
- from:
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 5432
|
四、可观测性 / 安全 / 策略层(5 个)
19. Custom Metrics API
作用:让 HPA 根据自定义业务指标弹性伸缩,而不仅是 CPU/内存。
简单例子:Prometheus Adapter 把 http_requests_per_second 暴露给 K8s,HPA 直接消费:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 20
metrics:
- type: Pods
pods:
metric:
name: http_requests_per_second
target:
type: AverageValue
averageValue: "100"
|
20. Logging Agent
作用:以 DaemonSet 形式收集容器日志并发送到中心化系统。
常见实现:Fluent Bit、Fluentd、Vector、Promtail。
简单例子:Fluent Bit 配置,从节点读取所有容器日志发到 Loki:
1
2
3
4
5
6
7
8
9
10
11
12
|
[INPUT]
Name tail
Path /var/log/pods/*/*/*.log
Parser cri
Tag kube.*
[OUTPUT]
Name loki
Match kube.*
Host loki.monitoring.svc
Port 3100
Labels job=fluentbit
|
21. Policy Engine
作用:基于策略的准入控制,比手写 Webhook 更易维护。
常见实现:Kyverno(YAML 写策略)、OPA Gatekeeper(Rego 写策略)。
简单例子:用 Kyverno 强制所有镜像必须来自公司私有仓库:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: registry-restriction
spec:
validationFailureAction: Enforce
rules:
- name: only-internal-registry
match:
any:
- resources:
kinds: ["Pod"]
validate:
message: "镜像必须来自 registry.example.com"
pattern:
spec:
containers:
- image: "registry.example.com/*"
|
22. Pod Security Admission(PSA)
作用:K8s 内置的 Pod 安全准入控制,PSP 的继任者。
简单例子:给 namespace 打标签,强制启用 restricted 安全级别:
1
2
3
|
kubectl label namespace prod \
pod-security.kubernetes.io/enforce=restricted \
pod-security.kubernetes.io/warn=restricted
|
之后该 namespace 内不允许特权容器、必须非 root 运行、必须设置 securityContext 等。
23. Runtime Security
作用:容器运行时异常行为检测,基于 eBPF 或系统调用监控。
常见实现:Falco、Tetragon、Tracee。
简单例子:Falco 规则检测"容器内开 shell"行为并告警:
1
2
3
4
5
6
7
8
|
- rule: Terminal shell in container
desc: 容器内启动了交互式 shell
condition: >
container and shell_procs and proc.tty != 0
output: >
容器内 shell (user=%user.name container=%container.name
image=%container.image.repository)
priority: WARNING
|
发现后可推送到 Slack / PagerDuty。
五、生命周期 / 编排层(4 个)
24. CSI Snapshotter
作用:CSI 的子扩展,为 PVC 做快照、克隆、从快照恢复。
简单例子:给数据库的 PVC 打个快照:
1
2
3
4
5
6
7
8
|
apiVersion: snapshot.storage.k8s.io/v1
kind: VolumeSnapshot
metadata:
name: db-snapshot-20251107
spec:
volumeSnapshotClassName: csi-snapshot
source:
persistentVolumeClaimName: db-data
|
之后可以从这个快照创建新 PVC,用于克隆环境或灾难恢复。
25. Cluster API(CAPI)
作用:用 K8s 的方式声明式管理 K8s 集群本身——用 CRD 描述集群。
简单例子:在管理集群里创建一个 AWS 集群:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
apiVersion: cluster.x-k8s.io/v1beta1
kind: Cluster
metadata:
name: prod-cluster
spec:
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta2
kind: AWSCluster
name: prod-cluster
controlPlaneRef:
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
kind: KubeadmControlPlane
name: prod-cluster-cp
|
管理集群上的 controller 会自动调用 AWS API 创建 EC2、VPC、ELB 等资源,并把 K8s 装上。
26. GitOps Controller
作用:以 Git 仓库为唯一事实源,集群状态自动向 Git 同步。
常见实现:ArgoCD、Flux。
简单例子:让 ArgoCD 监听 Git 仓库并自动部署:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
namespace: argocd
spec:
source:
repoURL: https://github.com/my-org/my-app
path: k8s/
targetRevision: main
destination:
server: https://kubernetes.default.svc
namespace: prod
syncPolicy:
automated:
prune: true
selfHeal: true
|
开发者改代码、提 PR、合并到 main,集群自动更新。回滚 = git revert。
27. Backup / DR
作用:备份 K8s 资源 + PV 数据,支持跨集群迁移。
常见实现:Velero、Kasten K10。
简单例子:用 Velero 创建定时备份,每天凌晨备份所有 namespace 到 S3:
1
2
3
4
|
velero schedule create daily-backup \
--schedule="0 2 * * *" \
--ttl 720h \
--include-namespaces "*"
|
集群挂了可以一条命令恢复到新集群。
六、新兴扩展(3 个)
28. Multiple Schedulers
作用:在同一集群跑多个调度器,Pod 通过 schedulerName 选择使用哪个。
简单例子:跑一个自定义调度器处理特殊业务,普通业务走默认调度器:
1
2
3
4
5
6
7
8
9
|
apiVersion: v1
kind: Pod
metadata:
name: special
spec:
schedulerName: my-custom-scheduler
containers:
- name: app
image: nginx
|
29. Workflow Engine
作用:把复杂任务编排为有依赖关系的 DAG 在 K8s 上运行。
常见实现:Argo Workflows、Tekton。
简单例子:Argo Workflows 定义一个 ETL 流水线:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: etl-
spec:
entrypoint: pipeline
templates:
- name: pipeline
dag:
tasks:
- name: extract
template: run
arguments:
parameters: [{ name: cmd, value: "extract.sh" }]
- name: transform
dependencies: [extract]
template: run
arguments:
parameters: [{ name: cmd, value: "transform.sh" }]
- name: load
dependencies: [transform]
template: run
arguments:
parameters: [{ name: cmd, value: "load.sh" }]
- name: run
inputs:
parameters: [{ name: cmd }]
container:
image: my-etl:latest
command: [sh, -c, "{{inputs.parameters.cmd}}"]
|
30. Dynamic Resource Allocation(DRA)
作用:Device Plugin 的下一代,支持设备共享、跨节点拓扑感知、复杂分配语义。
简单例子:声明一个 ResourceClaim,Pod 引用它来获得设备:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
apiVersion: resource.k8s.io/v1beta1
kind: ResourceClaim
metadata:
name: shared-device
spec:
spec:
devices:
requests:
- name: dev
deviceClassName: my-device
---
apiVersion: v1
kind: Pod
metadata:
name: app
spec:
resourceClaims:
- name: device
resourceClaimName: shared-device
containers:
- name: app
image: alpine
resources:
claims:
- name: device
|
未来会逐步取代 Device Plugin,成为接入特殊设备的标准方式。
总结表格
| # |
扩展点 |
所在层 |
一句话作用 |
典型实现 |
| 1 |
CRI |
节点 |
对接容器运行时 |
containerd / CRI-O |
| 2 |
CNI |
节点 |
Pod 网络 |
Calico / Cilium / Flannel |
| 3 |
CSI |
节点 |
持久化存储 |
EBS CSI / Ceph-CSI / Longhorn |
| 4 |
Device Plugin |
节点 |
暴露特殊硬件资源 |
NVIDIA / SR-IOV / FPGA |
| 5 |
CDI |
节点 |
标准化设备注入 spec |
NVIDIA Toolkit |
| 6 |
NRI |
节点 |
容器创建钩子,精细化资源管理 |
自研插件 |
| 7 |
Image Credential Provider |
节点 |
动态获取镜像拉取凭证 |
ECR / GCR / ACR Provider |
| 8 |
CRD |
API |
定义自定义资源类型 |
一切 Operator 的基础 |
| 9 |
Operator / Controller |
API |
控制循环,运维自动化 |
cert-manager / Prometheus Op |
| 10 |
Admission Webhook |
API |
准入时拦截/修改对象 |
Istio / Kyverno |
| 11 |
Aggregated API Server |
API |
注册外部 API 到 kube-apiserver |
metrics-server |
| 12 |
Scheduler Framework Plugin |
API |
扩展默认调度器逻辑 |
自研插件 |
| 13 |
AuthN / AuthZ Webhook |
API |
认证/授权外包 |
OIDC / Keycloak |
| 14 |
Audit Webhook |
API |
审计日志外送 |
ES / SIEM |
| 15 |
Ingress Controller |
流量 |
外部 HTTP 流量入口 |
Nginx / Traefik |
| 16 |
Gateway API |
流量 |
Ingress 的下一代 |
Istio / Envoy Gateway |
| 17 |
Service Mesh |
流量 |
服务间流量/安全/可观测性 |
Istio / Linkerd / Cilium |
| 18 |
NetworkPolicy |
流量 |
Pod 间访问控制 |
Calico / Cilium |
| 19 |
Custom Metrics API |
可观测 |
HPA 自定义指标源 |
Prometheus Adapter / KEDA |
| 20 |
Logging Agent |
可观测 |
容器日志收集 |
Fluent Bit / Vector |
| 21 |
Policy Engine |
安全 |
策略化准入控制 |
Kyverno / OPA Gatekeeper |
| 22 |
Pod Security Admission |
安全 |
内置 Pod 安全策略 |
K8s 自带 |
| 23 |
Runtime Security |
安全 |
运行时异常行为检测 |
Falco / Tetragon |
| 24 |
CSI Snapshotter |
生命周期 |
PVC 快照与克隆 |
external-snapshotter |
| 25 |
Cluster API |
生命周期 |
用 K8s 管理 K8s 集群 |
CAPI / CAPA / CAPZ |
| 26 |
GitOps Controller |
生命周期 |
Git 为事实源的部署 |
ArgoCD / Flux |
| 27 |
Backup / DR |
生命周期 |
集群备份恢复 |
Velero / Kasten K10 |
| 28 |
Multiple Schedulers |
调度 |
多调度器并存 |
K8s 自带 |
| 29 |
Workflow Engine |
编排 |
DAG 任务编排 |
Argo Workflows / Tekton |
| 30 |
Dynamic Resource Allocation |
节点 |
Device Plugin 的下一代 |
K8s 1.32+ Beta |
结语
K8s 设计哲学就一句话:通过 CRD 定义资源,通过 Controller 协调状态,通过 Webhook 拦截改写。把这三件事吃透,再多新的扩展机制出现都能秒懂。