Kubernetes Pods

Overview

A Pod (as in a pod of whales or pea pod) is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers. A Pod's contents are always co-located and co-scheduled and run in a shared context. A Pod models an application-specific logical host: it contains one or more application containers that are relatively tightly coupled. In non-cloud contexts, applications executed on the same physical or virtual machine are analogous to cloud applications executed on the same logical host.

The shared context of a Pod is a set of Linux namespaces, cgroups, and potentially other facets of isolation - the same things that isolate a Docker container. Within a Pod's context, the individual applications may have further sub-isolations applied.

In terms of Docker concepts, a Pod is similar to a group of Docker containers with shared namespaces and shared filesystem volumes.

kubernetes-kubernetes-pods

Kubernetes Pods Key Points

  • Kubernetes pod is the smallest unit in the Kubernetes
  • Kubernetes doesn't run containers directly; instead it wraps one or more containers into a higher-level structure called a pod.
  • One pod can have multiple containers so that tightly coupled application can have multiple tier containers in one pod
  • Kubernetes created pods to support many container runtime environments, so we can create pods of multiple runtime environments like docker, rocket etc
  • It is also the smallest deployable unit that can be created, schedule, and managed on a Kubernetes cluster. Each pod is assigned a unique IP address within the cluster.
  • In pod we can have multiple pods and multiple volumes, these containers will use the same network and the port assigned to the pod will be the same so if any of the containers is using port 80 inside the pod then other containers in that same pod can not use port 80
  • Pods can hold multiple containers as well, but you should limit yourself when possible. Because pods are scaled up and down as a unit, all containers in a pod must scale together, regardless of their individual needs. This leads to wasted resources.
  • Any containers in the same pod will share the same storage volumes and network resources and communicate using localhost
  • K8s uses YAML to describe the desired state of the containers in a pod. This is also called a Pod Spec. These objects are passed to the kubelet through the API server.
  • Pods are used as the unit of replication in Kubernetes. If your application becomes too popular and a single pod instance can't carry the load, Kubernetes can be configured to deploy new replicas of your pod to the cluster as necessary.
  • Volume can be shared between multiple pods

Kubectl run

run will start running 1 or more instances of a container image on your cluster.

Use the below command to create a pod

kubectl run firstpod --image=coolgourav147/nginx-custom

Kubectl Get

kubectl get command is use toDisplay one or many resources.

Prints a table of the most important information about the specified resources. You can filter the list using a label selector and the --selector flag. If the desired resource type is namespaced you will only see results in your current namespace unless you pass --all-namespaces.

To list the pod use the below command

kubectl get pod

To list wide o/p of pod use below command

kubectl get pods -o wide

To get the o/p in YAML format use the below command

kubectl get pods -o yaml

To get the o/p in JSON format use the below command

kubectl get pods -o json

Kubectl Explain

To get information about the component of Kubernetes we can use explain command, for example, if we wish to know more about pods, we can use the below command

this command will open the manual for pods, this will be useful to know the version of Kubernetes component while writing the YAML file

kubectl explain pods

Kubectl Describe

Below command will describe the content of a particular pod and can track the event happening inside the pod

kubectl describe pod <PodName>

use -w parameter to continuously watch the output of any command