Giter Site home page Giter Site logo

titilambert / k8s-blue-green-deployment Goto Github PK

View Code? Open in Web Editor NEW

This project forked from codefresh-io/k8s-blue-green-deployment

1.0 3.0 0.0 18 KB

Fully automated K8s blue green deployments

Home Page: https://codefresh.io/kubernetes-tutorial/fully-automated-blue-green-deployments-kubernetes-codefresh/

License: MIT License

Dockerfile 10.86% Go 9.06% Shell 80.08%

k8s-blue-green-deployment's Introduction

Kubernetes blue green deployments

This repository holds a bash script that allows you to perform blue/green deployments on a Kubernetes cluster. See also the respective blog post

Description

The script expects you to have an existing deployment and service on your K8s cluster. It does the following:

  1. Finds the current deployment (by looking at the selector of the service)
  2. Copies the old deployment to a new one changing the Docker image to the new version
  3. Applies the new deployment on the cluster. At this point both deployments co-exist
  4. Waits for a configurable amount of seconds
  5. Checks the health of the new pods. If there are restarts, it considers the new deployment unhealthy. In that case it removes it and the cluster is unaffected by the deployment
  6. If the health is ok it switches the service to point to the new deployment
  7. It removes the old deployment

Of course during the wait period when both deployments are active, you are free to run your own additional checks or integration tests to see if the new deployment is ok.

Prerequisites

As a convention the script expects

  1. The name of your deployment to be $APP_NAME-$VERSION
  2. Your deployment should have a label that shows it version
  3. Your service should point to the deployment by using a version selector, pointing to the corresponding label in the deployment

Notice that the new color deployment created by the script will follow the same conventions. This way each subsequent pipeline you run will work in the same manner.

You can see examples of the tags with the sample application:

How to use the script on its own

The script needs one environment variable called KUBE_CONTEXT that selects the K8s cluster that will be used (if you have more than one)

The rest of the parameters are provided as command line arguments

Parameter Argument Number Description
Service 1 Name of the existing service
Deployment 2 Name of the existing deployment
New version 3 Tag of the new docker image
Health command 4 Currently unused
Health seconds 5 Time where both deployments will co-exist
Namespace 6 Kubernetes namespace that will be used

Here is an example:

./k8s-blue-green.sh myService myApp 73df943 true 30 my-namespace

How to do Blue/Green deployments in Codefresh

The script also comes with a Dockerfile that allows you to use it as a Docker image in any Docker based workflow such as Codefresh.

For the KUBE_CONTEXT environment variable just use the name of your cluster as found in the Codefresh Kubernetes dashboard. For the rest of the arguments you need to define them as parameters in your codefresh.yml file.

  blueGreenDeploy:
    title: "Deploying new version ${{CF_SHORT_REVISION}}"
    image: codefresh/k8s-blue-green:master
    environment:
      - SERVICE_NAME=my-demo-app
      - DEPLOYMENT_NAME=my-demo-app
      - NEW_VERSION=${{CF_SHORT_REVISION}}
      - HEALTH_SECONDS=60
      - NAMESPACE=colors
      - KUBE_CONTEXT=myDemoAKSCluster

The CF_SHORT_REVISION variable is offered by Codefresh and contains the git hash of the version that was just pushed. See all variables in the official documentation

Dockerhub

The blue/green step is now deployed in dockerhub as well

https://hub.docker.com/r/codefresh/k8s-blue-green/

Future work

Further improvements

  • Make the script create an initial deployment/service if nothing is deployed in the kubernetes cluster
  • Add more complex and configurable healthchecks

k8s-blue-green-deployment's People

Contributors

kostis-codefresh avatar

Stargazers

 avatar

Watchers

Thibault Cohen avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.