Must Have Kubernetes Tools

I figured I would wrap up my three-part mini-series on Kubernetes by talking about some tools that make interacting with Kubernetes easier. Read on to learn more!

In my last post, I talked about kubectl and minikube — these are must haves. As a refresher:

$ brew install kubectl
$ brew cask install minikube

Kubectl has a lot of commands so autocompletion is important. With HomeBrew you get this for free if you configured auto-completion for your shell already:

$ brew install bash-completion

Not too long after you start interacting with k8s you will run into namespace and/or multiple clusters. As soon as you find yourself interacting with multiple namespaces or clusters you should definitely take a look at the kubectx and kubens commands:

$ brew install kubectx
$ kubectx
prod
stag
$ kubectx prod
Switched to context "prod".
$ kubens
kube-system
kube-public
default
$ kubens default
Context "prod" modified.
Active namespace is "default".

If you leverage Git today via the CLI, you likely have prompt information whenever you access a Git repo. The same can be achieved with k8s:

$ brew install kube-ps1
$ source /path/to/kube-ps1.sh
$ PS1='[\u@\h \W $(kube_ps1)]\$ ' # or update your prompt as desired
[user@hostname ~ (⎈ |stag:default)]$ kubeoff -g # turn off kube-ps1 status globally
$ kubeon
[user@hostname ~ (⎈ |stag:default)]$ 

As you begin to install and upgrade items running on k8s you may find a package manager helpful. While perhaps not a must-have today, helm is worth considering:

$ brew install kubernetes-helm
$ helm init

Finally, aliases and functions can make your life easier. I have created and leverage the following today:

### Kubernetes aliases

alias k='kubectl'
alias mks='minikube --cpus 4 --memory 8192 start --extra-config=kubelet.CAdvisorPort=4194'
alias tls='cluster=$(kubectl config current-context); echo -n "--tls --tls-cert $(helm home)/tls/$cluster/cert.pem --tls-key $(helm home)/tls/$cluster/key.pem"'
alias h='helm $(tls)'
alias grafana='kpf app=grafana 3000'

# Separate file per k8s configuration
if [ -d $HOME/.kube ]; then
    for CONFIG in $(ls $HOME/.kube/config.*); do
        export KUBECONFIG=$KUBECONFIG:$CONFIG
    done
fi

### Kubernetes functions

# Kubernetes Port Forward
kpf () {
    if [ -z "$1" ]; then
        echo "USAGE: $0 <pod-selector> <pod-port> (<local-port>)"
        echo "Example: $0 app=grafana 3000"
        echo "Example: $0 component=web 80 8080"
        return 2
    fi
    POD_SELECTOR=$1
    POD_PORT=$2
    LOCAL_PORT=$3
    if [ -z "${LOCAL_PORT}" ]; then LOCAL_PORT=${POD_PORT}; fi
    POD_FOUND=$(kubectl -n monitoring get pods -l ${POD_SELECTOR} -o name --no-headers=true | head -n 1)
    if [ ! -z "${POD_FOUND}" ]; then
        echo "INFO: Found pod with name: ${POD_FOUND}"
        while (true); do
            kubectl -n monitoring port-forward ${POD_FOUND} ${LOCAL_PORT}:${POD_PORT}
        done
    else
        echo "ERROR: Unable to find pod with selector ${POD_SELECTOR}"
        return 1
    fi
}

© 2018, Steve Flanders. All rights reserved.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top