Setup Kubernetes Pods via API Call using Go and Operator SDK
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If you want Go code to create Pods in Kubernetes, you can talk to the Kubernetes API directly through a client library. If you want that behavior to be long-lived, declarative, and driven by a custom resource, Operator SDK is the better fit because it wraps the client logic in a reconcile loop instead of a one-time script.
Know the Difference Between client-go and an Operator
Use plain API calls when you need an imperative tool or service action:
- create one Pod now
- inspect cluster state
- run an administrative task
Use Operator SDK when you want:
- a custom resource such as
ExampleApp - a controller that keeps actual state aligned with desired state
- repeated reconciliation after restarts, deletes, or spec changes
Operators do not replace the Kubernetes API. They structure how you keep using it.
Scaffold an Operator Project
The typical Operator SDK flow starts with a Go-based operator project and a custom resource definition.
Example high-level commands:
That creates the API types and a controller skeleton where your Pod-management logic will live.
Define Desired Pod State in the Custom Resource
A small spec keeps the controller flexible.
This lets a user declare intent through YAML instead of calling Pod creation functions manually every time.
Create Pods in the Reconcile Loop
Inside the controller, use the controller-runtime client to read the custom resource and create dependent Pods.
This is the core operator pattern: read desired state, compare against actual state, then create or repair resources.
Why Owner References Matter
ctrl.SetControllerReference is not optional boilerplate. It tells Kubernetes that the Pod belongs to the custom resource. That gives you:
- garbage collection when the custom resource is deleted
- correct watch relationships
- clearer cluster ownership semantics
Without owner references, your controller may create orphaned Pods that outlive the thing that requested them.
RBAC and Deployment
Your operator needs permission to watch custom resources and manage Pods. Operator SDK usually scaffolds RBAC markers, but you still need to verify them.
Typical capability requirements:
- get, list, watch on the custom resource
- create, get, list, watch, update, delete on Pods
After generating manifests, deploy the operator and apply a custom resource instance.
Then the reconcile loop should create the managed Pod.
When Not to Manage Bare Pods
In many real applications, you should manage a Deployment instead of a bare Pod, because Deployments handle restart behavior and rolling updates more naturally. A Pod example is fine for understanding API calls, but production operators often reconcile higher-level workloads.
That design decision is worth mentioning because it shows operational awareness, not just API familiarity.
Common Pitfalls
The most common mistake is treating an operator like a one-shot Pod creation script and ignoring reconciliation. Another is creating child resources without owner references, which leads to orphaned Pods. Teams also often start by reconciling bare Pods when a Deployment would better represent the desired workload. Finally, RBAC is frequently under-specified, so the controller compiles and starts but fails at runtime with authorization errors.
Summary
- Use direct Kubernetes API calls for imperative one-off actions and Operator SDK for declarative reconciliation.
- In an operator, create resources from the reconcile loop rather than from ad hoc endpoints.
- Set owner references so managed Pods are tracked and garbage-collected correctly.
- Verify RBAC before debugging controller logic.
- For real applications, consider reconciling a Deployment instead of a bare Pod.
Related reading
- Setup securityContext inside kubernetes deployment
- Share persistent volume claims amongst containers in Kubernetes/OpenShift
- Share storage/volume between worker nodes in Kubernetes?
- Shared dependencies with HELM
- Sharing precompiled assets across docker containers
- Sharing resources between Terraform workspaces
- Should API and message consumer be in the same microservice?
- Should EndReceive ever return zero if the socket is still connected?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.