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:
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:
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:
openshift/deployment-java-app/jenkins/jcasc.yaml: You need to assign your dockerhub login to the Credential Id:
openshift/deployment-java-app/jenkins/Jenkinsfile: In the
environmentsection you need to update it with your DockerHub account name, so i.e DOCKER_ORG intead If your account is ‘bob’ then you will to update it with ‘bob`
openshift/deployment-java-app/kubernetes/microservices.yaml: You will need to update
image: 'danielahcardona/microservice_java_prometheus:latest'with your Dockerhub account name, so i.e If your account is ‘bob’ then you will need to update it with ‘bob/microservice_java_prometheus:latest’`
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:
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
Test your Java microservice endpoint
Now your code should be running in this endpoint: