sonarqube migration kubernetes

Seamless SonarQube Migration on Kubernetes: A Step-by-Step Guide

Spread the love

Do you want to ensure you push quality code to production and have a Robust CI workflow that goes beyond a build operation? Then Sonarqube is a wonderful tool for you. You can read more about the meaning and features of Sonarqube and how to set it up here.

There are not many materials on migrating Sonarqube from an already running instance of the app to a fresh instance. It could be a migration from an on-premises environment to the cloud, or from one cloud provider to another cloud provider. Whatever the scenario is, the principles will remain the same.

But before we talk about the migration of Sonarqube, let us dive deeper than the 101 articles mentioned previously on the outer part, and the components of Sonarqube. Let us delve into the technologies that make up Sonarqube.

Technologies used by Sonarqube

First of all, Sonarqube is an open-source technology and a Silver Member of CNCF since 2023 (https://www.sonarsource.com/blog/cncf-silver-membership/). Sonarqube’s core is built with Java, requiring the Java Runtime Environment (JRE) to run. Javascript/Typescript was used for t For the database, it stores its data in a relational database. It usually works with PostgreSQL, MySQL, or Oracle database technologies to store most of its data. Lastly, it uses Elasticsearch as its search engine to improve the speed of searching for projects created in the app.

Now that we understand this, we can use this knowledge to ensure the migration works efficiently. For my use case, we shall be using the Postgres database. We shall assume that I have Sonarqube running in a Kubernetes Cluster A and we want to migrate it to a Kubernetes Cluster B. Here are the steps to set up and migrate.

Setting up Sonarqube in Kubernetes

To set up Sonarqube in Kubernetes is quite straightforward especially if you are using the Helm Chart for the configuration. These are the steps to deploy sonarqube on your Kubernetes cluster.

– Navigate to www.artifacthub.io

– Search for Sonarqbue in the address bar

– One the search results, click on the link that has the Sonarqube logo as shown in the following screenshot.

pPAAAAAElFTkSuQmCC

– Click the install button and follow the instructions to install Sonarqube

Take note that two major components will be deployed when the installation is complete

– A Statefulset for Sonarqube (which comes with a PVC)

– A Statefulset for Postgres (which comes with a PVC)

When you open your browser to the service you have configured you will have a fresh Sonarqube running. At this point, the data in Postgres has been hydrated to the internal Elasticsearch that is deployed with the Sonarqube stateful set.

Now that we have a fresh setup of Sonarqube running, how do we migrate an existing Sonarqube to this new Sonarqube?

Migrating from one SonarQube instance to a fresh Setup

The first thing to note is that Sonarqube uses Elasticsearch as its internal search engine and not Postgres. Sonarqube needs to run mostly in the Postgres instance, but some data needs to be migrated into Elasticsearch for the projects to be visible. With that understanding, these are the steps to migrate Sonarqube.

  1. Back up the existing Sonarqube database into a file. You can use the command line or any database ID,E too. My tool of preference for database management is DBeaver.
  2. Connect to the new instance database. When connected, you will find the default database that has been created.
  3. Create a new database with a new name, and restore the data that has been backed up from the previous Sonarqube. At this point, do not open Sonarqube, because the database name has changed.
  4. Update the database name in the configuration parameters, such as jdbcOverwrite.jdbcUrlpostgresql.postgresqlDatabase, with the new database name
  5. Re-deploy the Helm Chart of the new setup with the updated configuration.
  6. Finally, manually delete the pod for the Sonarqube Statefulset to clear the data in the Elasticsearch and allow Sonarqube to pull from the newly updated PostgreSQL.

With these steps, you can open Sonarqube again via the URL configured, and the projects will be visible.

Note

If the projects are not visible after the steps listed above, then it means Elasticsearch still has the data from the initial setup, which has no data in its database. So the goal is to ensure Elasticsearch pulls the relevant data from Sonarqube.

Conclusion

The goal of this article is to help anyone struggling with Sonarqube migration who might miss the Elasticsearch part of restarting the statefulset pod to allow Elasticsearch to rehydrate the data and make it visible on the Sonarqube dashboard.


Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
×