0

I have a simple Statefulset with 3 replicas/pods.

enter image description here

enter image description here

the pg-master-0 is the actual master and the rest (pg-master-1 and pg-master2) are standby servers or slaves.

Please ignore the naming i'll be working on that soon

So I have 2 Services

enter image description here

Question : In the current NodePort service if i goto or connect to IP:30006 I am connecting to pg-master-0. Is there a way for me to create a separate service like a LoadBalancer that handles my request for ONLY pg-master-1 and pg-mastr-2 actually all replicas except on the pg-master-0?

I'm planning to create separate service for them but the problem is they all have the same labels

enter image description here

apiVersion: v1
kind: Service
metadata:
  name: pg-master
  labels:
    app: pg-master
spec:
  type: NodePort
  ports:
  - port: 5432
    name: pg-port
  selector:
    app: pg-master
---
apiVersion: v1
kind: Service
metadata:
  name: pg-master-headless
  labels:
    app: pg-master-headless
spec:
  clusterIP: None
  ports:
  - port: 5431
    name: pg-port-headless
    targetPort: 5432
  selector:
    app: pg-master
---    
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: pg-master
spec:
  replicas: 1
  serviceName: pg-master-headless
  selector:
    matchLabels:
      app: pg-master
  template:
    metadata:
      labels:
        app: pg-master
    spec:
      containers:
      - name: pg-master
        image: mjayson/ms-rpi-pg  
        env:
        - name: POSTGRES_USER
          value: postgres
        - name: POSTGRES_PASSWORD
          value: postgres         
        ports:
        - containerPort: 5432
          name: http-port
        volumeMounts:
        - name: pv-data
          mountPath: /var/lib/postgresql/data
      volumes:
      - name: pv-data
        persistentVolumeClaim:
            claimName: master-pv-claim
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: pg-slave
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pg-slave
  template:
    metadata:
      labels:
        app: pg-slave
    spec:
      containers:
      - name: pg-slave
        image: mjayson/ms-rpi-pg
        env:
        - name: POSTGRES_USER
          value: postgres
        - name: POSTGRES_PASSWORD
          value: postgres     
        ports:
        - containerPort: 5432
          name: http-port
        volumeMounts:
        - name: pv-data
          mountPath: /var/lib/postgresql/data
      volumes:
      - name: pv-data
        persistentVolumeClaim:
            claimName: slave-pv-claim            
Jayson Gonzaga
  • 140
  • 1
  • 4
  • 11
  • I don't know if i got your point, but you can create another service poiting only to pg-master-1 and pg-master-2. You can use a different [label selector](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/) to create the service. Is it what you want? Note: LoadBalancer only supports HTTP/HTTPS request... there is a workaround using nginx-ingress, see [here](https://kubernetes.github.io/ingress-nginx/user-guide/exposing-tcp-udp-services/) – Mr.KoopaKiller Aug 06 '20 at 10:27
  • @KoopaKiller the both have the same – Jayson Gonzaga Aug 06 '20 at 18:59
  • @KoopaKiller as you can see in my screenshot, at the bottom part they have the same labels – Jayson Gonzaga Aug 06 '20 at 19:01
  • Did install the postgres using helm chart? Also, what's the purpose to separate? If is redirect only select request for other nodes, i think the best way is use a separate deployment for "slave" pods. – Mr.KoopaKiller Aug 06 '20 at 19:24
  • @KoopaKiller no they are not using helm chart. I found existing solution on this but none of they are compatible with arm devices. Yeah i'll separate it for slave. – Jayson Gonzaga Aug 07 '20 at 11:04
  • What is the solution you've found? Are you running on Raspberry Pi? I think the best way in this case is create separate deployments for master and slave, then you can use 2 services. Could you share the yamls files in the question? – Mr.KoopaKiller Aug 07 '20 at 12:33
  • @KoopaKiller - I mean i found bitnami but it's not compatible with arm devices. So I created my own docker image and take advantage of this headless-service for master slave part. However, I did an experiment.. i tried to reboot my rpis when it came back on it got an error which i posted here https://stackoverflow.com/questions/63300718/error-updating-endpoint-slices-for-service-node-not-found – Jayson Gonzaga Aug 07 '20 at 17:27
  • kind of simillar to that one.. – Jayson Gonzaga Aug 07 '20 at 18:16

0 Answers0