How to Deploy SonarQube on Azure Kubernetes service

FoxuTech
3 min readApr 30, 2022

SonarQube is a web-based open source platform used to measure and analyze the source code quality. Code quality analysis makes your code more reliable and more readable.

SonarQube is written in Java but it can analyze and manage code of more than 20 programming languages, including c/c++, PL/SQL, Cobol etc through plugins. Plugins extend the functionality of SonarQube. More than 50 plugins are available.

SonarQube is maintained by Sonar Source.

Find about more benefits on https://foxutech.com/benefits-of-sonarqube/

In this post we are going to see how to deploy the SonarQube on Azure Kubernetes Service (AKS), as we have seen in last post How to setup AKS via terraform, I may recommend to use that and setup your environment in Azure.

Prerequisites

  • Azure Kubernetes Service up and running
  • Kubectl installed in the VM or machine you are going to manage the AKS.
  • Postgresql server available, if not please find the optional step below. If you are creating new server, please make sure you have terraform installed in the VM.

Provision Azure PostgreSQL Server (Optional)

You can check our repository https://github.com/foxutech/kubernetes.git.

# git clone https://github.com/foxutech/kubernetes.git 
# cd kubernetes/sonarqube/database

Change the required values in the variables.tf or main.tf file. Once all done, please run following terraform commands,

# terraform init 
# terraform plan
# terraform apply

This should help you to create the azure PostgreSQL server, please use the details in the deployment manifest.

Create PersistentVolumeClaims to store SonarQube data

AKS comes with built-in Storage Classes for HDD (default) and SSD (premium) storage: Please refer more details on azure portal and check the storage classes using

# kubectl get sc

As we need to create 2 PVCs as SonarQube uses two locations to store data /opt/sonarqube/data/ and /opt/sonarqube/extensions/. Once you check out the directory, you need to change the directory to kubernetes/sonarqube/application and run the commands or copy the file below and create a files.

PVC for Sonar’s data directory

# cat sonar-data.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sonar-data
spec:
accessModes:
- ReadWriteOnce
storageClassName: default
resources:
requests:
storage: 30Gi

PVC for Sonar’s extensions directory

# cat sonar-extensions.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sonar-extensions
spec:
accessModes:
- ReadWriteOnce
storageClassName: default
resources:
requests:
storage: 30Gi

Create the PVC now using following commands,

# kubectl apply -f sonar-data.yaml
# kubectl apply -f sonar-extensions.yaml

Create a Secret to store PostgreSQL password

Kubernetes has a built-in capability to store secrets. To create a secret, you need to base64 encode a secret value

# echo -n 'myson@rTempP@$$' | base64
bXlzb25AclRlbXBQQCQk

and create a k8s secret using YAML file as below,

# cat sonar-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: postgres
type: Opaque
data:
#my password is myson@rTempP@$$, change it before using
password: bXlzb25AclRlbXBQQCQk

Create a deployment

After creating PVCs and Postgres secret we are ready to deploy using the following YAML file

Please note, here what we did,

  • We have set container limits so that it cannot put cluster node into Out-of-memory state and also used init-containers to set the permissions and elasticsearch vm.max_map_count. (This is mandatory, otherwise you may end with elasticsearch error)
  • We use environment variables to pass database connection info to container

Continue reading this full article on https://foxutech.com/how-to-deploy-sonarqube-on-azure-kubernetes-service/

--

--

FoxuTech

Discuss about #Linux, #DevOps, #Docker, #kubernetes, #HowTo’s, #cloud & IT technologies like #argocd #crossplane #azure https://foxutech.com/