DEV Community

Erick πŸ™ƒ Navarro
Erick πŸ™ƒ Navarro

Posted on • Originally published at erick.navarro.io on

Creating helper commands for k8s using fzf

Kubernetes cli tool kubectl is pretty useful but when I need to execute some tasks many times during a work day it could be too verbose. So I wrote some bash functions to handle a few common tasks I use often.

I used the power of fzf to create an interactive experience when I run any of these functions. Basically it pipes the output of a kubectl command, make some filtering using sed and awk and then build a final command which will execute what I want.

The common behavior of these functions is first ask for a namespace and then ask for a specific pod to make some action over its.

We can see an example of how it works in the image below:

/images/creating-helper-commands-for-k8s-using-fzf/pod_shell.gif

Open a shell or a custom command inside a pod

If we execute pod_shell without any argument it will connect to the selected pod and run bash, otherwise it will run the given command.

function pod_shell {
    local namespace=`kubectl get ns | sed 1d | awk '{print $1}' | fzf`
    local pod=`kubectl get pods -n $namespace | sed 1d | awk '{print $1}' | fzf`
    echo "Connecting to $pod"

    if [-z $1]
    then
        kubectl -n $namespace exec -ti $pod bash
    else
        kubectl -n $namespace exec -ti $pod $1
    fi
}
Enter fullscreen mode Exit fullscreen mode

Run a proxy over a pod

Same as the previous function but this one ask for a port mapping, for example 9999:5432 will map the port 9999 from the host machine to 5432 port on the pod.

function pod_proxy {
    local namespace=`kubectl get ns | sed 1d | awk '{print $1}' | fzf`
    local pod=`kubectl get pods -n $namespace | sed 1d | awk '{print $1}' | fzf`
    local port_mapping
    echo "Enter port mapping using the form local_port:pod_port"
    read port_mapping
    echo "Setting up proxy to $pod on $port_mapping..."

    kubectl port-forward -n $namespace $pod $port_mapping
}
Enter fullscreen mode Exit fullscreen mode

See realtime logs for a given pod

This one just ask for a pod and attach a kubectl log command.

function pod_logs {
    local namespace=`kubectl get ns | sed 1d | awk '{print $1}' | fzf`
    local pod=`kubectl get pods -n $namespace | sed 1d | awk '{print $1}' | fzf`
    echo "Showing logs for $pod"

    kubectl -n $namespace logs -f $pod
}
Enter fullscreen mode Exit fullscreen mode

These are some common tasks I need for a day of work but with the same logic we can build some other commands.

Top comments (0)