Cloud Native 3 min read

Generating Kubernetes YAML Manifests for Deployments, Services, and Pods

This guide demonstrates how to use kubectl commands to generate YAML definitions for a Deployment, expose it as a Service, and retrieve the Pod specification, providing complete code snippets and example manifests for each step.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Generating Kubernetes YAML Manifests for Deployments, Services, and Pods

1. Generate a Deployment YAML

Run the following command to create a Deployment manifest for an Nginx container without actually creating the resource:

kubectl create deployment nginx --image=nginx:1.20 --dry-run -o yaml

2. Generate a Service YAML

Expose the Deployment on port 80 (target 8080) as a NodePort Service and output the manifest:

kubectl expose deployment nginx --port=80 --target-port=8080 --type=NodePort --dry-run -o yaml

Note: the --dry-run flag is deprecated; use --dry-run=client instead.

The generated Service manifest includes:

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 8080
    protocol: TCP
status:
  loadBalancer: {}

3. Retrieve a Pod YAML

Obtain the full YAML definition of the Pod created by the Deployment:

kubectl get pod nginx-7fb9867-cjnzj -o yaml

4. Simplified Pod YAML Example

A minimal Pod manifest for Nginx and an additional Tomcat container looks like:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: default
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.20
    imagePullPolicy: IfNotPresent
  - name: tomcat
    image: tomcat
    imagePullPolicy: IfNotPresent

These steps provide a quick way to generate and inspect Kubernetes resource definitions directly from the command line.

DeploymentkubernetesYAMLServicePodkubectl
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.