What is Pod?

  • POD is the smallest Deployable Unit
  • Containers are not directly deployed in Kubernetes on Worker node but deployed as Pod
  • Single Pod can contain one or more Containers.
  • Characteristics of containers deployed in one POD:
    • Address to each other via localhost
    • Share IP Address
    • Share environment
    • Share Volumes 
    • Share same fate (live together, die together)
    • Scheduled on the same worker node
Ride Together Best Friends GIF by CBS All Access

So to scale up do we add another container within POD?

no no no no GIF by Barstool Sports

No. You have add another Pod.

How to run a POD?

Command:

kubectl run <pod-name> --image <docker-image-name>

Example:

kubectl run my-nginx-pod --image nginx

I did some cheating, this command not only does create POD but also create deployment.

cinemax golf cinemax cheating jett GIF

How to find all running pods?

Command:

kubectl get pods

Output:

$ kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
my-nginx-pod 1/1     Running   0          34s

What if I want to check IP address and node on which pod is running?

Command:

kubectl get pods -o wide

Output:

$ kubectl get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP          NODE     NOMINATED NODE   READINESS GATES
my-nginx-pod 1/1     Running   0          82s   10.32.0.2   node01   <none>           <none>

Ok if you think you are that smart, what if I want to check Pod image being run under Pod?

Well enough, you will keep asking, here is the super command.

kubectl describe pod <pod-name>

This command will give:

  • On which node Pod is running
  • Name of image being used
  • Events related to pods
Smart Think About It GIF by Friends

Output:

$ kubectl describe pod my-nginx-pod
Name: my-nginx-pod
 Namespace: default
 Node: kubernetes-node-wul5/10.240.0.9
 Start Time: Thu, 24 Mar 2016 01:39:49 +0000
 Labels: app=nginx,pod-template-hash=1006230814
 Annotations:    kubernetes.io/created-by={"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"ReplicaSet","namespace":"default","name":"nginx-deployment-1956810328","uid":"14e607e7-8ba1-11e7-b5cb-fa16" ...
 Status: Running
 IP: 10.244.0.6
 Controllers: ReplicaSet/nginx-deployment-1006230814
 Containers:
  nginx:
    Container ID: docker://90315cc9f513c724e9957a4788d3e625a078de84750f244a40f97ae355eb1149
    Image: nginx
    Image ID: docker://6f62f48c4e55d700cf3eb1b5e33fa051802986b77b874cc351cce539e5163707
    Port: 80/TCP
    QoS Tier:
      cpu: Guaranteed
      memory: Guaranteed
    Limits:
      cpu: 500m
      memory: 128Mi
    Requests:
      memory: 128Mi
      cpu: 500m
    State: Running
      Started: Thu, 24 Mar 2016 01:39:51 +0000
    Ready: True
    Restart Count: 0
    Environment:        <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-5kdvl (ro)
 Conditions:
  Type          Status
  Initialized   True
  Ready         True
  PodScheduled  True
 Volumes:
  default-token-4bcbi:
    Type: Secret (a volume populated by a Secret)
    SecretName: default-token-4bcbi
    Optional:   false
 QoS Class:      Guaranteed
 Node-Selectors: <none>
 Tolerations:    <none>
 Events:
  FirstSeen LastSeen Count From SubobjectPath Type Reason Message
  --------- -------- ----- ---- ------------- -------- ------ -------
  54s 54s 1 {default-scheduler } Normal Scheduled Successfully assigned nginx-deployment-1006230814-6winp to kubernetes-node-wul5
  54s 54s 1 {kubelet kubernetes-node-wul5} spec.containers{nginx} Normal Pulling pulling image "nginx"
  53s 53s 1 {kubelet kubernetes-node-wul5} spec.containers{nginx} Normal Pulled Successfully pulled image "nginx"
  53s 53s 1 {kubelet kubernetes-node-wul5} spec.containers{nginx} Normal Created Created container with docker id 90315cc9f513
  53s 53s 1 {kubelet kubernetes-node-wul5} spec.containers{nginx} Normal Started Started container with docker id 90315cc9f513

How to create a pod using yaml based configuration file?

Here is an example: Lets create a pod-def.yaml file containing pod definition:

apiVersion: v1
kind: Pod
metadata:
  name: my-nginx-pod
spec:
  containers:
    - name: my-nginx-container
      image: nginx

Note: All apiVersion, kind, metadata and spec are required fields.

Command:

kubectl create -f <pod-definition.yaml>

Output:

$ kubectl create -f pod-def.yaml
pod my-nginx-pod created

Now also check if you pod is created:

$ kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
my-nginx-pod 1/1     Running   0          34s

How to delete a pod which was created using yaml file?

delete matt hardy GIF by WWE

Command:

kubectl delete -f <pod-definition.yaml>

How to delete a pod using pod name ?

Command:

kubectl delete pod <pod-name>

How to delete multiple pods in single command ?

Command:

kubectl delete pod <pod1> <pod2> <pod3>

Can I edit a running Pod?

Well yes, but not all parameters. Use the below command:

kubectl edit pod <pod-name>

What you can edit:

  • spec.containers[*].image
  • spec.initContainers[*].image
  • spec.activeDeadlineSeconds
  • spec.tolerations

You cannot edit the environment variables, service accounts, resource limits (we will cover these items later) of a running pod.

Hmmm, what if I edit the property you are saying is un-editable?

Well you will get an error 🙂

error rd GIF by Dominicana's Got Talent
$ kubectl edit pod my-nginx-pod

[And I am assuming you editing a property like resource limits now and tried saving it]

Now you will see an error on screen:

error: pods “my-ngix-pod” is invalid.
A copy of your changes has been stored on “/tmp/kubectl-edit-xxyq.yaml”
error: Edit cancelled, no valid changes were saved

What if I still want to edit this property, is there no way?

Well, you can delete the existing pod by running the command:

kubectl delete pod my-nginx-pod

Then create a new pod with your changes using the temporary file, created above.

kubectl create -f /tmp/kubectl-edit-xxyq.yaml

Does it mean I have to get the above error first to get the YAML file from Pod? Is there a way to create a YAML definition file from the running pod?

Yes, you are in luck here buddy, you can run below command to have definition file from pod:

kubectl get pod my-ngixn-pod -o yaml > my-new-pod.yaml
Smart Genius GIF by YoungerTV

Now you know the drill, what you need to do

Step 1: Edit whatever parameters you want to edit:

vi my-new-pod.yaml

Step 2: Delete the pod

kubectl delete pod webapp

Step 3: Re-create the pod from definition file

kubectl create -f my-new-pod.yaml

What are different Pod life-cycle phases?

life cycle love GIF by Erick Oh

The phase of a Pod is a simple, high-level summary of where the Pod is in its lifecycle.

  • Pending: The Pod has been accepted by the Kubernetes system, but one or more of the 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.

How to check pod’s logs ?

Command:

kubectl logs <pod-name>

How to get logs of the specific container running inside pod ?

Command:

kubectl logs <pod-name> -c <container-name>

Can I check pod logs even when pod is deleted?

NO.

You can only retrieve container logs of pods that are still alive, but when the pod is deleted, its logs are also deleted.

Can Pod which consists of multiple containers can have containers spawned of two different worker nodes?

NO

ryan newman no GIF by Alexander IRL

Why each container should have one running process only instead of multiple?

Reaction GIF

Containers provide features like, restarting containers when container crashes which happen when process inside it crashes, and logs are sent to standard output which can be read from there or managed further.

But consider if you are running multiple processes inside a container, then how will you manage that which process is crashed and how to restart that specific process. Also in terms of logs, now you have logs of both the containers going on standard output, and you have find your ways to figure out which log belongs to which process.

Hence easier to have container per process.

Why do we need Pods?

Pop Tv Question GIF by Schitt's Creek

Since in the case of containers you can’t have multiple processes in a single container, hence there is a need for abstraction which can contain multiple containers together which needs to interact with each other, share IP addresses and environment. Basically containers which need to run closely.

Else consider container as the basic unit in Kubernetes, and Kubernetes will start scheduling containers which have to run closely and very much dependent upon each other for existence on different worker nodes. Even then these containers will require configuration for interacting with each other.

Containers inside a pod run under the same Network namespace, hence share the same IP address (hostname) and port space. Containers in a pod have the same loopback network interface, hence these containers can interact with each other through localhost.

Can containers running in the same pod share the file system also?

NO

old man no GIF

Container’s file system comes from the container image, and the filesystem of each container is fully isolated from other containers. Still, in Kubernetes there is a feature to share file directories using Volume, which we will read later.

Should containers running inside Pod use the same Port binding?

NO

no GIF

Since containers running inside Pod share the same IP address and ports space, this means that if both containers will try to bind to the same port number, they will have a port conflict.

Can Pod interact with each other inside the cluster?

YES

daniel bryan yes GIF by WWE

All pods in the Kubernetes cluster are given IP addresses under the same network address space, this means that no NAT (Network Address Translation) gateway is required.

It doesn’t matter if two pods are scheduled on the same or different worker nodes, Pods can interact with easily using each other IP address.

How to specify container port on Pod definition?

Here is an example of the same:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - image: <docker-image-to-pull>
    name: my-docker-container
    ports:
    - containerPort: 8080
      protocol: TCP

Note: Providing port information in Pod definition is just for information, even if you will remove it, it will have no impact.

Categories: Devops

Rakesh Kalra

Hello, I am Rakesh Kalra. I have more than 15 years of experience working on IT projects, where I have worked on varied complexity of projects and at different levels of roles. I have tried starting my own startups, 3 of those though none of it were successful but gained so much knowledge about business, customers, and the digital world. I love to travel, spend time with my family, and read self-development books.

0 Comments

Leave a Reply