1

I setup 1 master node and 2 worker nodes on bare matel server. I deploy my postgressSQL with 3 replica sets. This is my deployment file.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
   matchLabels:
    app: postgres
  replicas: 3
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:latest
          imagePullPolicy: "IfNotPresent"
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim
---
    kind: PersistentVolume
    apiVersion: v1
    metadata:
      name: postgres-pv-volume
      labels:
        type: local
        app: postgres
    spec:
      storageClassName: standard
      capacity:
        storage: 15Gi
      accessModes:
        - ReadWriteMany
      hostPath:
        path: "/mnt/data"
---
    kind: PersistentVolumeClaim
    apiVersion: v1
    metadata:
      name: postgres-pv-claim
      labels:
        app: postgres
    spec:
      storageClassName: standard
      accessModes:
        - ReadWriteMany
      resources:
        requests:
          storage: 15Gi
---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: postgres-config
      labels:
        app: postgres
    data:
      POSTGRES_DB: postgresdb
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: root
---

I also follow the MetalLB https://metallb.universe.tf/installation/ and set up layer2 load balancer. which is running fine and I can even expose nginx pod with this service.

As you can see here.

NAME         TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1       <none>         443/TCP        4h28m
nginx        LoadBalancer   10.107.29.158   153.10.19.35   80:30703/TCP   162m

These are my running pods

NAME                        READY   STATUS    RESTARTS   AGE
nginx-76d6c9b8c-lrljz       1/1     Running   0          3h29m
postgres-7dff8d6d74-8mlnt   1/1     Running   0          136m
postgres-7dff8d6d74-9zxsk   1/1     Running   0          136m
postgres-7dff8d6d74-xzkkx   1/1     Running   0          136m

What Issue Do I face?

When I try to expose the postgresSQL pods with load balancer I am not able to connect. Server is not reachable. I try to expose as follow

kubectl expose deploy postgres --port 30432 --type LoadBalancer

I also try to create a yaml file for this service and still not successful.

kind: Service
apiVersion: v1
metadata:
  name: postgres-svc
  labels:
    app: postgres
spec:
  type: LoadBalancer
  ports:
    - port: 5432
      targetPort: 30432
  type: LoadBalancer
  selector:
    metallb-service: postgres

What Do I expect? I want to expose my Pods to external network with this load balancer service so all the new data should be updated in all 3 replica set. Can you please help me to fix my service.yaml file? I will be very thanks full

Jonas
  • 121,568
  • 97
  • 310
  • 388

1 Answers1

0

You don't specify a port in your postres container.

With kubectl expose you should specify a targetPort:

kubectl expose deploy postgres --port 30432 --target-port 5432 --type LoadBalancer

In your YAML you have to switch ports:

kind: Service
apiVersion: v1
metadata:
  name: postgres-svc
  labels:
    app: postgres
spec:
  type: LoadBalancer
  ports:
    - port: 30432
      targetPort: 5432
  type: LoadBalancer
  selector:
    app: postgres

Here also the selector was wrong. It has to match labels on the pod.

user2311578
  • 798
  • 3
  • 7