如何使用 kustomize 将内存存储替换为持久存储

How to replace the in-memory storage with persistent storage using kustomize

我正在尝试使用 kustomizeGrafana 部署的内存存储替换为 persistent storage。我想做的是删除 in-memory storage,然后映射 persistent storage。但是当我部署它时它给我一个错误。

错误

部署“grafana”无效:spec.template.spec.containers[0].volumeMounts[1].name:未找到:“grafana-storage”

Kustomize 版本

{Version:kustomize/v4.0.5 GitCommit:9e8e7a7fe99ec9fbf801463e8607928322fc5245 BuildDate:2021-03-08T20:53:03Z GoOs:linux GoArch:amd64}

kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- https://github.com/prometheus-operator/kube-prometheus
- grafana-pvc.yaml
patchesStrategicMerge:
- grafana-patch.yaml

grafana-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-storage
  namespace: monitoring
  labels:
    billingType: "hourly"
    region: sng01
    zone: sng01
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
  storageClassName: ibmc-file-bronze

grafana-patch.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: grafana
  name: grafana
  namespace: monitoring
spec:
  template:
    spec:
      volumes:
        # use persistent storage for storing users instead of in-memory storage
        - $patch: delete  <---- trying to remove the previous volume
          name: grafana-storage
        - name: grafana-storage
          persistentVolumeClaim:
            claimName: grafana-storage
      containers:
        - name: grafana
          volumeMounts:
            - name: grafana-storage
              mountPath: /var/lib/grafana

请帮忙。

$patch: delete 似乎没有像我预期的那样工作。

最好在 kustomize github 上提出问题:https://github.com/kubernetes-sigs/kustomize/issues 并询问开发人员。


尽管这是我试过的补丁,它似乎有效:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: grafana
  name: grafana
  namespace: monitoring
spec:
  template:
    spec:
      volumes:
        - name: grafana-storage
          emptyDir: null
          persistentVolumeClaim:
            claimName: grafana-storage
      containers:
        - name: grafana
          volumeMounts:
            - name: grafana-storage
              mountPath: /var/lib/grafana

基于https://github.com/kubernetes/community/blob/master/contributors/design-proposals/api-machinery/add-new-patchStrategy-to-clear-fields-not-present-in-patch.md

以下理论上也应该有效:

spec:
  volumes:
    - $retainKeys:
      - name
      - persistentVolumeClaim
      name: grafana-storage
      persistentVolumeClaim:
        claimName: grafana-storage

但实际上并没有,我认为这是因为 kustomize 有自己的战略合并实现(与 k8s 不同)。