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

No comments:

Post a Comment