Kubernetes YAML 清单模板 - 码剑客

可直接改用的 Kubernetes YAML 清单:Deployment 与探针、资源限制、Service 与 Ingress、ConfigMap 与 Secret、PVC 存储、HPA 与 ResourceQuota,覆盖从部署到扩缩容的常用片段。

Kubernetes 1.28+更新于 2026-10-30共 20 条

工作负载

最小可用 Deployment,image 建议固定版本标签而非 latest

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: web
          image: registry.example.com/web:1.0.0
          ports:
            - containerPort: 8080

声明资源请求与上限,避免节点资源被单个 Pod 吃满;requests 直接影响调度

          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              cpu: 500m
              memory: 512Mi

就绪探针决定是否接流量,存活探针失败会重启容器;两者路径应区分

          readinessProbe:
            httpGet:
              path: /healthz
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /healthz
              port: 8080
            initialDelaySeconds: 30
            failureThreshold: 3

滚动更新时先起新 Pod 再停旧 Pod,做到零不可用,但需要额外资源

  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0

定时任务清单,schedule 使用标准 cron 表达式,时区由控制器决定

apiVersion: batch/v1
kind: CronJob
metadata:
  name: report
spec:
  schedule: "0 2 * * *"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: Never
          containers:
            - name: report
              image: registry.example.com/report:1.0.0

Service 与 Ingress

集群内访问入口,selector 必须与 Pod 标签一致,否则 Endpoints 为空

apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080
  type: ClusterIP

通过节点端口暴露服务,nodePort 取值范围 30000-32767

  type: NodePort
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 30080

域名与 TLS 入口,secretName 需提前创建包含证书的 Secret

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web
spec:
  ingressClassName: nginx
  tls:
    - hosts:
        - app.example.com
      secretName: app-tls
  rules:
    - host: app.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web
                port:
                  number: 80

Service 不通时先看 Endpoints 是否有地址,再看 Ingress 是否拿到 ADDRESS

kubectl get endpoints web -n <NS>
kubectl get ingress web -n <NS>

ConfigMap 与 Secret

把配置文件整体写入 ConfigMap,挂载后即为文件

kubectl create configmap app-config --from-file=app.conf -n <NS>

一次性把 ConfigMap 与 Secret 的键注入为环境变量

          envFrom:
            - configMapRef:
                name: app-config
            - secretRef:
                name: app-secret

把 Secret 挂载成文件,适合证书与密钥文件;只读挂载避免容器内篡改

          volumeMounts:
            - name: tls
              mountPath: /etc/tls
              readOnly: true
      volumes:
        - name: tls
          secret:
            secretName: app-tls

stringData 可写明文,提交到仓库前务必改为外部注入

apiVersion: v1
kind: Secret
metadata:
  name: app-secret
stringData:
  password: <PASSWORD>
type: Opaque

Secret 只是 base64 编码,不是加密,需配合 RBAC 与加密存储

存储

申请持久卷,accessModes 决定能否多点挂载

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: app-data
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: standard
  resources:
    requests:
      storage: 10Gi

自定义存储类,Retain 可避免删除 PVC 时连带删除数据

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: ssd
provisioner: kubernetes.io/aws-ebs
reclaimPolicy: Retain
allowVolumeExpansion: true

把 PVC 挂到容器目录;多副本共享写需改用支持 RWX 的存储

          volumeMounts:
            - name: data
              mountPath: /var/lib/app
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: app-data

运维配套

创建命名空间,后续清单统一加 -n <NS>

kubectl create namespace <NS>

限制命名空间总资源,防止单团队占满集群

apiVersion: v1
kind: ResourceQuota
metadata:
  name: team-quota
spec:
  hard:
    requests.cpu: "4"
    requests.memory: 8Gi
    limits.cpu: "8"
    limits.memory: 16Gi

按 CPU 使用率自动扩缩容,前提是容器已声明 requests

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70

用 kustomize 叠加环境差异,避免维护多套完整清单

kubectl apply -k overlays/prod -n <NS>

本网站使用 Cookie 以确保您在我们的网站上获得最佳体验。 隐私政策.