kubectl¶
Install
Completion¶
Prevent kubectl from trying to connect to cluster for completion:
Useful Tools¶
- For Plugins see
./kubectl-plugins.md - fubekctl Reduces repetitive interactions with kubectl
- kubecolor colorizes kubectl output
- watch changes in ConfigMap and Secrets and then restart pods
Usage¶
Use jsonpath to parse output¶
Show pod name and assigned node:
Show .status.reason from cert-manager challenge:
List All Container Images Running in a Cluster
kubectl get pods --all-namespaces \
-o jsonpath="{.items[*].spec.containers[*].image}" \
| tr " " "\n" | uniq | sort | grep -i bitnami
Show volumeName from PVC:
List PVC name and selectedNode:
kubectl get pvc -A \
-o jsonpath='{range .items[*]}{@.metadata.annotations.volume\.kubernetes\.io\/selected-node}{" "}{@.metadata.name}{"\n"}{end}' \
| sort
kubectl get pvc -A -o \
jsonpath='{range .items[*]}{@.metadata.name}{" "}{@.spec.storageClassName}{"\n"}{end}'
List all images used by pods/containers:
kubectl get pods --all-namespaces \
-o jsonpath="{range .items[*]}{@.metadata.namespace}{','}{@.metadata.name}{','}\
{@.spec['initContainers', 'containers'][*].image}{'\n'}{end}"
Use field-selector to filter resource by certain keys¶
Examples:
Show all PVCs not setup by flux:
Show all resources with single-sign-on velero backup label:
Etc:
kubectl get -n kube-system pods -lname=tiller --field-selector=status.phase=Running
kc -n oas get pod kube-prometheus-stack-prometheus-node-exporter-72qz8 -o=jsonpath='{.spec.containers[].resources}'
Show events filtering for multiple fields:
kubectl -n stackspin-apps get events \
--field-selector 'involvedObject.name=nextcloud,type!=Normal' -o yaml
Rolling updates of an app/pod¶
kubectl -n varac rollout restart deployment website kubectl -n varac rollout
status deployment website
Show all available APIs¶
sting all resources in a namespace:
Get all resources with their namespace¶
kubectl get -A hr --template '{{range .items}}{{.metadata.namespace}}/{{.metadata.name}}{{"\n"}}{{end}}'
Show event timestamps instead of relative time¶
kubectl -n stackspin get events -o --no-headers=true \
--field-selector type!=Normal \
custom-columns=FirstSeen:.firstTimestamp,LastSeen:.lastTimestamp,Count:.count,\
Component:.source.component,Object:.involvedObject.name,Type:.type,Reason:.reason,Message:.message
Trigger rollout of new container¶
kubectl -n presentation patch deployment presentation -p \
"{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}"
Set current namespace¶
Get all resources¶
kubectl get all ...
kubectl api-resources --verbs=list --namespaced -o name | \
xargs -n 1 kubectl get --show-kind --ignore-not-found -n gitlab-nextcloud
See also ./kubectl-plugins.md for the ketall plugin
Scale deployment¶
Failed resources¶
List all failed jobs:
Delete all failed jobs:
Context switching apps¶
see ./contexts.md