Skip to content

Kubernetes

Pause a pod

kubectl scale --replicas=0 deployment/<your-deployment>

kubectl

Installation

# Ubuntu
$ sudo snap install kubectl --classic

krew

(
  set -x; cd "$(mktemp -d)" &&
  curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/krew.tar.gz" &&
  tar zxvf krew.tar.gz &&
  KREW=./krew-"$(uname | tr '[:upper:]' '[:lower:]')_$(uname -m | sed -e 's/x86_64/amd64/' -e 's/arm.*$/arm/' -e 's/aarch64$/arm64/')" &&
  "$KREW" install krew
)
# Update
$ kubectl krew version

ns

$ kubectl krew install ns

Short Names List

$ kubectl api-resources

knsk

$ wget -q https://raw.githubusercontent.com/thyarles/knsk/master/knsk.sh -O - | bash

Usage

Switch Contexts (Multiple Clusters)

kubectl config use-context CONTEXT_NAME

Config

WARNING: Kubernetes configuration file is group-readable. This is insecure. Location: /home/nicholas/.kube/config-turing-pi
chmod 600 /home/nicholas/.kube/config-turing-pi

Change the Reclaim Policy of a PersistentVolume

List the PersistentVolumes in your cluster:

kubectl get pv

Choose one of your PersistentVolumes and change its reclaim policy:

kubectl patch pv <your-pv-name> -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'

Apply ROLE label to worker nodes (#379).

kubectl label node worker-02 node-role.kubernetes.io/worker=worker
NAME          STATUS   ROLES    AGE    VERSION
turing-main   Ready    master   106m   v1.19.9+k3s1
worker-07     Ready    worker   105m   v1.19.9+k3s1
worker-06     Ready    worker   105m   v1.19.9+k3s1
worker-05     Ready    worker   105m   v1.19.9+k3s1
worker-04     Ready    worker   105m   v1.19.9+k3s1
worker-03     Ready    worker   105m   v1.19.9+k3s1
worker-02     Ready    worker   105m   v1.19.9+k3s1

Get a shell to the running container

kubectl exec --stdin --tty pod/name -- /bin/bash

Cheat Sheet

Config Permissions

chmod go-r ~/.kube/config

Resources

Back to top