Use context: kubectl config use-context k8s-c1-H
There are various Pods in all namespaces. Write a command into /opt/course/5/find_pods.sh
which lists all Pods sorted by their AGE (metadata.creationTimestamp
).
Write a second command into /opt/course/5/find_pods_uid.sh
which lists all Pods sorted by field metadata.uid
. Use kubectl
sorting for both commands.
Sorting Kubernetes Pods by Creation Timestamp and UID
When managing a Kubernetes cluster, you may need to sort and view Pods based on their creation timestamp or unique identifier (UID). In this post, we’ll demonstrate how to achieve this using simple shell scripts.
Useful Resource: kubectl Cheat Sheet
Before diving into the scripts, it’s worth mentioning that the kubectl cheat sheet is an excellent resource for many Kubernetes commands and operations. You can easily find it by searching for “cheat sheet” in the Kubernetes documentation.
Script 1: Find Pods Sorted by Creation Timestamp
The first script will list all Pods in the cluster, sorted by their creation timestamp:
1 2 3 4 5 6 |
# /opt/course/5/find_pods.sh kubectl get pod -A --sort-by=.metadata.creationTimestamp |
To execute the script, simply run:
1 2 3 4 5 |
➜ sh /opt/course/5/find_pods.sh |
This will output a list of Pods sorted by their age:
1 2 3 4 5 6 7 8 9 10 |
NAMESPACE NAME ... AGE kube-system kube-scheduler-cluster1-controlplane1 ... 63m kube-system etcd-cluster1-controlplane1 ... 63m kube-system kube-apiserver-cluster1-controlplane1 ... 63m kube-system kube-controller-manager-cluster1-controlplane1 ... 63m ... |
Script 2: Find Pods Sorted by UID
The second script will list all Pods, sorted by their unique identifier (UID):
1 2 3 4 5 6 |
# /opt/course/5/find_pods_uid.sh kubectl get pod -A --sort-by=.metadata.uid |
To execute this script, run:
1 2 3 4 5 |
➜ sh /opt/course/5/find_pods_uid.sh |
This will output a list of Pods sorted by their UID:
1 2 3 4 5 6 7 8 9 10 |
NAMESPACE NAME ... AGE kube-system coredns-5644d7b6d9-vwm7g ... 68m project-c13 c13-3cc-runner-heavy-5486d76dd4-ddvlt ... 63m project-hamster web-hamster-shop-849966f479-278vp ... 63m project-c13 c13-3cc-web-646b6c8756-qsg4b ... 63m ... |
These simple scripts are helpful for organizing and viewing Kubernetes Pods based on different criteria like creation time or UID. By leveraging the flexibility of kubectl
, you can easily manage and troubleshoot your Kubernetes environment more effectively.