Last active
December 1, 2020 07:58
-
-
Save zawyelwin/122d91f76ed1ee41840527db034e2690 to your computer and use it in GitHub Desktop.
Some handy kubectl commands to make your life easier.Collected from some great resources I found out and some tweaks from their implementations. All credits goes to the original authors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cd /etc/kubernetes/pki/ | |
$ mv {apiserver.crt,apiserver-etcd-client.key,apiserver-kubelet-client.crt,front-proxy-ca.crt,front-proxy-client.crt,front-proxy-client.key,front-proxy-ca.key,apiserver-kubelet-client.key,apiserver.key,apiserver-etcd-client.crt} ~/certs-bak | |
$ kubeadm init phase certs all | |
$ cd /etc/kubernetes/ | |
$ mv {admin.conf,controller-manager.conf,kubelet.conf,scheduler.conf} ~/certs-bak | |
$ kubeadm init phase kubeconfig all | |
$ reboot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
find /etc/kubernetes/pki/ -type f -name "*.crt" -print|egrep -v 'ca.crt$'|xargs -L 1 -t -i bash -c 'openssl x509 -noout -text -in {}|grep After' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
kubectl get pod --all-namespaces --field-selector="status.phase==Failed" | |
kubectl delete pod --all-namespaces --field-selector 'status.phase==Failed' | |
kubectl get pod -n $NAMESPACE --field-selector="status.phase==Failed" | |
kubectl delete pod -n $NAMESPACE --field-selector 'status.phase==Failed' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# suspend cronjob via patch | |
kubectl patch cronjobs <job-name> -p '{"spec" : {"suspend" : true }}' | |
# delete completed cronjob pods | |
kubectl delete jobs --field-selector status.successful=1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# scale all deployments under all namespaces. | |
for ns in $(kubectl get ns --no-headers | awk '{print $1}'); | |
do for deploy in $(kubectl -n $ns get deploy --no-headers --ignore-not-found | awk '{print $1}'); | |
do kubectl -n $ns scale deploy $deploy --replicas=1; done; | |
done; | |
# scale all deployments under a namespace. | |
for i in $(kubectl get deploy -n $NAMESPACE | awk '{print $1}'); do kubectl scale deploy --replicas=1 -n $NAMESPACE $i; done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# scale all statefulsets under all namespaces. | |
for ns in $(kubectl get ns --no-headers | awk '{print $1}'); | |
do for sts in $(kubectl -n $ns get sts --no-headers --ignore-not-found | awk '{print $1}'); | |
do kubectl -n $ns scale sts $sts --replicas=1; done; | |
done; | |
# scale all statefulsets under a namespace. | |
for i in $(kubectl get sts -n $NAMESPACE | awk '{print $1}'); do kubectl scale sts --replicas=1 -n $NAMESPACE $i; done |
GG!
Thanks for this useful cmds!
- Deleting stuck ns
(
NAMESPACE=STUCK_NS
kubectl proxy &
kubectl get namespace $NAMESPACE -o json |jq '.spec = {"finalizers":[]}' > temp.json
curl -k -H "Content-Type: application/json" -X PUT --data-binary @temp.json 127.0.0.1:8001/api/v1/namespaces/$NAMESPACE/finalize
)
- Deleting stuck crd
kubectl patch crd/STUCK_CRD -p '{"metadata":{"finalizers":[]}}' --type=merge
Thanks bro
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Other useful commands can be found here Kubectl Kubernetes CheatSheet and kubectl Cheat Sheet