Kubernetes kubectl commands

A collection of kubectl commands that are handy.

~/.kube/config

Here’s the Kubernetes config. Note that I believe that minikube creates this on creation:

apiVersion: v1
clusters:
- cluster:
    certificate-authority: /Users/USERNAME/.minikube/ca.crt
    server: https://192.168.99.100:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: /Users/USERNAME/.minikube/apiserver.crt
    client-key: /Users/USERNAME/.minikube/apiserver.key

commands

  • Get pods in wide view (shows what nodes they’re running on):
kubectl get pods --all-namespaces -o wide
  • Get Kubernetes nodes labels:
kubectl get nodes --show-labels
  • Remove all pods from a node:
kubectl drain ip-10-1-1-2.ec2.internal
  • Cordon a node, allowing no more scheduling of pods (but existing pods are unaffected). Note that load balances will NOT route traffic to this node.
kubectl cordon ip-10-1-1-2.ec2.internal
  • Uncordon a node, allowing scheduling.
kubectl uncordon ip-10-1-1-2.ec2.internal
  • View config (config is stored at ~/.kube/config):
kubectl config view
  • If you have multiple clusters, you can specify cluster:
kubectl --cluseter=minikube get pods
  • Explain something in Kubernetes, such as a service:
kubectl describe service
  • List all services (comes from the expose command):
kubectl get service
  • Delete the exposed service (created above in the Installation section):
kubectl delete service hello-minikube
  • Bummer, it can’t handle a different etcd SSL config. I’m considering making them the same.
  • Once you get Kubernetes control plane deployed, you can run (get componentstatuses will have SSL troubles):
kubectl get componentstatuses
  • Get Kubernetes nodes.
kubectl get nodes
  • To get Kubernetes Pod logs, first get the pod name:
kubectl get pods --all-namespaces
kubectl logs kube-dns-v18-3y804 --namespace=kube-system -c kubedns
  • get Kubernetes pod CIDRs and kubelet IP addresses:
kubectl get nodes --output=jsonpath='{range .items[*]}{.status.addresses[?(@.type=="InternalIP")].address} {.spec.podCIDR} {"\n"}{end}'
  • Interactive Kubernetes pod terminal (using CentOS for the additional tools like curl and host):
kubectl run -i -t centos7interactive --restart=Never --image=centos:7 /bin/bash
  • Describe Kubernetes Pods (if you specifiy the pod name, it will describe only that pod):
kubectl describe pods