Continuous Integration/Deployment using Jenkins, Docker, Kubernetes on Oracle Cloud
By: Aditya Vishwekar
Tags: Jenkins, Oracle Cloud, Kubernetes, Docker, Microservices

Pre-requisites

Kubernetes Cluster -https://www.oracle.com/webfolder/technetwork/tutorials/obe/oci/oke-full/index.html

Jenkins — https://www.linuxtechi.com/install-configure-jenkins-on-centos-7-rhel-7/

Plugins to be installed on Jenkins

· Docker

· Kubectl, Kubernetes

· Oci-cli

Introduction

Problems which people face in IT Industry is long release cycle, tedious management of artifacts, rollbacks, patching software, versioning, etc. Below innovative solution describes methodologies which resolve these issues at a great extent.

This blog is partitioned in two parts — Continuous Integration (CI) and Continuous Deployment (CD).

Continuous Integration (CI) — It is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early.

Continuous Deployment (CD) — It is a strategy for software releases wherein any code commit that passes the automated testing phase is automatically released into the production environment, making changes that are visible to the software’ users.

Architecture Diagram

Developers can use Eclipse, JDeveloper, etc. to design and develop their micro-service. As soon as, developer commits his/her code in Git repository, Jenkins will compile the code using Maven, Gradle, NodeJS, etc. and create a Docker image of the micro-service. After creation of the image, Jenkins stage will tag and push the image to Docker Hub or Oracle Cloud Infrastructure Registry. So far, we’ve seen working of continuous integration pipeline. Deployment job will be triggered after successful completion of integration pipeline. This job will pull the newly created Docker image from the registry and deploy it on Kubernetes cluster using kubeconfig file.

Execution

For this tutorial, we’ll build and containerize a twitter-feed microservice. This service extract static tweets from twitter’s website. Following steps will walk you through on installation of Continuous Integration/Deployment pipeline on Jenkins for building and deploying twitter-feed micorservice.

Steps :-

Continuous Integration Pipeline (CI)

Fork Github Repository — https://github.com/AdityaVishwekar/twitter-feed

2. Login into Jenkins Dashboard

3. Click on “New item” → Enter an item name (e.g. twitter-feed-microservice-build) → Select “Pipeline” → “OK”

4. Configuration of twitter-feed-microservice-build Pipeline

a. Select “This project is parameterized” → Click on “Add Parameter” → Add following three String parameters

i. REGISTRY_USERNAME — {cloud_account_name}/{username}

ii. REGISTRY_TOKEN — Follow Step 2 : Get an Auth Token (https://www.oracle.com/webfolder/technetwork/tutorials/obe/oci/registry/index.html)

iii. DOCKER_REPO — iad.ocir.io/{ cloud_account_name }/{repository_name}

b. Under “Build Triggers” → Select “GitHub hook trigger for GITScm polling”

c. Under “Pipeline” → Select “Definition” as “Pipeline script from SCM”

d. Under “SCM” → Select “Git” →Paste your Github repository link beside “Repository URL” → Add “Credentials”

e. In “Script Path” → Type “Jenkinsfile” (without quotes)

f. Click “Apply” and “Save”

5. Click on “Build with Parameters” → Click “Build”. Newly created pipeline will start executing all the stages defined in Jenkinsfile.

Jenkinsfile

A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline and is checked into source control. Consider the following Pipeline which implements a basic continuous delivery pipeline.

Jenkinsfile

Pipeline Stages

First stage pulls the code from Git repository, next it compiles the source code as well as perform unit tests. After successfully compiling the code, third stage creates the docker image of the java application. Last stage tags and pushes the newly created docker image to Oracle Cloud Infrastructure Registry.

Continuous Deployment (CD)

1. Go to Jenkins Dashboard. Click on “New item” → Enter an item name (e.g. twitter-feed-microservice-deploy) → Select “Freestyle Project” → “OK”.

2. Under “Source Code Management” → Select “Git” → Paste your Github repository link beside “Repository URL” → Select “Credentials” already added in previous steps.

3. Scroll down to “Build Triggers” → Check the box “Build after other projects are built” → In “Projects to watch” textbox type “twitter-feed-microservice-build” → Select “Trigger only if build is stable”.

4. Go to “Build” step → Click on “Add Build Step” → Select “Deploy to Kubernetes” → Besides “Kubeconfig” click on “Add” and select “Jenkins”.

a. Add Credentials, Under “Kind” select “Kubernetes configuartion”.

b. Give some “ID” name and select “Enter directly” besides “Kubeconfig”.

c. Copy and Paste the kubeconfig file here and click “Add”.

5. In “Config Files” textbox, enter “kubernetestwitter.yml” and click “Apply” and “Save”.

Additional Information

Make a Bug Fix to Your Java Microservice

You’ll work on fixing a bug found in Java microservice that you deployed to Kubernetes in the previous step. You will make a code change that fixes the bug, commit the code to GitHub, and observe the execution of the build and deploy workflow that is managing your application. Then you’ll test the application to verify that the bug fix was successful.

In a new browser tab, navigate to your forked twitter-feed repository on GitHub. If you’ve closed the tab, you can get back by going to GitHub, scrolling down until you see the repositories box on the right side of the page, and clicking the twitter-feed link.

2. Click the Find file button.

3. Type statictweets in the search bar, then click on StaticTweets.java to open the file.

4. Click the pencil button to begin editing the file.

5. Scroll down to approximately line 57. This is the REST endpoint that should return only tweets that match a given search term, but it’s not working. Delete line 57 to fix the bug that is breaking this feature.

6. Scroll to the bottom of the page and click Commit changes.

Go back to Jenkins dashboard, where you can see that “twitter-feed-microservice-build” pipeline was triggered by recent commit. After new code is build, “twitter-feed-microservice-deploy” job is initiated to deploy the image from Oracle Cloud Infrastructure Registry (OCIR) or Docker Hub to Kubernetes Cluster (Oracle Kubernetes Engine OKE).

Conclusion

This blog helps to practically understand DevOps concept of Continuous Integration and Deployment of microservices using Jenkins, Docker containers, Kubernetes, etc. on Oracle Cloud. This solution reduces the release cycle from 2 weeks to 1 day. You can deploy multiple microservices at same time on multiple servers within few minutes.