[英]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.default
或postgres-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.