Kubernetes Operator is a convenient feature that allows you to automate and extend Kubernetes functionality through Custom Resources. The main language used to create Operators is go, but it's not limited to go. In this article, I will introduce a framework for creating an Operator in Python and how to use it easily.
Kubernetes Operator is an extension that automatically deploys applications such as DB and web server according to the definition of custom resources created by yourself. In addition to creating a normal container, you can automate according to your own code, so instead of just using the default function of Kubernetes, we went one step further, such as linking with an external cloud service or a database outside Kubernetes. You can achieve automation. Frameworks for creating Operators include operator-framework, which are often created in the go language.
See also: kubernetes official documentation (https://kubernetes.io/en/docs/concepts/extend-kubernetes/operator/)
kopf (Kubernetes Operator Pythonic Framework) is a framework for creating Kubernetes Operators in Pyhon. Simply add a decorator to the code you want to automate, and you can execute your code as Kubernetes resources change.
See also: https://github.com/nolar/kopf
Implementation environment:
You can install it from pip.
pip install kopf
It's easy to run, just specify the Operator code you want to run with the kopf command introduced during installation.
kopf run your_code.py --verbose
TIPS:
$ HOME/.kube/config
.There are a lot of sample code on github, so basically you can understand the function by looking at it. I would like to explain by quoting a part of the sample code below.
kopf/examples/01-minimal/example.py
import kopf
@kopf.on.create('zalando.org', 'v1', 'kopfexamples')
def create_fn(spec, **kwargs):
print(f"And here we are! Creating: {spec}")
return {'message': 'hello world'} # will be the new status
In kopf, you can add a decorator to a function to execute code as the kubernetes resources change. This sample code prints the resource definition to standard output when the kopfexamples
resource is created.
In addition, the return value is saved in the status of the created resource, and you can check the contents with the kubectl describe
command.
kopf/examples/02-children/example.py
import kopf
import pykube
import yaml
@kopf.on.create('zalando.org', 'v1', 'kopfexamples')
def create_fn(spec, **kwargs):
# Render the pod yaml with some spec fields used in the template.
doc = yaml.safe_load(f"""
apiVersion: v1
kind: Pod
spec:
containers:
- name: the-only-one
image: busybox
command: ["sh", "-x", "-c"]
args:
- |
echo "FIELD=$FIELD"
sleep {spec.get('duration', 0)}
env:
- name: FIELD
value: {spec.get('field', 'default-value')}
""")
# Make it our child: assign the namespace, name, labels, owner references, etc.
kopf.adopt(doc)
# Actually create an object by requesting the Kubernetes API.
api = pykube.HTTPClient(pykube.KubeConfig.from_env())
pod = pykube.Pod(api, doc)
pod.create()
api.session.close()
# Update the parent's status.
return {'children': [pod.metadata['uid']]}
When creating a resource, a new pod with a definition according to the resource content is created.
The point is kopf.adopt (doc)
, and when you do this, the information of the triggered resource will be recorded in the ownerReferences
field of the created pod.
By having this information, a parent-child relationship is formed between the triggered resource and the created pod, and when the parent resource is deleted, the child pod will be deleted at the same time.
I introduced how to create an Operator for kubernetes in Python using the kopf framework. If you haven't touched go but Python, why not take this opportunity to create an Operator for Kubernetes?
Recommended Posts