k8s部署单节点postgres

给postgres服务器节点打标签

kubectl get node --show-labels=true
kubectl label nodes k8s-02  postgres-server=master

postgres服务器节点创建目录

mkdir -p /home/postgres/data/data

编辑部署文件postgres.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: k8s-postgres

---

apiVersion: v1
kind: Service
metadata:
  labels:
    app: postgres-master-out
  name: postgres-master-out
  namespace: k8s-postgres
spec:
  type: NodePort
  ports:
    - port: 5432
      targetPort: 5432
      protocol: TCP
      nodePort: 30032
  selector:
    app: postgres-master

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app: postgres
    app: postgres-master
  name: postgres-master
  namespace: k8s-postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres-master
  template:
    metadata:
      labels:
        app: postgres-master
    spec:
      hostNetwork: true
      hostAliases:
      - ip: "172.21.16.232"
        hostnames:
        - "postgres-master"
      containers:
        - name: postgres-master
          image: postgres:10.20-alpine
          imagePullPolicy: IfNotPresent
          env:
            - name: POSTGRES_PASSWORD
              value: "********"
            - name: ALLOW_IP_RANGE
              value: "0.0.0.0/0"
            - name: TZ
              value: "Asia/Shanghai"
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-lib
              mountPath: /var/lib/postgresql/
            - name: postgres-data
              mountPath: /var/lib/postgresql/data/
          resources:
            limits:
              cpu: 3
              memory: 30Gi
            requests:
              cpu: 100m
              memory: 2Gi
      volumes:
        - name: postgres-lib
          hostPath:
            path: /home/postgres/data/
        - name: postgres-data
          hostPath:
            path: /home/postgres/data/data/
      nodeSelector:
        postgres-server: master

执行部署

kubectl apply -f postgres.yaml

查看部署情况

kubectl get pod -n k8s-postgres -o wide