====== Pods ======
==Debug running pod without shell access==
kubectl debug [existing-pod-name] -it --image alpine --share-processes --copy-to=my-new-debug-pod-name
kubectl debug -it [existing-pod-name] --share-processes --image=apko.kontain.me/busybox/curl:latest --target=[container_name]
==Delete terminated pods==
#!/bin/sh
for namespace in $(kubectl get ns | awk '{print $1}' | grep -v NAME); do
for pod in $(kubectl get pod -n $namespace --field-selector=status.phase!=Running | grep Terminated | awk '{print $1}' | grep -v NAME); do
kubectl delete pod -n "$namespace" "$pod"
done
done
== Get all pods running on a node ==
function kpodnode() {
kubectl get pods --all-namespaces -o wide --field-selector spec.nodeName=$1
}
====== Secrets ======
== Create secret manifest from literal ==
kubectl create secret generic [secret-name] -o yaml --dry-run --from-literal='ENV_VAR1=value1' \
--from-literal='ENV_VAR2=value2'
== Create secret from file ==
kubectl create secret generic [secret-name] -o yaml --dry-run --from-file [filepath]
==Delete all secrets starting with name wildcard==
kubectl get secret --no-headers=true | awk '/[secret-partial-name]/{print $1}' | xargs kubectl delete secret
====== Configmaps ======
==Delete all configmaps with custom label==
kubectl get configmap --no-headers=true -l label=value -o custom-columns=":metadata.name" | xargs kubectl delete configmap
====== Wait ======
== Wait for resource deletion ==
kubectl wait --for=delete kind/name -n default --timeout=1200s
====== CRD ======
== Get an object by querying a specific API version ==
kubectl get [crd].[version].[group]
Example:
kubectl get applications.v1alpha1.argoproj.io
====== GET ==========
== Get objects sorted by creation date ==
kubectl get [crd] --sort-by=.metadata.creationTimestamp
====== LABEL ==========
== Label a node ==
kubectl label nodes k8-master role=master
====== Volumes ======
==Resize statefulset PVC==
# for prometheus volumes, delete operator pod first
k scale deploy monitoring-operator --replicas=0
kubectl delete sts --cascade=orphan sts_name
# update pvc size
kubectl edit pvc pvc_name
# update pvc size in sts
kubectl -n ns edit sts sts_name