Learnitweb

Kubernetes Pod YAML Manifest

1. Introduction to Pods

In Kubernetes, a Pod is the smallest and most basic deployable unit. It represents a single instance of a running process in a cluster. A Pod can contain one or more containers, which:

  • Share the same network namespace (same IP address and port space).
  • Can share storage volumes.
  • Are scheduled together on the same node.

Pods are ephemeral – they can be stopped, deleted, or replaced by Kubernetes as needed. They are typically managed by higher-level controllers like Deployments or StatefulSets, but can also be created directly.

2. Structure of a Pod Manifest (YAML Format)

Below is the basic syntax of a Pod manifest in YAML:

apiVersion: v1
kind: Pod
metadata:
  name: pod-nginx
spec:
  containers:
    - name: nginx
      image: nginx:1.14.2
      ports:
        - containerPort: 80
          protocol: TCP

Explanation of Fields:

FieldDescription
apiVersionDefines the version of Kubernetes API being used. For Pods, it’s always v1.
kindSpecifies the type of object being created. Here, it is Pod.
metadataMetadata about the Pod, including its name, optional labels, and namespace.
specThe desired state of the Pod. Includes container details, volumes, restart policies, etc.
containersA list of containers running inside the Pod. Each container has its own name, image, ports, and other settings.

3. YAML vs JSON Representations

Kubernetes manifests can be written in either YAML or JSON. YAML is more human-readable and is the preferred format.

YAML Example:

apiVersion: v1
kind: Pod
metadata:
  name: pod-nginx

Equivalent JSON:

{
  "apiVersion": "v1",
  "kind": "Pod",
  "metadata": {
    "name": "pod-nginx"
  }
}

4. Lists and Maps in Pod YAML

YAML structures include:

A. List

A sequence of items indicated with a hyphen -, equivalent to a JSON array.

YAML Example:

args:
  - "HOSTNAME"
  - "PORT_NUMBER"
  - "9900"

JSON Equivalent:

"args": [ "HOSTNAME", "PORT_NUMBER", "9900" ]

B. List of Maps

A list containing map structures (key-value pairs), equivalent to an array of objects in JSON.

YAML Example:

containers:
  - name: nginx-container
    image: nginx
    volumeMounts:
      - name: shared-data
        mountPath: /usr/share/nginx/html
  - name: debian-container
    image: debian
    volumeMounts:
      - name: shared-data
        mountPath: /pod-data

JSON Equivalent:

{
  "containers": [
    {
      "name": "nginx-container",
      "image": "nginx",
      "volumeMounts": [
        {
          "name": "shared-data",
          "mountPath": "/usr/share/nginx/html"
        }
      ]
    },
    {
      "name": "debian-container",
      "image": "debian",
      "volumeMounts": [
        {
          "name": "shared-data",
          "mountPath": "/pod-data"
        }
      ]
    }
  ]
}

5. Complete Pod Manifest with Labels and Ports

apiVersion: v1
kind: Pod
metadata:
  name: pod-label-demo
  labels:
    environment: production
    app: nginx
spec:
  containers:
    - name: nginx
      image: nginx:1.14.2
      ports:
        - containerPort: 80

Explanation:

  • This Pod runs a container based on the nginx:1.14.2 image.
  • Exposes port 80.
  • Labels environment: production and app: nginx are attached to help other objects (like Services) interact with it.

6. View Running Pod Manifest

You can view the complete YAML definition (manifest) of a running Pod by using:

kubectl get pod pod-nginx -o yaml

This command shows:

  • All metadata (including UID, creation timestamp).
  • Spec (container configuration, resource limits, volume mounts, etc.).
  • Status (conditions, state, readiness, etc.).

7. Additional Fields in Pod Spec (Advanced Use)

You can extend the Pod manifest with more configurations:

a. Restart Policy

restartPolicy: Always # OnFailure | Never

b. Environment Variables

env:
  - name: LOG_LEVEL
    value: DEBUG

c. Command and Args

command: ["/bin/sh"]
args: ["-c", "echo Hello from the Pod"]

d. Volumes

volumes:
  - name: shared-data
    emptyDir: {}

e. Security Context

securityContext:
  runAsUser: 1000
  runAsGroup: 3000

8. Pod Lifecycle Recap

  • Pods are created from YAML or JSON manifests.
  • Containers inside a Pod start together and run in the same network and storage context.
  • Pods are not meant to be permanent; they are often managed by higher-level objects like Deployments.

9. Summary Table

SectionKey Concept
Basic ManifestDescribes a single Pod with containers and ports
MetadataUniquely identifies the Pod via name and labels
SpecDefines the desired configuration: containers, volumes, env vars
YAML FormatUses lists (-) and maps (key-value) to define structure
LabelsHelp identify Pods and enable communication via Selectors
kubectl -o yamlShows full live state of a running Pod