Easy Deploy SonarQube on Kubernetes with YAML configuration

David Layardi
CodeX
Published in
4 min readJul 22, 2021

--

Created using Canva

Kubernetes has made easier our deployment and scaling of applications we create. Not only just it, but the tools we used to develop applications can be easier to maintain if they have been deployed on Kubernetes.

This time we will deploy SonarQube on Kubernetes, these tools mostly use to check our Code Quality score and Code Vulnerabilities. If you directly look at SonarQube's official documentation about the deployment method on Kubernetes, they will direct us to use Helm charts as the deployment method. But maybe this will be a consideration if you have never use the Helm charts before or there are some circumstances in your place that make you unable to use the Helm charts.

It’s okay because we have an alternative solution to deploy it with YAML configuration, there’s several YAML created in this process and I will explain further in the section below

Some requirement

The version of Kubernetes I’m using in this article is 1.18.17-gke.1901. If you’re using a version newer or older than this, just modify the configuration to match the version you are using now. I’ll make the YAML configuration as general as possible.

For computation resource requirements, we need at least as many resources as the system requirements written on the website. The SonarQube image that I will use is the image that comes from the official docker and the PostgreSQL image from Bitnami for the databases. Just adjust the image parameter and drive mounting location if you use another source of the image.

You can get all snippets in this repository, just check it out.

Deploying Databases (PostgreSQL)

Create configuration of Persistent Volume Claim and Deployment for PostgreSQL. You can skip this step if you’re using an external service like a fully managed database service, etc.

And this is my YAML configuration for PostgreSQL deployment, don’t forget to set up the PostgreSQL credential in the config map section, we’ll use it later on Sonar YAML.

Deploying Application

Let’s start with create a configuration of Persistent Volume Claim used by SonarQube

Now we’ll create a Deployment configuration for SonarQube. At this stage, we need to do a little trick in configuring SonarQube. One of the configurations needed to run SonarQube in a Linux environment is to increase the maximum amount of virtual memory by changing the value of max_map_countto 524288. If SonarQube is deployed on a VM, this might not be a big deal as we can change it permanently. But in Kubernetes, it takes a bit of an extra step so this configuration can continue to be applied when pods are created. We can do this by adding a step initContainer in the Deployment template. In this step, we’ll deploy busybox and run the max_map_countconfiguration. I use the value 262144as the minimum required virtual memory to running SonarQube.

The configuration that we need also to pay attention to is the database connection settings. Official SonarQube image parameter using JDBC URL format to set the database connection. You can see the example below

“jdbc:postgresql://postgres:5432/sonar_db”
  • postgresvalue is based on PostgreSQL container name in Kubernetes environment, we can access the container by using the container name. Change it to your defined container name.
  • sonar_db value is based on POSTGRESQL_DATABASEwhat we define on the Postgres config map

Deploying Ingress

After the deployment configuration is applied, create an ingress to expose the service. In this step, I will use ingress with the Nginx controller. Deploying an Nginx controller is not too difficult because you can follow the steps in their documentation. Let’s start by adding a secret for the SSL certificates, you’ll need the SSL key and the SLL certificate.

This step is not required if you want to expose the SonarQube in HTTP protocol. You can just remove the tlssection in the Ingress YAML and apply it.

kubectl create secret tls my-fancy-certs — key ssl.key — cert ssl.crt -n sonar

after the secret is created, configure ingress with the Nginx class

And, here we go! just do some quick setup and your sonar is ready to use.

Closing

by installing SonarQube in the Kubernetes environment, we can easily maintain the version we are using. Simply by changing the image tag on the Deployment specs, and voila*!

*Please make sure you read the Release Upgrade Notes to prevent something unwanted

Another thigs to notes is if you’re application compiled in Java 15 environment, make sure you using Jacoco at least version 0.8.7(If you using Jacoco for code coverage reporting), because the Jacoco with version below 0.8.7 doesn’t support Java 15 yet.

In Gradle, you can add the following line to define the Jacoco version :

jacoco {    toolVersion = “0.8.7”}

--

--

David Layardi
CodeX

Code Savvy | Pipeline & Automation enthusiast. Focus on learning and implementing DevOps culture | 🌐 https://layardi.com