Blog Detail

Deploy SonarQube on Red Hat OpenShift

By:DALEEP SINGH

2020-Jun-25 08:06:58

My previous articles were my effort to deploy SonarQube on container and later use PostgreSQL as database with SonarQube deployment. The next step in this journey is to deploy SonarQube and PostgreSQL on Red Hat OpenShift Container Platform ( OCP ).

Red Hat OpenShift is an open source container application platform based on the Kubernetes and used widely for enterprise application development and deployment. Red Hat OpenShift ships with Red Hat Enterprise Linux CoreOS for the Kubernetes master, and supports Red Hat Enterprise Linux for worker nodes. Red Hat OpenShift supports standard Docker and other CRI-O compliant runtimes.

I have a small OpenShift cluster running which I am able to access from CLI. Awell defined and user friendly web console is also available. I connected to the cluster using the OpenShift CLI command.

#oc login -u username -p password https://master.console.example.com

On successful login, I create a new project to deploy my application pods. I have created a project named ‘sonar-ocp’ for this demonstration.

First step would be deploy PostgreSQL pod for data persistence. It would be a good practice to create a PVC and map it to a available PV which has network attached storage mapped. However, for this demonstration, I am going ahead with ephemeral storage.

#oc new-app --name sonardb --docker-image 

> registry.access.redhat.com/rhscl/postgresql-95-rhel7 -l app=db

You might get that pod deployment has failed and Status shows Error. If you check the pod logs, you will get to know that deployment was expecting some environment specific information to start, such as POSTGRESQL_USER, POSTGRESQL_PASSWORD & POSTGRESQL_DATABASE.

To pass information like password which are confidential in nature, I am creating a secret resource to store the Username, respective password and even the database to be created. I could have passed non-confidential informations as config map also.

#oc create secret generic sonardb-secret --from-literal=POSTGRESQL_USER=sonar 

> --from-literal=POSTGRESQL_PASSWORD=sonar –from-literal=POSTGRESQL_DATABASE=sonar

Once the secret resource has been created, we need to inject the environment variable in the deployment config so that required values are used to deploy a new pod.

#oc set env dc/sonardb --from secret/sonardb-secret

Now when you check the pod status, you will find that the pod is in ‘Running’ status.

Once postgres database is up and running, we can deploy SonarQube. I have modified SonarQube community image to run on OpenShift platform without any changes. It is available for download from my Quay repository at SonarQube-For-OCP and the source code used to create this image is available at my Github page ( https://github.com/dbais76/sonarqube-postgre ). I am using this image now to deploy SonarQube image and create a pod.

Before deploying it, please ensure that you have sufficient amount of compute resources available and if you have some limit set on your project by OCP cluster admins, it is enough for the pod to start and sustain. Else, you might find that pod restart count is increasing and checking the pod events, you will find error messages point to OOM Killed.

I have shown the limit set on my project as reference. However, actual usage might depend on the workload and number of simultaneous connections. In my case, it is a small test case we are running to check deployment on OpenShift.

Now I ready to deploy SonarQube and create a pod.

#oc new-app --name sonar-app --docker-image quay.io/dbais76/sonarqube:8.2-ocp 

> -e JDBC_USERNAME=sonar -e JDBC_PASSWORD=sonar 

> -e JDBC_URL=jdbc:postgresql://sonardb:5432/sonar -l app=sonarapp

After sometime, you will find that sonarapp pod is up and Status show ‘Running’.

Ensure that oc logs command for sonarapp pod shows ‘INFO app[][o.s.a.SchedulerImpl] SonarQube is up’. This is to confirm that SonarQube is UP and Running for us now and we can now connect and run our tests.

We need to expose the sonarapp service to create a route to access the SonarQube console.

Once you have the URL/route with you, you just open your favorite browser and access SonarQube console. Rest all remains same as we have seen in previous articles. You can also integrate now to a running instance of Jenkins and create a pipeline to automate the code testing process.

I hope this will definitely encourage you to try your hands on deploying SonarQube on OpenShift.

 

Happy Reading !!