Forum Topic

Thanks for your participation and contribution.


Back to Kubernetes

kubectl Cheat Sheet

Post a reply
1958 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

1-4 of 4

Reply to this discussion

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