连接Kubernetes中的PostgreSQL失败

huangapple go评论127阅读模式
英文:

Failed to connect PostgreSQL in Kubernetes

问题

我正在使用Kubernetes。我有两个Pod。

在一个Pod中,正在运行一个服务器。以下是尝试连接另一个Pod中的PostgreSQL服务的代码部分。

import (
	"context"
	"github.com/jackc/pgx/v4/pgxpool"
	"github.com/rs/zerolog/log"
)

databaseURL := "postgres://admin:passw0rd@my-database-service.hm:40072/my_db"
pg, err := pgxpool.Connect(context.Background(), databaseURL)
if err != nil {
	log.Error().Err(err).Msg("conn.Close")
	return nil
}

目前,上述代码报错:

{"level":"error","error":"failed to connect to host=my-database-service.hm user=admin database=my_db: dial error (dial tcp 10.43.140.140:40072: connect: connection refused)","time":1627849943,"message":"conn.Close"}

然而,当我通过SSH进入服务器Pod后,我可以成功通过psql连接到PostgreSQL Pod。

/usr/src/app # psql --host=my-database-service.hm --port=40072 --dbname=my_db --username=admin --password
Password: 
psql (13.3)
Type "help" for help.

my_db=#

我尝试重新启动我的服务器Pod,以确保它在PostgreSQL数据库准备好后运行,但仍然遇到相同的错误。

对于进一步调试的任何建议将非常有帮助。谢谢!


更新

实际上,我正在使用Linkerd。可能与以下链接相关:

https://linkerd.io/2.10/features/protocol-detection/

我尝试将PostgreSQL更改为默认端口5432,但仍然存在问题。如果找到解决方案,我会进行更新。

英文:

I am using Kubernetes. I have two pods.

In one pod, it is running a server. Here is the part of codes trying to connect PostgreSQL service in another pod.

import (
	"context"
	"github.com/jackc/pgx/v4/pgxpool"
	"github.com/rs/zerolog/log"
)

databaseURL := "postgres://admin:passw0rd@my-database-service.hm:40072/my_db"
pg, err := pgxpool.Connect(context.Background(), databaseURL)
if err != nil {
	log.Error().Err(err).Msg("conn.Close")
	return nil
}

Currently, above code gives the error

> {"level":"error","error":"failed to connect to host=my-database-service.hm user=admin database=my_db: dial error (dial tcp 10.43.140.140:40072: connect: connection refused)","time":1627849943,"message":"conn.Close"}

However, after I ssh into the server pod, I can successfully connect the PostgreSQL pod by psql.

/usr/src/app # psql --host=my-database-service.hm --port=40072 --dbname=my_db --username=admin --password
Password: 
psql (13.3)
Type "help" for help.

my_db=# 

I tried to restart my server pod to make sure it runs after PostgreSQL database is ready, and still got same error.

Any suggestion for further debugging would be helpful. Thanks!


UPDATE

I am actually using Linkerd. Might be related with

https://linkerd.io/2.10/features/protocol-detection/

I tried to change the PostgreSQL to default port 5432, but still have issue. Will update if find the solution.

答案1

得分: 3

经过一些实验,我可以确认我的问题与Linkerd无关。

我认为我的PostgreSQL容器端口/目标端口仍然使用默认的5432端口。它通过ClusterIP在网络内部以40072端口暴露,这就是为什么我不需要在Linkerd中将端口标记为不透明的原因。

事实证明,在添加了os.Exit(1)之后,

pg, err := pgxpool.Connect(context.Background(), databaseURL)
if err != nil {
    log.Error().Err(err).Msg("conn.Close")
    os.Exit(1) // <- 这个有帮助
}

当服务器pod在初始失败时,我的Kubernetes将帮助重新启动它,以等待PostgreSQL准备就绪。

经过几次重新启动,服务器pod现在可以成功连接到PostgreSQL。

我认为如果我提前手动重新启动多次,可能也会有所帮助。但肯定不像让Kubernetes帮助重新启动这种方式可靠。

英文:

After some experiments, I can confirm my issue is not related with Linkerd.

I think my PostgreSQL containerPort/targetPort is still using the default 5432. And it exposed by ClusterIP inside of the network by port 40072, which is why I don't need mark the port as opaque in Linkerd.

It turns out that after adding os.Exit(1),

pg, err := pgxpool.Connect(context.Background(), databaseURL)
if err != nil {
	log.Error().Err(err).Msg(&quot;conn.Close&quot;)
	os.Exit(1) // &lt;- this helps
}

my Kubernetes will help restart the server pod when it fails initially to wait the PostgreSQL is ready.

After a few times restarts, the server pod can connect PostgreSQL well now.

I think if I manually restarts more times early, it might help too. But definitely not reliable like this way letting Kubernetes to help restart.

huangapple
  • 本文由 发表于 2021年8月2日 05:28:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/68614476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定