Useful kubectl command (lines) - Wed, Dec 2, 2020
Some useful kubectl commands that help in every day work
Useful kubectl commands
This post contains some useful kubectl
commands that make every day life easier. The full command reference can be found here
.
Deleting all resources of a specific type
Deleting all resources in a namespace of a specific type can easily be done by using the --all
switch:
kubectl delete --all taskruns.tekton.dev -n ci
taskrun.tekton.dev "source-to-docker-get-tag-and-sha-cdsqx" deleted
taskrun.tekton.dev "source-to-docker-source-to-docker-rr8xp" deleted
The command above deletes all tekton taskruns
in the namespace ci
. This switch comes handy, if a namespace became messy.
Getting logs from all containers in a Pod
Pods may contain a lot of containers. For example when using Tekton
, every step of a pipeline run is run in a separate container in a single pod. Getting every log of each step individually can thus be cumbersome.
The --all-containers
flag can be used to retrieve the logs of all containers:
kubectl logs -f -n ci source-to-docker-get-tag-and-sha-jlm6d-pod-wfx42 --all-containers
Templating without helm
Sometimes it is handy to parameterized manifests. Tools like HELM
could be used for that, but sometimes the additional effort introduced by theses tools is just not worth it.
For these cases using simple shell commands like cat
, environment variables and envsubst
can be a convenient alternative.
For example when using PodPresets for setting proxy variables as explained here
. The modified PodPreset manifest looks like this:
apiVersion: settings.k8s.io/v1alpha1
kind: PodPreset
metadata:
name: http-proxy
namespace: tekton-pipelines
spec:
env:
- name: http_proxy
value: "${http_proxy}"
- name: https_proxy
value: "${https_proxy}"
- name: no_proxy
value: "${no_proxy}"
Note the variable names for the proxy env entries. In order to replace these with the environment variables values a simple command line is sufficient:
cat proxy-presets.yaml | envsubst | kubectl apply -f -
The file is piped to envsubst using cat
which in turn replaces the variables names with the actual values of the environment variables. This output is then piped to kubectl using the -
flag which indicates to take the input from standard in.