orion
anderson

Handy Kubernetes Commands

Set current cluster

gcloud container clusters get-credentials <CLUSTER>

Run command on remote container

kubectl exec --stdin --tty <POD> -c <COMMAND>

Get CPU, memory use of all containers

kubectl top pod --containers --use-protocol-buffers

Get CPU, memory use of all nodes

kubectl top node --use-protocol-buffers

Create secret

kubectl create secret generic <SECRET_NAME> --from-literal=<key>=<value>

Copy files from local machine to pod

Useful when backing up or restoring persistent volume claims.

# copier.yaml

apiVersion: v1
kind: Pod
metadata:
  name: copier
spec:
  containers:
    - name: caddy
      image: caddy:alpine
      volumeMounts:
        - name: persistent-storage
          mountPath: /var/lib/ghost/content
  volumes:
    - name: persistent-storage
      persistentVolumeClaim:
        claimName: pvc-rwo
kubectl apply -f ./copier.yaml

Copy from local to pod

kubectl cp ./backup/dir copier:/srv/www

Copy from pod to local

kubectl cp copier:/srv/www ./backup/dir 

Perform rolling update of a service

kubectl rollout restart deployment <DEPLOYMENT_NAME>