CFN Cloud
Cloud Future New Life
en zh
2025-10-06 · 0 次浏览

声明式对象配置

用 YAML 管理资源,形成可审计的变更流程。

声明式配置的核心是:把资源状态写进文件,然后用 kubectl apply 统一管理。

为什么推荐声明式

  • 变更可追踪(适合 git 管理)
  • 易回滚(用历史版本重放)
  • 结果一致(重复 apply 不会破坏状态)

多对象示例

apiVersion: v1
kind: Namespace
metadata:
  name: demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  namespace: demo
spec:
  replicas: 2
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        - name: api
          image: nginx:1.25

常用流程

kubectl apply -f app.yaml
kubectl diff -f app.yaml
kubectl delete -f app.yaml

小建议

  • 统一用 labels 管理选择器
  • 配置变更前先 kubectl diff

实操要点

  • 先做快速盘点:kubectl get nodeskubectl get pods -Akubectl get events -A
  • 对比“期望状态”和“实际状态”,kubectl describe 往往能解释漂移或失败原因。
  • 名称、Label、Selector 要一致,避免 Service 或控制器找不到 Pod。

快速检查清单

  • 资源定义与业务意图一致。
  • Namespace、权限、镜像与环境匹配。
  • 上线前具备健康探针与可观测日志。

参考链接