- Notifications
You must be signed in to change notification settings - Fork0
A tutorial on doing blue/green deployments with Kubernetes
License
dreadbird/kubernetes-bluegreen-deployment-tutorial
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This is a simple tutorial on how to doBlue/Green Deployment on Kubernetes.
Any Kubernetes cluster 1.3+ should work. Creating a cluster onGKE is pretty easy.
gcloud container clusters create bluegreenThe blue Deployment is the version that is deployed live in production. It can be accessed externally by end users via a Service with type=LoadBalancer
The Deployment will start up a few nginx containers as the application. The Deployment has aname andversion label. This is significant as the Service will use these labels to switch to the green version later.
apiVersion:extensions/v1beta1kind:Deploymentmetadata:name:nginx-1.10spec:replicas:3template:metadata:labels:name:nginxversion:"1.10"spec:containers: -name:nginximage:nginx:1.10ports: -name:httpcontainerPort:80
Create the Blue Deployment:
$ kubectl apply -f kubernetes/blue-deploy.yamlThe service is of type=LoadBalancer so it can be accessed via a Network Load Balancer on GCP. It uses thename andversion labels specified in the Deployment to select the pods for the service.
apiVersion:v1kind:Servicemetadata:name:nginxlabels:name:nginxspec:ports: -name:httpport:80targetPort:80selector:name:nginxversion:"1.10"type:LoadBalancer
Create the Service:
$ kubectl apply -f kubernetes/service.yamlThe currently deployed version can be tested in a separate window by polling the server. This will print the current deployed nginx version.
$ EXTERNAL_IP=$(kubectl get svc nginx -o jsonpath="{.status.loadBalancer.ingress[*].ip}")$ while true; do curl -s http://$EXTERNAL_IP/version | grep nginx; sleep 0.5; doneNow we are ready to deploy a new version.
A new Deployment will be created to update the application and the Service will be updated to point at the new version. This is mostly instantaneous.
The Green Deployment is cerated by updating to the next version. An entirely new Deployment will be created with different labels. Note that these labels don't match the Service yet and so requests will not be sent to pods in the Deployment.
apiVersion:extensions/v1beta1kind:Deploymentmetadata:name:nginx-1.11spec:replicas:3template:metadata:labels:name:nginxversion:"1.11"spec:containers: -name:nginximage:nginx:1.11ports: -name:httpcontainerPort:80
You can update the Blue Deployment's file directly or use a tool likesed:
Create the new Deployment:
$ sed 's/1\.10/1.11/' kubernetes/blue-deploy.yaml | kubectl apply -f -We will update the Service to select pods from the Green Deployment. This will cause new requests to be set to the new pods.
You can update the file directly or use a tool likesed:
$ sed 's/1\.10/1.11/' kubernetes/service.yaml apiVersion: v1kind: Servicemetadata: name: nginx labels: name: nginxspec: ports: - name: http port: 80 targetPort: 80 selector: name: nginx version: "1.11" type: LoadBalancerUpdate the Service:
sed 's/1\.10/1.11/' kubernetes/service.yaml | kubectl apply -f -At this point traffic should be sent to the Green version and the output of our test loop should show "nginx/1.11.X". However, in a production environment, the Blue version may have some long running requests so it may take some time before the requests are fully drained. That means that some time should be given before deleting the old Blue Deployment.
While ideally Blue/Green Deployments would be implemented server side, one way to automate them is on the client-side using scripts. This very simple bash script creates the new Deployment and waits for it to become ready before updating the Service's selector.
#!/bin/bash# bg-deploy.sh <servicename> <version> <green-deployment.yaml># Deployment name should be <service>-<version>DEPLOYMENTNAME=$1-$2SERVICE=$1VERSION=$2DEPLOYMENTFILE=$3kubectl apply -f$DEPLOYMENTFILE# Wait until the Deployment is ready by checking the MinimumReplicasAvailable condition.READY=$(kubectl get deploy$DEPLOYMENTNAME -o json| jq'.status.conditions[] | select(.reason == "MinimumReplicasAvailable") | .status'| tr -d'"')while [["$READY"!="True" ]];do READY=$(kubectl get deploy$DEPLOYMENTNAME -o json| jq'.status.conditions[] | select(.reason == "MinimumReplicasAvailable") | .status'| tr -d'"') sleep 5done# Update the service selector with the new versionkubectl patch svc$SERVICE -p"{\"spec\":{\"selector\": {\"name\":\"${SERVICE}\",\"version\":\"${VERSION}\"}}}"echo"Done."
About
A tutorial on doing blue/green deployments with Kubernetes
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- Shell100.0%