Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A tutorial on doing blue/green deployments with Kubernetes

License

NotificationsYou must be signed in to change notification settings

dreadbird/kubernetes-bluegreen-deployment-tutorial

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is a simple tutorial on how to doBlue/Green Deployment on Kubernetes.

Prerequisites

Any Kubernetes cluster 1.3+ should work. Creating a cluster onGKE is pretty easy.

gcloud container clusters create bluegreen

Setup

The 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

Create the Blue Deployment

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.yaml

The 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.yaml

Test the Blue Deployment

The 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; done

Now we are ready to deploy a new version.

Update the application

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.

Create the Green Deployment

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 -

Switch Traffic to the Green Version

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: LoadBalancer

Update the Service:

sed 's/1\.10/1.11/' kubernetes/service.yaml | kubectl apply -f -

Test the New Version

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.

Automating Blue/Green Deployments

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell100.0%

[8]ページ先頭

©2009-2025 Movatter.jp