-1

I am trying to setup SonarQube in a Kubernetes cluster. The cluster was made using KubeADM in the AWS cloud. I have the following files for the setup.

  1. Sonarqube App Deployment file
  2. Sonarqube App Secrets file
  3. Sonarqube-PostgreSQL Deployment file.
  4. Sonarqube-PostgreSQL Persistent Volume and Volume Claim file
  5. Sonarqube-PostgreSQL Secrets file.

Using these YAML files, I am able to bring the application up. I am using NodePort as the service type for the SonarQube App and it's working fine. Both the App and DB pods are running fine and I am even able to login to the SonarQube UI, configure my projects/data and do everything.

enter image description here

enter image description here

However, the problem is that, when I turn off the Servers and bring it back again after some time or delete the SonarQube App pod and when a new one spawns in it's place, the whole data is kind of lost.

I can also see that the PostgreSQL DB is ready to accept connections, however the App pod isnt supplying anything.

enter image description here

Seems that the data is being stored in the App pod itself and when something happens to it, the whole data is lost. So, when I delete the App pod and try to access the UI one more time, this is what I am getting. enter image description here

Literally all the data is flushed.

The following are the YAML files that I was using:

sonarqube-app.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sonarqube
  namespace: sonarqube
  labels:
    app: sonarqube
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sonarqube
  template:
    metadata:
      labels:
        app: sonarqube
    spec:
      containers:
      - name: sonarqube
        image: sonarqube:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 9000
        env:
          - name: POSTGRES_SERVICE_HOST
            value: "10.107.194.222" # update this to reflect your IP 
          - name: POSTGRES_USER
            valueFrom:
              secretKeyRef:
                name: postgres-db-credentials
                key: user
          - name: POSTGRES_PASSWORD
            valueFrom:
              secretKeyRef:
                name: postgres-db-credentials
                key: password
          - name: POSTGRES_NAME
            value: 'postgres'
          - name: DATABASE_URL
            value: jdbc:postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@postgresql:5432/$(POSTGRES_NAME)
          - name: SECRET_KEY
            valueFrom:
              secretKeyRef:
                name: sonarqube-secret-key
                key: secret_key    

        volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgresql-volume-mount
      volumes:
      - name: postgresql-volume-mount
        persistentVolumeClaim:
          claimName: postgres-pv-claim
---
apiVersion: v1
kind: Service
metadata:
  name: sonarqube-service
  namespace: sonarqube 
  labels:
    app: sonarqube
spec:
  ports:
  - port: 9000
    targetPort: 9000
    protocol: TCP
    nodePort: 31000
  type: NodePort
  selector:
    app: sonarqube

postgres-deployment.yaml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgresql
  namespace: sonarqube 
  labels:
    app: postgres-db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres-db
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: postgres-db
        tier: postgreSQL
    spec:
      containers:
        - name: postgresql
          image: postgres:10.3
          ports:
          - containerPort: 5432
          env:
          - name: POSTGRES_USER
            valueFrom:
              secretKeyRef:
                name: postgres-db-credentials
                key: user
          - name: POSTGRES_PASSWORD
            valueFrom:
              secretKeyRef:
                name: postgres-db-credentials
                key: password
          - name: POSTGRES_NAME
            value: 'postgres'
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgresql-volume-mount
          resources:
            requests:
              memory: "64Mi"
              cpu: "250m"
            limits:
              memory: "128Mi"
              cpu: "500m"
      volumes:
      - name: postgresql-volume-mount
        persistentVolumeClaim:
          claimName: postgres-pv-claim
      tolerations:
      - key: "blue"
        operator: "Equal"
        value: "true"
        effect: "NoSchedule"
---
apiVersion: v1
kind: Service
metadata:
  name: postgresql
  namespace: sonarqube
  labels:
    app: postgres-db
spec:
  clusterIP: 10.107.194.222
  type: ClusterIP
  ports:
  - port: 5432
    targetPort: 5432
    protocol: TCP
  selector:
    app: postgres-db
    tier: postgreSQL

pv-pvc.yaml

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgres-pv
  namespace: sonarqube 
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 200M
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata: 
  labels: 
    app: postgres-db
  name: postgres-pv-claim
  namespace: sonarqube
spec: 
  accessModes: 
    - ReadWriteOnce
  resources: 
    requests: 
      storage: 100M
  storageClassName: manual

So, how can I get the App Pod to connect to the DB pod and also use PVC and PV as a means of storage of data?

Does anyone know how to fix this?

arjunbnair
  • 143
  • 1
  • 2
  • 11

1 Answers1

1

You are using the container incorrectly.

The sonarqube and postgres deployment are both referencing the same PVC, which cannot work. Only postgres needs it, as the sonarqube container only comes with an embedded H2 DB and is supposed to use the postgres deployment.

Add a service to your postgres deployment and use the cluster DNS name in the JDBC URL. Here's the reference for postgres.

Adhere to the documentation about running SonarQube in a container.

The environment variables for the DB connection are supposed to be:

    -e SONAR_JDBC_URL=... \
    -e SONAR_JDBC_USERNAME=... \
    -e SONAR_JDBC_PASSWORD=... 

You need those variables only on the container connecting to postgres, i.e. the sonarqube container.

fuero
  • 9,591
  • 1
  • 35
  • 40