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.
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.
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.
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.
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
- Label mismatch between selector and template. If the labels in
spec.template.metadata.labelsdo not matchspec.selector.matchLabelsin the parent Deployment or StatefulSet, Kubernetes will reject the manifest with a validation error. Always keep these in sync. - 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
requeststo guarantee minimum resources. - Using
latesttag in production. Thelatestimage tag makes it unclear which version is running, and combined withimagePullPolicy: IfNotPresent(the default for named tags), Kubernetes might not pull the newest image. Use explicit version tags for production workloads. - Forgetting restart policies. Deployments require
restartPolicy: Always, while Jobs typically useNeverorOnFailure. Setting the wrong policy causes unexpected behavior. - 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
- How to use rolling update to re-pull container image?
- How to use Session Affinity on requests to Kubernetes service?
- How to use skaffold with volumes
- How to use the kubernetes go-client to get the same Pod status info that kubectl gives
- How to use rabbitmqctl to connect to the rabbitmqserver in the docker container?
- How to use sudo inside a docker container?
- How to use SageMaker Estimator for model training and saving
- How to use spot instance with amazon elastic beanstalk?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.