How To Run Kubernetes Commands in Go: Steps and Best Practices

Key takeaways:
- You can run Kubernetes commands in Go using the
client-go
library or by executing rawkubectl
commands withexec.Command
. - Use retry loops and backoff strategies to handle API timeouts, conflicts and transient errors reliably.
- Follow best practices like input validation, structured output and CLI frameworks to build production-ready tools.
Running Kubernetes commands programmatically can feel overwhelming at first. You might find yourself shelling out to kubectl
in scripts or trying to wrangle complex APIs just to list a few pods or apply a config. It’s not always clear where to start or how to do it cleanly in Golang (Go).
The good news? You don’t need to rely on shell hacks or guesswork. Go is the language Kubernetes itself is written in — and with the official client-go
library, you can interact with your cluster directly, just like kubectl
does.
Learn how to run core Kubernetes operations in Go. From setting up the client to handling authentication, parsing output and writing testable code, get a practical foundation to build your own tools and automations.
Why Use Go for Kubernetes Automation
Go is one of the best languages for working with Kubernetes. In fact, Kubernetes itself is written in Go. That means you get first-class support and access to official client libraries when writing tools or automations.
Here are some key reasons to use Go for Kubernetes automation:
- Official support: The Kubernetes client libraries are written and maintained in Go.
- Strong community: Lots of examples, tools and open source projects use Go.
- Fast performance: Go is compiled and fast, making it ideal for command line interface (CLI) tools and controllers.
- Easy concurrency: Go’s built-in concurrency (via goroutines) helps manage many Kubernetes resources at once.
- Static typing: You catch errors early, which is important for automation tools.
- Cross-platform: Build and run your tools on any OS with ease.
- Lightweight binaries: Create small, self-contained executables with no runtime dependencies.
Prerequisites for Running Kubernetes Commands in Go
Before you can run Kubernetes commands in Go, you need to have a few tools and settings in place. These will ensure your Go code can connect to your cluster and perform actions safely. Here’s what you will need.
Go Toolchain and Modules
To write and run Go code, you need to have the Go toolchain installed. This includes the Go compiler, the go
command line tool and support for modules (i.e., Go’s dependency management system).
Here’s why it’s important:
- Compiling your code: You’ll need the Go compiler to build your Kubernetes tools.
- Managing dependencies: Go modules help you pull in the Kubernetes client libraries and keep versions organized.
- Reproducible builds: With modules, your code can be shared or deployed consistently across systems.
💡 | To check if Go is installed, run go version in your terminal. To initialize a module, use go mod init <your-module-name> . |
Kubeconfig Access and RBAC
To interact with a Kubernetes cluster, your Go program needs access to a kubeconfig file. This file tells your code how to connect to the cluster and what credentials to use. Role-based access control (RBAC) is also important. It defines what actions your code is allowed to perform.
Here’s why this matters:
- Cluster connection: The kubeconfig file is how your code knows where the cluster is and how to talk to it.
- Permissions: Without the right RBAC roles, your code might get denied when trying to list pods, create deployments, etc.
- Safety: RBAC helps limit actions to only what you need, reducing the risk of accidental changes.
💡 | Make sure the user or service account you’re using has the right roles for the tasks you plan to automate. |
Installing and Configuring client-go
To run Kubernetes commands in Go, you need the official Go client library called client-go
. This library gives your code the tools to connect to your cluster and work with Kubernetes resources. Here’s how you can add it to your project and load your cluster credentials.
Adding the Module
First, you’ll need to add client-go
to your Go module. We do it using the go get
command, which pulls the library into your project and lets you use it in your code.
Use this command to fetch the latest version of the client-go
library and add it to your go.mod
file:
You may also need related packages depending on your setup:
These libraries help define and manage Kubernetes objects. After that, your go.mod
file should include the dependencies, and you’re ready to start coding.
Loading Cluster Credentials in Code
To connect to a Kubernetes cluster, client-go
uses your kubeconfig file — the same file you use with kubectl
. Here’s a basic example that loads your credentials and creates a client:
Here’s what this code does:
- Locates the kubeconfig file in the default location.
- Loads the config and creates a Kubernetes client.
- Prepares the client so you can run commands like listing pods or creating deployments.
Running Core kubectl
-Equivalent Commands
Once you’ve set up your Go project with client-go
, you can start performing the same tasks you’d normally do with kubectl
— but programmatically. This is useful for building custom tools, automating workflows or writing controllers.
Below are examples of how to list, create and delete Kubernetes resources using Go.
Listing Pods, Deployments and Services
Here’s how to list common resources in a specific namespace:
This is similar to running kubectl get pods
, kubectl get deployments
or kubectl get services
.
Creating or Updating Resources
You can create a new Kubernetes deployment (or other resources) using Go structs. Here’s a basic example for a deployment:
For updates, you can use Update() instead of Create(), typically after fetching and modifying the existing resource.
Deleting Resources
To delete a resource, you just call the delete method on the client:
This works the same way for deployments, services or other resources — just use the appropriate client group.
Executing ‘Raw’ kubectl
Commands Programmatically
Sometimes, it’s easier or more flexible to run actual kubectl
commands from your Go code, especially if you don’t need full control over the Kubernetes API or if you want to reuse familiar CLI behavior. This approach is helpful for quick scripts, automation or when you want to avoid dealing with complex Kubernetes types directly.
Using exec.Command
The os/exec
package in Go lets you run shell commands, including kubectl
. Here’s how you can use it:
This code runs kubectl get pods -n default
and prints the result. It combines both stdout and stderr in case there’s an error. Before running this code, make sure kubectl
is installed and available in your system’s PATH.
Streaming Stdout/Stderr
If you want to stream the output as the command runs — instead of waiting for it to finish — you can do this:
This approach is useful for commands like kubectl logs -f
or kubectl exec
where live output is important.
Handling Authentication and Authorization
To interact with a Kubernetes cluster, your Go program needs proper authentication and permissions. Kubernetes supports different ways to authenticate depending on where your code is running — inside the cluster or outside of it. Below are the two most common approaches.
In-Cluster Service Accounts
If your Go program runs inside a Kubernetes cluster (e.g., in a Pod), it can use a built-in service account for authentication.
Here’s how it works:
- Kubernetes automatically mounts a token and certificate into your Pod at:
/var/run/secrets/kubernetes.io/serviceaccount/
. client-go
uses this path by default when running in-cluster.
Use this to set it up in your code:
You’ll also need to set proper RBAC roles or role bindings for the service account to control what it can access.
Out-of-Cluster Tokens and Certificates
If your code runs outside the cluster, like on your laptop or CI/CD pipeline, you’ll typically use a kubeconfig file that holds your credentials.
client-go
automatically reads this file when you use clientcmd.BuildConfigFromFlags
.
This kubeconfig file can contain:
- User tokens
- Client certificates and keys
- Cluster CA certificates
Here’s a quick example:
This setup is useful for developers testing locally or for automation tools interacting with Kubernetes securely.
Parsing and Printing Kubernetes Objects
Once you fetch Kubernetes resources using Go, you may want to display or export them in a readable format, such as YAML, JSON or custom views. This is helpful for debugging, logging or building CLI tools with output similar to kubectl
. Below are two common ways to format Kubernetes objects in Go.
Converting to YAML/JSON
Kubernetes objects can be serialized into YAML or JSON using Go’s encoding libraries.
Use this code to convert them to JSON:
Here’s the code to convert them to YAML:
Both formats are useful when saving or displaying the full object definition.
Using Go Templates for Custom Output
If you want to print only specific fields, like kubectl get pods -o custom-columns
, you can use Go templates.
Try this code:
This approach gives you full control over what gets printed and how. You can also use this technique to build scripts or tools with clean, user-friendly output.
Error Handling, Retries and Backoff Strategies
When working with Kubernetes in Go, things won’t always go smoothly. Network hiccups, temporary unavailability or permission errors are common. That’s why it’s important to handle errors gracefully and retry when needed.
Retries help your app recover from transient issues, and backoff strategies make sure you’re not overloading the API by retrying too aggressively.
Here are some simple and effective strategies to consider:
- Check and log errors: Always check for
err != nil
and log the error details. This helps with debugging. - Use exponential backoff: Wait longer between retries to avoid overwhelming the system. Libraries like
k8s.io/apimachinery/pkg/util/wait
make this easy. - Limit retry attempts: Don’t retry forever. Set a max retry count to avoid hanging or stuck processes.
- Retry only on specific errors: Some errors, like 500 or timeout, are worth retrying. Others, like 403 or 404, usually are not.
- Use context timeouts or cancellations: This prevents your code from retrying too long and gives users better control over request timing.
✅ | The Kubernetes Go client includes built-in helpers like wait.ExponentialBackoff() for retry logic. You can use them, too. |
Unit and Integration Testing
Testing your Kubernetes code is key to avoiding surprises in production. Go makes it easy to write both unit and integration tests using the Kubernetes client libraries.
Unit tests check your logic without needing a real cluster. Integration tests run your code against a real (or simulated) Kubernetes cluster. Here’s how to handle both.
Fake Clientset for Unit Tests
The client-go
library provides a fake client you can use to mock Kubernetes interactions. This lets you test your code without needing a live cluster.
KinD-Based Integration Tests
Kubernetes in Docker (KinD) is great for running real Kubernetes clusters locally for integration testing.
With KinD, you can:
- Spin up a real cluster in CI pipelines or locally.
- Deploy and test your Go code end-to-end.
- Validate how your code interacts with real Kubernetes behavior.
Here’s how to go about it:
- Use KinD to create a test cluster.
- Run your app or controller inside that cluster.
- Use
client-go
to test actual resource behavior.
- Tear down the cluster after tests.
Tools like envtest
and controller-runtime
also help with integration tests in custom controllers.
10 Best Practices for Production-Grade CLI Tools
If you’re building a CLI tool that interacts with Kubernetes, it’s important to go beyond just “working code.” Your tool should be reliable, user-friendly, and ready for real-world use.
Here are some best practices to follow:
- Use CLI frameworks like Cobra: These frameworks help structure commands, add help text and handle flags cleanly.
- Validate user input: Always check for required flags, invalid values or missing context before executing a command.
- Provide helpful error messages: Make sure error output is clear and actionable. Avoid cryptic stack traces.
- Support multiple kubeconfig contexts: Let users specify a
--kubeconfig
file or--context
if they work with multiple clusters. - Print progress and status: Show users what the tool is doing (e.g., “Creating deployment…”). This helps with transparency and trust.
- Respect Kubernetes RBAC: Don’t assume the user has full access. Catch permission errors and explain what’s missing.
- Gracefully handle timeouts and cancellations: Support
--timeout
flags andCtrl+C
to let users exit cleanly. - Include logging and debug modes: Allow
--verbose
or--debug
flags for deeper insights during troubleshooting. - Use structured output options: Support flags like
--output=json
or--output=yaml
for scripting and automation. - Write unit and integration tests: Test your CLI logic and Kubernetes interactions to avoid regressions.
Following these best practices can turn a simple script into a robust tool that your team or community can rely on.
Running Kubernetes Commands in Go: Conclusion
Go is a natural fit for Kubernetes automation. With the official client libraries, you can interact with clusters directly, build reliable CLI tools and handle complex tasks programmatically — all while keeping things fast and efficient.
Whether you’re managing resources, building custom controllers or writing internal tools, mastering these patterns will help you create more powerful and production-ready Kubernetes applications in Go.
Learn Go to take full control of your Kubernetes workflows and build tools that scale with your infrastructure.