Monday, March 29, 2021

Kubernetes for Developers #11: Pod Organization using Labels

In Microservice architecture, The number of deployed Pods will be replicated more than ones and multiple releases (i.e. stable, beta, canary) will run concurrently. This can lead to hundreds of pods within no time. So, organizing pods are crucial in microservice architecture.


Pods without Labels:
As per below image, we are running multiple microservices with multiple replicas and multiple releases without Pod labels. It is evident that we need to find a way to organize the Pods into smaller groups based on arbitrary criteria. Here, Pod Labels help us to organizing Pods into different groups.

Pods with Labels:
As per below image, we are running multiple microservices with multiple replicas and multiple releases with Pod labels. Each Pod is labeled with two labels
  • app, it specifies Pod belongs to which app, component, or service
  • release, it specifies Pod running under stable, beta or a canary release

Labels

Label is a key-value pair which is attached to pods, deployments, etc.

  • It acts an identifier on K8 object (Ex: Pod or Deployment). So, the other Kubernetes objects (Ex: Service, DaemonSet) can communicate by matching same label names.
  • It can be attached to Kubernetes objects at creation time or directly on live objects by using Imperative way
  • Same label key/value can be assigned to multiple Kubernetes objects
  • Each object in the label can have set of key/value and each key must be unique for a given object
  • Label key should not exceed 63 characters and allowed characters are alphanumeric, dash(-), underscore(_), dot(.)
  • Label value should not exceed 63 characters and allowed characters are alphanumeric, dash(-), underscore(_), dot(.)
metadata:
  namepod-label-demo
  labels:
    releasestable
    appui


Example: Creating labels on Pod
apiVersionv1
kindPod
metadata:
  namepod-label-demo
  labels:
    releasestable
    appui
spec:
  containers:
    - namenginx
      imagenginx:1.14.2
      ports:
        - containerPort80


There are two labels i.e. app: ui, release: stable are assigned to Pod

Use following kubectl commands to find each k8 object and respective labels

// show all labels on each Pod
> kubectl get pods --show-labels

// show all labels on each Deployment
> kubectl get deployments --show-labels

// show all Pods where label app: ui
> Kubectl get pods -l app=ui

// add label to running Pod
// syntax
kubectl label Pod <podname> <key>=<value>
// example
> kubectl label Pod pod-label-demo release=stable

// delete Pods using label
> kubectl delete pod -l app=ui


Kubernetes for Developers Journey.

Happy Coding :)


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

Sunday, March 14, 2021

Kubernetes for Developers #9: Kubernetes Pod Lifecycle

A Pod is a group of one or more containers (consider docker containers) with shared storage and network resources.

  • Network: Pods get unique IP address automatically and all the containers in the Pod communicate each other using localhost and Port
  • Storage: Pods can be attached to Volumes that can be shared among the containers

A Pod is designed to run a single instance of an application inside node of Kubernetes cluster.

Types of Pod:

  • Single container Pod: The “one-container-per-pod” is the most common use case and Kubernetes manage Pod rather than container directly.
  • Multi container Pod: A Pod can group multiple containers with shared storage volumes and network resources. Generally, we name it as Primary container and Sidecar container.
Ex: Primary container used to serve data stored in the filesystem/volume to outer world whereas Sidecar container used to refresh the filesystem/volume.

Pod lifecycle:

Pods are ephemeral and not designed to run forever, when Pod is terminated it cannot be repair themselves rather it gets deleted and recreated based on Pod policy.

Pod lifecycle starts from “Pending” phase, moving through “Running” if at least one container starts and then will move to “Succeeded” or “Failed” phase depending on the container exit status.  

Pod phases are continuing to update when,

  • Kubelet constantly monitor container states and send info back to KubeAPI Server for updating Pod phase.
  • Kubelet stops reporting to the KubeAPI Server.

Pod phases:

Pending
Pod has been created by the cluster, but one or more of its containers are not yet running. This phase includes time spent being scheduled on a node and downloading images

Running
The Pod has been allotted to a node; all the containers have been created. At least one container is still running, or is in the process of starting or restarting

Succeeded

All containers in the Pod have terminated successfully

Failed
One or more containers terminated with non-zero status

Unknown
The state of the Pod cannot be determined. This occurs due to error while communicating with the node


Container States:

The way Kubernetes maintain Pod phases, it maintains state of each container in the Pod.

Once the scheduler assigns a Pod to a Node, the kubelet starts creating containers for that Pod using a container runtime. There are 3 possible states for the container.

Waiting
When the container still pulling image, applying Secret data etc.

Running
When the container executing without any issues

Terminated
When the container exited with non-zero status


// Create Pod using imperative way
> kubectl run pod-nginx --image=nginx 

# Create Pod using Declarative way (.yaml file )
apiVersionv1
kindPod
metadata:
  namepod-nginx
spec:
  containers:
    - namenginx
      imagenginx:1.14.2
      ports:
        - containerPort80
          protocolTCP


// Create Pod using Declarative way
> kubectl apply -f ./pod-nginx.yaml


// View all running pods and their status
> kubectl get po

// View specific pod running status
// syntax: kubectl get po <pod-name>
> kubectl get po pod-nginx

// Get running Pod definition
// syntax: kubectl get po <pod-name> -o <outout-format>
> kubectl get po pod-nginx -o yaml

// Get Pod phase
> kubectl get po pod-nginx -o yaml | grep phase

// Describe Pod in-detail
// syntax: kubectl describe po <pod-name>
> kubectl describe po pod-nginx

// View logs from Pod running container
// syntax: kubectl logs <pod-name>
> kubectl logs pod-nginx

// View logs from Pod when multiple running containers
// syntax: kubectl logs <pod-name> -c <container-name>
> kubectl logs pod-nginx -c nginx

// Stream logs from Pod
> kubectl logs -f pod-nginx

// Expose Pod for debugging or testing purpose using port-forward proxy
// Syntax: kubectl port-forward <pod-name> <host-port>:<container-port>
// Ex: http://localhost:8444
> kubectl port-forward pod-nginx 8444:80

// Shell to a running Pod
> kubectl exec -it pod-nginx –-sh

// Shell to specific container when multiple containers running in the Pod
// Syntax: kubectl exec -it <pod-name> --container <container-name> –- sh
> kubectl exec -it pod-nginx --container nginx –-sh

// View last 100 messages from Pod
> kubectl logs --tail=100 pod-nginx

// Delete a Pod
> kubectl delete po pod-nginx


Kubernetes for Developers Journey.

Happy Coding :)