DEV Community

Cover image for Running kubectl commands on multiple clusters
Nahum Litvin
Nahum Litvin

Posted on Originally published at linkedin.com on

Running kubectl commands on multiple clusters

Managing multiple Kubernetes clusters can be a real headache. I know firsthand how frustrating it can be to juggle multiple contexts, keep deployments consistent, and maintain stability across different environments.

so I found this awesome repo: https://github.com/ahmetb/kubectl-foreach

Run a kubectl command in one or more contexts (clusters) in parallel (similar to GNU parallel/xargs).

Usage

kubectl foreach [OPTIONS] [PATTERN]... -- [KUBECTL_ARGS...]
Enter fullscreen mode Exit fullscreen mode

and created a small helper function to use it. It's a simple way to run kubectl commands across multiple clusters in one go, making life a bit easier.

Here's the function you can add to your bashrc or zshrc:

function kfe() {
  eval "kubectl foreach -q dc1 dc2 dc3 -- $*"
}
Enter fullscreen mode Exit fullscreen mode

Just use kfe followed by your kubectl command, for example:

kfe get pods
Enter fullscreen mode Exit fullscreen mode

and it will execute across dc1, dc2, and dc3 without any hassle. No more repetitive tasks or constant context switching, just a smoother workflow.

I hope this helps lighten the load for anyone dealing with the complexities of Kubernetes. We're all in this together, and sharing little tools like this can make a big difference.

Top comments (0)