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

Kubernetes 基础

用最少概念串起 Kubernetes 的对象、控制器与工作流。

Kubernetes 的核心思路很简单:把“期望状态”写进 YAML,控制器会不断把现实拉回到这个状态。

四类核心对象

  • Pod:最小调度单元
  • Deployment:管理副本与滚动更新
  • Service:稳定访问入口
  • Namespace:逻辑隔离边界

声明式模型

  • 你描述“要什么”
  • 控制器负责“怎么做到”
  • 同一份 YAML 可以重复 apply,不会破坏一致性

最小 Deployment 示例

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
spec:
  replicas: 2
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
        - name: web
          image: nginx:1.25
          ports:
            - containerPort: 80

常用命令

  • kubectl apply -f hello.yaml
  • kubectl get deploy,pods
  • kubectl describe deploy hello
  • kubectl logs deploy/hello

常见误区

  • 忘记 selector 与 labels 对齐
  • 只改 YAML,不观察 rollout 状态
  • 把 Service 当成“反向代理”来理解

实操要点

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

快速检查清单

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

参考链接