Kubernetes Deployments
Kubernetes Deployments
1. What is a Deployment?
• Declarative: You declare the desired state of the application (e.g., the number of
replicas, image version), and Kubernetes ensures that the state matches the desired
state.
• Pod Management: A Deployment manages the Pods for you, ensuring the correct
number of Pods are running at all times.
• Rollback: If there’s an issue with the updated version, Kubernetes allows you to roll
back to a previous, stable state.
2. Key Concepts and Components
To understand Deployments in detail, it’s important to familiarize yourself with the key
components related to it:
• Pod: A group of one or more containers running in a shared environment. Pods are
the basic unit in Kubernetes.
3. Creating a Deployment
A Deployment is usually defined using a YAML file, which contains configuration details such
as the application’s Docker image, the number of replicas, the update strategy, and more.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:v1
ports:
- containerPort: 80
In this YAML:
• apiVersion: Specifies the API version for the Deployment (e.g., apps/v1).
o template: The Pod template that includes container definitions and ports.
To create a Deployment, save the YAML file and run the following kubectl command:
4. Managing Deployments
Once a Deployment is created, you can manage it using kubectl commands. Some common
commands include:
• Scaling a Deployment:
• Delete a Deployment:
• Scaling: The number of Pods running in a Deployment can be adjusted at any time.
This can be done either manually (as shown in the command above) or automatically
via Horizontal Pod Autoscalers (HPA).
Example of Horizontal Pod Autoscaler (HPA):
Example:
Kubernetes will initiate a rolling update, gradually replacing the old Pods with new ones.
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
• Rollback: If the new version of the application has issues, you can easily roll back to a
previous version using the following command:
• Version Control: Always use specific version tags for your container images (e.g., my-
app:v1, not my-app:latest) to ensure consistency in production environments.
• Limit Resources: Define resource requests and limits for each container in the Pod
template to avoid resource contention and ensure fair usage.
Example:
containers:
- name: my-app
image: my-app:v1
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
Health Checks: Implement liveness and readiness probes for containers to ensure Pods are
only considered healthy when they are ready to serve traffic. This helps Kubernetes manage
unhealthy Pods.
Example:
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
Labels and Selectors: Always use labels and selectors properly to ensure that your
Deployment only manages the intended Pods.
Conclusion