未处理的异常。Npgsql.NpgsqlException (0x80004005):无法连接到10.152.183.151:5432。

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

Unhandled exception. Npgsql.NpgsqlException (0x80004005): Failed to connect to 10.152.183.151:5432

问题

无法连接我的.NET 6应用程序(来自Docker镜像)到Kubernetes(MicroK8s)中的PostgreSQL数据库。

Unhandled exception. Npgsql.NpgsqlException (0x80004005): Failed to connect to 10.152.183.151:5432
 ---> System.Net.Sockets.SocketException (111): Connection refused
   at Npgsql.Internal.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   at Npgsql.Internal.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
   at Npgsql.Internal.NpgsqlConnector.RawOpen(SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
   at Npgsql.Internal.NpgsqlConnector.<Open>g__OpenCore|215_1(NpgsqlConnector conn, SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)

这是我的服务:

NAME          	TYPE    	CLUSTER-IP   	EXTERNAL-IP   PORT(S)    	AGE
web-app-service   NodePort	10.152.183.40	<none>    	80:31069/TCP   23m
pgsql-service 	ClusterIP   10.152.183.151   <none>    	5432/TCP   	23m

这是我的YAML文件(密钥文件中的值是正确的):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app-deployment
  namespace: exo-kubernetes
spec:
  replicas: 1
  selector:
	matchLabels:
  	app: web-app
  template:
	metadata:
  	labels:
    	app: web-app
	spec:
  	containers:
  	- image: my-image:latest
    	name: web-app
    	ports:
      	- containerPort: 80
    	resources:
      	requests:
        	memory: "64Mi"
        	cpu: "250m"
      	limits:
        	memory: "128Mi"
        	cpu: "500m"

---

apiVersion: v1
kind: Service
metadata:
  name: web-app-service
  namespace: exo-kubernetes
spec:
  selector:
	app: web-app-deployment
  type: NodePort
  ports:
  - port: 80
	targetPort: 80
	protocol: TCP

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgsql-deployment
  namespace: exo-kubernetes
spec:
  replicas: 1
  selector:
	matchLabels:
  	app: pgsql
  template:
	metadata:
  	labels:
    	app: pgsql
	spec:
  	containers:
  	- image: postgres:15.3
    	name: pgsql
    	env:
    	- name: POSTGRES_USER
      	valueFrom:
        	secretKeyRef:
          	name: db-access
          	key: USER
    	- name: POSTGRES_PASSWORD
      	valueFrom:
        	secretKeyRef:
          	name: db-access
          	key: PASSWORD
    	- name: POSTGRES_DB
      	valueFrom:
        	secretKeyRef:
          	name: db-access
          	key: DB_NAME
    	ports:
      	- containerPort: 5432
    	resources:
      	requests:
        	memory: "512Mi"
        	cpu: "600m"
      	limits:
        	memory: "2Gi"
        	cpu: "1200m"
    	volumeMounts:
      	- mountPath: /var/lib/postgresql/data
        	name: pgsql-vm
  	volumes:
    	- name: pgsql-vm

---

apiVersion: v1
kind: Service
metadata:
  name: pgsql-service
  namespace: exo-kubernetes
spec:
  selector:
	app: pgsql-deployment
  ports:
  - port: 5432
	targetPort: 5432

---

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web-app-ing
  namespace: exo-kubernetes
spec:
  rules:
  - http:
  	paths:
  	- path: /
    	pathType: Prefix
    	backend:
      	service:
        	name: web-app-service
        	port:
          	number: 80

还有我的连接字符串:

"DbConnection": "Host=pgsql-service;Port=5432;Database=Kubernetes;UserID=kubernetes;Password=kubernetes;"

如果有人有解决方案,我会非常高兴。

我尝试了几种解决方案,如修改端口或连接字符串,但都没有得出令人满意的结果。

英文:

I can't connect my .net 6 application (from docker image) to a postgresql database from kubernetes (microk8s).

Unhandled exception. Npgsql.NpgsqlException (0x80004005): Failed to connect to 10.152.183.151:5432
---> System.Net.Sockets.SocketException (111): Connection refused
at Npgsql.Internal.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
at Npgsql.Internal.NpgsqlConnector.Connect(NpgsqlTimeout timeout)
at Npgsql.Internal.NpgsqlConnector.RawOpen(SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)
at Npgsql.Internal.NpgsqlConnector.<Open>g__OpenCore|215_1(NpgsqlConnector conn, SslMode sslMode, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken, Boolean isFirstAttempt)

These are my services:

NAME          	TYPE    	CLUSTER-IP   	EXTERNAL-IP   PORT(S)    	AGE
web-app-service   NodePort	10.152.183.40	<none>    	80:31069/TCP   23m
pgsql-service 	ClusterIP   10.152.183.151   <none>    	5432/TCP   	23m

Here is my yml file (the values in the secret file are correct) :

apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app-deployment
namespace: exo-kubernetes
spec:
replicas: 1
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- image: my-image:latest
name: web-app
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
---
apiVersion: v1
kind: Service
metadata:
name: web-app-service
namespace: exo-kubernetes
spec:
selector:
app: web-app-deployment
type: NodePort
ports:
- port: 80
targetPort: 80
protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pgsql-deployment
namespace: exo-kubernetes
spec:
replicas: 1
selector:
matchLabels:
app: pgsql
template:
metadata:
labels:
app: pgsql
spec:
containers:
- image: postgres:15.3
name: pgsql
env:
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: db-access
key: USER
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: db-access
key: PASSWORD
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: db-access
key: DB_NAME
ports:
- containerPort: 5432
resources:
requests:
memory: "512Mi"
cpu: "600m"
limits:
memory: "2Gi"
cpu: "1200m"
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: pgsql-vm
volumes:
- name: pgsql-vm
---
apiVersion: v1
kind: Service
metadata:
name: pgsql-service
namespace: exo-kubernetes
spec:
selector:
app: pgsql-deployment
ports:
- port: 5432
targetPort: 5432
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-app-ing
namespace: exo-kubernetes
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web-app-service
port:
number: 80

and my ConnectionStrings :

"DbConnection": "Host=pgsql-service;Port=5432;Database=Kubernetes;UserID=kubernetes;Password=kubernetes;"

If anyone has a solution, I'd be delighted.

I've tried several solutions like port modification or connectionStrings but without any conclusive result.

答案1

得分: 0

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgsql-deployment
  namespace: exo-kubernetes
  labels: # 添加这个字段
    app: pgsql-deployment # 添加与服务中相同的标签
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pgsql
  template:
    metadata:
      labels:
        app: pgsql
    spec:
      containers:
      - image: postgres:15.3
        name: pgsql
        env:
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: db-access
              key: USER
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-access
              key: PASSWORD
        - name: POSTGRES_DB
          valueFrom:
            secretKeyRef:
              name: db-access
              key: DB_NAME
        ports:
        - containerPort: 5432
        resources:
          requests:
            memory: "512Mi"
            cpu: "600m"
          limits:
            memory: "2Gi"
            cpu: "1200m"
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
          name: pgsql-vm
      volumes:
      - name: pgsql-vm
英文:

The selector label that you're using in postgres service (app: pgsql-deployment) should be present in your postgre's Deployment manifest. In your case, it's missing. It should be like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: pgsql-deployment
  namespace: exo-kubernetes
  labels: # add this field
    app: pgsql-deployment # add the same label as the one present in service
spec:
  replicas: 1
  selector:
    matchLabels:
    app: pgsql
  template:
    metadata:
    labels:
      app: pgsql
    spec:
    containers:
    - image: postgres:15.3
        name: pgsql
        env:
        - name: POSTGRES_USER
        valueFrom:
            secretKeyRef:
            name: db-access
            key: USER
        - name: POSTGRES_PASSWORD
        valueFrom:
            secretKeyRef:
            name: db-access
            key: PASSWORD
        - name: POSTGRES_DB
        valueFrom:
            secretKeyRef:
            name: db-access
            key: DB_NAME
        ports:
        - containerPort: 5432
        resources:
        requests:
            memory: "512Mi"
            cpu: "600m"
        limits:
            memory: "2Gi"
            cpu: "1200m"
        volumeMounts:
        - mountPath: /var/lib/postgresql/data
            name: pgsql-vm
    volumes:
        - name: pgsql-vm

huangapple
  • 本文由 发表于 2023年5月28日 06:24:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349285.html
匿名

发表评论

匿名网友

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

确定