Home > other >  Postgresql Kubernetes: The connection attempt failed. UnknownHostException: undefined
Postgresql Kubernetes: The connection attempt failed. UnknownHostException: undefined

Time:08-25

I'm trying to deploy an old app, and I've had some issues, first I've just updated an enum, then I tried to deploy, and after this, my app doesn't recognize my DB.

Note: This app connects to Postgres in another pod.

Here it's my properties:

logging.level.org.springframework=ERROR
server.port=8088
server.servlet.context-path=/portal
environment=local

# Elasticsearch
dev.elastic-search-url=localhost
dev.load-search-data=true
dev.load-search-data-on-init=false

spring.main.allow-bean-definition-overriding=true
spring.jpa.open-in-view=false
management.health.elasticsearch.enabled=false

spring.data.elasticsearch.repositories.enabled=true

spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
#spring.autoconfigure.exclude[1]=org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration

spring.datasource.platform=postgresql
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver


# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT 1


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.repositories.bootstrap-mode=lazy

spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create 
#spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.hibernate.naming.implicit-strategy = org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl
spring.jpa.properties.hibernate.format_sql=true



spring.datasource.hikari.maximum-pool-size=30
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.max-lifetime=2000000
spring.datasource.hikari.idle-timeout=10000
spring.datasource.hikari.leak-detection-threshold=15000
spring.datasource.hikari.initialization-fail-timeout=0

spring.flyway.enabled=false


# Hibernate ddl auto (create, create-drop, validate, update)


spring.jpa.properties.hibernate.cache.use_native_client=true
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastCacheRegionFactory

spring.jpa.properties.javax.persistence.sharedCache.mode=ALL

spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB


# Mail configuration
spring.mail.host=xxxxx
spring.mail.port=xxx
spring.mail.username=xxxxx
spring.mail.password=xxxx
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true


keycloak.enabled=false

My docker-compose.yaml:

version: "3.3"
services:
    web:
        build:
            context: ./
            target: app
        ports:
            - "8080:8080"
        environment:
            SPRING_DATASOURCE_URL: jdbc:postgresql://db/app
            SPRING_DATASOURCE_USERNAME: postgres
            SPRING_DATASOURCE_PASSWORD: postgres
            ELASTICSEARCH_CLUSTER_NAME: elastic
            ELASTICSEARCH_CLUSTER_NODES: elastic:9300
            ELASTICSEARCH_USER: elastic
            ELASTICSEARCH_PASSWORD: elastic
            ELASTICSEARCH_CLUSTER_URL: elastic
            SERVER_PORT: 8080
        depends_on:
            - db
            - elastic

    db:
        image: postgres:latest
        ports:
            - "5432:5432"
        environment:
            POSTGRES_DB: app
            POSTGRES_USER: postgres
            POSTGRES_PASSWORD: postgres
        volumes:
            - ./data:/var/lib/postgresql/data

    elastic:
        image: docker.elastic.co/elasticsearch/elasticsearch:6.8.6
        ports:
            - "9200:9200"
            - "9300:9300"
        environment:
            node.name: elastic-0
            cluster.name: elastic
            discovery.type: "single-node"
            ES_JAVA_OPTS: "-Xms1g -Xmx1g"
            bootstrap.memory_lock: "true"
            xpack.security.enabled: "false"
            xpack.monitoring.collection.enabled: "true"
            xpack.monitoring.elasticsearch.collection.enabled: "false"
            xpack.security.transport.ssl.enabled: "false"
        ulimits:
            nproc: 65536
            nofile:
                soft: 65536
                hard: 65536
            memlock:
                soft: -1
                hard: -1

My chart:

import { App } from "cdk8s";


const joinNonEmpty = (arr: Array<string | undefined>, char: string): string =>
  arr.filter((e) => e !== undefined).join(char);

const name = "portal-api";
const env: Environment = process.env.ENVIRONMENT === "production" ? "prod" : "staging";
const dns = process.env.DNS_ZONE;
const database = process.env.DATABASE;
const repository = process.env.DOCKER_REPOSITORY;
const version = process.env.VERSION;
const namespace = env === "prod" ? "production" : "staging"



// When available, add a prefix to the app name.
const prefixedName = joinNonEmpty([process.env.APP_PREFIX, name], "-");
const imageName = joinNonEmpty([repository, name], "/");
const domain = joinNonEmpty([prefixedName, env === "staging" ? "staging" : undefined, dns],".")

const environmentVariables = [  
 {
    name: "SERVER_PORT",
    value: "8080",
  },
  {
    name: "SPRING_DATASOURCE_URL",
    value: `jdbc:postgresql://${database}/app`,
  },  
  {
    name: "SPRING_DATASOURCE_USERNAME",
    value: "app",
  },
  {
    name: "SPRING_DATASOURCE_PASSWORD",
    valueFrom: {
      // Secret password for the "app" role.
      secretKeyRef: {
        key: "pass",
        name: "pg-app",
        optional: false,
      },
    },
  },
  {
    name: "ELASTICSEARCH_CLUSTER_URL",
    value: `elastic-${namespace}-es-transport.search.svc`,
  }, 
]

const app = new App()

new WebApp(app, prefixedName, env, {
  namespace,
  domain,
  subdomains: ["/"],
  replicas: 1,
  certificate: true,
  container: {
    name: "web",
    imageName,
    version,
    imagePullPolicy: "Always",
    portMapping: [
      {
        port: 8080,
        externalPort: 80,
      },
    ],
    env: environmentVariables,
  },
  // Needed for authorization.
  serviceAccountName: "postgres-client",
})

app.synth()

Here it's my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
    </parent>
    <groupId>br.com.dev</groupId>
    <artifactId>portal-api</artifactId>
    <version>1.0.2-SNAPSHOT</version>
    <name>portal-api</name>
    <description>Portal API project for dev</description>

    <properties>
        <java.version>14</java.version>
        <dev-commons.version>1.0.4-SNAPSHOT</dev-commons.version>
        <dev-searchlibs.version>1.0.8-SNAPSHOT</dev-searchlibs.version>
        <dev-corelibs.version>1.1.0-SNAPSHOT</dev-corelibs.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>br.com.dev</groupId>
            <artifactId>commons</artifactId>
            <version>${dev-commons.version}</version>
        </dependency>      
        <dependency>
            <groupId>br.com.dev</groupId>
            <artifactId>core-libs</artifactId>
            <version>${dev-corelibs.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.modelmapper</groupId>
            <artifactId>modelmapper</artifactId>
            <version>2.3.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>maven-group</id>
            <url>https://repo.dev.com.br/repository/maven-snapshots/</url>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
            </snapshots>
        </repository>
    </repositories>
    <distributionManagement>
        <snapshotRepository>
            <id>mit-nexus-snapshots</id>
            <url>https://repo.dev.com.br/repository/maven-snapshots/</url>
        </snapshotRepository>
        <repository>
            <id>mit-nexus-releases</id>
            <url>https://repo.dev.com.br/repository/maven-releases/</url>
        </repository>
    </distributionManagement>
</project>

Finally, my persistence Config, I'm getting an error when trying to create a new Hikari DataSource:

@Primary
    @Bean
    DataSource dataSource(Environment env) {
        HikariConfig dataSourceConfig = new HikariConfig();
        dataSourceConfig.setPoolName("pool-postgres-core");
        dataSourceConfig.setDriverClassName(env.getRequiredProperty(PROPERTY_NAME_DB_DRIVER_CLASS));
        dataSourceConfig.setJdbcUrl(env.getRequiredProperty(PROPERTY_NAME_DB_URL));
        dataSourceConfig.setUsername(env.getRequiredProperty(PROPERTY_NAME_DB_USER));
        dataSourceConfig.setPassword(env.getRequiredProperty(PROPERTY_NAME_DB_PASSWORD));

        String hikariMaximumPoolSize = env.getProperty(PROPERTY_HIKARI_MAXIMUM_POOL_SIZE);
        dataSourceConfig.setMaximumPoolSize(hikariMaximumPoolSize == null || hikariMaximumPoolSize.isEmpty() ? 50 : Integer.valueOf(hikariMaximumPoolSize));

        String hikariMinimumIdle = env.getProperty(PROPERTY_HIKARI_MINIMUM_IDLE);
        dataSourceConfig.setMinimumIdle(hikariMinimumIdle == null || hikariMinimumIdle.isEmpty() ? 20 : Integer.valueOf(hikariMinimumIdle));

        String hikariMaxLifetime = env.getProperty(PROPERTY_HIKARI_MAXLIFETIME);
        dataSourceConfig.setMaxLifetime(hikariMaxLifetime == null || hikariMaxLifetime.isEmpty() ? 300000 : Integer.valueOf(hikariMaxLifetime));

        String hikariIdleTimeout = env.getProperty(PROPERTY_HIKARI_IDLETIMEOUT);
        dataSourceConfig.setIdleTimeout(hikariIdleTimeout == null || hikariIdleTimeout.isEmpty() ? 240000 : Integer.valueOf(hikariIdleTimeout));

        String hikariLeakDetectionThreshold = env.getProperty(PROPERTY_HIKARI_LEAK_DETECTION_THRESHOLD);
        dataSourceConfig.setLeakDetectionThreshold(hikariLeakDetectionThreshold == null || hikariLeakDetectionThreshold.isEmpty() ? 60000 : Integer.valueOf(hikariLeakDetectionThreshold));

        return new HikariDataSource(dataSourceConfig);
    }

The stack trace:

{
  "textPayload": "org.postgresql.util.PSQLException: The connection attempt failed.
   org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292) ~[postgresql-42.2.8.jar!/:42.2.8]
   org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49) ~[postgresql-42.2.8.jar!/:42.2.8]
    org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:195) ~[postgresql-42.2.8.jar!/:42.2.8]
     org.postgresql.Driver.makeConnection(Driver.java:458) ~[postgresql-42.2.8.jar!/:42.2.8]
     org.postgresql.Driver.connect(Driver.java:260) ~[postgresql-42.2.8.jar!/:42.2.8]\
      com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.1.jar!/:na]
      com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:353) ~[HikariCP-3.4.1.jar!/:na]
      com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-3.4.1.jar!/:na]
       com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473) ~[HikariCP-3.4.1.jar!/:na]
        com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:562) ~[HikariCP-3.4.1.jar!/:na]
        com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) ~[HikariCP-3.4.1.jar!/:na]
         com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81) ~[HikariCP-3.4.1.jar!/:na]
         br.com.dev.portalapi.configuration.PersistenceContextConfig.dataSource(PersistenceContextConfig.java:102) ~[classes!/:1.0.1-SNAPSHOT]
         br.com.dev.portalapi.configuration.PersistenceContextConfig$$EnhancerBySpringCGLIB$$af4354f8.CGLIB$dataSource$0(<generated>) ~[classes!/:1.0.1-SNAPSHOT]
br.com.dev.portalapi.configuration.PersistenceContextConfig$$EnhancerBySpringCGLIB$$af4354f8$$FastClassBySpringCGLIB$$6689c394.invoke(<generated>) ~[classes!/:1.0.1-SNAPSHOT]
org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)

Caused by: java.net.UnknownHostException: undefined
java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567) ~[na:na]
 java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333) ~[na:na]
  java.base/java.net.Socket.connect(Socket.java:648) ~[na:na]
   org.postgresql.core.PGStream.<init>(PGStream.java:75) ~[postgresql-42.2.8.jar!/:42.2.8]
    org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:91) ~[postgresql-42.2.8.jar!/:42.2.8]
     org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:192) ~[postgresql-42.2.8.jar!/:42.2.8]
     . 164 common frames omitted",

I've searched for many forums and questions, but nothing helped me, so anything you guys bring to me will be light.

Thanks!

CodePudding user response:

I don't know exactly how it works, but now it's working...

What I did: remove from my main class the annotation @EnableConfigurationProperties, because it may cause conflicts when I'm trying to manually configure the DataSource.

I added to my properties this: spring.datasource.platform=postgresql

I didn't do any changes in PersistenceContextConfig (where I create the DataSource)

And in my pom.xml I accidentally deleted the relativePath tag when I updated the Spring boot version, so after fixing that, the code looked like this:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.1.RELEASE</version>
        <relativePath/> 
    </parent>

What helped me was the comment of @alltej, the logs bring the information that none of my env variables was being loaded, so after these changes, they started loading.

I hope this helps someone.

  • Related