Technotes

Technotes for future me

Kubernetes

Kubernetes snippets

Scaling k8s daemonset down to zero

kubectl -n kube-system patch daemonset myDaemonset -p '{"spec": {"template": {"spec": {"nodeSelector": {"non-existing": "true"}}}}}'

Scaling up k8s daemonset

kubectl -n kube-system patch daemonset myDaemonset --type json -p='[{"op": "remove", "path": "/spec/template/spec/nodeSelector/non-existing"}]'

Scaling up k8s deployment/statefulset

kubectl scale deployment --replicas=5 my-app
kubectl scale --replicas=5 statefulset.apps/my-statefulset

Pods not ready on deployment / statefulset / daemonset

kubectl get pods -o json | jq -r '.items[] | select(.status.phase != "Succeeded" and (.status.phase != "Running" or ([ .status.conditions[] | select(.type == "Ready" and .status == "False") ] | length ) == 1 )) | .metadata.namespace + "/" + .metadata.name'

Pods with issues

kubectl get pods -A -o json | jq -r '.items[] | select(.status.phase != "Succeeded" and (.status.phase != "Running" or ([ .status.conditions[] | select(.type == "Ready" and .status == "False") ] | length ) == 1 )) | .metadata.namespace + "/" + .metadata.name'

Delete pods with certain filter

kubectl delete pod --field-selector="status.phase==Failed"

kubectl get po -A --all-namespaces -o json | jq  '.items[] | select(.status.reason!=null) | select(.status.reason | contains("Evicted")) | "kubectl delete po \(.metadata.name) -n \(.metadata.namespace)"' | xargs -n 1 bash -c

Filter not completed in cluster

watch kubectl get nodes -o wide \; echo \; kubectl get pods --all-namespaces -o wide \| egrep -v \' \([1-9]\)\/\\1 \' \| grep -e prod -e platform -e kube-system \| grep -v Completed

Node uptime in cluster

for I in $(kubectl get nodes | tail -n +2 | awk '{print $1}') ; do echo "$I - $(ssh $I uptime)" ; done

exec

kubectl exec nodehelloworld.example.com -- ls -l

describe

kubectl describe svc nodehelloworld-service

#Endpoints:                <ip>:3000
telnet <ip> 3000
Connected to <ip>
GET /

portforward

kubectl port-forward nodehelloworld.example.com 8081:3000
curl http://127.0.0.1:8081

nodeport

kubectl expose pod nodehelloworld.example.com --type=NodePort --name nodehelloworld-service

service

kubectl get svc
#NAME                     TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
#nodehelloworld-service   NodePort    <ip>   <none>        3000:30878/TCP   41s
curl http://127.0.0.1:30878

debug

kubectl attach nodehelloworld.example.com -i
kubectl exec -it nodehelloworld.example.com --bash
kubectl logs nodehelloworld.example.com
kubectl run -i --tty busybox --image=busybox --restart=Never --sh

Copy local file to pod

kubectl cp /tmp/my <some-namespace>/<some-pod>:/tmp/server

Copy pod file to local

kubectl cp <some-namespace>/<some-pod>:/tmp/server /tmp/my
Last updated on 4 Jan 2024
Published on 25 Dec 2019
Edit on GitHub