Upload JSON file to Kubernetes Minikube Cluster node to use in ASP.NET Core application in a pod

12/9/2021

I have a data-config.json that is used by my ASP.NET Core application.

The app was built to a image and the goal is to create a Kubernetes environment (using Minikube and a myapp.yaml to create and deploy the Minikube and the pods) and copy the data-config.json from a specific place in my local machine to a directory in the Node (the place I want to be ~/app/publish/data-config.json, in other words, in the root directory of the node).

I read a lot the documentation and I think ConfigMap can be useful in this case. I already implemented a Volume too. But I don't think write the json content inside the ConfigMap configuration is the best way to do that, I want to depend only of the data-config.json file and the YAML.

In the docker-compose.yml file, to test in Docker Desktop, it works and the code is showed above:

    dataService:
        image: webapp
        build:
          context: ../..
          dockerfile: webapp
        container_name: webapp-container
        ports:
            - "9000:8080"
        volumes:
            - "../data-service/data-config.json:/app/publish/data-config.json"

And it worked. Now I need to translate or find a way to copy this file and save it in the /app/publish/ directory of my node.

-- Hugo Mata
asp.net-core
json
kubernetes
minikube
yaml

2 Answers

12/10/2021

You don't do anything like these.

volumes:
    - "../data-service/data-config.json:/app/publish/data-config.json"

I guess you maybe use data-config.json in your project. So you can use ItemGroup to include the specific file by modifying your your_project_name.csproj file.

You also can refer my answer in below posts:

1. Azure Functions don't publish appsettings.prod.json file

2. Unable to find files located in my root project folder when hosted on Azure

-- Jason
Source: StackOverflow

1/20/2022

I solved this question by creating a ConfigMap that maps the data-config.json from my local machine directory to the container pod. The example shows the implementation of the YAML file used by Minikube to create and start the cluster:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: data-deployment
  labels:
    app: data-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: data-app
  template:
    metadata:
      labels:
        app: data-app
    spec:
      imagePullSecrets:
      - name: regcred
      containers:
      - name: data-app
        image: validation-image:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
        env:
        - name: DataSettings_ConfigPath
          value: /app/publish/data-config.json
        volumeMounts:
        - name: data-config-dir
          mountPath: /app/publish/data-config.json
          subPath: data-config.json
      restartPolicy: Always
      hostname: data-service
      volumes: 
      - name: data-config-dir
        configMap:
           name: data-configmap
           items:
             - key: data-config.json
               path: data-config.json

PS: you must run the command below in terminal to create the ConfigMap for data-config.json file:

$ kubectl create configmap data-configmap --from-file ../data-service/data-config.json
-- Hugo Mata
Source: StackOverflow