0

我有一个简单Statefulset的 3 个副本/豆荚。

在此处输入图像描述

在此处输入图像描述

pg-master-0是实际的主服务器,其余的(pg-master-1pg-master2)是备用服务器或从属服务器。

请忽略我很快就会处理的命名

所以我有 2 个服务

在此处输入图像描述

问题:在当前NodePort服务中,如果我转到或连接到IP:30006,我将连接到pg-master-0。有没有办法让我创建一个单独的服务,比如LoadBalancer来处理我对pg-master-1和pg- mastr -2实际上所有副本的请求,除了pg-master-0

我计划为他们创建单独的服务,但问题是他们都有相同的标签

在此处输入图像描述

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            
4

0 回答 0