一冒晰、創(chuàng)建 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
,按我的理解應(yīng)該用可以支持分布式的存儲方式竟块。
二壶运、創(chuàng)建 PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgresql-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
三、創(chuàng)建 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
四浪秘、創(chuàng)建 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
注:這里是設(shè)定對外開發(fā)的端口為 30432 蒋情。
五埠况、測試連接
psql -U postgres -h localhost -p 30432