Home > Software engineering >  Cannot see PostgreSQL in Kubernetes Through a Browser
Cannot see PostgreSQL in Kubernetes Through a Browser

Time:07-28

I am testing a PostgreSQL configuration in kubernetes.

Windows 11 HyperV Minikube

Everything works (or seems to work) fine I can connect to the dabase via

kubectl exec -it pod/postgres-0 -- bash
bash-5.1$ psql --username=$POSTGRES_USER -W --host=localhost --port=5432 --dbname=pg_test
Password:
psql (13.6)
Type "help" for help.

pg_test=#

I cam also view the database through DBeaver.

But when I try to connect from any browser, localhost:5432 I get errors such as :

firefox canot connect,
ERR_CONNECTION_REFUSED

I have no proxy

when I try

kubectl port-forward service/postgres-service 5432:5432
Forwarding from 127.0.0.1:5432 -> 5432
Forwarding from [::1]:5432 -> 5432
Handling connection for 5432
Handling connection for 5432
... this line repeats indefinitely for connections attempt
Handling connection for 5432
Handling connection for 5432
...

Here is my YAML config file

...

apiVersion: v1
data:
  db: pg_test
  user: admin
kind: ConfigMap
metadata:
  name: postgres-config

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
spec:
  serviceName: "postgres"
  replicas: 2
  selector:
    matchLabels:
      env: prod
      domain: infrastructure
  template:
    metadata:
      labels:
        env: prod
        domain: infrastructure
    spec:
      terminationGracePeriodSeconds: 20
      securityContext:
        runAsUser: 70
        fsGroup: 70
      containers:
      - name: kubia-postgres
        image: postgres:13-alpine
        env:
        - name: POSTGRES_PASSWORD
          value: admin
          # valueFrom:
          #   secretKeyRef:
          #     name: postgres-secret
          #     key: password
        - name: POSTGRES_USER
          value: admin
          # valueFrom:
          #   configMapKeyRef:
          #     name: postgres-config
          #     key: user
        - name: POSTGRES_DB
          valueFrom:
            configMapKeyRef:
              name: postgres-config
              key: db
        ports:
        - containerPort: 5432
          protocol: TCP
        volumeMounts:
        - name: postgres-test-volume
          mountPath: /var/lib/postgresql
        resources:
          requests:
            memory: "64Mi"
            cpu: "250m"
          limits:
            memory: "128Mi"
            cpu: "500m"
      volumes:
      - name: postgres-test-volume
        emptyDir: {}

---

apiVersion: v1
kind: Service
metadata:
  name: postgres-service
  labels:
    env: prod
    domain: infrastructure
spec:
  ports:
  - port: 5432
    targetPort: 5432
    protocol: TCP
    name: pgsql
  clusterIP: None
  selector:
    env: prod
    domain: infrastructure

What am I doing wrong ?

CodePudding user response:

If you want to access your Postgres instance using a web browser, you need to deploy and configure something like pgAdmin.

CodePudding user response:

You haven't opened the service to the internet. You were only tunneling the port to you localhost. To do so you well need one of these Kubernetes services:

  • Port forwarding
  • Nodeport: Maps a port to your hosts port.
  • ClusterIP: It gives your service an internal Ip to be refered to in-cluster.
  • LoadBalancer: Assigns an Ip or a cloud providers load balancers to the service effectively making it available to external traffic.

Since you are using Minikube, you should try a LoadBalancer or a ClusterIP. By the way, you are creating a service without a type and you are not giving it an ip.

The important parts in a service for it to work on development are the selector labels, port and type.

Exposing an IP || Docs

  • Related