用途
Kubernetes Operations 是一个面向 Kubernetes 日常运维的技能,资料涵盖部署清单、Helm Chart、Operator、故障排查与资源管理等主题。其内容以可直接参考的 YAML 示例和 kubectl 命令为主,用于编写、审查和调试 Kubernetes 工作负载配置。
主要能力
部署清单(Deployment Manifest)
资料给出一个完整的 Deployment 示例,包含:
- 元数据:name 为 api-server,标签 app: api-server、version: v1。
- 副本与更新策略:replicas: 3;strategy.type 为 RollingUpdate,maxSurge: 1,maxUnavailable: 0。
- 选择器与模板标签:selector.matchLabels 与 template.metadata.labels 均为 app: api-server。
- 容器配置:镜像 registry.example.com/api:1.2.0,端口 containerPort: 8080。
- 资源:requests 为 cpu 100m、memory 128Mi;limits 为 cpu 500m、memory 512Mi。
- 探针:livenessProbe 使用 httpGet /healthz:8080,initialDelaySeconds 10、periodSeconds 15;readinessProbe 使用 httpGet /ready:8080,initialDelaySeconds 5、periodSeconds 5。
- 环境变量:DATABASE_URL 通过 secretKeyRef 引用 db-credentials 的 url 键。
- 拓扑分布约束:maxSkew 1,topologyKey 为 kubernetes.io/hostname,whenUnsatisfiable 为 DoNotSchedule,labelSelector 匹配 app: api-server。
资料强调:始终设置资源 requests 和 limits;使用拓扑分布约束实现高可用。
Helm Chart 结构
资料给出 Chart 目录结构:
chart/
Chart.yaml
values.yaml
values-staging.yaml
values-production.yaml
templates/
deployment.yaml
service.yaml
ingress.yaml
hpa.yaml
_helpers.tpl
并给出 values.yaml 示例,包含 replicaCount: 2;image 的 repository、tag "1.2.0"、pullPolicy IfNotPresent;resources 的 requests(cpu 100m、memory 128Mi)与 limits(cpu 500m、memory 512Mi);autoscaling 的 enabled: true、minReplicas: 2、maxReplicas: 10、targetCPUUtilization: 70。
HorizontalPodAutoscaler
资料给出 autoscaling/v2 的 HPA 示例:scaleTargetRef 指向 Deployment api-server;minReplicas 2、maxReplicas 10;指标包含 CPU(Utilization,averageUtilization 70)与内存(Utilization,averageUtilization 80);behavior.scaleDown.stabilizationWindowSeconds 为 300。
故障排查命令
资料列出以下 kubectl 命令:
- Pod 诊断:
kubectl describe pod <pod-name> -n <namespace>;kubectl logs <pod-name> -c <container> --previous;kubectl exec -it <pod-name> -- /bin/sh。 - 资源使用:
kubectl top pods -n <namespace> --sort-by=memory;kubectl top nodes。 - 网络调试:
kubectl run debug --image=nicolaka/netshoot --rm -it -- bash;nslookup <service-name>.<namespace>.svc.cluster.local。 - 事件按时间排序:
kubectl get events -n <namespace> --sort-by='.lastTimestamp'。 - 查找非 Running 的 Pod:
kubectl get pods -A --field-selector=status.phase!=Running。
反模式
资料列出的反模式包括:
- 容器以 root 运行,未设置 securityContext.runAsNonRoot: true。
- 缺少资源 requests/limits(导致调度问题和 noisy neighbors)。
- 使用 latest 标签而非固定镜像版本。
- 未为关键工作负载设置 PodDisruptionBudget。
- 将密钥存放在 ConfigMap 而非 Secret(或外部密钥管理器)。
- 对有副本的部署忽略 Pod 反亲和性。
检查清单
资料给出的检查项:
- 所有容器都有资源 requests 和 limits。
- 配置了存活与就绪探针。
- 镜像使用具体版本标签,而非 latest。
- 密钥存储在 Kubernetes Secrets 或外部 vault。
- 生产工作负载设置了 PodDisruptionBudget。
- NetworkPolicies 限制命名空间之间的流量。
- 使用拓扑分布约束或反亲和性实现高可用。
- Helm values 按环境拆分(staging、production)。
适用场景
适合在编写或审查 Kubernetes Deployment、构建 Helm Chart、配置 HPA 自动扩缩容、排查 Pod 与网络问题、以及制定上线前检查清单时参考。
限制
资料未提供该技能的安装方式、运行环境要求、收费信息或具体平台支持范围,这些内容暂未提供。