Pods in Kubernates with Yaml

Pods in Kubernates with Yaml

Play this article

What is a Pod in Kubernetes?

Pods are the smallest deployable units in Kubernetes. A Pod represents a single instance of a running process or a group of tightly coupled processes running together on a single node. While containers are commonly used within Pods, Pods can also include multiple containers that share resources and network connectivity.

The purpose of a Pod is to provide a cohesive and isolated environment for containers to run and share resources, including storage, network, and inter-process communication. Containers within a Pod can communicate with each other via localhost, as they share the same network namespace.

Key Characteristics of Pods:

  • Single or multiple containers within a Pod: Pods can contain one or more containers that are scheduled and deployed together on the same node.

  • Shared network namespace: Containers in a Pod share the same network namespace, allowing them to communicate using localhost.

  • Shared storage and volumes: Pods can share storage volumes, allowing data sharing and persistence between containers within the same Pod.

Single Node K8 cluster:

Containers in the pods are coupled within the node, and if the number of users increases then we have to increase the number of pods in the nodes and if the pod capacity has reached its maximum then we have to create a new container pod with a new node.

Pods usually have one on one relationship with the container.

CKA & CKAD Series (Part 3): Replication controller &  replicaset_kubernetes_weixin_0010034-K8S/Kubernetes

Multi-Cluster node:

A single pod can have multiple containers until and unless they all are not same. We can have additional pods as helper containers to support tasks, like processing user data.

In that kind of scenario, we have the application container with the support container in the same pod. Support container will be created with the application container and die with the same.

Also, the application container and support container share the same network and storage.

YAML:

In YAML, we keep things in Key: value format

Image description

here keys are Fruit, Vegetable etc. & values are Apple, Carrot etc.

Also, this is how the array & dictionary is placed in YAML:

Image description

Note: You need to make sure the gaps are formatted well.

Also, you may have a mix-up of all of these:

Image description

Let's check an example where we can use YAML to represent one

Example 1:

Image description

You can see the key's color, model, transmission & price. All of them had their different values. Even the model is not a key here. Rather a dictionary which has keys Name & Year in it.

So, this is how we use YAML.

Example 2:

Image description

Here we we have added all the car's names with colors in the list format.

But when we would like to have every detail of them, we will use dictionary format here. Like this

Image description

and this is a list of dictionaries.

Let's create a pod using the YAML file

Let's create a file called pods-definition.yaml

Image description

For a pod, we must have these contents. There might be different values for different things but for our purpose, we will just use this one.

Image description

We will use the apiVersion as V1 for now.

Image description

For the kind, we will use "pod" as we are creating a pod.

Image description

The metadata, provides data about the object, in our case object is a pod. we are going to provide additional information like the name of the pod and labels we want to add so that we can filter later among many many pods to find this one.

Image description

Look, there are strings for some values and dictionaries for some.

Image description

You can also add additional information for labels:

Image description

Now, let's check the spec one:

Here we are going to set the containers list etc. For this pod, we just want 1 container to be created. We are going to use the "nginx" image with "nginx-container" as the name of the container.
We have formatted it in list format. Because there might be several containers in a pod.

Image description

Image description

Also, we can have additional information for a container like resources, ports etc.

Image description

Now let's create a pod. For that, use these codes:

Image description

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod1
  labels:
    name: myapp1
spec:
  containers:
  - name: nginx-container
    image: nginx

To create the pod go with the below command:

kubectl apply -f pod.yml

We have used -f to force it to use pod.yml to create a pod using the apply command.

You can check the pod using:

kubectl get pods

Also to know more about a particular pod, you can use this command :

"kubectl describe pod "

kubectl describe pod myapp-pod1

You can delete the pod using "kubectl delete pod "

kubectl delete pod myaap-pod

And then check if it is available or not:

kubectl get pods

Few more commands that will be useful:

kubectl get pods -o wide: To check on which node pods are placed 
kubectl run —help : This will help you in many ways

Hope this was helpful. Thank you for your time !!

Stay Connected, many more to come !!

You can connect with me : https://www.linkedin.com/in/aman-srivastava-dev/

Did you find this article valuable?

Support Aman Srivastav by becoming a sponsor. Any amount is appreciated!