Sunday, March 21, 2021

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
apiVersionv1
kindPod

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

Excomplex key-value structure where value act as another Map

#yaml
apiVersionv1
kindPod
metadata:
  namepod-nginx

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

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

Exdefining sequence of values

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

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

ExDefining List item as Map like Json array of Objects.

#yaml
containers:
  - namenginx-container
    imagenginx
    volumeMounts:
      - nameshared-data
        mountPath/usr/share/nginx/html
  - namedebian-container
    imagedebian
    volumeMounts:
      - nameshared-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:

apiVersionv1    # v1, apps/v1, and extensions/v1beta1
kindPod   # Pod, Deployment, Service etc.
metadata:
  namenginx-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
apiVersionv1
kindPod
metadata:
  namepod-nginx
spec:
  containers:
    - namenginx
      imagenginx:1.14.2
      ports:
        - containerPort80
          protocolTCP


Example: Creating labels on Pod

apiVersionv1
kindPod
metadata:
  namepod-label-demo
  labels:
    environmentproduction
    appnginx
spec:
  containers:
    - namenginx
      imagenginx:1.14.2
      ports:
        - containerPort80


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 :)

No comments:

Post a Comment