How to generate YAML template with kubectl command?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
You can generate Kubernetes YAML with kubectl in two main ways. If you want YAML for a resource that already exists, use kubectl get ... -o yaml. If you want a manifest template without actually creating the resource, use a create command together with --dry-run=client -o yaml.
Those two cases are easy to confuse, but they solve different problems. One exports the current state of an existing resource, while the other generates a manifest from the command you would have run.
Export YAML for an Existing Resource
To fetch the YAML for something already in the cluster:
That prints the resource as Kubernetes currently knows it.
You can redirect it to a file:
This is useful for inspection, debugging, or starting from a live resource as a baseline.
Generate a Manifest Without Creating the Resource
If you want a YAML template from a kubectl create command, use --dry-run=client -o yaml:
That prints the deployment manifest but does not create anything in the cluster.
You can save it directly:
This is the most common answer when people ask how to generate a starter YAML template from kubectl.
Example with a Service
You can do the same for other resource types:
This gives you a starting service manifest that you can edit further before applying it.
Know the Difference Between Generated and Exported YAML
Generated YAML from kubectl create ... --dry-run=client -o yaml is usually cleaner for authoring because it contains only the fields needed for the desired resource.
Exported YAML from kubectl get -o yaml often contains extra cluster-managed metadata such as:
- '
resourceVersion' - '
uid' - '
managedFields' - status sections
Those fields are useful for inspection, but you usually do not want to keep all of them when creating a reusable manifest file.
That is why many people prefer generating a fresh manifest or cleaning an exported one before putting it under version control. It leads to quieter diffs and less accidental drift from server-generated fields. It also makes code review easier for teams using declarative Git-based workflows.
Apply the Generated YAML Later
Once the file looks the way you want, apply it normally:
This is a nice workflow for teams that want to start from kubectl syntax but end with declarative YAML checked into a repository.
Common Pitfalls
- Using
kubectl get -o yamlwhen what you really wanted was a clean starter template. - Forgetting
--dry-run=client, which may create the resource instead of only printing YAML. - Committing exported YAML with cluster-generated metadata that should not be managed manually.
- Assuming every imperative
kubectlcommand supports the same generation pattern without checking the command syntax.
Summary
- Use
kubectl get <resource> <name> -o yamlto export an existing resource. - Use
kubectl create ... --dry-run=client -o yamlto generate a starter manifest without creating anything. - Redirect the output to a file if you want a reusable YAML template.
- Generated YAML is usually cleaner than raw exported YAML for authoring.
- Be careful not to keep cluster-managed metadata in manifests you plan to maintain manually.
Related reading
- How to get a custom healthcheck path in a GCE L7 balancer serving a Kubernetes Ingress?
- How to get a PDF/print version of kubernetes' docs?
- How to get all Kubernetes pod IP in each pods?
- how to get container host machine ip address and container name in EKS
- How to get a list of images on docker registry v2
- How to get additional lines of context in a CloudWatch Insights query?
- How to get current date in Helm
- How to get current namespace from running pod usin client api

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.