k8s安装hostPath方式存储的PostgreSQL15

1.配置 PostgreSQL 的 ConfigMap

cat > postgres-configmap.yaml << EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
  namespace: dev
data:
  POSTGRES_DB: postgresdb
  POSTGRES_USER: postgresadmin
  POSTGRES_PASSWORD: admin12345
EOF

kubectl 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-volume
  labels:
    type: local
    app: postgres
spec:
  storageClassName: manual
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
  labels:
    app: postgres
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
EOF

kubectl 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: Recreate
  selector:
    matchLabels:
      app: postgres
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image:  postgres:15.3
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim
EOF

kubectl apply -f  postgres-deployment.yaml            

4. PostgreSQL Service

cat > postgres-service.yaml << EOF
apiVersion: v1
kind: Service
metadata:
  name: postgres-service
  labels:
    app: postgres
spec:
  type: NodePort
  ports:
  - port: 5432
    targetPort: 5432
    protocol: TCP
  selector:
   app: postgres
EOF

kubectl 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

你可能感兴趣的:(kubernetes,docker容器相关,kubernetes,容器,云原生)