SonarQube + Postgresql Connection refused error in Kubernetes Cluster

4/7/2020

sonar-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sonarqube
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sonarqube
  template:
    metadata:
      labels:
        app: sonarqube
    spec:
      containers:
        - image: 10.150.0.131/devops/sonarqube:1.0
          args:
            - -Dsonar.web.context=/sonar
          name: sonarqube
          env:
            - name: SONARQUBE_JDBC_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-pwd
                  key: password
            - name: SONARQUBE_JDBC_URL
              value: jdbc:postgresql://sonar-postgres:5432/sonar
          ports:
            - containerPort: 9000
              name: sonarqube

sonar-service.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    name: sonarqube
  name: sonarqube
spec:
  type: NodePort
  ports:
    - port: 80
      targetPort: 9000
      name: sonarport
  selector:
    name: sonarqube

sonar-postgres-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sonar-postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sonar-postgres
  template:
    metadata:
      labels:
        app: sonar-postgres
    spec:
      containers:
        - image: 10.150.0.131/devops/postgres:12.1
          name: sonar-postgres
          env:
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres-pwd
                  key: password
            - name: POSTGRES_USER
              value: sonar
          ports:
            - containerPort: 5432
              name: postgresport
          volumeMounts:
            # This name must match the volumes.name below.
            - name: data-disk
              mountPath: /var/lib/postgresql/data
      volumes:
        - name: data-disk
          persistentVolumeClaim:
            claimName: claim-postgres

sonar-postgresql-service.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    name: sonar-postgres
  name: sonar-postgres
spec:
  ports:
    - port: 5432
  selector:
    name: sonar-postgres

Kubernetes Version:1.18.0 Docker Version : 19.03

**I am having a connection problem between the Sonarqube pod and the Postgresql pod. I use the flannel network plug. Can you help with the error?

Postgresql pod log value does not come.

**

ERROR

enter image description here

-- Levent
docker
kubernetes
postgresql
sonarqube

1 Answer

4/7/2020

Try with:

apiVersion: v1
kind: Service
metadata:
  labels:
    name: sonar-postgres
  name: sonar-postgres
spec:
  ports:
    - port: 5432
  selector:
    app: sonar-postgres

because it looks like your selector is wrong. The same issue with sonar-service.yaml, change name to app and it should work.

-- FL3SH
Source: StackOverflow