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)文章来源地址https://www.toymoban.com/news/detail-774103.html
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
- 查看运行结果
# 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
文章来源:https://www.toymoban.com/news/detail-774103.html
到了这里,关于k8s安装hostPath方式存储的PostgreSQL15的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!