Nahum LitvinKubernetes, containers and sandboxes, from the production side
Kubernetes1 min read

Running kubectl commands on multiple clusters

One shell function that runs any kubectl command on every cluster.

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...]

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 -- $*"
}

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

kfe get pods

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.

References

Comments