Kubernetes for Developers #10: Kubernetes Pod YAML manifest in-detail

In General, K8 resources are usually created using YAML or JSON manifest.

YAML Structure:

YAML is a Indentation based and superset of JSON, which means any valid JSON file is also a valid YAML file. There are two types of structures we mostly use.

  • Map: It is key-value pair like Json Object.

Ex: simple key-value structure where key and value are strings

#yaml
apiVersion: v1
kind: Pod

#json
{
    "apiVersion": "v1",
    "kind": "Pod"
}

Ex: complex key-value structure where value act as another Map

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

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

  • List: It is sequence of values like Json Array represented using dash (-) sign.

Ex: defining sequence of values

#yaml
args:
  - "HOSTNAME"
  - "PORT_NUMBER"
  - "9900"

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

Ex: Defining List item as Map like Json array of Objects.

#yaml
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
{
    "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"
          }
        ]
      }
    ]
  }


Pod Manifest:

A pod is the most basic unit of the Kubernetes cluster. It usually contains one or more running containers. Pods are designed to be ephemeral in nature which means that they can be destroyed at any time. Containers in a pod share the same network and storage.


Syntax:

apiVersion: v1    # v1, apps/v1, and extensions/v1beta1
kind: Pod   # Pod, Deployment, Service etc.
metadata:
  name: nginx-pod
spec:

  • apiVersion - version of the Kubernetes API you are using to create an object
  • kind - kind of object you want to create
  • metadata - It helps to uniquely identify the object, including a name, labels and optional namespace
  • spec – It is used to define desired state for the object
apiVersion: v1
kind: Pod
metadata:
  name: pod-nginx
spec:
  containers:
    - name: nginx
      image: nginx:1.14.2
      ports:
        - containerPort: 80
          protocol: TCP


Example: Creating labels on Pod

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


There are two labels i.e. app:nginx, environment:production are assigned to Pod

Running Pod Manifest:

  execute following command to get running pod manifest details
// show all labels on each Deployment
> kubectl get pod pod-nginx -o yaml


Kubernetes for Developers Journey.
Happy Coding :)

Comments