Things you must know in Kubernetes

This series will contain multiple questions regarding kubernetes:
you can ask me anyhthing regarding this series either by commenting below or directly on my linkedIn post here:

Question 1.

List all the Pods in all namespaces sorted by their AGE in ascending order

kubectl get pods --all-namespaces --sort-by=.metadata.creationTimestamp | tac

Question 2.

Create a Static pod with image nginx and make sure that it is recreated/restarted automatically in case of any failure happens

apiVersion: v1
kind: Pod
metadata:
    name: static-nginx
spec:
    containers:
    - name: container-ngix
      image: nginx

Question 3.

Create a multi container pod with two containers as mentioned below:
a. Container 1
image: nginx

b. Container 2
image: busybox

Container 2 should sleep for 20 seconds

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: pod-multi
  name: pod-multi
spec:
  containers:
  - image: nginx
    name: containerl
  - image: busybox
    name: container2
    command:["sleep","20"]

Question 4.

Use Nginx 1.28.0 and create a pod named nginx-pod in webserver namespace belonging to the development environment (env=dev) and frontend tier (tier=front)
Imperative Approach:

kubectl run nginx-pod --image=nginx:1.28.0 --port=80 -n webserver --labels=env=dev,tier=front

Verify the namesapce & labels assigned to the pod:

kubectl get pod -n webserver --show-labels

Verify Nginx version used to create pod:

kubectl -n webserver describe pod nginx-pod

kubectl get svc -n webserver

Note down the random port number and open it in browser, Nginx will be running

Declarative Approach:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  namespace: webserver
  labels:
    env: dev
    tier: front
spec:
  containers:
    - name: nginx
      image: nginx:1.28.0
      ports:
        - containerPort: 80

Save the above yaml and run kubectl apply command to create pod:

kubectl apply -f 4-nginx-manifest.yaml

kubectl -n webserver describe pod nginx-pod

Question 5.

Create a new deployment with name webserver-ngx-128 with image nginx:1.28.0 and 1 replica. Next upgrade the deployment to version 1.29.1 using rolling update. Make sure that the version upgrade is recorded in the resource annotation

kubectl create deployment webserver-ngx-128 --image=nginx:1.28.0

kubectl get deploy
kubectl get pod

kubectl describe deploy webserver-ngx-128

Note: —record argument is deprecated and will be permanently removed in new upcoming kubernetes versions
refrence link: https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#checking-rollout-history-of-a-deployment

kubectl set image deploy webserver-ngx-128 nginx=nginx:1.29.1 --record


Updated kubectl command for rolling out deployment:

kubectl annotate deploy webserver-ngx-128 kubernetes.io/change-cause="Update nginx to 1.29.1" --overwrite

kubectl describe deploy webserver-ngx-128

kubectl rollout history deploy webserver-ngx-128

Question 6.

Use JSON PATH query to retrieve the oslmages of all the nodes and store it in a file
“all_Nodes_oslmage.txt” at root location

Question 7.

List the Internal IP of all nodes of the cluster and save the result in a file “lnternal_IP_List.txt” at root location