In this workshop, we'll cover the basics of setting up a barebone deployment pipeline, in support of a green-blue deployment strategy. We will be able to build upon this exercise in the upcoming homework and DEPLOYMENT milestone.
To start with, you'll need some files in this repo to help setup the blue-green infrastructure.
git clone https://github.com/CSC-DevOps/Deployment.git
npm install
We'll create two endpoints for our deployment, a "green" endpoint for our baseline, and a "blue" endpoint for our test commits. We will be using git repositories to help with copying over bits. See guide for more details.
Create a folder structure as follows:
- deploy/
- blue.git/
- blue-www/
- green.git/
- green-www/
To ensure we have a git repo that will always have files that reflect the most current state of the repo, we will use a "bare" repository, which will not have a working tree. Using a hook script, the changes will then be checked out to public directory.
cd deploy/green.git
git init --bare
cd ..
cd blue.git
git init --bare
Inside green.git/hooks/
inside a post-receive
file, place the following:
GIT_WORK_TREE=<...path...>/green-www/ git checkout -f
Repeat for blue.
Hints
- You must create the *-www folder manually.
- You may have to add executable permissions using in *nix systems
chmod +x post-receive
. - Ensure that there is a script header, such as
#!/bin/sh
, on the first line. - For the purpose of this workshop,
<...path...>
refers to the absolute path of your folder. - It will not work the first time.
Clone the app repo, and set the following remotes. See help on file protocol syntax.
git remote add blue file://<...path...>/blue.git
git remote add green file://<...path...>/green.git
You can now push changes in the following manner.
git push green master
git push blue master
You may have to create a simple commit before pushing.
Install a node process supervisor, globally, as needed by the demo, run:
npm install -g forever
Then bring up the infrastructure:
node infrastructure
When you first run it. It will not work! Notice that *-www, doesn't have any node_modules/ installed. Think about some of the conceptual issues of deploying code versus a build. For now, you can add into a hook, a step to run: "npm install".
You should be able to visit localhost:8080 and access the green slice! In expanding on this concept, we could do the same exact steps, but on a different AWS instances, droplets, etc.
Change the message to report, "Hello Blue".
Push the change.
Test the blue server directly, using port 9090.
Notice, it hasn't updated yet...
You will need to modify how "forever" is run, by including a "--watch" flag which will restart the process if the file it is running changes. Think carefully on where to place the flag. You may also need to use "--watchDirectory" depending on where you have placed the deploy folders.
Push another change, "Hello Blue 2". Now see if you can observe on the blue server.
Have the default TARGET to be BLUE now.
Modify the app repo, to explicitly fail with : res.status(500).send('Something broke!');
Have a heartbeat that checks every 30 second for a http 500, and if so, will switch the proxy over to the green environment.
This idea can be generalized to be triggered by any other monitoring/alerts/automated testing (during staging). E.g., See how to use toobusy.
Instead of sending over the files and building, you can modify pipeline to build a docker image that is pushed to a registery and deployed.
Start a private registry on port 5000.
docker run -d -p 5000:5000 --restart=always --name registry registry:2
More information about setting up a TLS-secured registry, which is remotely accessible.
cd App
docker build -t ncsu-app .
docker run -p 50100:8080 -d --name app ncsu-app
docker logs <containerid>
If successful, can deploy to a private registry.
docker tag ncsu-app localhost:5000/ncsu:latest
docker push localhost:5000/ncsu:latest
A script like this can run after a git hook or deploy command. The server will pull from registry, stop existing app container and run new instance.
docker pull localhost:5000/ncsu:latest
docker stop app
docker rm app
docker rmi localhost:5000/ncsu:current
docker tag localhost:5000/ncsu:latest localhost:5000/ncsu:current
docker run -p 50100:8080 -d --name app localhost:5000/ncsu:latest
curl -i localhost:50100