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 along with username and password.
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.
* Put your postgress db password here for encode.
echo -n 'yourpassword' | base64
It will give you encode password , Copy the password and put it in the secret file.
3. Apply 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" #Put your db username
- name: "SONARQUBE_JDBC_URL"
value: "jdbc:postgresql://yourdatabaseip/sonar" #DB URL
- name: "SONARQUBE_JDBC_PASSWORD"
valueFrom:
secretKeyRef:
name: postgres #don't change it
key: password # don't change it
ports:
- containerPort: 9000
protocol: TCP
volumes:
- name: sonar-data
persistentVolumeClaim:
claimName: sonar-data
- name: sonar-extensions
persistentVolumeClaim:
claimName: sonar-extensions
* Update database username and DB URL in deployment file. don’t change password field, it will fetch password from the secret file which we have applied.
6. Expose it service to the load balancer.
apiVersion: v1
kind: Service
metadata:
name: sonarqube-service
spec:
type: LoadBalancer
selector:
app: sonarqube
ports:
- protocol: TCP
port: 9000
targetPort: 9000
- 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
Linuxguru
It's working fine. Great tutorial.