How to Create a Kubernetes Operator: Step-by-Step Guide
To create a
Kubernetes operator, use the Operator SDK to scaffold a new operator project, define your custom resource and controller logic, then build and deploy it to your cluster. This automates managing complex applications by extending Kubernetes with custom behaviors.Syntax
Creating a Kubernetes operator involves these main steps:
- Initialize Operator SDK project: Sets up the operator framework.
- Define Custom Resource Definition (CRD): Specifies the new resource schema.
- Implement Controller logic: Handles resource lifecycle events.
- Build and deploy: Package the operator and run it in the cluster.
bash
operator-sdk init --domain=example.com --repo=github.com/example/memcached-operator operator-sdk create api --group=cache --version=v1alpha1 --kind=Memcached --resource --controller # Edit api/v1alpha1/memcached_types.go to define spec/status # Edit controllers/memcached_controller.go to add reconciliation logic make docker-build docker-push IMG="example.com/memcached-operator:tag" make deploy IMG="example.com/memcached-operator:tag"
Example
This example creates a simple Memcached operator that manages Memcached instances as custom resources.
bash
operator-sdk init --domain=example.com --repo=github.com/example/memcached-operator operator-sdk create api --group=cache --version=v1alpha1 --kind=Memcached --resource --controller # Edit api/v1alpha1/memcached_types.go to define MemcachedSpec and MemcachedStatus structs # Edit controllers/memcached_controller.go to implement Reconcile method that creates a Deployment make docker-build docker-push IMG="example.com/memcached-operator:v0.1.0" make deploy IMG="example.com/memcached-operator:v0.1.0"
Output
INFO[0000] Creating new project version v3.5.0
INFO[0001] API creation complete
INFO[0002] Controller creation complete
INFO[0003] Build complete
INFO[0004] Deployment complete
Common Pitfalls
- Not defining the CRD schema correctly causes validation errors.
- Forgetting to update the controller logic to handle resource changes leads to no effect.
- Using incorrect image tags or not pushing the operator image prevents deployment.
- Running the operator without proper RBAC permissions causes failures.
yaml
### Wrong: Missing RBAC permissions in config/rbac/role.yaml # This causes permission denied errors ### Right: Add necessary permissions apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: name: memcached-operator rules: - apiGroups: - cache.example.com resources: - memcacheds verbs: - get - list - watch - create - update - patch - delete
Quick Reference
Steps to create a Kubernetes operator:
- Install Operator SDK CLI.
- Initialize a new operator project.
- Create API and controller for your custom resource.
- Implement reconciliation logic.
- Build and push the operator image.
- Deploy operator to Kubernetes cluster.
Key Takeaways
Use Operator SDK to scaffold and manage Kubernetes operators easily.
Define clear CRD schemas and implement controller logic for resource management.
Ensure operator image is built, pushed, and deployed with correct permissions.
Test operator behavior by creating and updating custom resources.
Common errors include missing RBAC permissions and incorrect CRD definitions.