Skip to content
Kubernetes operator example in Python3
Branch: master
Clone or download
Latest commit da9a3f2 Mar 21, 2019


This repository implements an example Kubernetes operator in Python 3, called "ImmortalContainers". This operator enables the user to define, using custom resources, containers that must run and if terminated must be restarted.

The following diagram shows the main components of this operator controller:

components diagram

Venv and project dependencies

To create a virtual env and install the project dependencies follow these steps:

python3 -m venv venv
. ./venv/bin/activate
make dep

Install CRD and RBAC permissions

To install CRDs and RBAC configurations to your currently set cluster use:

make install

Running the operator outside the cluster

. ./venv/bin/activate
python src/ --kubeconfig ~/.kube/config

Running inside the cluster

You must first generate the image using make docker-build and push it to your repo.

If using minikube follow these steps:

eval $(minikube docker-env)
make docker-build

Then create the system namespace

kubectl apply -f config/namespace.yaml

And then run make deploy.

After this you should check that everything is running, ex:

$ kubectl get pods --namespace system                     
NAME                                          READY   STATUS    RESTARTS   AGE
exampleoperatorpy-controller-7cb7f99658-97zjs   1/1     Running   0          24m

$ kubectl logs exampleoperatorpy-controller-7cb7f99658-97zjs --namespace=system

INFO:controller:Controller starting

Using the operator

Once the operator is running you can create immortal containers using a custom resource like this one:

kind: ImmortalContainer
  name: example-immortal-container
  image: nginx:latest

Run kubectl apply -f config/example-use.yaml to try it.

Then run kubectl get pods and check the pod is created. If you kill the pod it will be recreated.

Remove the operator

To remove the operator, CDR and RBAC use make undeploy

Pods created by the operator will not be deleted, but will not be restarted if deleted later.

You can’t perform that action at this time.