mcpField
K
kubernetes-operations

kubernetes-operations

github.com/rohitg00云与 DevOps2.8k182 次安装24 周前更新
代码仓库

关于 kubernetes-operations

面向 Kubernetes 运维人员的实践指南,涵盖部署清单、Helm 配置、自动扩缩容和故障排查,并列出常见反模式与生产检查项。

主要功能

  1. 01部署清单与资源配置
  2. 02Helm Chart 环境配置
  3. 03HorizontalPodAutoscaler 设置
  4. 04Pod 与节点诊断命令
  5. 05生产部署检查清单

适用场景

  1. 01规划高可用 Kubernetes 部署
  2. 02按环境组织 Helm 配置
  3. 03排查 Pod 故障与资源用量

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/rohitg00/awesome-claude-code-toolkit --skill kubernetes-operations

安装后在 Claude Code 中输入/kubernetes-operations确认已加载。

详细介绍

Kubernetes 运维

Deployment 清单

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-server
  labels:
    app: api-server
    version: v1
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  selector:
    matchLabels:
      app: api-server
  template:
    metadata:
      labels:
        app: api-server
        version: v1
    spec:
      containers:
        - name: api
          image: registry.example.com/api:1.2.0
          ports:
            - containerPort: 8080
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              cpu: 500m
              memory: 512Mi
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8080
            initialDelaySeconds: 10
            periodSeconds: 15
          readinessProbe:
            httpGet:
              path: /ready
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 5
          env:
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: db-credentials
                  key: url
      topologySpreadConstraints:
        - maxSkew: 1
          topologyKey: kubernetes.io/hostname
          whenUnsatisfiable: DoNotSchedule
          labelSelector:
            matchLabels:
              app: api-server

始终设置资源请求和限制。使用拓扑分布约束来实现高可用性。

Helm 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: registry.example.com/api
  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

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api-server
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-server
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 80
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300

故障排查命令

# Pod diagnostics
kubectl describe pod <pod-name> -n <namespace>
kubectl logs <pod-name> -c <container> --previous
kubectl exec -it <pod-name> -- /bin/sh

# Resource usage
kubectl top pods -n <namespace> --sort-by=memory
kubectl top nodes

# Network debugging
kubectl run debug --image=nicolaka/netshoot --rm -it -- bash
nslookup <service-name>.<namespace>.svc.cluster.local

# Events sorted by time
kubectl get events -n <namespace> --sort-by='.lastTimestamp'

# Find pods not running
kubectl get pods -A --field-selector=status.phase!=Running

反模式

  • 容器以 root 身份运行,且未设置 securityContext.runAsNonRoot: true
  • 缺少资源请求/限制(会导致调度问题和邻居噪声)
  • 使用 latest 标签,而非固定的镜像版本
  • 未为关键工作负载设置 PodDisruptionBudget
  • 将机密信息存储在 ConfigMaps 中,而非 Secrets(或外部机密管理器)
  • 忽略副本部署的 Pod 反亲和性

检查清单

  • 所有容器都设置了资源请求和限制
  • 已配置存活探针和就绪探针
  • 镜像使用特定版本标签,而非 latest
  • 机密信息存储在 Kubernetes Secrets 或外部保险库中
  • 为生产工作负载设置了 PodDisruptionBudget
  • NetworkPolicies 限制命名空间之间的流量
  • 为高可用性设置了拓扑分布约束或反亲和性
  • 按环境拆分 Helm values(staging、production)

评论

支持 Markdown