Practical Hashicorp Nomad and Consul — CI/CD Pipeline to deploy the API and WebApp Part 2

Arunkumar Gunasekaran
6 min readOct 1, 2021

--

Co-authored by Manikandan S, Jawahar Suresh Babu S, Navin Nair, ankita srivastava

If you are here directly part of google search and missed out Part 1 of the tutorial, please click the link to go through it.

If you are here as a continuation of Part 1, welcome back!

In part 1, we went through the steps of setting up a 3-Tier application and deploying it in a Nomad/Consul cluster as part of a service mesh. The deployment package was fixed and uploaded as jar/zip in github and we were using that to deploy it to our cluster. In this tutorial, we will setup a CI/CD pipeline to pick up the code for Spring-Petclinic frontend and API from github, build it and then deploy it to our Nomad cluster.

Clone the Github repo, if it is not done in part1 !!

We will be using the following CI/CD tools to set up a pipeline.

  • Jenkins - to setup the CD pipeline
  • JFROG Artifactory - as a binary repository management tool
  • Terraform - for deployment automation into Nomad

Let see the high level view of the pipeline setup!!!

Note: Entire setup is done on the local machine, although it can be done on a cloud instance as well.

CI/CD Pipeline

Before installing the tools, increase your CPU & Memory in docker desktop. Since default docker desktop resource will not be sufficient to setup the pipeline in your local machine.

Install Jenkins

Most of the plugins as part of pipeline setup will be pre installed in Jenkins using the below Docker file

  • Maven- to build a Spring Petclinic API artifact.
  • Terraform- to deploy the nomad job file.
  • Node JS- to build a Spring Petclinic Frontend artifact.

Build the docker file and create the Jenkins container using below docker commands, Jenkins will be hosted on the port 8080.

cd jenkins-dockerdocker build -t jenkins:local .docker run -p 8080:8080 -p 50000:50000 -v ~/jenkins_home:/var/jenkins_home jenkins:local

During first time setup, install the suggested plugin by Jenkins as shown below and create the admin user name and password. (Note: You can create your own admin user name and password)

Install

Install the JFROG Artifactory to maintain a single place solution for teams to manage all their binary artifacts efficiently.

Run the below docker command to set up the artifactory in your local machine

Note: We will be using JFROG Artifactory open source image to create the container.

docker run -d --name artifactory -p 9082:8082 -p 9081:8081 -v artifactory-data:/var/opt/jfrog/artifactory releases-docker.jfrog.io/jfrog/artifactory-oss:latest

Access the artifactory on the port 9082 in your local machine and Login with default credentials (username: admin & password: password).

Configure Artifactory

Repositories and service profile should be created in artifactory in order to access the Artifactory repository seamlessly from the Jenkins container. When pipeline is triggered, Jenkins will clone the Petclinic app repo into the workspace and it will run the local build & push artifact to artifactory repository using service profile.

Here we will be creating service profile for Jenkins in artifactory UI under admin tab (username: jenkins password: Admin@123). You can give administrator privilege, since this is your local machine.

Note: Keep the password (Admin@123) as mentioned above for this tutorial since the same password will be used later in the article for configuration.”

Jenkins Service Profile to connect JFROG artifactory
Jenkins Service Profile to connect artifactory

Create the two local repository in artifactory as shown in below name and repository type (Spring-Petclinic-Angular-Local, Spring-Petclinic-Rest-Local).

Configure Artifactory Plugin in Jenkins

Install the artifactory plugins in Jenkins, that integrates Jenkins and Artifactory to publish, resolve, promote and release traceable build artifacts.

Manage Jenkins → Manage Plugins → Search for “Artifactory” and Install it.

Artifactory Plugin

Now integrate the artifactory plugin with Jenkins using below step.

Manage Jenkins → Configure Systems. You would find JFROG Platform instance like below. (Instance ID: artifactory-server JFROG Platform URL : http://host.docker.internal:9082 username: jenkins password: Admin@123).

Now you can test the connection and check !!!

Create the pipeline and play around in Jenkins

Pipeline script is written in the form of jenkinsfile that contains the definition of Jenkins pipeline. You can setup the pipeline job by directly configuring the github repo and the rest will be taken care by pipeline script.

1) Create the Spring Petclinic Restapi pipeline

  • Under Jenkins Dashboard, Click on New item and name the pipeline as spring-petclinic-restapi and select the option as pipeline script shown in below.

2) Similarly create the pipeline for Petclinic Web and name the pipeline as spring-petclinic-web.

3) Now create the CD Jenkins pipeline

Create the new pipeline job and name the jobs as practical-nomad-consul (Job name is very important, CI pipeline that we created for petclinic api & web will trigger the CD pipeline using Job Name).

  • Configure the CD pipeline git url under Pipeline Script from SCM.
  • Pipeline script path should be pipeline-scripts/Jenkinsfile and branch should be main as shown in below(Only for this pipeline job)

Trigger the Jenkins Pipeline !!!!

Now stop the Nomad Jobs which is created manually before triggering the pipeline

nomad stop -purge petclinic-web
nomad stop -purge petclinic-api
  • Go into spring-petclinic-restapi Jenkins job and click Build Now.
  • Restapi pipeline will automatically trigger the practical-nomad-consul job and post that go into the practical-nomad-consul job and approve the pipeline manually for Nomad Deployment as below.

Note: Pipeline will stop here for your confirmation to do terraform apply. Approve the pipeline to complete the deployment.

Jenkins Approval
  • Follow the above steps for triggering spring-petclinic-web job.

Trigger the Jenkins Pipeline and you would be seeing pipeline stages like below

Congratulations!!!

Congratulation you have successfully setup the CI/CD pipeline for Spring-Petclinic frontend and API to deploy into Nomad Cluster. In the next part, we will see how we can add observability to our Nomad cluster & service mesh and also setup auto-scaling based on load.

--

--