Search code examples
dockerkubernetesdocker-volumepersistent-volumeshorizontal-pod-autoscaling

HorizontalPodAutoscaler overwrites volume when releasing a Pod


i have a question to ask you about kubernates. I need to configure a Horizontal Load Balancer on a service with Kubernates. Specifically in my yaml file I used the following kind: HorizontalPodAutoscaler; the problem arises on the logic of uploading files to the volume: in a nutshell, when a pod is released, the data inside is overwritten, so with a new pod the volume is emptied. Am I wrong in configurations? Specifically, I set the volume in the Dockerfile to build the corresponding image of the service launched with Kubernates.


Solution

  • it's not good idea to store data in Dockerimage however still what you can do is,

    • Copy data to docker image
    • Inside the Kubernetes POD create the PVC volume
    • Attach POD to PVC and use the volume mount
    • Use the init container to copy data from the docker path to the volume mount file path

    Dockerfile

    FROM python:latest
    COPY data.csv ./tmp/
    

    Kubernetes YAML

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: POD-name
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: test
      template:
        metadata:
          labels:
            app: test
        spec:
          initContainers:
          - name: copy
            image: busybox:1.28
            command: ["/bin/sh", "-c", "cp /tmp/data.csv /data/data.csv"]
            volumeMounts:
            - name: pvc
              mountPath: /data/
          containers:
          - name: test
            image: image:tag
            ports:
            - containerPort: 8080
            env:
            - name: PORT
              value: "8080"
            volumeMounts:
            - mountPath: "/data/"
              name: pvc
          volumes:
          - name: pvc
            persistentVolumeClaim:
              claimName: pvc-claim