gpt4 book ai didi

kubernetes StorageClass 不保留现有数据

转载 作者:行者123 更新时间:2023-12-02 11:37:11 34 4
gpt4 key购买 nike

当 pod 被删除并使用我的 postgresql 数据库重新部署时,我的 Kubernetes StorageClass 卷不会保留现有数据。当我删除 pod 时,会创建新的 pod,但数据库是空的。

我遵循了不同版本的教程(https://kubernetes.io/docs/concepts/storage/persistent-volumes/)的变体,但似乎没有任何效果。

我粘贴了所有 YAML 文件,因为问题可能出在组合中。

存储-google.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: spingular-pvc
spec:
storageClassName: standard
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 7Gi
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: standard
provisioner: kubernetes.io/gce-pd
parameters:
type: pd-standard
zone: us-east4-a

jhipsterpress-postgresql.yml
apiVersion: v1
kind: Secret
metadata:
name: jhipsterpress-postgresql
namespace: default
labels:
app: jhipsterpress-postgresql
type: Opaque
data:
postgres-password: NjY0NXJxd24=
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: jhipsterpress-postgresql
namespace: default
spec:
replicas: 1
template:
metadata:
labels:
app: jhipsterpress-postgresql
spec:
volumes:
- name: data
persistentVolumeClaim:
claimName: spingular-pvc
containers:
- name: postgres
image: postgres:10.4
env:
- name: POSTGRES_USER
value: jhipsterpress
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: jhipsterpress-postgresql
key: postgres-password
ports:
- containerPort: 5432
volumeMounts:
- name: data
mountPath: /var/lib/postgresql/
---
apiVersion: v1
kind: Service
metadata:
name: jhipsterpress-postgresql
namespace: default
spec:
selector:
app: jhipsterpress-postgresql
ports:
- name: postgresqlport
port: 5432
type: LoadBalancer

jhipsterpress-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: jhipsterpress
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: jhipsterpress
version: "v1"
template:
metadata:
labels:
app: jhipsterpress
version: "v1"
spec:
initContainers:
- name: init-ds
image: busybox:latest
command:
- '/bin/sh'
- '-c'
- |
while true
do
rt=$(nc -z -w 1 jhipsterpress-postgresql 5432)
if [ $? -eq 0 ]; then
echo "DB is UP"
break
fi
echo "DB is not yet reachable;sleep for 10s before retry"
sleep 10
done
containers:
- name: jhipsterpress-app
image: galore/jhipsterpress
env:
- name: SPRING_PROFILES_ACTIVE
value: prod
- name: SPRING_DATASOURCE_URL
value: jdbc:postgresql://jhipsterpress-postgresql.default.svc.cluster.local:5432/jhipsterpress
- name: SPRING_DATASOURCE_USERNAME
value: jhipsterpress
- name: SPRING_DATASOURCE_PASSWORD
valueFrom:
secretKeyRef:
name: jhipsterpress-postgresql
key: postgres-password
- name: JAVA_OPTS
value: " -Xmx256m -Xms256m"
resources:
requests:
memory: "256Mi"
cpu: "500m"
limits:
memory: "512Mi"
cpu: "1"
ports:
- name: http
containerPort: 8080
readinessProbe:
httpGet:
path: /management/health
port: http
initialDelaySeconds: 20
periodSeconds: 15
failureThreshold: 6
livenessProbe:
httpGet:
path: /management/health
port: http
initialDelaySeconds: 120

jhipsterpress-service.yml
apiVersion: v1
kind: Service
metadata:
name: jhipsterpress
namespace: default
labels:
app: jhipsterpress
spec:
selector:
app: jhipsterpress
type: LoadBalancer
ports:
- name: http
port: 8080


当我包含保留策略时,我收到了这个错误:
@cloudshell:~ (academic-veld-230622)$ kubectl apply -f storage-google.yaml
error: error validating "storage-google.yaml": error validating data:
ValidationError(PersistentVolumeClaim.spec): unknown field "persistentVolumeReclaimPolicy" in io.k8s.api.core.v1.PersistentVolumeClaimSpec; if you choose to ignore these errors, turn validation off with --validate=false

拜托,如果您知道有效的公共(public)图像的完整示例(在 postgresql 中,我可以使其与 Mongo 一起使用),我将不胜感激。

谢谢大家。

最佳答案

请注意,要使其正常工作,您需要让您的 PVC 动态配置一个 PV 以满足其要求,然后将有一个永久的 binding在 PVC 和 PV 之间,每次您的工作负载使用 PVC 时,它将使用相同的 PV。这段摘录特别指出:

If a PV was dynamically provisioned for a new PVC, the loop will always bind that PV to the PVC



如果在您的情况下,Google Persistent Disk 是由 PVC 配置的,并且您可以验证在 GCP 上每次使用的 PV 都是相同的,那么这可能是 Pod 启动过程中删除所有数据的问题。 (您使用 /var/lib/postgresql//var/lib/postgresql 有什么原因吗?)

另外, persistentVolumeReclaimPolicy: Retain适用于 PV,而不是 PVC。对于动态配置的 PV,该值为 Delete .在您的情况下,它不适用,因为您的动态配置卷应该绑定(bind)到您的 PVC。换句话说,您没有回收该卷。

说了这么多部署数据库的推荐方法是使用 StatefulSets类似于 mysql使用 volumeClaimTemplate 的示例.

关于kubernetes StorageClass 不保留现有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55620417/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com