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.
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