DevOps tool-chain setup on Kubernetes cluster. Part - 2/3
Tags: kubernetes, devops
Published: 2018-06-27 05:57:20

---
title: DevOps tool-chain setup on Kubernetes cluster. Part - 2/3
published: true
description: 
tags: #kubernetes #devops
---
![alt text](https://thepracticaldev.s3.amazonaws.com/i/298l0v54pjqkldy2j34e.png "Kubernetes")
###Introduction
This is in continuation of my previous article - DevOps tool-chain setup on Kubernetes cluster. Part - 1/3. In this article, I have covered on how to setup the SonarQube and PostgreSQL on the Kubernetes cluster.

###SonarQube setup on Kubernetes cluster
SonarQube is a web-based open source platform used to measure and analyse the source code quality. Code quality analysis makes your code more reliable and readable. It can analyse and manage code of more than 20 programming languages.

SonarQube needs a database and it supports databases like MySQL, PostgreSQL, MSSQL, etc. In this article I have used PostgreSQL as the database for SonarQube. I have followed a sequence of creating  PostgreSQL DB kubernetes components followed by SonarQube Kubernetes components.

Let me configure PostgreSQL and SonarQube on the Kubernetes cluster by using the below scripts.

###PostgreSQL setup on Kubernetes cluster
In this section, I have setup PostSQL on Kubernetes cluster by using the below scripts.

####Storage class for PostgreSQL
I have created the Storage Class for PostgreSQL Database.

```yaml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: sonar-storage
  labels: 
    app: sonar-storage
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp2
  zone: eu-west-2a
allowVolumeExpansion: true
```

####Persistent Volume for PostgreSQL
I have created Persistent Volume and allocated 4GB as the storage space for PostgreSQL.

```yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: sonar-postgres-data
  labels: 
    app: Sonar-Postgres-Data
  annotations:
    volume.beta.kubernetes.io/storage-class: "sonar-storage"
spec:
  accessModes: 
    - ReadWriteOnce
  resources:
    requests: 
      storage: 4Gi
```

####Deployment for PostgreSQL
I have created a deployment script which pulls the PostgreSQL image, if the image is not persent in the Kubernetes cluster. I have configured PostgreSQL with the port 5432. Its data are persisted on the persistent volume which is created in the previous step. This deployment is created with single Replica. PostgreSQL Database credentials (user name & password) are mentioned in clear text. In production environment it is not advisable to have password in clear text.

```yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: postgresql-sonar
spec:
  replicas: 1
  template:
    metadata: 
      name: postgresql-sonar
      labels:
        app: postgresql-sonar
    spec:
      containers:
        - name: postgreqsl-sonar
          image: postgres:9.6
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 5432
              name: postgresql-port
          env:
            - name: POSTGRES_USER
              value: sonar
            - name: POSTGRES_PASSWORD
              value: password
            - name: PGDATA
              value: /var/lib/postgresql/data/pgdata
          volumeMounts:
            - name: db-data
              mountPath: /var/lib/postgresql/data
      volumes:
        - name: db-data
          persistentVolumeClaim:
            claimName: sonar-postgres-data

```

####Service for PostgreSQL
I have created service for PostgreSQL on port 5432.

```yaml
kind: Service
apiVersion: v1
metadata:
  name: postgresql-db
  labels:
    name: postgresql-db
spec:
  type: NodePort
  ports:
  - port: 5432
    targetPort: 5432
    protocol: TCP
    name: postgresql-db-port
  selector:
    app: postgresql-sonar
```
####SonarQube Configuration
I have created all the required Kubernetes components for the SonarQube Database in the previous sections. Now, I am going to create Kubernetes components for SonarQube which will use all the Database components. 

####Deployment for SonarQube
I have created a deployment script which pulls the SonarQube image, if not persent in the Kubernetes cluster and configured on port 9000. I have configured SonarQube to connect with the PostgreSQL DB which is exposed on port 5432 by providing the user credentials. 


```yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: sonar
spec:
  replicas: 1
  template:
    metadata:
      name: sonar
      labels: 
        app: sonar
    spec: 
      containers:
      - name: sonar
        image: sonarqube:6.7
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 9000
          name: sonarport
        env: 
          - name: SONARQUBE_JDBC_USERNAME
            value: sonar
          - name: SONARQUBE_JDBC_PASSWORD
            value: password
          - name: SONARQUBE_JDBC_URL
            value: jdbc:postgresql://postgresql-db:5432/sonar
```
####Service for SonarQube
I have created a service for SoanrQube on its default port 9000.

```yaml
kind: Service
apiVersion: v1
metadata:
  name: sonar
  labels:
    app: sonar
spec:
  type: NodePort
  ports:
    - port: 9200
      targetPort: 9000
      name: sonarport
  selector:
    app: sonar
  type: LoadBalancer
```

By now I have created storage class, persistent volume, deployment, and service for SonarQube and PostgreSQL DB and it is up & running. 

```shell
$kubectl get deployment
$kubectl get pod
$kubectl get svc
```
###Conclusion
By now I have covered how to setup SonarQube and PostgreSQL with single Replicaset on a Kubernetes cluster. I will discuss the Nexus setup in the final article.

Note:
yaml files are space sensitive and the scripts that are available in this article might have tab spaces instead of whitespace. These scripts will fail, if you copy and paste without changing the tab to whitespace.