k8s安装hostPath方式存储的PostgreSQL15

1.配置 PostgreSQL 的 ConfigMap

cat > postgres-configmap.yaml << EOF
apiVersion: v1
kind: ConfigMap
metadata:name: postgres-configlabels:app: postgresnamespace: dev
data:POSTGRES_DB: postgresdbPOSTGRES_USER: postgresadminPOSTGRES_PASSWORD: admin12345
EOFkubectl create -f postgres-configmap.yaml 

2. 持久化卷 Persistent Storage Volume

在这里,我们使用本地目录/路径作为永久存储资源(/mnt/data)

cat > postgres-pv-pvc.yaml << EOF
kind: PersistentVolume
apiVersion: v1
metadata:name: postgres-pv-volumelabels:type: localapp: postgres
spec:storageClassName: manualcapacity:storage: 5GiaccessModes:- ReadWriteManyhostPath:path: "/mnt/data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:name: postgres-pv-claimlabels:app: postgres
spec:storageClassName: manualaccessModes:- ReadWriteManyresources:requests:storage: 5Gi
EOFkubectl apply -f  postgres-pv-pvc.yaml

3. PostgreSQL Deployment

cat >  postgres-deployment.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:name: postgres-deployment
spec:strategy:type: Recreateselector:matchLabels:app: postgresreplicas: 1template:metadata:labels:app: postgresspec:containers:- name: postgresimage:  postgres:15.3imagePullPolicy: "IfNotPresent"ports:- containerPort: 5432envFrom:- configMapRef:name: postgres-configvolumeMounts:- mountPath: /var/lib/postgresql/dataname: postgredbvolumes:- name: postgredbpersistentVolumeClaim:claimName: postgres-pv-claim
EOFkubectl apply -f  postgres-deployment.yaml            

4. PostgreSQL Service

cat > postgres-service.yaml << EOF
apiVersion: v1
kind: Service
metadata:name: postgres-servicelabels:app: postgres
spec:type: NodePortports:- port: 5432targetPort: 5432protocol: TCPselector:app: postgres
EOFkubectl create -f postgres-service.yaml 
  1. 查看运行结果
# kubectl get svc postgres
NAME               TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
postgres-service   NodePort   10.245.62.239   <none>        5432:32312/TCP   6h10m