Thursday, 5 October 2023

How to prints out the environment variables within each pod of k8s

 

for pod in $(kubectl get po --all-namespaces --output=jsonpath={.items..metadata.name}); do   namespace=$(kubectl get po --all-namespaces --field-selector=metadata.name=$pod --output=jsonpath={.items..metadata.namespace});   echo "Pod: $pod (Namespace: $namespace)";   kubectl exec -it -n $namespace $pod -- env; done




Wednesday, 4 October 2023

Count the occurrences of each unique image in k8s cluster

 
kubectl get pods --all-namespaces -o jsonpath="{..image}" |tr -s '[[:space:]]' '\n' |sort |uniq -c




Monday, 2 October 2023

Copying files to pod using kubectl

                                                   Copying files to pod using kubectl



-----

controlplane ~ ➜  kubectl get pods

NAME    READY   STATUS    RESTARTS   AGE

nginx   1/1     Running   0          39s

-----

controlplane ~ ➜  kubectl exec -it nginx /bin/bash

kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.


----

root@nginx:/# 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


----

root@nginx:/# mkdir raj

root@nginx:/# ls

bin   dev                  docker-entrypoint.sh  home  lib32  libx32  mnt  proc  root  sbin  sys  usr

boot  docker-entrypoint.d  etc                   lib   lib64  media   opt  raj   run   srv   tmp  var


----

root@nginx:/# cd raj/

root@nginx:/raj# pwd

/raj

root@nginx:/raj# exit

exit

----

controlplane ~ ➜  ls

sample.yaml

----

controlplane ~ ➜  kubectl cp sample.yaml nginx:/raj

----

controlplane ~ ➜  kubectl exec -it nginx /bin/bash

kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.


----

root@nginx:/# cd raj/

root@nginx:/raj# ls

sample.yaml


----






Resource types in Kubernetes

 

                    Resource types in Kubernetes


Below command will provide you resource type 

kubectl api-resource

If namespaced column are:- 
namespaced=true # All namespaced resources namespaced=false # All non-namespaced resources means cluster resources



kubectl api-resources --namespaced=true



kubectl api-resources --namespaced=false