This project demonstrates how to build a Kubernetes Operator using Python and the kopf (Kubernetes Operator Pythonic Framework) library.
Instead of deploying individual Pods, Deployments, and Services, companies often create "Custom Resource Definitions" (CRDs) that abstract these primitives into high-level concepts (e.g., kind: Database, or kind: WebApp). The Operator watches the Kubernetes API for these Custom Resources and automatically creates the underlying infrastructure.
- Custom Resource Definition (CRD): Extending the Kubernetes API to understand a new type of object (
EpycAppin this demo). - Operator Pattern: A software loop that watches for changes to your Custom Resource and reacts to them (create, update, delete).
You must have a running Kubernetes cluster (like Minikube or Docker Desktop K8s) and your ~/.kube/config set up.
- Install requirements:
pip install -r requirements.txt
- Apply the Custom Resource Definition (CRD) to your cluster:
kubectl apply -f crd.yaml
- Run the Operator locally (it will use your local kubeconfig to watch the cluster):
kopf run operator.py --verbose
- In another terminal, create an instance of your custom app:
(The second block in
crd.yamldefines a test objectmy-first-app)kubectl apply -f crd.yaml
- Observe the Operator logs! It will intercept the creation event and print your logic.