简体   繁体   English

Spring 引导无法连接到部署在另一个 kubernetes pod 上的 postgre 数据库

[英]Spring boot cannot connect to postgre database deployed on another kubernetes pod

I deployed my microservice hello-k8s in a pod and another pod as postgre-server, I have successfully connected to localhost using port-forward and try to create a table in the deployed database.我将我的微服务 hello-k8s 部署在一个 pod 和另一个 pod 作为 postgre-server,我已经使用端口转发成功连接到 localhost 并尝试在部署的数据库中创建一个表。 But when I deploy hello-k8s it return error due to database connection attempt failed.但是当我部署 hello-k8s 时,由于数据库连接尝试失败而返回错误。

Disclaimer: kube-dns is working fine, I already try nslookup to the postgre-svc and it works well.免责声明:kube-dns 工作正常,我已经尝试 nslookup 到 postgre-svc 并且效果很好。

Here is the error这是错误

Caused by: java.net.UnknownHostException: postgre-svc引起:java.net.UnknownHostException:postgre-svc
at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220) ~[na:na] at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[na:na] at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220) ~[na:na] at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]在 java.base/java.net.Socket.connect(Socket.java:609) ~[na:na]
at org.postgresql.core.PGStream.(PGStream.java:81) ~[postgresql-42.2.12.jar:42.2.12]在 org.postgresql.core.PGStream.(PGStream.java:81) ~[postgresql-42.2.12.Z68995FCBF432492D15484D04A9D21]ACBF432492D15484D04A9D21]40Z.
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:93) ~[postgresql-42.2.12.jar:42.2.12]在 org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:93)~[postgresql-42.2.12.Z68995FCBF4242492D15484D02]42A.922]。
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197) ~[postgresql-42.2.12.jar:42.2.12]在 org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:197) ~[postgresql-42.2.12.Z68995FC4BF4BF442492DAC40Z8:197)

Here is the application.yml file这是 application.yml 文件

spring:
  application:
    name: hello-k8s
  datasource:
    jdbc-url: jdbc:postgresql://${PG_SERVER}/first-db
    username: postgres
    password: password
    hikari:
      maximum-pool-size: 2
      connection-test-query: SELECT 1
    driver-class-name: org.postgresql.Driver
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

Here is the configuration code to connect to database这是连接数据库的配置代码

@Configuration
@ConfigurationProperties(prefix = "spring.datasource")
@EnableJpaRepositories(entityManagerFactoryRef = "localEntityManagerFactory",
    transactionManagerRef = "localTransactionManager",
    basePackages = "id.hellok8s.repository")
public class JpaConfig extends HikariConfig{

    @Value("${spring.jpa.show-sql}")
    private boolean showSql;

    public JpaConfig(){}

    @Bean(name = "localDataSource")
    @Primary
    public DataSource dataSource(){
        return new HikariDataSource(this);
    }

    @Bean(name = "localEntityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean locaEntityManagerFactory(final EntityManagerFactoryBuilder builder, 
        @Qualifier("localDataSource") final DataSource dataSource){
            final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
            entityManagerFactoryBean.setJpaVendorAdapter(this.vendorAdapter());
            entityManagerFactoryBean.setDataSource(dataSource);
            entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
            entityManagerFactoryBean.setPackagesToScan("id.hellok8s.model");
            entityManagerFactoryBean.afterPropertiesSet();
            return entityManagerFactoryBean;
    }

    @Bean(name = "localTransactionManager")
    @Primary
    public PlatformTransactionManager localTransactionManager(
            @Qualifier("localEntityManagerFactory") final EntityManagerFactory emf){
        return new JpaTransactionManager(emf);
    }

    private HibernateJpaVendorAdapter vendorAdapter(){
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setShowSql(showSql);
        return vendorAdapter;
    }
}

Here is the postgre service这是postgre服务

apiVersion: v1
kind: Service
metadata:
  name: postgre-svc
spec:
  selector:
    app: postgre
  ports:
    - port: 5432
      targetPort: 5432
  type: ClusterIP

Here is my hello-k8s.yml deployment这是我的 hello-k8s.yml 部署

apiVersion: "apps/v1"
kind: "Deployment"
metadata:
  name: "hello-k8s"
  namespace: "hello-k8s"
  labels:
    app: "hello-k8s"
spec:
  replicas: 1
  selector:
    matchLabels:
      app: "hello-k8s"
  template:
    metadata:
      labels:
        app: "hello-k8s"
    spec:
      containers:
      - name: hello-k8s
        image: [my-docker-container-registry]/hello-k8s:1.0.0-SNAPSHOT
        imagePullPolicy: Always
        env:
          - name: PG_SERVER
            value: "postgre-svc"

I have feeling it is due to HikariConfig, but I haven't found any reference yet for that.我觉得这是由于 HikariConfig,但我还没有找到任何参考。 anyway, anyone in the world has face issue like this?无论如何,世界上有人遇到过这样的问题吗? Feel blessed if anyone can help or have advice:)如果有人可以提供帮助或提供建议,请感到幸运:)

Your client Deployment is explicitly labeled to deploy into a different namespace, but the database Service isn't.您的客户端部署被明确标记为部署到不同的命名空间,但数据库服务不是。 By default this will cause the database Service to get deployed into the default namespace;默认情况下,这将导致数据库服务部署到default命名空间; the two objects being in different namespaces will cause the DNS issue you're getting.位于不同命名空间中的两个对象将导致您遇到的 DNS 问题。

I typically don't include an explicit namespace: in my Kubernetes YAML files.我通常不包含显式namespace:在我的 Kubernetes YAML 文件中。 Instead, I use the kubectl --namespace option if I want to install things in a specific namespace.相反,如果我想在特定的命名空间中安装东西,我会使用kubectl --namespace选项。 This also makes it a little easier to reuse a set of YAML files in a slightly different context.这也使得在稍微不同的上下文中重用一组 YAML 文件变得更容易一些。

You should also be able to make this work by pointing at the Service in the default namespace;您还应该能够通过指向default命名空间中的服务来完成这项工作; set PG_SERVER to postgre-svc.default or postgres-svc.default.svc.cluster-local , including the other namespace name in the DNS name.PG_SERVER设置为postgre-svc.defaultpostgres-svc.default.svc.cluster-local ,包括 DNS 名称中的其他命名空间名称。 (The database's StatefulSet object also needs to be in the same namespace as its Service, so double-check that they're correctly deployed together.) (数据库的 StatefulSet object 也需要与其服务位于同一命名空间中,因此请仔细检查它们是否正确部署在一起。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将Dockerized Spring Boot应用程序连接到Dockerized Postgre SQL - Cannot connect dockerized spring boot app to dockerized postgre sql 无法在 spring 引导中连接非主数据库 - Cannot connect non primary database in spring boot 使用docker和kubernetes部署的Spring-boot微服务应用程序:服务无法通信 - Spring-boot microservice application deployed with docker and kubernetes: Services not communicating spring部署在Kube.netes中的启动微服务的日志集中处理的正确方法是什么? - What are the proper ways to centralize the logs of spring boot microservices deployed in Kubernetes? spring boot 无法连接rabbitmq - spring boot cannot connect to rabbitmq Spring Boot Microservices cannot run on Kube.netes (java.net.SocketTimeoutException: connect timed out) - Spring Boot Microservices cannot run on Kubernetes (java.net.SocketTimeoutException: connect timed out) Java Spring Kube.netes后端无法连接到Mariadb - Java Spring Backend cannot connect to Mariadb in Kubernetes 无法从 kubernetes pod 内部连接到外部数据库 - Cannot connecto to external database from inside kubernetes pod 控制通过Kubernetes部署的POD的主机名 - Control the hostname of the POD deployed via Kubernetes Spring项目无法连接到数据库 - Spring project cannot connect to the database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM