Blue Green Deployment

create version 1 and 2 and services

# prepare namespace


mkdir ~/blue-green && cd ~/blue-green
kubectl create namespace blue-green
kubectl config set-context --current --namespace blue-green

# create manifests

cat >nginx-v1.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
    version: V1.0.0
  name: webv1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
      version: V1.0.0
  template:
    metadata:
      labels:
        app: web
        version: V1.0.0
    spec:
      containers:
      - image: nginx:1.19.3
        name: nginx
        ports:
        - name: http
          containerPort: 80
EOF

cat >nginx-v2.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: web
    version: V2.0.0
  name: webv2
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
      version: V2.0.0
  template:
    metadata:
      labels:
        app: web
        version: V2.0.0
    spec:
      containers:
      - image: nginx:1.19.8
        name: nginx
        ports:
        - name: http
          containerPort: 80
EOF
cat >nginx-svc.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
  labels:
    app: web
  name: nginx
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: web
    version: V1.0.0
EOF

Deployment from blue to green


# deploy blue

kubectl apply -f nginx-v1.yaml 
kubectl apply -f nginx-svc.yaml
kubectl describe deployment webv1
kubectl describe svc nginx
kubectl get ep nginx

# deploy green
kubectl apply -f nginx-v2.yaml

# traffic shift
kubectl patch service nginx --patch '{"spec":{"selector":{"version":"V2.0.0"}}}'

# Test if the second deployment was successful
kubectl describe deployment webv2
kubectl describe svc nginx
kubectl get ep nginx

# cleanup v1

kubectl delete deploy webv1

WARNING:Traefik shift don't change established connection!!

kubectl run nc -it --rm --image curlimages/curl -- /bin/sh
nc  nginx.blue-green.svc.cluster.local 80
GET /index.html HTTP/1.1
Host: localhost

...

# switch at other terminal the load with patch
#kubectl patch service nginx --patch '{"spec":{"selector":{"version":"V1.0.0"}}}'

GET /index.html HTTP/1.1
Host: localhost

# same server/pod answer ...

