Skip to content

概念关系速查 ​

一句话:先判断眼前的是 API object、主动 actor 还是 data plane,再问“谁观察它、谁写它、它选择或引用谁、生命周期由谁决定”。

Namespace 是大多数应用对象的隔离边界,但 Node、PersistentVolume、StorageClass、ClusterRole、ClusterRoleBinding 等是 cluster-scoped。namespaced 对象只能直接引用 API 允许范围内的对象;label selector 是动态集合关系,不等于 ownership。

主关系图 ​

flowchart TD
  subgraph OBJECTS["API objects stored through API Server"]
    D["Deployment API object"]
    RS["ReplicaSet API object"]
    P["Pod API object"]
    S["Service API object"]
    ES["EndpointSlice API object"]
    IG["Ingress or Gateway API object"]
    CM["ConfigMap or Secret API object"]
    PVC["PersistentVolumeClaim API object"]
    HPA["HorizontalPodAutoscaler API object"]
  end
  subgraph ACTORS["actors running control loops"]
    DC["Deployment controller actor"]
    RC["ReplicaSet controller actor"]
    EC["EndpointSlice controller actor"]
    IC["Ingress or Gateway controller actor"]
    SPA["service proxy agent actor"]
    HC["HPA controller actor"]
    KL["kubelet actor"]
    CR["container runtime actor"]
  end
  subgraph PLANE["data plane"]
    PX["managed proxy or gateway data plane"]
    SD["optional Service data plane"]
    C["container process"]
    EP["ready endpoint"]
  end

  D -.->|is watched by 被观察| DC
  DC -->|creates or updates 创建或更新| RS
  RS -.->|is watched by 被观察| RC
  RC -->|creates deletes or adopts 创建删除或收养| P
  S -->|selects 选择 Pods by labels| P
  P -->|references 引用| CM
  P -->|references 引用 claim| PVC
  S -.->|is watched by 被观察| EC
  P -.->|is watched by 被观察| EC
  EC -->|creates or updates 创建或更新| ES
  IG -.->|is watched by 被观察| IC
  S -.->|is watched by 被观察| IC
  ES -.->|is watched by 被观察| IC
  IC -->|configures 配置| PX
  S -.->|is watched by 被观察| SPA
  ES -.->|is watched by 被观察| SPA
  SPA -->|configures 配置| SD
  PX -->|forwards 转发| EP
  SD -->|forwards 转发| EP
  HPA -.->|is watched by 被观察| HC
  HC -->|updates scale subresource 更新伸缩子资源| D
  P -.->|is watched by assigned kubelet 被分配节点的 kubelet 观察| KL
  KL -->|invokes 调用| CR
  CR -->|starts 启动| C

  classDef api fill:#eef6f2,stroke:#28755d,color:#1f2933
  classDef actor fill:#fff3d6,stroke:#8a6500,color:#2d2a22
  classDef plane fill:#eaf1fb,stroke:#315e91,color:#1f2933
  class D,RS,P,S,ES,IG,CM,PVC,HPA api
  class DC,RC,EC,IC,SPA,HC,KL,CR actor
  class PX,SD,C,EP plane

图中的写操作表示 actor 通过 API Server 管理 API object,不是一个资源对象主动调用另一个资源;ReplicaSet controller 对 Pods 的动作是 creates、deletes or adopts,通常不能原地更新 Pod spec。selects 是 label selector 计算出的动态集合,references 是 spec 中按名称或类型保存的引用。Service/EndpointSlice metadata 先被具体实现的 service proxy agent 或 Ingress/Gateway controller 观察,actor 再配置所管理的数据面;proxy/gateway 或可选 Service data plane 才 forwards 请求。图不限定 agent 是 kube-proxy、eBPF controller 还是其他实现。

对象关系表 ​

对象作用域谁创建或管理它选择或引用什么生命周期主要命令
PodNamespace用户或 workload controller 创建;scheduler 绑定;kubelet 报状态引用 ServiceAccount、ConfigMap、Secret、PVC;labels 被其他对象选择UID 对应一次 Pod;owner 可重建替代 Podkubectl get pod、kubectl describe pod
DeploymentNamespace用户创建;Deployment controller 管理 rolloutselector 选择自己的 Pod template labels;拥有 ReplicaSet独立存在;删除时通常级联所属 ReplicaSet/Podkubectl rollout status deployment
ReplicaSetNamespace通常由 Deployment controller 创建;ReplicaSet controller 调副本selector 选择 Pods;常由 Deployment ownerReference 拥有通常随 Deployment revision 保留或被回收kubectl get replicaset
StatefulSetNamespace用户创建;StatefulSet controller 管理selector、Service name、Pod template、volumeClaimTemplatesPod 身份稳定;PVC 保留行为需单独规划kubectl rollout status statefulset
DaemonSetNamespace用户创建;DaemonSet controller 管理selector 与符合放置约束的 Nodes随合格 Node 集合变化创建/删除 Podkubectl rollout status daemonset
Job / CronJobNamespace用户创建;各自 controller 创建 Pod 或 JobPod/Job template;ownerReferences 串联完成后对象仍可保留,受 history/TTL 策略清理kubectl get job,cronjob
ServiceNamespace用户创建;Service controller 可能管理外部负载均衡selector 动态选择同 Namespace Pods;ports 引用目标端口独立于 Pods;ClusterIP 通常伴随对象kubectl get service
EndpointSliceNamespaceselector Service 通常由 EndpointSlice controller 管理;selectorless Service 可由用户/外部 controller 管理以 label 关联 Service;endpoints 引用地址和可选 targetRef后端或 Service 变化时被调谐;不要手改受管 slicekubectl get endpointslice
IngressNamespace用户创建;Ingress controller 观察并写 status引用 IngressClass、Service 名称与端口、TLS Secret配置对象独立;删除后 controller 应撤销数据面配置kubectl describe ingress
Gateway / HTTPRouteNamespace用户创建;Gateway controller 观察并写 conditionsGateway 引用 GatewayClass;Route 用 parentRefs/backendRefsRoute 与 Gateway 独立,可分别附加或删除kubectl get gateway,httproute
NetworkPolicyNamespace用户创建;支持它的 CNI/data plane 执行podSelector 选 Pods;规则可选 Namespace/Pod/IP/port独立策略;删除即撤销该策略贡献的允许规则kubectl describe networkpolicy
ConfigMap / SecretNamespace用户、operator 或 controller 创建被 Pod env、envFrom、volume 等按名称引用独立对象;更新传播方式取决于消费方式kubectl get configmap,secret
PersistentVolumeClaimNamespace用户或 StatefulSet controller 创建;storage controllers 绑定引用 StorageClass;Pod volume 引用 claim 名称独立于引用 Pod;删除后 PV 行为取决于 reclaim policykubectl describe pvc
PersistentVolumeClusterstatic admin 或 external-provisioner 创建;storage controllers 管理绑定claimRef 指向一个 PVC;引用 CSI/backend volume独立对象;Released 后按 Retain/Delete 等策略处理kubectl get pv
StorageClassCluster集群管理员创建;provisioner 消费指定 provisioner、parameters、binding/reclaim policy独立配置;删除不等于删除既有 PVkubectl get storageclass
ServiceAccountNamespace用户或 Namespace 初始化创建;token controller/issuer 提供凭据机制Pod spec 按名称引用;RBAC binding 把权限授予它独立身份;删除会使后续引用失效kubectl get serviceaccount
Role / ClusterRoleNamespace / Cluster用户或平台 controller 创建rules 引用 API groups/resources/verbs;ClusterRole 可聚合独立权限集合,本身不授予任何主体kubectl describe role、kubectl describe clusterrole
RoleBinding / ClusterRoleBindingNamespace / Cluster用户或平台 controller 创建subjects 引用用户、组、ServiceAccount;roleRef 引用 Role/ClusterRole独立授权关系;roleRef 不可变kubectl auth can-i
HorizontalPodAutoscalerNamespace用户创建;HPA controller 更新 status 与目标 scalescaleTargetRef 引用 scalable workload;metrics 引用指标源独立对象;删除后停止自动写 replicaskubectl describe hpa
VerticalPodAutoscalerNamespace安装 VPA CRD 后由用户创建;VPA components 管理 recommendationtargetRef 引用 workload;policy 选择 containers/resources附加组件对象;删除后不自动还原已改 requestskubectl describe verticalpodautoscaler
PodDisruptionBudgetNamespace用户创建;Eviction API 在 voluntary disruption 时读取selector 选择 Pods;定义 minAvailable 或 maxUnavailable独立预算;没有 controller ownership 关系kubectl describe pdb
NodeClusterkubelet 注册或平台创建;node controller 管状态Pod 通过 nodeName 绑定;labels/taints 被调度约束使用对应一个注册节点身份,不由 workload 拥有kubectl describe node

容易混淆的四种关系 ​

关系保存在哪里示例删除含义
ownerReferencechild metadataReplicaSet 拥有 Podowner 删除时 garbage collector 可级联 child
label selectorselector 所在对象 specService 选择 Pods删除 selector 对象不等于删除被选 Pods
name referenceconsumer specPod 引用 PVC/Secret删除 consumer 通常不删除被引用对象
controller watchactor 的控制循环HPA controller 观察 HPA 与 metricsactor 停止会令调谐停滞,对象仍可存在

scope 会限制引用方式:RoleBinding 只能在自身 Namespace 授权,但可 roleRef 一个 ClusterRole;ClusterRoleBinding 才是 cluster-wide。Service selector 与 NetworkPolicy podSelector 都只在策略对象所在 Namespace 内选择 Pods。PV 是 cluster-scoped,却通过 claimRef 绑定 namespaced PVC,这是 API 明确定义的跨 scope 关系,不应类推到任意对象。

从命令回到责任边界 ​

bash
NS=${NS:-demo}
kubectl api-resources --namespaced=true
kubectl api-resources --namespaced=false
kubectl -n "$NS" get deployment,replicaset,pod,service,endpointslice,hpa,pdb
kubectl -n "$NS" get pod -o custom-columns='NAME:.metadata.name,OWNER_KIND:.metadata.ownerReferences[0].kind,OWNER_NAME:.metadata.ownerReferences[0].name,NODE:.spec.nodeName'
kubectl -n "$NS" auth can-i --list

看到 status 不推进时,先找负责写该 status 或下游对象的 actor;看到流量失败时,再把 API configuration 与真正转发流量的 data plane 分开。资源“存在”只证明 API 保存了配置,不证明 controller 已观察、data plane 已应用或应用已经 Ready。

继续阅读 ​

前置:系统化排障。回到概念总览,或按表中的主要命令从具体对象重新进入对应章节。