Cheat Sheet for K8s

Kubernetes

Client Configuration

  • Setup autocomplete in bash; bash-completion package should be installed first
1source <(kubectl completion bash)
  • View Kubernetes config
1kubectl config view
  • View specific config items by json path
1kubectl config view -o jsonpath='{.users[?(@.name == "k8s")].user.password}'
  • Set credentials for foo.kuberntes.com
1kubectl config set-credentials kubeuser/foo.kubernetes.com --username=kubeuser --password=kubepassword
  • Set active namespace
1kubectl config set-context --current --namespace=namespace_name

Viewing, Finding resources

  • List all services in the namespace
1kubectl get services
  • List all pods in all namespaces in wide format
1kubectl get pods -o wide --all-namespaces
  • List all pods in json (or yaml) format
1kubectl get pods -o json
  • Describe resource details (node, pod, svc)
1kubectl describe nodes my-node
  • List services sorted by name
1kubectl get services --sort-by=.metadata.name
  • List pods sorted by restart count
1kubectl get pods --sort-by='.status.containerStatuses[0].restartCount'
  • Rolling update pods for frontend-v1
1kubectl rolling-update frontend-v1 -f frontend-v2.json
  • Scale a replicaset named ‘foo’ to 3
1kubectl scale --replicas=3 rs/foo
  • Scale a resource specified in “foo.yaml” to 3
1kubectl scale --replicas=3 -f foo.yaml
  • Execute a command in every pod / replica
1for i in 0 1; do kubectl exec foo-$i -- sh -c 'echo $(hostname) > /usr/share/nginx/html/index.html'; done

Manage Resources

  • Get documentation for pod or service
1kubectl explain pods,svc
  • Create resource(s) like pods, services or daemonsets
1kubectl create -f ./my-manifest.yaml
  • Apply a configuration to a resource
1kubectl apply -f ./my-manifest.yaml
  • Start a single instance of Nginx
1kubectl run nginx --image=nginx
  • Create a secret with several keys
 1cat <<EOF | kubectl create -f -
 2apiVersion: v1
 3kind: Secret
 4metadata:
 5 name: mysecret
 6type: Opaque
 7data:
 8 password: $(echo "s33msi4" | base64)
 9 username: $(echo "jane"| base64)
10EOF
  • Delete a resource
1kubectl delete -f ./my-manifest.yaml

Monitoring & Logging

  • Deploy Heapster from Github repository
1kubectl create -f deploy/kube-config/standalone/
  • Show metrics for nodes
1kubectl top node
  • Show metrics for pods
1kubectl top pod
  • Show metrics for a given pod and its containers
1kubectl top pod pod_name --containers
  • Dump pod logs (stdout)
1kubectl logs pod_name
  • Stream pod container logs (stdout, multi-container case)
1kubectl logs -f pod_name -c my-container

Interacting with running pods

  • Run command in pod
1kubectl exec pod_name -- command_name
  • Run command in pod with multiple containers
1kubectl exec pod_name -c container_name -- command_name
  • Get terminal of pod
1kubectl exec -it pod_name /bin/sh
  • Get terminal of a container running in pod with multiple containers
1kubectl exec -it pod_name -c container_name /bin/sh

Force Fsck on Boot
Cheat Sheet for Javascript&Node.js&Vue.js