kubectl
Install
sudo snap install kubectl --classic
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 volumeName
from pvc:
kubectl -n oas get pvc prometheus-0 -o=jsonpath='{.spec.volumeName}'
Show .status.reason
from challenge:
kubectl -n oas-apps get challenge oas-rocketchat -o jsonpath='{.status.reason}'
List All Container Images Running in a Cluster
kubectl get pods --all-namespaces \
-o jsonpath="{.items[*].spec.containers[*].image}" | uniq
List PVC name and selectedNode:
kubectl get pvc -A \
-o jsonpath='{range .items[*]}{@.metadata.annotations.volume\.kubernetes\.io\/selected-node}{" "}{@.metadata.name}{"\n"}{end}' # <!-- markdownlint-disable-line -->
Use field-selector to filter resource by certain keys
Examples:
Show all PVCs not setup by flux:
kubectl get pvc -l kustomize.toolkit.fluxcd.io/namespace!=flux-system -A
Show all resources with single-sign-on
velero backup label:
kubectl get all -A -l stackspin.net/backupSet=single-sign-on
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
-
Rolling Updates with Kubernetes Deployments
kubectl -n varac rollout restart deployment website kubectl -n varac rollout status deployment website
Show all available APIs
sting all resources in a namespace:
kubectl api-resources --verbs=list --namespaced -o name
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
alias kcd='kubectl config set-context $(kubectl config current-context) --namespace'
kcd nextcloud
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
kubectl -n gitlab-nextcloud scale --replicas=0 deployment/nextcloud-test
Dealing with multiple clusters/contexts
To use different contexts, set the KUBECONFIG
env var like this:
KUBECONFIG=/home/varac/.kube/admin@hexacab.org.yml:/home/varac/.kube/admin@varac-oas.yml
Then you can use:
kubectl config get-contexts
kubectl config set-context varac-oas
Context switching apps
see ./contexts.md