Jenkins 2, Java, Openshift, Docker, Kubernetes

2020-01-03 jenkins kubernetes docker java

Overview

This is an example that might help you to get some ideas on how you can use Jenkins, Java microservices, Docker, Kubernetes, and Openshift. This Jenkins pipeline will build a Java microservice based on Spring Boot, wrap this microservice in a Docker image, push this image to DockerHub and create the required Kubernetes objects to expose this microservice in the Openshift/Kubernetes cluster.

You should have some basic understandings of Kubernetes to go through the article, however, If you can start Openshift locally, you can poke around the whole code of the project and review a little about the concepts and the usage of DeploymentConfig, Service, Route, SecurityContextContraint, and RoleBindings.

Setup your local environment.

Download Openshift 3 CLI

Download the CLI for Openshift 3 and setup this CLI release on your environment:

https://github.com/openshift/origin/releases/tag/v3.11.0

For example, if you are using a Linux distribution you can download this:

oc 3.11.0. You need to add it to your PATH.

After you are done you can test the command as below:

oc version

Setup insecure docker registry:

In /etc/docker/daemon.json add this configuration:

Download the code

Start Openshift 3 locally

oc cluster up

This will create a folder ‘openshift.local.clusterup’ where you executed the command. This folder will keep your configuration for Openshift in your local environment.

Create the Kubernetes objects

Store this token in your Jenkins Configuration as Code file.

You can see it on the image the token is the value starting with ‘eyJhbGciOiJSUzI1NiI….“.

In the openshift/deployment-java-app/jenkins/jcasc.yaml file there is a Credential with Id ‘openshift-login’. Assign to the ‘secret’ the value you got from the token so Jenkins can perform operations using the API.

Setup your DockerHub account in the pipeline and Deployment Config

In order to successfuly run the Pipeline poiting to your DockerHub account you need to update these files:

Start jenkins

For this I have used a Jenkins instance I have already built-in docker-compose, I could add it inside Openshift but this is out of the scope so be sure you have docker-compose installed and bring it up with below command:

After starting Jenkins you should be able to access with below Jenkins Job:

http://localhost:8080/job/microservice-deploy/

See the result

If everything is okay you should notice these stages running fine:

- git clone: Checkout the repository with the Java code

- maven build: Perform a `mvn package` to produce the .jar

- docker build and publish: Wrap the .jar file inside a Java based Docker image

- openshift deployment: Update the YAML definition of the Openshift Deployment Config. Basically, this will update the image tag used for the application

- openshift monitoring: This will check the status of the deployment until it gets the number of replicas expected to be in `ready` state

Pipeline code:

Test your Java microservice endpoint

Now your code should be running in this endpoint:

http://java-microservice.127.0.0.1.nip.io/actuator