Not able to write to a file in static directory of dockerised flask application deployed in kubernetes

12/28/2017

We have a web application of flask framework written in python. The application code is dockerized and deployed in kubernetes.

The application writes to a file in static directory once a user hits an API. I am unable to do so. I know that as when I check the list of files in the static location using another API, it does not show that file. Please help.

YAML file is given below:

apiVersion: v1
kind: ReplicationController
metadata:
  name: backend-rc
  labels:
    type: backend-type
spec:
  replicas: 32
  template:
    metadata:
      labels:
        type: backend-type
    spec:
      containers:
      - name: backend-container
        image: gcr.io/syw-msm/ts-m:pc-v1.0
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
          protocol: TCP
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace

The dockerfile configuration is given below:

FROM ubuntu:latest

RUN apt-get update -y

RUN apt-get install -y python-pip python-dev build-essential python-tk

COPY . /app

WORKDIR /app

RUN chmod -R 777 /app/static

RUN pip install -r requirements.txt

RUN pip install fbprophet

ENTRYPOINT ["/usr/bin/python"]

CMD ["app.py"]
-- abhishek jha
docker
flask
kubernetes

1 Answer

12/28/2017

You have 32 replicas in your definition. The API call which makes the app to write the file locally hits one pod (replica), and the call that checks for its existence hits a different one. It is expected that the file is not there.

If you scale the number of replicas to 1, it will work. You can try for testing purposes, but this probably isn't what you want.

If you need storage with r/w access from multiple replicas, you will need to rework your app to use some form of shared file system or database.
In case you care about that file this is a good idea anyway - pods are meant to be ephemeral and are not for storing state.

-- Robert Lacok
Source: StackOverflow