创建PV
apiVersion: v1
kind: PersistentVolume
metadata:
name: postgresql-pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 4Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/home/.k8s_data"
创建PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgresql-pv-claim
namespace: postgres
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 4Gi
创建Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgresql-deployment
namespace: postgres
spec:
strategy:
type: Recreate
selector:
matchLabels:
app: postgresql
template:
metadata:
labels:
app: postgresql
spec:
containers:
- image: postgres:13.2
name: postgresql
env:
- name: POSTGRES_USER
value: postgres
- name: POSTGRES_PASSWORD
value: postgres
- name: POSTGRES_ROOT_PASSWORD
value: postgres
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: postgres-service
namespace: ebcms
labels:
app: postgresql
spec:
type: NodePort
selector:
app: postgresql
ports:
- protocol: TCP
port: 5432
targetPort: 5432
nodePort: 30201
将所有配置按顺序放同一个yaml配置
执行kubectl apply -f postgresql-k8s.yaml
亲测可用