Tuesday, January 10, 2017

kubectl Kubernetes Cheat Sheet

A complement to the official kubectl cheat sheet.

Getting kubectl

There's better ways to install it for permanent use, but here's a quick way for temporary use:
export PATH=/tmp:$PATH
cd /tmp; curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl; chmod 555 kubectl
Nodes
$ kubectl get nodes
$ kubectl get nodes/gke-hello-world-default-pool-9dbb0d2c-5qkl --show-labels
$ kubectl label nodes --all mylabel=myvalue
$ kubectl label nodes --all mylabel-

Namespaces

$ kubectl get all -n mynamespace
RBAC

What permissions do I have?
kubectl auth can-i --list
All clusterrolebindings:
kubectl get clusterrolebinding -o yaml
Role bindings for all namespaces:
kubectl get rolebinding --all-namespaces -o yaml
Privileges of current user:
kubectl create -f - -o yaml << EOF
apiVersion: authorization.k8s.io/v1
kind: SelfSubjectRulesReview      
spec:
  namespace: system
EOF

DaemonSet

Creating a daemonset:
$ echo 'apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: daemonset-example
spec:
  template:
    metadata:
      labels:
        app: daemonset-example
    spec:
      containers:
      - name: daemonset-example
        image: ubuntu:trusty
        command:
        - /bin/sh
        args:
        - -c
        - >-
          while [ true ]; do
          echo "DaemonSet running on $(hostname)" ;
          sleep 10 ;
          done
' | kubectl create -f -
$ kubectl delete daemonset daemonset-example

Get a shell in a container
$ kubectl run --rm=true -i --tty ubuntu --image=ubuntu -- /bin/bash
# Or with an existing container
$ kubectl exec -it shell-demo -- /bin/bash