In the previous post we’ve looked into Kubernetes System commands. Now it’s time to start diving deeper into some of the most fundamental building blocks of the Kubernetes ecosystem. Probably the best start is to look deep into the platform and learn about one of the smallest, atomic, yet very important resource – the Pod.
Pod, in its simplest implementation, is essentially your Container running on Kubernetes. You can, technically, run multiple containers in a single Pod. However, for this tutorial we’ll keep it simple to one Pod = one Container.
More details: Kubernetes Pods
Create Pod
Let’s start with creating Pod workloads. You can generally do this either by adopting Imperative or Declarative syntax with the kubectl create command.
More Details: kubectl create
Imperative
kubectl create po [POD NAME]
Declarative
kubectl apply -f [POD TEMPLATE].yaml
[POD TEMPLATE].yaml
apiVersion: v1
kind: Pod
metadata:
name: [POD NAME]
labels:
role: my-role
spec:
containers:
- name: my-container
image: nginx
ports:
- name: my-container
containerPort: 80
protocol: TCP
List Pods
To find out what Pods are running and in which Namespaces you’ll probably want to use some of these kubectl get po commands. It’s also possible to filter the queries on Pod attributes or fields.
More Details: kubectl get
List All Pods in Namespace
kubectl get po -n [NAMESPACE]
List All Pods in All Namespaces
kubectl get po --all-namespaces
List All Pods with Labels
kubectl get po --all-namespaces --show-labels
List All Pods by Status – Not Running
kubectl get pods --field-selector=status.phase!=Running --all-namespaces
List All Pods by Status – Running
kubectl get pods --field-selector=status.phase==Running --all-namespaces
Pod Details
These commands will allow you to look deeper into your chosen Pod where you’ll find out all the Pod details, labels, specification in either YAML or Wide output.
More Details: kubectl describe
Get Pod
kubectl get po [POD NAME] -n [NAMESPACE]
Get Pod – Wide Output
kubectl get po [POD NAME] -n [NAMESPACE] -o wide
Get Pod – YAML Output
kubectl get po [POD NAME] -n [NAMESPACE] -o yaml
Export Pod – YAML Output to File
kubectl get po [POD NAME] -n [NAMESPACE] -o yaml --export > [FILE NAME].yaml
Describe Pod
kubectl describe po [POD NAME] -n [NAMESPACE]
Patch Pod
To modify an existing Pod directly on the Kubernetes data plane use the kubectl patch command.
More Details: kubectl patch
Patch Pod with bash
kubectl patch po [POD NAME] -n [NAMESPACE] -p "$(cat [POD TEMPLATE].yaml)"
Patch Pod with Powershell
kubectl patch po [POD NAME] -n [NAMESPACE] -p $(Get-Content [POD TEMPLATE].yaml -Raw)
Pod Logs
To access Logs on a running Pod, mainly for debugging or troubleshooting, use the kubectl logs command.
More Details: kubectl logs
Print Pod Logs
kubectl logs [POD NAME] -n [NAMESPACE]
Stream Pod Logs to STDOUT
kubectl logs [POD NAME] -n [NAMESPACE] -f
Run Pod
Now it’s time to start playing with some Pods where you’ll be able to start up a Pod, start & enter (exec) into it or event remove the Pod after exiting from it using the kubectl run command.
More Details: kubectl run
Start Pod
kubectl run [POD NAME] -n [NAMESPACE] --image=[IMAGE NAME] --generator=run-pod/v1
Start Pod & Exec Into
kubectl run -it [POD NAME] -n [NAMESPACE] --image=[IMAGE NAME] --generator=run-pod/v1 -- /bin/ash
Start Pod & Exec Into & Remove it on Exit
kubectl run -it --rm [POD NAME] -n [NAMESPACE] --image=[IMAGE NAME] --generator=run-pod/v1 -- /bin/ash
Execute Pod
If there’s a Pod that is already up and running then entering into it with kubectl exec command will allow you to establish a shell session inside.
More Details: kubectl exec
Exec Into Pod
kubectl exec -it [POD NAME] -n [NAMESPACE] -- /bin/ash
Delete Pod
Finally, to dispose of a Pod simply use the kubectl delete command allowing you to target single Pod or even filter on labels or namespaces.
More Details: kubectl delete
Imperative
Delete Pod By Name
kubectl delete po [POD NAME] -n [NAMESPACE]
Delete Pod By Label
kubectl delete pods -l name=[LABEL] -n [NAMESPACE]
Delete All Pods in Namespace
kubectl delete pods -n [NAMESPACE] --all
Declarative
kubectl delete -f [POD TEMPLATE].yaml
<< Kubernetes System | Kubernetes Pods | Kubernetes Deployments >>