Blue/Green Service

Tutorial - Using blue/green deployment

A blue/green deployment is a method of achieving zero downtime by running two versions of the same service (a “blue” and a “green” version) and having the load balancer switch between the two.

Blue/green deployment allows you to have two fully scaled versions of the service. If something goes wrong with one, you can quickly switch to the other by adjusting the load balancer.

Use Case: Service Upgrade

  1. The web-server service has “blue” and “green” versions that are running and the load balancer is routing traffic successfully to “blue”.
  2. The Edge-LB configuration is updated to route traffic to “green” and errors start occurring in the “green” service’s logs.
  3. The deployment is quickly rolled back by reverting Edge-LB to the original configuration.

Prerequisites

  • Edge-LB CLI is installed.

    dcos package install edgelb --cli
    
  • Enough capacity on the DC/OS cluster for two fully deployed services.

Example Deployment

Below is a minimal Edge-LB configuration, sample-minimal.json, that load balances “svc-blue”.

{
  "apiVersion": "V2",
  "name": "sample-config",
  "count": 1,
  "haproxy": {
    "frontends": [{
      "bindPort": 80,
      "protocol": "HTTP",
      "linkBackend": {
        "defaultBackend": "svc"
      }
    }],
    "backends": [{
      "name": "svc",
      "protocol": "HTTP",
      "services": [{
        "marathon": {
          "serviceID": "/svc-blue"
          },
          "endpoint": {
            "portName": "web"
          }
      }]
    }]
  }
}

Copy the example service and name it svc-blue.json.

{
    "id": "/svc-blue",
    "cmd": "/start $PORT0",
    "instances": 1,
    "cpus": 0.1,
    "mem": 32,
    "container": {
        "type": "DOCKER",
        "docker": {
            "image": "mesosphere/httpd"
        }
    },
    "portDefinitions": [
        {
            "name": "web",
            "protocol": "tcp",
            "port": 0
        }
    ],
    "healthChecks": [
        {
            "portIndex": 0,
            "path": "/",
            "protocol": "HTTP"
        }
    ]
}

Make another copy of the example service and name it svc-green.json.

{
    "id": "/svc-green",
    "cmd": "/start $PORT0",
    "instances": 1,
    "cpus": 0.1,
    "mem": 32,
    "container": {
        "type": "DOCKER",
        "docker": {
            "image": "mesosphere/httpd"
        }
    },
    "portDefinitions": [
        {
            "name": "web",
            "protocol": "tcp",
            "port": 0
        }
    ],
    "healthChecks": [
        {
            "portIndex": 0,
            "path": "/",
            "protocol": "HTTP"
        }
    ]
}
  1. Upload the configuration to Edge-LB.

    dcos edgelb create sample-minimal.json
    
  2. Start the service “svc-blue”.

    dcos marathon app add svc-blue.json
    
  3. Now, “svc-blue” is exposed at http://<public-ip>/.

  4. Begin blue/green deployment by deploying “svc-green”.

    dcos marathon app add svc-green.json
    
  5. Once svc-green is up and healthy, modify the Edge-LB configuration to point to svc-green by changing:

    {
      ...
      "haproxy": {
        "backends": [{
          ...
          "services": [{
            "marathon": {
              "serviceID": "/svc-blue"
            },
            ...
          }]
        }]
      }
    }
    

    to:

    {
      ...
      "haproxy": {
        "backends": [{
          ...
          "services": [{
            "marathon": {
              "serviceID": "/svc-green"
            },
            ...
          }]
        }]
      }
    }
    
  6. Upload the modified configuration to Edge-LB.

    dcos edgelb update sample-minimal.json
    
  7. Now “svc-green” is exposed at http://<public-ip>/.