Skip to Content

K8s YAML Alternative: Python

In a previous set of posts I have shown examples using JSON, .NET, Terraform, and Java instead of YAML. This post is going to show another alternative using Python. I will be using the same Kubernetes object to allow comparison.

YAML Object

We are going to create a busybox pod. This manifest isn’t a super simple example, nor is it a complex example. This manifest should allow demonstrating the differences. Here is the pod YAML.

apiVersion: v1
kind: Pod
  name: busybox
  - image: busybox
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
    name: busybox
  restartPolicy: Always

Now we can save this as busybox.yaml and create it on our cluster.

$ kubectl apply -f busybox.yaml
pod/busybox created

Now let’s take a look at this in Python.


This example is going to leverage the Kubernetes Client for Python. Let’s create a basic Python application.

$ mkdir k8spython && cd k8spython
$ touch

I will be using a virtual environment.

$ python3 -m venv .venv
$ source .venv/bin/activate

Now we can install the Kubernetes client.

$ pip install kubernetes

Now we can set up our client, create our object, and execute the Python.

from kubernetes import client, config
from kubernetes.client.models import V1Pod, V1ObjectMeta, V1Container, V1PodSpec

def create_pod():

    v1 = client.CoreV1Api()


    container=client.V1Container(name="busybox", image="busybox", image_pull_policy="IfNotPresent")
    container.args=["sleep", "3600"]

    spec=client.V1PodSpec(containers=[container], restart_policy="Always")
    pod.spec = spec

    print("Pod deployed.")

if __name__ == "__main__":

Now we can execute our Python app.

$ python
Pod deployed.

Now, if we get all pods, we should see it running.

$ kubectl get pods
NAME                READY   STATUS    RESTARTS   AGE
busybox-python      1/1     Running   0          20m

Don’t forget to clean up your cluster by deleting the object.

$ kubectl delete pod busybox-python
pod "busybox-python" deleted

Wrap Up

The key take away from this is that you don’t have to use YAML. As with any markup, an excellent tool goes a long way in making it more manageable. If you are already using Python in your environment, then I would recommend you trying out the Kubernetes client to see if it meets your needs or not. Combine this approach with something like mypy, and you would have a solid choice.

Stay tuned for more alternatives.

Thanks for reading,


If you enjoy the content then consider buying me a coffee.