Sonarqube is not connecting to postgres in Kubernetes Cluster

Hello,

I am having Kubernetes cluster on Redhat 8 servers in my on-premise environment.
I am using Calico as CNI in my cluster.

I am trying to deploy sonarqube with postgresql as database in my cluster. My issue is that sonarqube pod is not able to connect to postgresql, I tried multiple approach using postgresql service, postgresql service endpoint IP, etc, but its not working and sonarqube pod is giving me below error in logs.

2022.05.06 11:35:45 INFO  web[][o.sonar.db.Database] Create JDBC data source for jdbc:postgresql://sonarqube:5432/sonar
2022.05.06 11:35:50 WARN  web[][o.s.c.a.AnnotationConfigApplicationContext] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdk.internal.loader.ClassLoaders$AppClassLoader@277050dc-org.sonar.db.DefaultDatabase': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Fail to connect to database
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdk.internal.loader.ClassLoaders$AppClassLoader@277050dc-org.sonar.db.DefaultDatabase': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Fail to connect to database
Caused by: java.lang.IllegalStateException: Fail to connect to database
        at org.sonar.db.DefaultDatabase.start(DefaultDatabase.java:86)
        at org.sonar.core.platform.StartableBeanPostProcessor.postProcessBeforeInitialization(StartableBeanPostProcessor.java:33)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:440)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620)
        ... 24 common frames omitted
Caused by: java.lang.IllegalStateException: Can not connect to database. Please check connectivity and settings (see the properties prefixed by 'sonar.jdbc.').
        at org.sonar.db.DefaultDatabase.checkConnection(DefaultDatabase.java:118)
        at org.sonar.db.DefaultDatabase.start(DefaultDatabase.java:83)
        ... 28 common frames omitted
Caused by: java.sql.SQLException: Cannot create PoolableConnectionFactory (The connection attempt failed.)
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:331)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
        at org.postgresql.Driver.makeConnection(Driver.java:400)
        at org.postgresql.Driver.connect(Driver.java:259)
        at org.apache.commons.dbcp2.DriverConnectionFactory.createConnection(DriverConnectionFactory.java:52)
        at org.apache.commons.dbcp2.PoolableConnectionFactory.makeObject(PoolableConnectionFactory.java:374)
        at org.apache.commons.dbcp2.BasicDataSource.validateConnectionFactory(BasicDataSource.java:106)
        at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:649)
        ... 36 common frames omitted
Caused by: java.net.UnknownHostException: sonarqube
        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:229)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)

My sonarqube and postgresql pods are running on the same node in the cluster.

I tried deploying other application like wordpress with mysql in my same cluster, there wordpress pod is able to connect to mysql and its working fine and accessible using NodePort service.

So I am not able to understand why Sonarqube is having issue and giving this error?

Please suggest.

Hi,

It looks like you should revisit your JDBC URL.

 
Ann

Hi,

Thanks for your reply.

My JDBC URL is -

jdbc:postgresql://sonarqube:5432/sonar