Kubernetes Deployment Strategies
….
What is Deployment?
Deployment refers to the process of releasing or upgrading software for user access.
What is a Deployment Strategy?
A deployment strategy is a planned approach for releasing and updating software in a way that minimizes user disruption.
Key Deployment Strategies
Recreate
Definition: The Recreate strategy involves stopping all current instances of the application and then starting new ones with the updated configuration.
Advantages:
- Simple and straightforward.
- Ensures all instances run the same version.
Disadvantages:
- Causes downtime during the transition.
Example: To update from version 1.0 to 2.0, all instances running version 1.0 are terminated before deploying new instances running version 2.0.
When to Use: Suitable for applications where downtime is acceptable or startup time is short. Ideal for stateless applications that can afford temporary unavailability during updates.
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment
labels:
app: abhi
spec:
replicas: 3
selector:
matchLabels:
app: abhi
strategy:
type: Recreate
template:
metadata:
labels:
app: abhi
spec:
containers:
- name: deploy
image: abhi/test:v2
ports:
- containerPort: 80
Rolling Update (Default)
Definition: In a Rolling Update, new instances are created gradually while old ones are terminated, ensuring no downtime.
Advantages:
- Zero downtime deployment.
- Easy rollback in case of issues.
Disadvantages:
- Longer deployment time compared to Recreate.
Example: When updating from version 1.0 to 2.0, Kubernetes gradually replaces instances running version 1.0 with those running version 2.0, maintaining continuous availability.
When to Use: Suitable for applications requiring zero downtime and capable of running multiple versions simultaneously. Achieved by updating the image tag in the Deployment manifest, allowing Kubernetes to perform rolling updates.
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp
spec:
replicas: 3
selector:
matchLabels:
app: myapp
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v2.0
Apply this manifest with kubectl apply -f rolling-update-deployment.yaml.
Blue-Green
Definition: Blue-Green deployment involves maintaining two identical environments (Blue and Green) with only one active at any time. The inactive environment can be updated without affecting users, and traffic is switched to the updated environment.
Advantages:
- Zero downtime deployment.
- Easy rollback by switching traffic back to the previous environment.
Disadvantages:
- Requires double resources to maintain two identical environments.
Example: Version 1.0 runs in the Blue environment. Deploying version 2.0 to the Green environment allows for testing before switching user traffic.
When to Use: Useful for ensuring minimal downtime and easy rollback capabilities. Requires maintaining two identical environments.
apiVersion: v1
kind: Service
metadata:
name: myapp
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-blue
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
color: blue
spec:
containers:
- name: myapp
image: myapp:v1.0
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-green
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
color: green
spec:
containers:
- name: myapp
image: myapp:v2.0
Canary
Definition: Canary deployment involves rolling out updates to a small subset of users or servers before a full-scale deployment, allowing for real-world testing.
Advantages:
- Risk mitigation by testing with a small subset of users.
- Quick detection of issues before affecting all users.
Disadvantages:
- Requires careful monitoring and traffic routing management.
Example: Deploying version 2.0 to 10% of the user base to observe performance and gather feedback before a full rollout.
When to Use: Ideal for testing new features or changes with a small subset of users before a full deployment. Achieved by gradually routing a percentage of traffic to the new version.
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: myapp
spec:
hosts:
- myapp.example.com
http:
- route:
- destination:
host: myapp
subset: v2
weight: 10
- destination:
host: myapp
subset: v1
weight: 90
A/B Testing
Definition: A/B testing involves deploying multiple versions of an application simultaneously and directing different users or requests to each version to compare performance.
Advantages:
- Allows for comparison of different versions in real-world scenarios.
- Enables data-driven decision-making based on user behavior.
Disadvantages:
- Complexity in managing multiple versions simultaneously.
Example: Deploying versions A and B of a feature to different user segments to measure which performs better in terms of user engagement or conversion rates.
When to Use: Appropriate for comparing the performance or effectiveness of two different versions of an application.
if feature_enabled('new_version'):
# Use new version code
else:
# Use old version code