kubernetes下创建postgresql

使用kubernetes创建单机版postgresql服务

1、创建postgresql使用的PVC
vim postgresql-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: postgresql
namespace: test-basicservices
spec:
accessModes:
– ReadWriteMany
resources:
requests:
storage: 50Gi
storageClassName: nfs-storage

2、创建postgresql statefulset
vim postgre.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: postgresql
namespace: test-basicservices
labels:
k8s.kuboard.cn/name: postgresql
k8s.kuboard.cn/layer: db
spec:
podManagementPolicy: OrderedReady
replicas: 1
selector:
matchLabels:
k8s.kuboard.cn/name: postgresql
k8s.kuboard.cn/layer: db
serviceName: postgresql
template:
metadata:
labels:
k8s.kuboard.cn/layer: db
k8s.kuboard.cn/name: postgresql
spec:
containers:
– env:
– name: POSTGRES_PASSWORD
value: roCvtqE1e2
– name: PGDATA
value: /var/lib/postgresql/data/pgdata
image: postgres:12
imagePullPolicy: Always
name: postgresql
volumeMounts:
– mountPath: /var/lib/postgresql/data/pgdata
name: postgresql
restartPolicy: Always
volumes:
– name: postgresql
persistentVolumeClaim:
claimName: postgresql

3、创建postgresql service
vim postgresql-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: postgresql
namespace: test-basicservices
labels:
name: postgresql
spec:
type: NodePort
ports:
– name: postgresql
nodePort: 30032
port: 5432
protocol: TCP
selector:
k8s.kuboard.cn/name: postgresql
k8s.kuboard.cn/layer: db

4、运行命令创建PVC,statefulset,svc
kubectl apply -f postgresql-pvc.yaml -f postgre.yaml -f postgresql-svc.yaml

5、使用主机IP+30032端口连接验证即可

发表评论