Separate Service for same Statefulset replica pods

8/5/2020

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
cdk8s
kubernetes
microk8s

0 Answers