/ PYTHON, KUBERNETES

Automating Deployment of Applications using Kubernetes Python SDK

Automating Kubernetes Deployments using Python

Kubernetes has become the go-to orchestration platform for deploying, scaling, and managing containerized applications. Automating the deployment of applications can streamline the process and increase productivity. In this article, we’ll demonstrate how to use the Python Kubernetes SDK to automate the deployment of an Nginx application to a Kubernetes cluster.

Prerequisites

Before we start, make sure you have the following:

  • Python 3 and pip installed.
  • Kubernetes CLI kubectl installed and configured.

Table of Contents


Setting up the Python Kubernetes SDK

Before we start, make sure you have the Python Kubernetes SDK installed. If not, install it using pip:

pip install kubernetes

You must also have a valid kubeconfig file for your Kubernetes cluster. This file provides the necessary information for connecting to the kubernetes cluster, such as the cluster API endpoint and authentication details.

Creating a Kubernetes Deployment

Deployments are Kubernetes resources that describe the desired state for an application, including the number of replicas, container images, and update strategy.

First, let’s import the necessary classes and create a client to interact with the Kubernetes API:

from kubernetes import client, config

# Load the kubeconfig file
config.load_kube_config()

# Create a client instance
api_instance = client.AppsV1Api()

Next, we’ll create an Nginx deployment with one replica:

from kubernetes.client import V1Deployment, V1DeploymentSpec, V1PodTemplateSpec, V1PodSpec, V1Container, V1ObjectMeta

# Define the deployment metadata
metadata = V1ObjectMeta(name="nginx-deployment")

# Define the deployment spec
spec = V1DeploymentSpec(
    replicas=1,
    template=V1PodTemplateSpec(
        metadata=V1ObjectMeta(labels={"app": "nginx"}),
        spec=V1PodSpec(
            containers=[
                V1Container(
                    name="nginx",
                    image="nginx:1.14.2",
                    ports=[{"containerPort": 80}],
                )
            ]
        ),
    ),
    selector={"matchLabels": {"app": "nginx"}},
)

# Create the deployment object
deployment = V1Deployment(metadata=metadata, spec=spec)

# Create the deployment on the cluster
api_instance.create_namespaced_deployment(namespace="default", body=deployment)

Creating a Kubernetes Service

Services are Kubernetes resources that define how to access an application running in a set of Pods. In our example, we’ll create a simple NodePort service to expose the Nginx deployment:

from kubernetes.client import V1Service, V1ServiceSpec, V1ServicePort

# Define the service metadata
metadata = V1ObjectMeta(name="nginx-service")

# Define the service spec
spec = V1ServiceSpec(
    selector={"app": "nginx"},
    ports=[V1ServicePort(port=80, target_port=80, protocol="TCP")],
    type="NodePort",
)

# Create the service object
service = V1Service(metadata=metadata, spec=spec)

# Create the service on the cluster
api_instance = client.CoreV1Api()
api_instance.create_namespaced_service(namespace="default", body=service)

Creating a Kubernetes Secret

Secrets are Kubernetes resources that store sensitive information like passwords, tokens, and keys. In this section, we’ll create a simple Kubernetes secret containing an example API key:

from kubernetes.client import V1Secret

# Define the secret metadata
metadata = V1ObjectMeta(name="example-api-key")

# Define the secret data
data = {"api-key": "c2VjcmV0YXBpa2V5"}  # base64 encoded string "secretapikey"

# Create the secret object
secret = V1Secret(metadata=metadata, data=data)

# Create the secret on the cluster
api_instance = client.CoreV1Api()
api_instance.create_namespaced_secret(namespace="default", body=secret)

Creating a Kubernetes ConfigMap

ConfigMaps are Kubernetes resources that allow you to store and manage non-sensitive configuration information, such as application settings. In this example, we’ll create a ConfigMap containing a sample configuration file:

from kubernetes.client import V1ConfigMap

# Define the ConfigMap metadata
metadata = V1ObjectMeta(name="example-config")

# Define the ConfigMap data
data = {"config.yaml": "example: configuration\ndata: goes here"}

# Create the ConfigMap object
config_map = V1ConfigMap(metadata=metadata, data=data)

# Create the ConfigMap on the cluster
api_instance = client.CoreV1Api()
api_instance.create_namespaced_config_map(namespace="default", body=config_map)

Creating a Kubernetes Ingress

Ingress is a Kubernetes resource that manages external access to services within the cluster, typically via HTTP or HTTPS. We will create a simple ingress rule that routes traffic to our Nginx service:

from kubernetes.client import V1Ingress, V1IngressSpec, V1IngressRule, V1HTTPIngressRuleValue, V1IngressBackend

# Define the Ingress metadata
metadata = V1ObjectMeta(name="example-ingress")

# Define the Ingress spec
spec = V1IngressSpec(
    rules=[
        V1IngressRule(
            host="example.com",
            http=V1HTTPIngressRuleValue(
                paths=[
                    {
                        "path": "/",
                        "backend": V1IngressBackend(service_name="nginx-service", service_port=80),
                    }
                ]
            ),
        )
    ],
)

# Create the Ingress object
ingress = V1Ingress(metadata=metadata, spec=spec)

# Create the Ingress on the cluster
api_instance = client.NetworkingV1Api()
api_instance.create_namespaced_ingress(namespace="default", body=ingress)

By following the steps outlined in this article and using the Python Kubernetes SDK, you can automate the deployment and management of your kubernetes applications. This will streamline your workflows, reduce the chance of human error, and increase overall efficiency.

Conclusion

In conclusion, the Python Kubernetes SDK effectively automates application deployment and manages Kubernetes resources. With the examples in this article, you can easily create Kubernetes deployments, services, secrets, ConfigMaps, and ingress rules. By integrating this SDK into your development workflow, you can streamline processes, reduce the risk of human error, and increase overall efficiency, allowing you to focus on developing robust, scalable, and innovative applications for the Kubernetes platform.

faizan

Faizan Bashir

Principal Engineer | Architecting and building distributed applications in the Cloud | Adventurer

Read More