How to create a Deployment manifest?

· Category: Kubernetes

Short answer

A Deployment manifest is a YAML file that defines the desired state of a stateless application. It specifies the number of replicas, the Pod template, and update strategies.

Steps

  1. Set apiVersion: apps/v1 and kind: Deployment.
  2. Define metadata including name and namespace.
  3. Set spec.replicas.
  4. Define spec.selector.matchLabels.
  5. Write the spec.template for the Pods.

Example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
      - name: api
        image: myapi:v1.0
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "256Mi"
            cpu: "200m"

Tips

  • Always set resource requests and limits.
  • Use revisionHistoryLimit to cap stored revisions.
  • Label your Pods consistently with the selector.

Common issues

  • Mismatched labels between selector and template cause the Deployment to create no Pods.
  • Missing apiVersion or kind results in validation errors.
  • Container image pull failures leave Pods in ImagePullBackOff.