Skip to content

Instantly share code, notes, and snippets.

@vjove
Last active December 18, 2019 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vjove/08809ba265d5a99b172b4994755d6763 to your computer and use it in GitHub Desktop.
Save vjove/08809ba265d5a99b172b4994755d6763 to your computer and use it in GitHub Desktop.

Kubernetes Cheat Sheet

Restart deployments pods after a configmap change

date --utc '+{"spec": {"template": {"metadata": {"creationTimestamp": "%FT%TZ"}}}}'  > /dev/shm/$$.json
kubectl -n namespaceName patch deploy/deploymentName -p "$(cat /dev/shm/$$.json)"

Delete failed pods or jobs

For pods:

kubectl get pods -n NS --field-selector=status.phase=Failed | awk '{ if(NR>1)print $1}' | xargs kubectl delete pods -n NS

For pods, all namespaces:

kubectl get pods --all-namespaces --field-selector=status.phase=Failed | awk '{ if(NR>1)print $1 " " $2}' | xargs kubectl delete pods -n

For jobs:

kubectl get jobs -n NS -o json | jq -r '.items[] | select(.status.conditions[].type=="Failed") | .metadata.name' | xargs kubectl -n NS delete job

GCE: Run Container with a volume mounted

kubectl run -i --rm --tty ubuntu --overrides='
{
  "spec": {
    "containers": [
      {
        "name": "ubuntu",
        "image": "ubuntu:latest",
        "args": [
          "bash"
        ],
        "stdin": true,
        "stdinOnce": true,
        "tty": true,
        "volumeMounts": [{
          "mountPath": "/volume",
          "name": "volume-disk"
        }]
      }
    ],
    "volumes": [{
      "name":"volume-disk",
      "gcePersistentDisk": {"pdName": "DISK-NAME", "fsType": "ext4"}
    }]
  }
}
'  --image=ubuntu:latest --restart=Never -- /bin/bash    

GKE: Get the name of the cluster

In GKE, this command returns the name of the cluster where it's running:

curl -s http://metadata/computeMetadata/v1/instance/attributes/cluster-name -H "Metadata-Flavor: Google"

GKE: Remove a node from a cluster

This is a method to size down a pool by removing a node.

kubectl cordon NODE
kubectl drain NODE --ignore-daemonsets
gcloud compute instances describe --format='value[](metadata.items.created-by)' NODE
gcloud compute instance-groups managed delete-instances GROUP --instances=NODE --zone=ZONE

delete-instances scales down by 1 the node pool.

GKE: Access kube-api logs

kubectl proxy --port=8080 &
curl -s localhost:8080/logs/kube-apiserver.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment