Forum Topic

Thanks for your participation and contribution.


Back to Kubernetes

kubectl Cheat Sheet

Post a reply
2915 views
i90runner
i90runner

List of Namespaces

$ kubectl get namespaces

Delete Namespace

kubectl delete namespace cpu-example-ns

Get Deployment details

$ kubectl get deployment -n di-dev --output=json kubectl get deployment -n di-dev

Node Info

$ kubectl get nodes
$ kubectl describe nodes

Allocated resources:
  (Total limits may be over 100 percent, i.e., overcommitted.)
  CPU Requests  CPU Limits  Memory Requests  Memory Limits
  ------------  ----------  ---------------  -------------
  1650m (10%)   1550m (9%)  3525Mi (10%)     3325Mi (10%)
Events:         <none>

Read Container Logs

$ kubectl logs container-abc -n namespace123

Drain Nodes

$ kubectl get nodes $ kubectl drain aks-agentpool-node-name --ignore-daemonsets --delete-local-data

Sort option

kubectl top pod --all-namespaces | sort --reverse --key 4 --numeric

Last updated

i90runner
i90runner

Install latest kubectl Version

curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl"
chmod +x ./kubectl 
sudo mv ./kubectl /usr/local/bin/kubectl
kubectl -v --client

Last updated

i90runner
i90runner

Rename Contexts

* Get context information (you need the CLUSTER name and AUTHINFO for the next step): 
kubectl config get-contexts <old-context-name> 
* Create new context:
kubectl config set-context <new-context-name> --cluster <cluster-from-old-context> --user <auth-info-from-old-context>
*Delete old context:
kubectl delete-context <old-context-name>

Last updated

i90runner
i90runner

Rolling Restart of Deployments

namespace="kubernetes-production-namespace"

deployments="$(kubectl get deployment -n $namespace -o json | jq -r .items[].metadata.name)"

for deployment in $deployments ; do kubectl rollout restart deployment $deployment -n $namespace ; done

Last updated

i90runner
i90runner

Cron Jobs

kubectl get cronjob descheduler -n kube-system  -o yaml
i90runner
i90runner

jq leaf paths

$ kubectl get ingresses --all-namespaces -o json |jq  'leaf_paths | join(".")'
"apiVersion"
"items.0.apiVersion"
"items.0.kind"
"items.0.metadata.annotations.kubectl.kubernetes.io/last-applied-configuration"
"items.0.metadata.annotations.nginx.ingress.kubernetes.io/limit-rps"
"items.0.metadata.annotations.nginx.ingress.kubernetes.io/proxy-body-size"
"items.0.metadata.annotations.nginx.ingress.kubernetes.io/whitelist-source-range"
"items.0.metadata.creationTimestamp"
"items.0.metadata.generation"
"items.0.metadata.name"
"items.0.metadata.namespace"
"items.0.metadata.resourceVersion"

$ kubectl get ingresses --all-namespaces -o json |jq  '{name: .items[].metadata.name}'

Last updated

i90runner
i90runner

Kubernetes ingress hosts response code check

# Get hosts
 # hosts=$(kubectl get ingresses --all-namespaces -o wide -o json | jq -r .items[].spec.rules[].host) 
 hosts=$(kubectl get ingresses -n itester-kubernetes-uat -o wide -o json | jq -r '.items[].spec.rules[].host'|sort|uniq) 

# Loop through hosts
for host in $hosts ; do echo $host $(curl  -s -o /dev/null -w "%{http_code}"  https://$host ) ; done

h1bsalary.org 404
bbbb.tech.net 302
123.tec.org 200

Last updated

i90runner
i90runner

Events sort by TimeSttamp

$ kubectl get events --sort-by='.metadata.creationTimestamp' -A
i90runner
i90runner
 kubectl get ingresses --all-namespaces -o json |jq '.|keys'

kubectl get ingresses --all-namespaces -o json |jq 'leaf_paths | join(".")'
i90runner
i90runner

Loop through service endpoints and Invoke API and diff

ns="kubernetes-namespace"
svc="train-svc"

## Get list of hosts
hosts=$(kubectl get ingresses -n $ns -o wide -o json | jq -r '.items[].spec.rules[].host'|sort|uniq) 

### Loop through hosts
for host in $hosts ; 
do 
    # Get endpoints for a service 
    endpoints=$(kubectl get endpoints $svc -o jsonpath="{.subsets[*].addresses[*].ip}" -n $ns)

    # Loop through endpoints
    for endpoint in $endpoints ; do echo $endpoint ;  echo curl -o $host-$svc-$endpoint.json -H "Host: $host"   http://$endpoint:8080/api/configuration  ; 
    $(curl -o $host-$svc-$endpoint.json -H "Host: $host"   http://$endpoint:8080/api/configuration) ; done

done

### Compare the output files based on pattern
for host in $hosts ; 
do 
    # api response files by host
    configs=$(ls | grep $host-services) ;
    printf "$configs\n" ;
    diff $configs
done
i90runner
i90runner

Service Account Token

$ kubectl get secret $(kubectl get serviceaccount kubectl-apiaccess-sa -o jsonpath="{.secrets[0].name}") -o jsonpath="{.data.token}" | base64 --decode
i90runner
i90runner

Restart Deployments and namespaces

https://blog.boot.dev/open-source/how-to-restart-all-pods-in-a-kubernetes-namespace/.

deploys=`kubectl -n $1 get deployments | tail -n +2 | cut -d ' ' -f 1`
for deploy in $deploys; do
  kubectl -n $1 rollout restart deployments/$deploy
done

namespaces=`kubectl get ns | grep itester-kubernetes-review   | tail -n +2 | cut -d ' ' -f 1`
for namespace in $namespaces; do
  kubectl -n $1 rollout restart 
done

working version

kubectl get ns -o json | jq -r '.items[].metadata.name'| grep nmselector|uniq>rvw.txt

while read -r line ; do echo namespace $line ; kubectl get deployment -n $line  ; done <rvw.txt

Last updated

1-12 of 12

Reply to this discussion

You cannot edit posts or make replies: You should be logged in before you can post.