Kubernetes aliases and functions using fzf - updated
Something with kubectl gostring formatting changed in the last few years. I have updated the code below to fix any breaking changes (they were in the execute shell helpers).
I also added some functionality to get logs by deployment (klogsdeployment) so you can see the logs from all pods at once. Which I do not believe any of the tools that people are using do.
The only requirement is running:
brew install fzf
before hand.
alias kgp='kubectl get pods --all-namespaces'
alias kgs='kubectl get services --all-namespaces'
function kLogsPreviewAllContainers() {
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace} {.metadata.name}{"\n"}' | fzf --preview="kubectl logs {2} --namespace {1} --all-containers" --preview-window=up:80%
}
alias klogsp=kLogsPreviewAllContainers
function kLogsAllContainers() {
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace} {.metadata.name}{"\n"}' | fzf | read -r namespace pod
kubectl logs $pod --namespace $namespace --all-containers -f
}
alias klogs=kLogsAllContainers
function kLogsAllContainersAllPodsInDeployment() {
kubectl get deployments --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace} {.metadata.name}{"\n"}' | fzf | read -r namespace deployment
kubectl logs -f deployment/$deployment --namespace $namespace -f
}
alias klogsdeployment=kLogsAllContainersAllPodsInDeployment
function kLogsContainer() {
# The first argument to this function should be the container name
# kubectl get pods -o name | fzf --preview="kubectl logs {} --container $1 | tail -20" --preview-window=up:80%
local container
container=$1
if [[ ! -z "${container// }" ]]; then
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace} {.metadata.name}{"\n"}' | fzf --preview="echo kubectl logs {2} --namespace {1} --container $container" --preview-window=up:80% --preview-label="Logs for container $1"
else
print "Usage: kLogsContainer <container name>"
fi
}
function kexSh() {
local containers
local container
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}' | fzf | read -r namespace pod
containers=$(kubectl get pod -n $namespace $pod -o jsonpath='{.spec.containers[*].name}')
container=$(echo ${containers/ /\\n} | fzf)
kubectl exec -n $namespace --stdin --tty $pod --container $containers -- /bin/sh
}
function kexBash() {
local containers
local container
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}' | fzf | read -r namespace pod
containers=$(kubectl get pod -n $namespace $pod -o jsonpath='{.spec.containers[*].name}')
container=$(echo ${containers/ /\\n} | fzf)
kubectl exec -n $namespace --stdin --tty $pod --container $container -- /bin/bash
}
function kd() {
kubectl get $1 --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}' | fzf --preview="echo '{}' | xargs kubectl describe $1 -n" | xargs kubectl describe $1 -n
}
function kdelete() {
kubectl get $1 --all-namespaces -o jsonpath='{range .items[*]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}' | fzf | xargs kubectl delete $1 -n
}
alias kdpod='kd pod'
alias kdelpod='kdelete pod'
alias kdservice='kd service'
alias kdelservice='kdelete service'
alias kg='kubectl get '
function kgevents() { kubectl get events --sort-by='.metadata.creationTimestamp' }
alias kgev=kgevents
I use fzf pretty extensively. I really appreciate how much time it can save looking up something like an id or namespace which will just need to be copied into the next command.
To save time I created a number of alises and functions which work with Kubernetes' kubectl to help me get information about a cluster and also exec shells.
It starts off with some aliases to help with monotonous tasks like getting a specific column and then there are some helpful command to describe and delete resources
You could run:
kd service
or
kdservices
For pods:
kd pod
or
kdpods
The kd function will work with any resource type.
So
kd ingress
will even work to list all ingresses in all namespaces and then describe the one you select.
Command starting with kdel will delete resources.
To run a shell on a pod in a container run:
kexBash
or
kexSH
These are pretty useful little functions.
You will be able to select the pod you want to a shell into, and then a secondary fzf selection will occur for the container.
The last and mose useful of all these is probably just getting events in a sorted order (by timestamp).
Kubernetes events has helped me debug by far the majority of the more challenging Kubernetes issues I hace run into.
kgevents
When I run into frustrating issues with Kubernetes, things that I overlooked, I am usually able to pretty quickly figure out what I missed just by looking at the events being logged.
For example, if you are missing some secret or something needed for a volume to mount, that will show up in Kubernetes events if I remember correctly.
I would deffinately recommend checking it out if you ever are stuck.