PostgreSQL 学习笔记(2):Kubernetes 单节点部署

一、创建 PersistentVolume

apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgresql-pv
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/tmp/pgdb"

注:这里用的是 hostPath ,按我的理解应该用可以支持分布式的存储方式。

二、创建 PersistentVolumeClaim

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgresql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

三、创建 Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgresql-deployment
spec:
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: postgresql
  template:
    metadata:
      labels:
        app: postgresql
    spec:
      containers:
        - image: postgres:12.4
          name: postgresql
          env:
            - name: POSTGRES_PASSWORD
              value: 123456
          ports:
            - containerPort: 5432
              name: postgresql
          volumeMounts:
            - name: postgresql-persistent-storage
              mountPath: /var/lib/postgresql/data
      volumes:
        - name: postgresql-persistent-storage
          persistentVolumeClaim:
            claimName: postgresql-pv-claim

四、创建 Service

apiVersion: v1
kind: Service
metadata:
  name: postgresql-client-service
  labels:
    app: postgresql
spec:
  type: NodePort
  ports:
    - port: 5432
      targetPort: 5432
      nodePort: 30432
      protocol: TCP
  selector:
    app: postgresql

注:这里是设定对外开发的端口为 30432 。

五、测试连接

psql -U postgres -h localhost -p 30432

你可能感兴趣的:(PostgreSQL 学习笔记(2):Kubernetes 单节点部署)