Linux, DevOps and Cloud Expert at Linux Guru !

Image by sonarqube

Sonarqube deployment on Kubernetes GCP.

Sonarqube is an opensource software for code analysis and code inspection utility. You can detect bugs, security vunerabilities and code smells using sonarqube analysis. it's support 20+ programming languages. In this tutorial I will deploy sonarqube on Kubernetes. So let's start the deploying.

Prerequisites

Sonarqube will require database, I am using postgres, You can use other database also.
  • Bash/PowerShell terminal with kubectl installed
  • PostgreSQL database to store SonarQube’s data
  • Kubernetes cluster

1. Create Postgress DB GCP Cloud SQL.

Click here to create postgress db.

2. Generate base64 encoded password.

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 'yourpassword' | base64
Copy the password and put it in the secret file.

3. Applly secret file.

apiVersion: v1
kind: Secret
metadata:
  name: postgres
type: Opaque
data:
  password: MjM0dsdsdsJCNAITU=

4. Create PVC storage.

We need to create 2 PVCs since SonarQube uses two locations to store data /opt/sonarqube/data/ and /opt/sonarqube/extensions/.
  • PVC for Sonar’s data directory
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: sonar-data
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
  • PVC for Sonar’s extensions directory
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: sonar-extensions
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi

5. Apply deployment file.

After creating PVCs and Postgres secret we are ready to deploy using the following YAML file.
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: sonarqube
  name: sonarqube
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: sonarqube
    spec:
      containers:
        - name: sonarqube
          image: sonarqube:7.1
          volumeMounts:
          - mountPath: "/opt/sonarqube/data/"
            name: sonar-data
          - mountPath: "/opt/sonarqube/extensions/"
            name: sonar-extensions
          env:
          - name: "SONARQUBE_JDBC_USERNAME"
            value: "dbusername"
          - name: "SONARQUBE_JDBC_URL"
            value: "jdbc:postgresql://yourdatabaseip/sonar"
          - name: "SONARQUBE_JDBC_PASSWORD"
            valueFrom:
              secretKeyRef:
                name: postgres
                key: password
          ports:
          - containerPort: 9000
            protocol: TCP
      volumes:
      - name: sonar-data
        persistentVolumeClaim:
          claimName: sonar-data
      - name: sonar-extensions
        persistentVolumeClaim:
          claimName: sonar-extensions

6. Expose it service to the load balancer.

apiVersion: v1
kind: Service
metadata:
  name: sonarqube-service
spec:
  type: LoadBalancer
  selector:
    app: products
  ports:
  - protocol: TCP
    port: 9090
    targetPort: 9090

  • Check the endpoint
kubectl get svc
NAME                            TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
Sonarqube                      LoadBalancer   10.10.10.40      35.25.35.25   80:31546/TCP     10m

You can access it by URL. Default username and password will be admin/admin. Please change it after login.


Plugin Installation.

You have to go to the Administration > Marketplace and search the plugin name you want and install, Restart server after plugin installed.

Here we have successfully deploy sonarqube on Kubernetes engine. Here is part 2 for how you can add your code to sonarqube and analysis them. Click here to read the part 2

No comments:

Post a Comment