Skip to content

Instantly share code, notes, and snippets.

@zawyelwin
Last active December 1, 2020 07:58
Show Gist options
  • Save zawyelwin/122d91f76ed1ee41840527db034e2690 to your computer and use it in GitHub Desktop.
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.
$ 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
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'
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'
# 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
# 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
# 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
@zawyelwin
Copy link
Author

Other useful commands can be found here Kubectl Kubernetes CheatSheet and kubectl Cheat Sheet

@Zorgji
Copy link

Zorgji commented Dec 1, 2020

GG!

@Zorgji
Copy link

Zorgji commented Dec 1, 2020

Thanks for this useful cmds!

@Zorgji
Copy link

Zorgji commented Dec 1, 2020

  • 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

Source
Source

@zawyelwin
Copy link
Author

Thanks bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment