Using Deployment Health Checks In a Rainbow Deployment
- Difficulty - intermediate
- Time To Complete - 20-30 minutes
- Free Tier Compatible - no
Resources
Before starting this guide, several resources are required.
A GitHub repository has been provided with all the necessary resources for following along with this guide.
For those using a custom Stack file, it must meet the following requirements to ensure compatibility with the walkthrough:
- It should define and implement health checks.
- It must utilize an image that is publicly accessible and served over bind-all IPv6.
- The internal server port must be identified.
Creating the Stack
First, import the Stack into Cycle.
- Navigate to the Stacks tab
- Click Create in the top left above the Stacks list
- Add a Stack name
- Select Import from Git Repo
- Paste the link from the Git Repo into the Git Repo URL field
- The Git Repo has the stack file at cycle.json, which is the default, so Stack File field can use the default. If the stack file is named something else, set this field to the name of the Cycle Stack File
- Click Create Stack
Once the Stack is created, click Create Stack Build in the upper right of the stack to generate a build.
Creating the Deployment
Checkpoint
At this point you should have the following resources created:
- Server
- Environment
- DNS Zone (verified)
- Stack & Stack Build
Begin by creating a new deployment from the existing stack build. This step establishes a foundation for the next steps. From here, deployment tags will be created that can be associated with a LINKED Record.
Once the new stack is created, the system will automatically route to it. Click the Deploy button in the upper right corner.
Using the stack deploy modal, select Deployment type. Choose the previously created environment, then enter a pre-1.0 version (e.g., 0.1.0). The rest of the form can be left with default selections. Then, click the Deploy button.
Once the stack is deployed, click on Continue To Environment and start the environment containers. A key advantage of using a stack is that all container configurations are managed within the stack itself, eliminating the need for manual updates. Once the container is running, proceed with creating a tag for the deployment.
To do this:
- Click on the Deployments tab within the environment
- Click the + button next to the new deployment.
- In the popup, add a tag. For this tutorial, use the tag current.
Setting Up Records
Now that the tag is set up, a LINKED Record can be associated with the tag. This allows for an association between a domain and a container within a deployment. This mechanism will facilitate the rainbow deployment via the pipeline.
When the tag is later updated to point to a new deployment, traffic will automatically switch over.
To create the record:
- Return to the DNS Zone created earlier
- Click Add Record button in the top right
- In the Record Create dialog, create a LINKED Record
- Select the Environment with the Deployment
- Select Link to Deployment
- Add the container identifier on left-hand side of the form, and the tag on the right
- Select TLS Enabled to automatically generate a TLS cert for this domain.
- Navigate to the domain and a Hello World application should appear
Tag Mechanics
Each time the stack is created and deployed, the container identifier remains the same. The LINKED Record will look for a container identifier match within the deployment with the specified tag. Therefore, when the tag is updated to a new deployment, traffic will automatically route to the container within that new deployment.
Adding the Pipeline
The next step is to create the Pipeline.
- Navigate to the Pipelines Section of the Portal
- Click the Create button
- Provide a name
- Select Dynamic pipeline. This allows for variables to be inserted into the pipeline at runtime.
- Select the Editor tab to add stages and steps.
Pipeline Steps
Step 1: Create Stack Build
The first two steps in this pipeline involve creating and generating the assets (stack build) that will be used.
- Click on the Add New Step button, then select Create Stack Build from the dropdown menu.
- Before filling out the Stack portion of the form, add a step reference create at the top of the form.
- Use the lookup functionality to select the stack from Stack Build Search. Refer to the image below for guidance on locating this in the modal.
The rest of the form can stay use the defaults since the stack refererences the main branch and latest commit.
Step 2: Generate Stack Build
- Add the step Generate Stack Build from the dropdown menu.
- Add a step reference generate at the top of the form.
- In the Stack portion of the form, select From in the Target section and then select (Stage 1) -> create (Step 1) from the dropdown
- This will use the Stack specified in the previous step and generate a full build of all images to prepare for deployment
Step 3: Deploy Stack Build
- Add the step Deploy Stack Build.
- Select the appropriate environment using the environment search feature seen in step 1
- For the Stack Build location, select From and target (Stage 1) -> generate (Step 2), which is the output from the previous step.
- Check the deployment box and use
{{deployment-version}}
for the pipeline variable in the version field.- this will allow the variable to be specified at runtime
Step 4: Start Environment Deployment
- Add the step Start Environment Deployment
- Select the appropriate Environment
- Set the version variable to
{{deployment-version}}
to match the variable specified in the previous step
Tag
The tag option exists here in case a user wishes to start the environment deployment using a tag identifier instead of a version identifier. Ultimately a matter of preference and desired workflow.
Once the deployment is started, the next step is wait for the deployment's health check to pass. Only after the health check is successful should the process move on to updating the deployment tags to reflect the new deployment. This ensures that everything is online before the load balancer routes traffic to the new deployment.
Step 5: Wait for Environment Deployment Health Check
- Add the step Wait for Environment Deployment Health Check
- Select the Environment
- Add the version using the variable
{{deployment-version}}
- Optionally, add a duration
- the default is 5 minutes and is fine for this tutorial
Health Check
Inspect the stack in the companion
repo for
this tutorial to find a script named health_check.sh
. This script is
configured in the cycle.json
file to serve as the health check for the
container. It is essential that the script returns a 0 exit code for
success and a non-0 exit code for failure.
With the health check in place, the pipeline will pause to verify that the deployment is healthy. At this point, traffic can switch over, but it can be helpful to add a few more steps to facilitate easy rollback.
Step 6: Update Deployment Tag Version
- Add the step Update Deployment Tag Version.
- Select the Environment
- In the deployment section, set up two tags, the Existing Tag and the New Tag. The tag created earlier in the tutorial is named current. This will be the existing tag.
- For the New Tag field enter the string previous.
Tagging the existing deployment keeps the previous version available for rollback. This also enables later use of the prune function to clean up untagged deployments.
Step 7: Update Deployment Tag Version
- Add an additional Update Deployment Tag Version step.
- Select the Environment
- Set Deployment Type to Version
- For Existing Version target the
{{deployment-version}}
pipeline variable used in earlier steps - In the New Tag field, set the tag to current
There are now two deployments: one tagged previous for the earlier deployment and one tagged current for the latest deployment. This setup allows for easy rollback by simply updating the tag.
Step 8: Prune Environment Deployments
The final step is Prune Environment Deployments. This step will take the environment and remove all deployments in that environment that do not have a tag.
- Add the step Prune Environment Deployments
- Select the Environment
Next Steps
Trigger Pipeline
Once the pipeline is created, it can be run it using the Trigger Pipeline button at the top of the screen. Since a pipeline variable is used, click the V button next to Trigger Pipeline to open the variable entry modal. A prompt will appear to enter a variable that will replace all instances of {{deployment-version}}
in the pipeline.
Use Trigger Keys
A more advanced option for running the pipeline is using a trigger key. Generate a trigger key to run the pipeline remotely from a local machine or integrate it into a CI/CD pipeline.