💻 Tech
You can set up your zsh/bash profile to get the logs from a pod that has a consistent prefix. For example, you have a cluster that gets redeployed when Skaffold detects changes. This results to pods recreation, and you may not want to copy the pod name each time you want to view the logs. The pod name usually takes the name of the deployment and gets appended with a unique id.
In your ~/.zshrc
, if you place the function below and do source ~/.zshrc
, you can now use klog
by entering this into your terminal: klog jobservice
. jobservice
is the $1
parameter that gets appended to mycluster-
and can be the name of the deployment. With this, you can always the logs of the latest pod based on the prefix.
function klog() {
local prefix="mycluster-$1"
local pod_name=$(kubectl get pods --no-headers -o custom-columns=":metadata.name" | grep "^${prefix}" | head -n 1)
if [[ -z $pod_name ]]; then
echo "No pod found with prefix $prefix"
return 1
fi
kubectl logs "$pod_name" | less
}