Forum Topic

Thanks for your participation and contribution.


Back to Kubernetes

kubectl Cheat Sheet

Post a reply
2055 views
i90runner1
i90runner1

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

i90runner1
i90runner1

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

i90runner1
i90runner1

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

i90runner1
i90runner1

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

i90runner1
i90runner1

Cron Jobs

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

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

i90runner1
i90runner1

Kubernetes ingress hosts response code check

# Get hosts
 hosts=$(kubectl get ingresses --all-namespaces -o wide -o json | jq -r .items[].spec.rules[].host) 

# 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
i90runner1
i90runner1

Events sort by TimeSttamp

$ kubectl get events --sort-by='.metadata.creationTimestamp' -A

1-8 of 8

Reply to this discussion

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