Tips and tricks
Cheet sheet about all Kubernetes commands and contents.
Kubernetes cheat sheet
See all node are running with more details
kubectl get node --all-namespaces --output=wide
Short hand:
kubectl get node -A -o=wide
In the Debian distribution, you can also use the built-in watch
command, to run a command periodically:
watch --interval 1 kubectl get node --all-namespaces --output=wide
See all pods are running with more details and stay attach (--watch)
kubectl get pods --all-namespaces -o=wide --watch
Apply directly yaml file in command line
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-pod
image: mypod:latest
EOF
Set image to a delployment on the edge
kubectl set image -n my-namespace deployment/my-deployment my-pod=my-registry-username/my-image:1.1.0
Remove taint if the Kubernetes cluster is a Single Node Cluster
kubectl taint nodes <node-name> node-role.kubernetes.io/master:NoSchedule-
Add a label role to a node
A node can be labeled with a key and value. The key and value are separated by a colon. The key and value can be any string.
When running this command, kubectl get node
will show all nodes and their status, roles, age and version, as:
NAME | STATUS | ROLES | AGE | VERSION |
---|---|---|---|---|
master | Ready | control-plane,master | 3d19h | v1.21.5+k3s2 |
worker-0 | Ready | worker | 3d19h | v1.21.5+k3s2 |
worker-1 | Ready | worker | 3d19h | v1.21.5+k3s2 |
worker-2 | Ready | worker | 3d19h | v1.21.5+k3s2 |
To change the value of the role label, we can run the following command:
kubectl label nodes <list-of-nodes-separated-by-spaces> kubernetes.io/role=<role-name>
Here, we label the node with the key node-role.kubernetes.io/role
and the value is worker
.
This will allow us to filter nodes by the label node-role.kubernetes.io/role=worker
.
This is useful when you want to run a command on a specific node (Read documentation about NodeSelector).
Get a k8s secret value and decode it
kubectl get --all-namespaces secrets my-secret --template="{{index .data \"name-of-the-secret.key\"}}" | base64 --decode