My Own Kubectl 101

Background

I try to introduce Kubernetes to people recently, except for the K8S basic concept (Master, Node, Deployment, Pod, Service, …etc), there is another important thing need to be mentioned: How to use kubectl to communicate with the K8S cluster. After all, kubectl occupies engineers’ daily life, hence, it’s worthwhile to spend more time to setup a more efficient and productive environment.

Kubectl Shell AutoCompletion

There are too many characters need to type when managing K8S resources through kubectl, e.g. kubectl get pod, kubectl logs pod_name, …etc, It not only wastes lots of time, but also causes typo easily. So first, I recommend to enable shell autocompletion to save the valuable time. (For other platform instruction refer to Here, e.g. CentOS, Zsh)

  • Install kubectl
~$ brew install kubernetes-cli
# If you are tired to type many words in shell, you also cloud make alias for them
  • Install bash-completion
## If running Bash 3.2 included with macOS
~$ brew install bash-completion
## or, if running Bash 4.1+
~$ brew install bash-completion@2
  • Follow the “caveats” section of brew’s output to add the appropriate bash completion path to your local .bashrc.
  • If you have installed kubectl manually, you need to add kubectl autocompletion to the bash-completion:
~$ kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl

Multiple K8S Cluster Management

It’s easy to manage kubeconfig if there is only one cluster need to be taken care, e.g. Adding export KUBECONFIG=/Users/smalltown/kubeconfig into ~/.bashrc, but if there are multiple cluster, how can we dispose it?

I used to have N+1 files, N means kubeconfig files for each K8S cluster, and the 1 for merging the N kubeconfig files, below illustrates how to achieve that (learning more about KUBECONFIG) , and please keep the user’s name unique for each kubeconfig file

  • Here’s kubeconfig file for k8s-dev:
apiVersion: v1 
clusters:
- cluster:
certificate-authority: k8s-dev.crt
server: https://k8s-dev
name: k8s-dev
contexts:
- context:
cluster: k8s-dev
user: k8s-dev-smalltown
name: k8s-dev
current-context: k8s-dev
kind: Config
preferences: {}
users:
- name: k8s-dev-smalltown
user:
client-certificate: k8s-dev-apiserver.crt
client-key: k8s-dev-apiserver.key
  • And here’s kubeconfig for k8s-prod:
apiVersion: v1 
clusters:
- cluster:
certificate-authority: k8s-prod.crt
server: https://k8s-prod
name: k8s-prod
contexts:
- context:
cluster: k8s-prod
user: k8s-prod-smalltown
name: k8s-prod
current-context: k8s-prod
kind: Config
preferences: {}
users:
- name: k8s-prod-smalltown
user:
client-certificate: k8s-prod-apiserver.crt
client-key: k8s-prod-apiserver.key
  • Create a new empty file called k8s-all-phase-merge, and add below command into ~/.bashrc, let KUBECONFIG variable export when bash starts
export KUBECONFIG=k8s-all-phase-merge:k8s-dev:k8s-prod

Switch Contexts/Namespaces Between Several Clusters

You might find that I do not introduce the context concept from previous section, because I feel it’s annoying to use kubectl config subcommand, I prefer to use open source tool kubectx to switch context, learning more about kubectx

  • Install kubectx for macOS
~$ brew install kubectx

kubectx

Switch from multiple contexts/clusters

# type kubectx in command line, then click tab
~$ kubectx
k8s-dev k8s-prod
# then choose the context you want to switch to, don't forget to click tab to autocomplete
~$ kubectx k8s-prod

kubens

Switch namespaces in one cluster (free from --namespace=kube-system)

# type kubens in command line, then click tab
~$ kubens
defualt kube-system kube-public
# then choose the namespace you want to switch to, don't forget to click tab to autocomplete
~$ kubens default

Know Where You Are

Now we can switch contexts and namespaces easily, but how can I know which context/namespace is using currently? I don’t want to delete the deployment in k8s-dev, but the resource was deleted from k8s-prod accidentally XD Hence, there is another helpful open source tool kube-ps1, learning more about kube-ps1

  • Install kube-ps1 for macOS
~$ brew install kube-ps1
  • Then the current K8S context and namespace will appear in the shell prompt
(⎈ k8s-prod:default) ~ smalltown$

Conclusion

I hope this article can save everyone’s time when using kubectl to communicate with K8S, and if you know other tricks or tips, welcome sharing with everybody by replying below ^^

Reference