Kubernetes : Pods and Containers
Welcome back Readers !
The Kubernetes set up was intersting. That gave you the building base to set up Pods and containers (where the real magic happens).
This article is dedicated to the concept of Pods and Containers. If you remeber when I introduced Kubernetes and its architecture, I mentioned about the Pods and containers.
Image 1 : K8s cluster architecture
Containers are encapsulated within a Pod i.e. a Pod can have multiple containers. I will define a container as it is nothing but the technique for packaging an application along with its runtime dependencies. Every container is reusable, repeatable and standard meaning whenever you run a container, you will get the same behavour and output. It decouples applications from underlying hosts i.e. the nodes would not even know the container application strategy.
Container is dependent on two main things :
Container Image: Its a software package which contains the configuration including the code, system libraries and default config to run an application. These images are software bundles than can run standalone and that makes it defined assumptions about runtime
Container Runtimes: Its a software that is required to run containers like Dockers or any CRI (Container Runtime Interface)
Pods are considered to be the smallest and simplest deploable compute unit in K8s. Basically, it is the processes running on K8s cluster. Consider above image, Pod pocesses containers meaning Pod encapsulates an application's container(s). Also, it controls storage resources, an unique IP address as well as the governance of that container(s) which decides that how should the container run. One Pod can have different more than one CR (container runtime e.g docker, containerd etc).
A Pod acts as a logical host for its containers which shares IP addressing and port space. The containers within same pod communicate using standard inter-process communication like POSIX shared memory or System V semaphores.
Also, application within same pod have shared volumes which are available to be mounted into each application filesystem when it is required. In terms of Docker constructs, a Pod is modelled as a group of Docker containers with shared namespaces and shared filesystem volumes.
There is a point to be noted here that is you can have any number of containers in a pod (called mutilple-container pod) but it is not application to all scenarios e.g.
1. You have 3 containers in one pod and there was a updated pushed to that pod. It will result in the update of entire 3 containers as well even if you don't need that update.
2. Scaling purpose: if the pod needs to be scaped up or scale in, the containers associated to the pod will get affected with the same changes
3. you don't need to run their own process managers, worry about signal and exit-code propagation, etc.
So to avoid such situations, it is always recommended to have single application in one pod.
I think you have fair idea about the concepts of Pods and containers, lets try to deploy pods
You can deploy pod manually using below command
Whole point of having container is to make things simple and fast so another method to create K8s objects is through YAML based scripts. K8s uses YAML file to define the requirement using key value pairing. Lets create same object using YAML deployment files.
Below is default syntax for YAML to have declaration of the required object which is
1. apiVersion : to define the version to create K8s object
2. kind: as name suggests, what kind of pod
3. metadata: thats the definition of the pod which includes name, labels, type etc
4. spec: this section provides the characterstic of pod i.e. replicas, resources, images etc. This is the most important section of the YAML file
You can create this YAML file locally or can pull this from below k8s.io using below command. This is the most important command to deploy (Why ? explained in the last section of this article)
# kubectl apply -f https://k8s.io/examples/controllers/nginx-deployment.yaml
you should note that three pods created with different name because of ReplicaSets are 3
verify with below command :
# kubectl get deployments
Below commands is verifier for how many ReplicaSet is created
# kubectl get rs
If you need to have detailed view of your deplyment, run the command below
# kubectl descrie deployments
Another important command is
# kubectl get pods -o wide
this will show the additional info like IP address and worker node on which the pods are created.
So this is how the pods are created on nodes. You must be curious what exactly happens behind the curtains when we are in the process of deploying pods.
Well, Kubectl is the one who runs the show here. Kubectl is command line tool which lets you control K8s cluster. When you run the "kubectl apply" command, it actually runs through different phases of values. These phases are called Pod Lifecycle. The phase of the pod is simple , goes through various values and explains high-level summary of where the pod is in its lifecycle. Below are the possible values of phases
Pending : It means the pod has been accepted by K8s system but one or more of container images has not been created. This includes time before being scheduled as well as time spent downloading images over the network, which could take a while.
Running: The Pod has been bound to a node, and all of 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 in success, and will not be restarted.
Failed : All Containers in the Pod have terminated, and at least one Container has terminated in failure. That is, the Container either exited with non-zero status or was terminated by the system.
Unknown : For some reason the state of the Pod could not be obtained, typically due to an error in communicating with the host of the Pod.
Well, thats all for now in this short article of Pods and Contains, I am hoping this was simple yet informational explaination. I will post further articles soon.
Stay safe and keep reading.
Thank you !