PodTemplate
Kubernetes
DevOps
Container Orchestration
Cloud Computing

How to use PodTemplate

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In Kubernetes, a PodTemplate defines a blueprint for creating Pods with a specific configuration. While you rarely create PodTemplate objects on their own, they are embedded inside higher-level resources like Deployments, StatefulSets, DaemonSets, and Jobs. Understanding how PodTemplates work is essential because every workload controller in Kubernetes relies on them to describe what each Pod should look like. This article covers the structure of a PodTemplate, how to write one, and how it fits into broader Kubernetes resources.

What Is a PodTemplate?

A PodTemplate is a specification that contains metadata (like labels) and a Pod spec (containers, volumes, environment variables). When a controller such as a Deployment needs to create a new Pod, it reads the PodTemplate and stamps out a Pod matching that description. Every time the controller scales up or replaces a failed Pod, it uses the same template to ensure consistency.

The standalone PodTemplate resource (kind: PodTemplate) does exist in the Kubernetes API, but it is rarely used directly. Instead, the template is embedded within the spec.template field of controllers.

Structure of a PodTemplate

Here is a standalone PodTemplate manifest.

yaml
1apiVersion: v1
2kind: PodTemplate
3metadata:
4  name: my-pod-template
5  namespace: default
6template:
7  metadata:
8    labels:
9      app: my-app
10      tier: backend
11  spec:
12    containers:
13      - name: my-container
14        image: nginx:1.25
15        ports:
16          - containerPort: 80
17        resources:
18          requests:
19            cpu: "100m"
20            memory: "128Mi"
21          limits:
22            cpu: "250m"
23            memory: "256Mi"
24    restartPolicy: Always

The template.metadata.labels field is critical. Controllers use label selectors to identify which Pods belong to them, so these labels must match the selector defined in the parent resource.

PodTemplate Inside a Deployment

The most common place you will see a PodTemplate is inside a Deployment. The spec.template field is the PodTemplate.

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: web-app
5spec:
6  replicas: 3
7  selector:
8    matchLabels:
9      app: web-app
10  template:
11    metadata:
12      labels:
13        app: web-app
14    spec:
15      containers:
16        - name: web-container
17          image: my-registry/web-app:v2.1
18          ports:
19            - containerPort: 8080
20          env:
21            - name: DATABASE_URL
22              valueFrom:
23                secretKeyRef:
24                  name: db-credentials
25                  key: url
26          readinessProbe:
27            httpGet:
28              path: /healthz
29              port: 8080
30            initialDelaySeconds: 5
31            periodSeconds: 10

When you update the PodTemplate (for example, changing the image tag from v2.1 to v2.2), the Deployment controller triggers a rolling update. It gradually replaces old Pods with new ones that match the updated template.

PodTemplate Inside a Job

Jobs also use PodTemplates to define the Pod that executes a batch task.

yaml
1apiVersion: batch/v1
2kind: Job
3metadata:
4  name: data-migration
5spec:
6  template:
7    spec:
8      containers:
9        - name: migrate
10          image: my-registry/migrate:latest
11          command: ["python", "migrate.py"]
12      restartPolicy: Never
13  backoffLimit: 3

Notice the restartPolicy is set to Never here, which is typical for Jobs. The PodTemplate within a Job describes a one-off execution rather than a long-running service.

Key Fields to Configure

When writing a PodTemplate, these fields deserve particular attention.

Labels and annotations in template.metadata determine how the Pod is identified and discovered by services, selectors, and monitoring tools.

Resource requests and limits under each container spec control CPU and memory allocation. Setting these prevents a single Pod from starving others on the same node.

Probes (readiness, liveness, startup) tell Kubernetes how to check whether the container is healthy and ready to receive traffic.

Volumes and volume mounts let you attach persistent storage, ConfigMaps, or Secrets to your containers.

Environment variables can be set directly or pulled from ConfigMaps and Secrets using valueFrom.

Common Pitfalls

  1. Label mismatch between selector and template. If the labels in spec.template.metadata.labels do not match spec.selector.matchLabels in the parent Deployment or StatefulSet, Kubernetes will reject the manifest with a validation error. Always keep these in sync.
  2. Missing resource limits. Without resource limits, a container can consume all available CPU or memory on a node, causing other Pods to be evicted. Always define at least requests to guarantee minimum resources.
  3. Using latest tag in production. The latest image tag makes it unclear which version is running, and combined with imagePullPolicy: IfNotPresent (the default for named tags), Kubernetes might not pull the newest image. Use explicit version tags for production workloads.
  4. Forgetting restart policies. Deployments require restartPolicy: Always, while Jobs typically use Never or OnFailure. Setting the wrong policy causes unexpected behavior.
  5. Skipping health probes. Without readiness probes, Kubernetes routes traffic to Pods before they are ready to serve requests, leading to errors during deployments and scaling events.

Summary

A PodTemplate is the specification that tells Kubernetes exactly how to create each Pod in a workload. You define it inside Deployments, StatefulSets, DaemonSets, and Jobs. The template includes labels, container images, resource requirements, probes, and volumes. When you update the template, the parent controller handles creating new Pods to match the updated spec. Focus on label consistency, explicit resource limits, health probes, and versioned image tags to keep your workloads reliable.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.