skip to Main Content

I have a web server in Golang that connects to Postgres as a StatefulSet. However, I am getting an error with the connection where there is no such host found. The web server was created using ClusterIP as a service for networking and Deployment for creating the pods. Postgres was created using a Headless Service and StatefulSet. The following are my k8s configuration files and also the error message:

# headless-service.yml
apiVersion: v1
kind: Service
metadata:
  name: authentication-headless-service
  labels:
    ims: authentication
spec:
  clusterIP: None # headless service
  selector:
    ims: authentication-postgres
  ports:
    - name: authentication-postgres-h
      port: 5432
      targetPort: 5432

# statefulset.yml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: authentication-postgres-statefulset
spec:
  serviceName: authentication-headless-service
  replicas: 1
  selector:
    matchLabels:
      ims: authentication-postgres
  template:
    metadata:
      labels:
        ims: authentication-postgres
    spec:
      containers:
        - name: postgres
          image: postgres:14.2
          env:
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: authentication-secret
                  key: POSTGRES_USER
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: authentication-secret
                  key: POSTGRES_PASSWORD
            - name: POSTGRES_HOST
              valueFrom:
                secretKeyRef:
                  name: authentication-secret
                  key: POSTGRES_HOST
          ports:
            - containerPort: 5432
              name: postgres
          volumeMounts:
            - name: postgres-authentication-data
              mountPath: /var/lib/postgresql/data
              subPath: postgres # specific to postgres
          livenessProbe:
            tcpSocket:
              port: 5432
            initialDelaySeconds: 60
            periodSeconds: 30
            failureThreshold: 3
          readinessProbe:
            tcpSocket:
              port: 5432
            initialDelaySeconds: 60
            periodSeconds: 30
            failureThreshold: 3
  volumeClaimTemplates:
    - metadata:
        name: postgres-authentication-data
      spec:
        accessModes:
          - "ReadWriteOnce"
        resources:
          requests:
            storage: 500Mi
        storageClassName: ims-storage-class
apiVersion: v1
kind: Secret
metadata:
  name: authentication-secret
type: Opaque
data:
  POSTGRES_USER: YXV0aGVudGljYXRpb24tcG9zdGdyZXM=
  POSTGRES_PASSWORD: cGFzc3dvcmQ=

  # base64 encoded 'authentication-postgres-0.authentication-headless-service.default.svc.cluster.local'
  POSTGRES_HOST: YXV0aGVudGljYXRpb24tcG9zdGdyZXMtMC5hdXRoZW50aWNhdGlvbi1oZWFkbGVzcy1zZXJ2aWNlLmRlZmF1bHQuc3ZjLmNsdXN0ZXIubG9jYWw=

---
apiVersion: v1
kind: Service
metadata:
  name: authentication-clusterip
spec:
  type: ClusterIP
  ports:
    - targetPort: 8001
      port: 8001
  selector:
    ims: authentication

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: authentication-deployment
  namespace: default
spec:
  replicas: 4
  selector:
    matchLabels:
      ims: authentication
  template:
    metadata:
      labels:
        ims: authentication
    spec:
      containers:
        - name: authentication
          image: localhost:5050/ims-authentication:latest
          ports:
            - containerPort: 8001
          env:
            - name: MODE
              value: docker
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: authentication-secret
                  key: POSTGRES_USER
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: authentication-secret
                  key: POSTGRES_PASSWORD
            - name: POSTGRES_HOST
              valueFrom:
                secretKeyRef:
                  name: authentication-secret
                  key: POSTGRES_HOST
            - name: POSTGRES_PORT
              valueFrom:
                configMapKeyRef:
                  name: authentication-configmap
                  key: POSTGRES_PORT
            - name: POSTGRES_DB
              valueFrom:
                configMapKeyRef:
                  name: authentication-configmap
                  key: POSTGRES_DB
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0

This is the error message:

kubectl logs authentication-deployment-99c845645-vr48p

2024/09/18 17:16:10 failed to connect to postgres db: failed to connect to `user=authentication-postgres database=imsdb`: hostname resolving error: lookup authentication-postgres-0.authentication-headless-service.default.svc.cluster.local on 10.96.0.10:53: no such host

2

Answers


  1. I noticed the $.metadata.namespace is only specified as default for the authentication-deployment Deployment, not the StatefulSet or Service(s). Please verify all resources were created in the same namespace, either by specifying directly in the YAML or via kubectl with -n <namespace>. Otherwise, the DNS records won’t resolve as expected, since they’re built like <name>.<namespace>.svc.cluster.local.

    Login or Signup to reply.
  2. It looks like there is a misconfiguration in your Go code. The port says "53" while you should be targeting "5432". Try changing that.

    Also, make sure you use the correct service domain name. Try kubectl port-forward -n <NAMESPACE> svc/authentication-headless-service 8080:5432 and see if it works.

    Also, make sure the Postgres instances are healthy.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search