Home / Configs / Containers / Kubernetes Deployment and Service

Kubernetes Deployment and Service YAML: example and generator

A Deployment and Service YAML for your container image, with replicas, port, resource limits and health checks.

For Kubernetes 1.24 or newer. Check the cluster you use with kubectl version.

A Deployment tells Kubernetes to keep a number of copies of your container running. A Service gives those copies one stable address. Fill in your image and port to get both in one file, then apply it with kubectl.

Create your config file

Type your own values. The file below updates as you type, and nothing you type leaves your browser.

Lowercase letters, numbers and hyphens
For example myuser/myapp:1.0
How many pods
The port your app listens on

deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp
          image: nginx:1.27
          ports:
            - containerPort: 80
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              cpu: 500m
              memory: 256Mi
---
apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  type: ClusterIP
  selector:
    app: myapp
  ports:
    - port: 80
      targetPort: 80

How this setup works

Diagram: kubectl apply, then Deployment, then Service
kubectl apply (sends the file) → Deployment (keeps pods running) → Service (one stable address)

Step-by-step setup

  1. Save the file above as deployment.yaml.
  2. Make sure kubectl points at the right cluster: kubectl config current-context.
  3. Apply it: kubectl apply -f deployment.yaml.
  4. Watch the pods start: kubectl get pods.
  5. Look at the service: kubectl get service myapp.
  6. To try it from your computer: kubectl port-forward service/myapp 8080:80, then open http://localhost:8080.

What the important lines mean

Common errors and how to fix them

ImagePullBackOff or ErrImagePull
Kubernetes cannot download the image. Check the name and tag for typos, that the image exists, and for a private image add an image pull secret. Run kubectl describe pod name for the exact reason.
CrashLoopBackOff
The container starts and then exits. Read its log with kubectl logs pod-name (add --previous for the last crash). A wrong command, missing setting or port clash is common.
Pending forever
No node has enough free CPU or memory, or a volume cannot be attached. kubectl describe pod name shows the scheduling message.
The Service has no endpoints
The Service selector does not match the pod labels. Both use app: myapp here, so check you did not edit one of them.
The LoadBalancer stays at <pending>
Local clusters have no cloud load balancer. Use NodePort, port-forward, or minikube tunnel.

Frequently asked questions

What is the difference between a Pod and a Deployment?
A Pod is one running copy of your containers. A Deployment manages many Pods for you: it keeps the right number running, replaces failed ones and handles updates.
Which Service type should I use?
ClusterIP for traffic inside the cluster, NodePort for quick tests, and LoadBalancer when your cloud provider can create a public load balancer for you.
How do I update the image?
Change the image tag in the file and run kubectl apply again. Kubernetes starts new pods and stops the old ones gradually.

Written for: Kubernetes apps/v1 and v1 APIs · Updated 2026-10-11

Free tools that help

Fix a problem with this setup

More container configs

See all 41 config generators