Skip to main content

Command Palette

Search for a command to run...

Pods - Kubernetes

Updated
8 min read

Introduction to Pods

Pods are the smallest deployable units of computing that you can create and manage in Kubernetes.

A pod is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers.

As per the above image, IP is assigned to the pod and it has two different containers running whatever volume is attached will be shared with the containers.

node1 ~]$ kubectl create namespace mehdiproject
namespace/mehdiproject created
[node1 ~]$ 
[node1 ~]$ ls                 
anaconda-ks.cfg  pods-simple-pod-yaml
[node1 ~]$ kubectl apply -f pods-simple-pod-yaml -n mehdiproject
pod/nginx created
[node1 ~]$

How to create pods using a namespace

## I gave namespace name as mehdiproject ##

[node1 ~]$ kubectl get pods -n mehdiproject
NAME    READY   STATUS    RESTARTS   AGE
nginx   0/1     Pending   0          77s

[node1 ~]$ kubectl get pods   # This command will get default  namespace    
No resources found in default namespace.
[node1 ~]$

How to describe pods using namespace

Details of pods using describe and specify the namespace can be viewed

[node1 ~]$ kubectl describe pods -n mehdiproject
Name:             nginx
Namespace:        mehdiproject
Priority:         0
Service Account:  default
Node:             node2/192.168.0.12
Start Time:       Sat, 08 Jul 2023 20:07:39 +0000
Labels:           <none>
Annotations:      <none>
Status:           Running
IP:               10.5.1.3
IPs:
  IP:  10.5.1.3
Containers:
  nginx:
    Container ID:   containerd://b26fccb947713acd2141ae204587ee6da1ec89056f8b2e96a936fdb09b38eafa
    Image:          nginx:1.14.2
    Image ID:       docker.io/library/nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Sat, 08 Jul 2023 20:08:11 +0000
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-25qpq (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-25qpq:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason                  Age                  From               Message
  ----     ------                  ----                 ----               -------
  Warning  FailedScheduling        5m31s (x2 over 10m)  default-scheduler  0/1 nodes are available: 1 node(s) had untolerated taint {node-role.kubernetes.io/control-plane: }. preemption: 0/1 nodes are available: 1 Preemption is not helpful for scheduling..
  Normal   Scheduled               46s                  default-scheduler  Successfully assigned mehdiproject/nginx to node2
  Warning  FailedCreatePodSandBox  46s                  kubelet            Failed to create pod sandbox: rpc error: code = Unknown desc = failed to setup network for sandbox "39c71cf931d2c0cef242a5fb143381d56d9d108af362ca65deb19d3520e8eccb": plugin type="bridge" name="kubernetes" failed (add): no IP ranges specified
  Warning  FailedCreatePodSandBox  30s                  kubelet            Failed to create pod sandbox: rpc error: code = Unknown desc = failed to setup network for sandbox "fea660608cc890393d6d0972b0e41d182c92fc3a9d57d34629ba3f9237a2d271": plugin type="bridge" name="kubernetes" failed (add): failed to set bridge addr: could not set bridge's mac: invalid argument
  Normal   SandboxChanged          19s (x3 over 45s)    kubelet            Pod sandbox changed, it will be killed and re-created.
  Normal   Pulling                 19s                  kubelet            Pulling image "nginx:1.14.2"
  Normal   Pulled                  14s                  kubelet            Successfully pulled image "nginx:1.14.2" in 4.822591309s (4.822637709s including waiting)
  Normal   Created                 14s                  kubelet            Created container nginx
  Normal   Started                 14s                  kubelet            Started container nginx
[node1 ~]$ 

###  if want to check more details of pods
[node1 ~]$ kubectl get pods -n mehdiproject -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP         NODE    NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          14m   10.5.1.3   node2   <none>           <none>
[node1 ~]$ 

#### P
ods can be seen running status on node 2 which is worker node

Check duplicate pods are possible or not

In one namespace we can't have duplicated pod names but in different namespaces, we can have the same name pods.

[node1 ~]$ kubectl get pods -n mehdiproject -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP         NODE    NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          14m   10.5.1.3   node2   <none>           <none>
[node1 ~]$ kubectl run nginx --image=nginx:latest
pod/nginx created

[node1 ~]$ kubectl get pods                        
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          4m50s
[node1 ~]$ kubectl get pods -n mehdiproject -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP         NODE    NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          23m   10.5.1.3   node2   <none>           <none>
[node1 ~]$

Kubectl options

Kubectl has lots of options like edit, describe, delete, explain etc

[node1 ~]$ kubectl 
kubectl controls the Kubernetes cluster manager.

 Find more information at: https://kubernetes.io/docs/reference/kubectl/

Basic Commands (Beginner):
  create          Create a resource from a file or from stdin
  expose          Take a replication controller, service, deployment or pod and expose it as a new
Kubernetes service
  run             Run a particular image on the cluster
  set             Set specific features on objects

Basic Commands (Intermediate):
  explain         Get documentation for a resource
  get             Display one or many resources
  edit            Edit a resource on the server
  delete          Delete resources by file names, stdin, resources and names, or by resources and
label selector

Deploy Commands:
  rollout         Manage the rollout of a resource
  scale           Set a new size for a deployment, replica set, or replication controller
  autoscale       Auto-scale a deployment, replica set, stateful set, or replication controller

Cluster Management Commands:
  certificate     Modify certificate resources.
  cluster-info    Display cluster information
  top             Display resource (CPU/memory) usage
  cordon          Mark node as unschedulable
  uncordon        Mark node as schedulable
  drain           Drain node in preparation for maintenance
  taint           Update the taints on one or more nodes

Troubleshooting and Debugging Commands:
  describe        Show details of a specific resource or group of resources
  logs            Print the logs for a container in a pod
  attach          Attach to a running container
  exec            Execute a command in a container
  port-forward    Forward one or more local ports to a pod
  proxy           Run a proxy to the Kubernetes API server
  cp              Copy files and directories to and from containers
  auth            Inspect authorization
  debug           Create debugging sessions for troubleshooting workloads and nodes
  events          List events

Advanced Commands:
  diff            Diff the live version against a would-be applied version
  apply           Apply a configuration to a resource by file name or stdin
  patch           Update fields of a resource
  replace         Replace a resource by file name or stdin
  wait            Experimental: Wait for a specific condition on one or many resources
  kustomize       Build a kustomization target from a directory or URL

Settings Commands:
  label           Update the labels on a resource
  annotate        Update the annotations on a resource
  completion      Output shell completion code for the specified shell (bash, zsh, fish, or
powershell)

Other Commands:
  api-resources   Print the supported API resources on the server
  api-versions    Print the supported API versions on the server, in the form of "group/version"
  config          Modify kubeconfig files
  plugin          Provides utilities for interacting with plugins
  version         Print the client and server version information

Usage:
  kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
[node1 ~]$
node1 ~]$ kubectl edit pod nginx

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2023-07-08T20:16:15Z"
  labels:
    run: nginx
  name: nginx
  namespace: default
  resourceVersion: "2181"
  uid: 1f701027-de16-4e4e-a7f9-14ab7d188a4a
"/tmp/kubectl-edit-1024991799.yaml" 102L, 2771C

# i have changed it to 1.14 nginx from latest  ###
spec:
  containers:
  - image: nginx:latest 
    imagePullPolicy: Always
    name: nginx
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-c4bbq
      readOnly: true
[node1 ~]$ kubectl edit pod nginx
pod/nginx edited
[node1 ~]$ 
[node1 ~]$ kubectl describe pod nginx |grep -i Image
    Image:          nginx:1.14
    Image ID:       docker.io/library/nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
  Normal  Pulling    11m                kubelet            Pulling image "nginx:latest"
  Normal  Pulled     11m                kubelet            Successfully pulled image "nginx:latest" in 5.898887488s (5.898928988s including waiting)
  Normal  Pulling    55s                kubelet            Pulling image "nginx:1.14"
  Normal  Pulled     54s                kubelet            Successfully pulled image "nginx:1.14" in 658.981335ms (658.998234ms including waiting)
[node1 ~]$ 
[node1 ~]$ 
[node1 ~]$ kubectl describe pod nginx
Events:
  Type    Reason     Age                 From               Message
  ----    ------     ----                ----               -------
  Normal  Scheduled  12m                 default-scheduler  Successfully assigned default/nginx to node2
  Normal  Pulling    12m                 kubelet            Pulling image "nginx:latest"
  Normal  Pulled     12m                 kubelet            Successfully pulled image "nginx:latest" in 5.898887488s (5.898928988s including waiting)
  Normal  Killing    119s                kubelet            Container nginx definition changed, will be restarted
  Normal  Pulling    119s                kubelet            Pulling image "nginx:1.14"
  Normal  Created    118s (x2 over 12m)  kubelet            Created container nginx
  Normal  Started    118s (x2 over 12m)  kubelet            Started container nginx
  Normal  Pulled     118s                kubelet            Successfully pulled image "nginx:1.14" in 658.981335ms (658.998234ms including waiting)
[node1 ~]$ 
[node1 ~]$ kubectl get pods -o wide
NAME    READY   STATUS    RESTARTS        AGE   IP         NODE    NOMINATED NODE   READINESS GATES
nginx   1/1     Running   1 (3m49s ago)   14m   10.5.1.4   node2   <none>           <none>
[node1 ~]$

How to delete the pods - default namespace

[node1 ~]$ kubectl delete po nginx
pod "nginx" deleted
[node1 ~]$
[node1 ~]$ kubectl run nginx --image=nginx
pod/nginx created
[node1 ~]$ kubectl get pods -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP         NODE    NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          8s    10.5.1.5   node2   <none>           <none>

### To check logs of pods below is the command ####

[node1 ~]$ kubectl logs nginx            
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/07/08 20:34:05 [notice] 1#1: using the "epoll" event method
2023/07/08 20:34:05 [notice] 1#1: nginx/1.25.1
2023/07/08 20:34:05 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 
2023/07/08 20:34:05 [notice] 1#1: OS: Linux 4.4.0-210-generic
2023/07/08 20:34:05 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/07/08 20:34:05 [notice] 1#1: start worker processes
2023/07/08 20:34:05 [notice] 1#1: start worker process 29
2023/07/08 20:34:05 [notice] 1#1: start worker process 30
2023/07/08 20:34:05 [notice] 1#1: start worker process 31
2023/07/08 20:34:05 [notice] 1#1: start worker process 32
2023/07/08 20:34:05 [notice] 1#1: start worker process 33
2023/07/08 20:34:05 [notice] 1#1: start worker process 34
2023/07/08 20:34:05 [notice] 1#1: start worker process 35
2023/07/08 20:34:05 [notice] 1#1: start worker process 36
[node1 ~]$

How to Delete a pods with namespace

######### command to delte pods with namespace #####

[node1 ~]$ kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          3m52s
[node1 ~]$ kubectl delete namespace mehdiproject
namespace "mehdiproject" deleted

How to enter into Pods

#### To enter in the pod below is the command ####

[node1 ~]$ kubectl exec -it nginx sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# ls
bin   dev                  docker-entrypoint.sh  home  lib32  libx32  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc                   lib   lib64  media   opt  root  sbin  sys  usr
#

More from this blog

Welcome to Devops Learning !!!!

24 posts