我有我自己的集群,它有一个控制平面和工人。我尝试使用psql -h <ingress-url> -p 5432 -U postgres -W
连接到集群中的PostgreSQL,但出现错误:
psql: error: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
但是curl <ingress-url>
的响应是这样的:
2022-05-27 04:00:50.406 UTC [208] LOG: invalid length of startup packet
这个响应意味着我的请求已经到达PostgreSQL服务器,但是为什么我不能连接到我的PostgreSQL服务器?
以下是我的资源:
入口:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: preflight
labels:
helm.sh/chart: "preflight"
helm.sh/version: "0.1.0"
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /
pathType: Exact
backend:
service:
name: preflight
port:
number: 5432
部署:apiVersion: apps/v1
kind: Deployment
metadata:
name: preflight
labels:
helm.sh/chart: "preflight"
helm.sh/version: "0.1.0"
spec:
replicas: 1
selector:
matchLabels:
helm.sh/chart: "preflight"
helm.sh/version: "0.1.0"
template:
metadata:
labels:
helm.sh/chart: "preflight"
helm.sh/version: "0.1.0"
spec:
containers:
- name: preflight
image: postgres:14
env:
- name: POSTGRES_PASSWORD
valueFrom:
configMapKeyRef:
name: preflight
key: postgresPassword
ports:
- containerPort: 5432
服务:
apiVersion: v1
kind: Service
metadata:
name: preflight
labels:
helm.sh/chart: "preflight"
helm.sh/version: "0.1.0"
spec:
type: ClusterIP
selector:
helm.sh/chart: "preflight"
helm.sh/version: "0.1.0"
ports:
- port: 5432
targetPort: 5432
ConfigMap值是一个POSTGRES_PASSWORD=postgres
。
Nginx入口控制器是一个HTTP代理。您正在尝试通过HTTP路由PGSQL流量,这根本无法工作。
你需要做的是通过nginx入口控制器暴露一个TCP服务。
简单地说,你需要创建一个这样的configmap:
apiVersion: v1
kind: ConfigMap
metadata:
name: tcp-services
namespace: <namespace where you deployed ingress controller>
data:
5432: "default/preflight:5432"
然后确保您的nginx入口控制器以--tcp-services-configmap=tcp-services
标志启动。
最后,确保nginx ingress controller Service (type ==LoadBalancer
)暴露端口5432:
apiVersion: v1
kind: Service
metadata:
name: ingress-nginx
...
spec:
type: LoadBalancer
ports:
...
- name: pgsql
port: 5432
targetPort: 5432
protocol: TCP
...
请注意,你的提供商的负载均衡器应该支持TCP后端(大多数应该这样,但值得注意)。