Jenkins pipeline for blue green deployment using AWS EKS — Kubernetes — Docker — Capstone project, Cloud DevOps Nanodegree

Alvaro Andres Pinzon Cortes
3 min readSep 12, 2019

--

How to install docker, aws cli, eksctl, kubectl for Jenkins in Linux Ubuntu 18.04?

https://medium.com/@andresaaap/how-to-install-docker-aws-cli-eksctl-kubectl-for-jenkins-in-linux-ubuntu-18-04-3e3c4ceeb71

Set up Jenkins and the necessary packages

  1. In AWS Launch the EC2 t2.micro for the free tier, pick “Ubuntu 18.04 LTS amd64
  2. Connect your instance using SSH. This guide is helpful https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

Example command:

ssh -i /path/my-key-pair.pem ubuntu@ec2-198-51-100-1.compute-1.amazonaws.com

You need to make sure that the IAM user that you are using has all of the correct permissions.

3. Install java JDK

4. Install Jenkins On Ubuntu

5. Set Up Jenkins

6. Install the Blue Ocean plugin in jenkins

7. Install the pipeline-aws plugin in jenkins

8. Install docker. You can use this tutorial for linux ubuntu https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-18-04

9. Install the necessary dependencies to deploy your cluster with AWS EKS. Follow this tutorial https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html. Please look at the recomendations in this guide question 14 https://medium.com/@andresaaap/capstone-cloud-devops-nanodegree-4493ab439d48

10. Create in Jenkins the credentials for AWS

11. Create in Jenkins the credentials for Docker

Create kubernetes cluster

Example command:

aws eks --region us-east-1 update-kubeconfig --name capstonecluster

Create the blue green deployment pipeline in Blueocean

You need to create 1 pipeline that is going to be used by the blue and the green branches. This pipeline is going to be created in Blueocean because it already supports multibranch pipelines To understand in depth and see a good example of a multibranch jenkins pipeline please go to this tutorial.

First, create a Jenkinsfile to put the next stages:

  • Create a new stage to build the docker image
  • Create a new stage to push the docker image
  • Create a new stage to create the kubeconfig file to tell kubectl to which cluster to connect
  • Create a stage to deploy a blue container
  • Create a stage to redirect the service to your blue container
  • Create a stage to deploy a green container
  • Create a stage to redirect the service to your green container
stage(‘Linting') {}stage('Build image') {}stage(Push image') {}stage('create the kubeconfig file') {}stage('Deploy blue container') {  when { branch 'blue'}}stage('Redirect service to blue container') {  when { branch 'blue'}}stage('Deploy green container') {  when { branch 'green'}}stage('Redirect service to green container') {  when { branch 'green'}}

Please notice that the when statement is key for creating a multibranch pipeline and that with when we can execute some stages for just green and others for just blue.

Run the blue and green pipeline

After you create the pipeline you can run blue if you want that blue is the one that the users can access or can run green if you want that green is the one that the users can access.

You can run the pipeline for each branch by commiting to the blue branch if you want to deploy to the blue and the same with green. You can also run the branched from the UI of Blueocean.

Example of a simpler blue green deployment in you local machine (This is much simpler and less automitized than the one in Jenkins)

You can see here an example of a blue and green deployment locally in minikube https://medium.com/@andresaaap/simple-blue-green-deployment-in-kubernetes-using-minikube-b88907b2e267

How to deploy your microservices to kubernetes and access them with a Load Balancer External IP?

--

--