Python > Deployment and Distribution > Deployment Platforms > Container Orchestration (e.g., Kubernetes, Docker Swarm)
Kubernetes Deployment Configuration (YAML)
This snippet demonstrates a basic Kubernetes Deployment configuration file (YAML) for deploying a Python application containerized with Docker. It includes the essential elements for defining a Deployment, such as the number of replicas, container image, ports, and resource requests/limits.
Understanding the YAML Structure
apiVersion: apps/v1: Specifies the Kubernetes API version for Deployments.
kind: Deployment: Declares that this YAML file defines a Deployment resource.
metadata:: Contains metadata about the Deployment, such as its name.
spec:: Defines the desired state of the Deployment.
replicas: 3: Specifies that we want 3 replicas (instances) of our application running.
selector:: Defines how the Deployment identifies the Pods it manages, using labels.
matchLabels:: Specifies the labels that Pods must have to be managed by this Deployment.
template:: Defines the Pod template that the Deployment will use to create new Pods.
metadata:: Metadata for the Pods, including labels.
labels:: Labels to apply to the Pods.
spec:: Specifies the desired state of the Pods.
containers:: Defines the containers that will run within the Pod.
name:: The name of the container.
image:: The Docker image to use for the container (replace with your image).
ports:: The ports that the container exposes.
containerPort:: The port that the container listens on.
resources:: Defines the resource requests and limits for the container.
requests:: The minimum amount of resources the container needs to run.
cpu: 100m: Requests 100 millicores of CPU.
memory: 256Mi: Requests 256MB of memory.
limits:: The maximum amount of resources the container can use.
cpu: 500m: Limits CPU usage to 500 millicores.
memory: 512Mi: Limits memory usage to 512MB.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-python-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-python-app
template:
metadata:
labels:
app: my-python-app
spec:
containers:
- name: my-python-app-container
image: your-dockerhub-username/my-python-app:latest
ports:
- containerPort: 8000
resources:
requests:
cpu: 100m
memory: 256Mi
limits:
cpu: 500m
memory: 512Mi
Real-Life Use Case
Imagine you have a Python web application built using Flask or Django. You want to deploy this application to a production environment that can handle varying levels of traffic. Kubernetes allows you to easily scale the number of application instances (replicas) based on demand, ensuring high availability and responsiveness. This Deployment configuration automates the creation and management of these instances.
Best Practices
:latest
, use a specific version tag (e.g., :1.0.0
) to ensure consistent deployments.
When to use Kubernetes
Kubernetes is ideal for deploying and managing containerized applications at scale. Use it when you need:
Memory footprint
The memory footprint of the Kubernetes Deployment itself is relatively small. However, the memory usage of the Pods managed by the Deployment depends on the resource requests and limits specified in the YAML file and the actual memory consumption of the Python application within the container. Carefully configure resource requests and limits to prevent resource starvation or waste.
Alternatives
Pros
Cons
FAQ
-
How do I deploy this YAML configuration to Kubernetes?
You can use thekubectl apply -f deployment.yaml
command, wheredeployment.yaml
is the name of your YAML file. Make sure you havekubectl
installed and configured to connect to your Kubernetes cluster. -
How do I update the application image?
Update theimage
field in the YAML file and then apply the changes usingkubectl apply -f deployment.yaml
. Kubernetes will automatically perform a rolling update to replace the old Pods with new ones using the updated image.