Home » Kubernetes » What is Kubernetes pod? Pod Cheat Sheet

What is Kubernetes pod? Pod Cheat Sheet

What is Kubernetes pod? Pod Cheat Sheet
https://kubernetes.io/docs/tasks/configure-pod-container/assign-cpu-resource/
https://kubernetes.io/docs/concepts/workloads/pods/
Pods are the smallest and primary deployable objects of computing that you can create and manage in Kubernetes Cluster.
A Pod signifies a single instance of a running process in your Kubernetes cluster, The containers of pod are managed as a single a single entity and share the all resources of a pod.
A Pod is a group of one of more containers such as docker containers with shared network resources and storage, and instructions and specifications for how to run containers.
The Pods in a Kubernetes cluster are basically used in two main methods.
• Single container per Pod. The “one-container-per-Pod” model is that the commonest Kubernetes use case; In this situation, you’ll think about a Pod as a wrapper around one container; Kubernetes manages Pods instead of managing the containers directly.
• Pods that run multiple containers that need to collective work. A Pod can encapsulate an application composed of multiple co-located containers that are tightly coupled and need to share resources. These co-located containers form a single cohesive unit of service—for example, one container serving data stored in a shared volume to the public, while a separate sidecar container refreshes or updates those files. The Pod wraps these containers, storage resources, and an ephemeral network identity together as a single unit.
If you didn’t install Kubernetes kindly click here installation guide
Here is the example to create a pod, from a yaml file.

#vi nginx-pod.yaml
apiVersion: v1

kind: Pod

metadata:

name: nginx

spec:

containers:

name: nginx

image: nginx:1.23.3

ports:

containerPort: 80

Run the following command.

[root@client01 ~]# kubectl apply -f nginx-pod.yaml
pod/nginx created
[root@client01 ~]#

Verify and list the created pod in a name space

[root@client01 ~]# kubectl get pod
[root@client01 ~]# kubectl get pods
[root@client01 ~]# kubectl get po

To get list of pod with watch

[root@client01 ~]# kubectl get pod nginx –watch
[root@client01 ~]# kubectl get pod -A –watch
NAME   READY    STATUS      RESTARTS    AGE
nginx    1/1           Running        0          54s

To create the pod from STDIN

cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
    containers:
    - name: nginx
      image: nginx:latest
EOF

To delete the pod running below command.

[root@client01 ~]# kubectl delete -f nginx-pod.yaml

To get the detailed information about pod such as Name, Namespace, Nodes, IP etc execute the following command

[root@client01 ~]# kubectl describe po nginx

If you need to check the logs of running pod than need to run following command.

[root@client01 ~]# kubectl logs -f nginx


Use the following command if pod has multiple containers

[root@client01 ~]# kubectl logs nginx -c <container>

If you need to output the logs for all pods with a label

[root@client01 ~]#kubectl logs -l <label_name>=<label_value>

To manage and assign the pod resources like as CPU, Memory has a look into below example.

apiVersion: v1

kind: Pod

metadata:

name: nginx

spec:

containers:

name: nginx

image: nginx:1.23.3

resources:

requests:

memory: “64Mi”

cpu: “250m”

limits:

memory: “128Mi”

cpu: “500m”

ports:

containerPort: 80

[root@client01 ~]# kubectl apply -f nginx-pod.yaml

In above example pod requested 64Mi memory and 250m CPU, while limits options preventing the maximum memory and cpu.
To get the output of a pod in yaml format

[root@client01 ~]# kubectl get pod nginx -o yaml

To get output of a pod in json format

[root@client01 ~]# kubectl get pod nginx -o json

To store the output of a pod in yaml format.

[root@client01 ~]# kubectl get pod nginx -o yaml >> output.yaml

To get the wide ouput like as IP, Node Name, NOMINATED NODE READINESS GATES

[root@client01 ~]# kubectl get pod nginx -o wide

To get list of all pods in a namespace

[root@client01 ~]# kubectl get pods -n default

Note here “default” is the namespace name.
Create the pod from image.

[root@client01 ~]# kubectl run nginx –image=nginx

Start a nginx pod and let the container expose port 8888

[root@client01 ~]#kubectl run nginx –image=nginx:latest –port=8888

Dry run; print the corresponding API objects without creating them

[root@client01 ~]#kubectl run nginx –image=nginx –dry-run=client

Start a nginx pod and set environment variables “DNS_DOMAIN=cluster” and “POD_NAMESPACE=default” in the container

[root@client01 ~]# kubectl run nginx –image= nginx –env=”DNS_DOMAIN=cluster” –env=”POD_NAMESPACE=default”

Start a nginx pod and set labels “app= nginx ” and “env=prod” in the container

[root@client01 ~]# kubectl run nginx –image= nginx –labels=”app= nginx,env=prod”

Start a nginx pod, but overload the spec with a partial set of values parsed from JSON

[root@client01 ~]# kubectl run nginx –image=nginx –overrides='{ “apiVersion”: “v1”, “spec”: { … } }’

Start a busybox pod and keep it in the foreground, don’t restart it if it exits

[root@client01 ~]# kubectl run -i -t busybox –image=busybox –restart=Never

Run the pod in interactive shell command mode.

[root@client01 ~]# kubectl run -i –tty nginx –image=nginx – sh

Execute a command after creating a pod

[root@client01 ~]# kubectl run nginx –image=nginx — sleep 20000

Run the command in running container

[root@client01 ~]# kubectl exec -it nginx — ls -ld /var

To update patch in running pod

[root@client01 ~]# kubectl patch pod nginx -p ‘{“spec”:{“containers”:[{“name”:”kubernetes-serve-
hostname”,”image”:”new image”}]}}’

To get utilization metrics of a pod

[root@client01 ~]#kubectl top pod nginx

To get utilization metrics of all containers of pod

[root@client01 ~]#kubectl top pod pod-name –containers

About

I am founder and webmaster of www.linuxpcfix.com and working as a Sr. Linux Administrator (Expertise on Linux/Unix & Cloud Server) and have been in the industry since more than 14 years.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Time limit is exhausted. Please reload the CAPTCHA.

Categorized Tag Cloud