Blue-green deployment
Ce este blue-green deployment?
Blue-green deployment are următoarele caracteristici:
- două medii identice
- switch instant de trafic
- rollback foarte rapid (switch înapoi)
- zero downtime
Cum funcționează?
Pașii sunt următorii:
- Deploy Green (în paralel cu Blue)
- Testează Green (fără rularea pe producție)
- Switch trafic Blue → Green
- (opțional) Ține Blue pentru rollback rapid
Task
Mai jos aveți trei fișiere și executați comenzile de mai jos:
service.yaml:
apiVersion: v1
kind: Service
metadata:
name: web-app-service
labels:
app: web-app
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 80
selector:
app: web-app
env: blue # ← Initial point la BLUE
blue-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: blue-deployment
labels:
app: web-app
env: blue
spec:
replicas: 3
selector:
matchLabels:
app: web-app
env: blue
template:
metadata:
labels:
app: web-app
env: blue
version: "1"
spec:
containers:
- name: web
image: nginx:1.21
ports:
- containerPort: 80
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
configMap:
name: blue-config
---
apiVersion: v1
kind: ConfigMap
metadata:
name: blue-config
data:
index.html: |
<html>
<head><title>BLUE Environment</title></head>
<body style="background:#2196F3; color:white; font-size:2em; padding:50px">
<h1>🔵 BLUE Environment</h1>
<h2>Version 1.0</h2>
<p>This is the production environment</p>
</body>
</html>
green-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: green-deployment
labels:
app: web-app
env: green
spec:
replicas: 3
selector:
matchLabels:
app: web-app
env: green
template:
metadata:
labels:
app: web-app
env: green
version: "2"
spec:
containers:
- name: web
image: nginx:1.21
ports:
- containerPort: 80
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
configMap:
name: green-config
---
apiVersion: v1
kind: ConfigMap
metadata:
name: green-config
data:
index.html: |
<html>
<head><title>GREEN Environment</title></head>
<body style="background:#4CAF50; color:white; font-size:2em; padding:50px">
<h1>🟢 GREEN Environment</h1>
<h2>Version 2.0</h2>
<p>This is the new version!</p>
<p>Ready for production</p>
</body>
</html>
- executați comenzile de mai jos, folosind fișierele de mai sus:
# STEP 1: Deploy environment-ul BLUE (versiunea actuală)
kubectl apply -f blue-deployment.yaml
kubectl apply -f service.yaml
# Observă pod-urile BLUE:
kubectl get pods -l env=blue
# STEP 2: Testeaza BLUE (Production)
# Obține URL-ul:
BLUE_URL=http://localhost
echo "BLUE URL: $BLUE_URL"
curl $BLUE_URL
# Ar trebui să vezi "BLUE Environment - Version 1.0".
# STEP 3: Deploy Environment-ul GREEN (noua versiune)
# IMPORTANT: GREEN rulează în paralel, dar NU primește trafic încă.
kubectl apply -f green-deployment.yaml
# Verificăm ambele versiuni:
kubectl get deployments blue-deployment green-deployment
kubectl get pods -l app=web-app
# Output:
# blue-deployment-pod1 1/1 Running ← Receives traffic
# blue-deployment-pod2 1/1 Running ← Receives traffic
# blue-deployment-pod3 1/1 Running ← Receives traffic
# green-deployment-pod1 1/1 Running ← NO TRAFFIC yet
# green-deployment-pod2 1/1 Running ← NO TRAFFIC yet
# green-deployment-pod3 1/1 Running ← NO TRAFFIC yet
# Observa: 6 pod-uri rulează, dar doar 3 (BLUE) primesc trafic.
# STEP 4: Testeaza GREEN Direct (fără Service)
# Pentru a testa GREEN înainte de switch, accesează pod-ul direct:
kubectl port-forward $(kubectl get pod -l env=green -o jsonpath='{.items[0].metadata.name}') 8080:80 # aceasta comanda o sa blocheze terminalul curent
curl http://localhost:8080
# Ar trebui să vezi "GREEN Environment - Version 2.0".
STEP 5: SWITCH TRAFIC: BLUE → GREEN
# Schimbăm selector-ul service-ului:
kubectl patch service web-app-service -p '{"spec":{"selector":{"app":"web-app","env":"green"}}}'
# STEP 6: Observă switch-ul instant
# Testează imediat:
curl $BLUE_URL
# ar trebui să vezi imediat "GREEN Environment"!
# Observam:
# - Zero downtime
# - Switch instant
# - BLUE încă rulează (poate fi folosit pentru rollback)
# STEP 8: Rollback: switch înapoi la BLUE
kubectl patch service web-app-service -p '{"spec":{"selector":{"app":"web-app","env":"blue"}}}'
curl $BLUE_URL
# CLEANUP
kubectl delete deployment blue-deployment green-deployment
kubectl delete service web-app-service
kubectl delete configmap blue-config green-config