1

I'm using kubectl to deploy ASP.Net core apps to K8S cluster. At the current moment I hardcode container PORTs and ConnectionString for DataBase like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mydeploy
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mydeploy
  template:
    metadata:
      labels:
        app: mydeploy
    spec:
      containers:
        - name: mydeploy
          image: harbor.com/my_app:latest
          env:
          - name: MyApp__ConnectionString
            value: "Host=postgres-ap-svc;Port=5432;Database=db;Username=postgres;Password=password"
          ports:
            - containerPort: 5076

But the good solution I think - to use such variables from appsettings.json file (ASP.Net config file). So my question - How to load this vars from JSON file and use in yaml file?

JSON file is like this:

{
  "MyApp": {
    "ConnectionString": "Host=postgres-ap-svc;Port=5432;Database=db;Username=postgres;Password=password"
  },
  "ClientBaseUrls": {
    "MyApp": "http://mydeploy-svc:5076/api",
  }
}
Mikhail_Sam
  • 10,602
  • 11
  • 66
  • 102

3 Answers3

2

You can use following command to create configmap and then mount it to your contianer

kubectl create configmap appsettings --from-file=appsettings.json --dry-run -o yaml

For mounting you should add a volume to your deployment or sts like this:

  volumes:
    - name: config
      configMap:
        name: appsettings

And then mount it to your container:

 volumeMounts:
        - mountPath: /appropriate/path/to/config/appsettings.json
          subPath: appsettings.json
          name: config

Also if you want you can use the config map as your environment variables source like this:

envFrom:
      - configMapRef:
          name: config
  • thank you for response! I tried your solution - looks like it just create one env var that contains all json file. I can't understand how to use it. If I run `printenv` in container I see `appsettings.json={ "MyApp": { "ConnectionString": "Host=postgres-ap-svc;Port=5432;Database=db;Username=postgres;Password=password" }, "ClientBaseUrls": { "MyApp": "http://mydeploy-svc:5076/api", } }` while I want something like: `"MyApp__ConnectionString":"..."` and `"ClientBaseUrls__MyApp":"..."` – Mikhail_Sam Feb 01 '22 at 12:39
  • You should create the configmap using --from-env-file option instead. For example: `kubectl create configmap appsettings --from-env-file=appsettings.json --dry-run -o yaml` And use envFrom like mentioned in the post kubernetes.io/docs/tasks/configure-pod-container/… – Hamed Heidarian Feb 01 '22 at 13:45
  • looks like `--from-env-file` option doesn't work with `json`: `error: "{" is not a valid key name`... :( – Mikhail_Sam Feb 01 '22 at 14:23
  • 1
    Sorry, the only workaround I know is either to manually convert json to key vlaues or to convert json to properties file using tools like jq – Hamed Heidarian Feb 01 '22 at 15:20
1

You can try using literal mode:

kubectl create configmap appsettings --from-literal='ConnectionString'='Host=postgres-ap-svc;Port=5432;Database=db;Username=postgres;Password=password' --from-literal='MyApp'='http://mydeploy-svc:5076/api'

If this doesn't work you may need to write separate JSON-YAML parser.

Sergiusz
  • 1,175
  • 4
  • 13
-1

I'm putting all the comments as a community wiki answer for better usability.

If you run printenv in a container and see:

appsettings.json={ "MyApp": { "ConnectionString": "Host=postgres-ap-svc;Port=5432;Database=db;Username=postgres;Password=password" }, "ClientBaseUrls": { "MyApp": "http://mydeploy-svc:5076/api", } }

instead of something like: "MyApp__ConnectionString":"..." and "ClientBaseUrls__MyApp":"..."

you should create the configmap using --from-env-file option instead.

For example: kubectl create configmap appsettings --from-env-file=appsettings.json --dry-run -o yaml And use envFrom like mentioned in the post kubernetes.io/docs/tasks/configure-pod-container/.

Wojtek_B
  • 4,245
  • 1
  • 7
  • 21
  • Hi! Thank you for response, but this is not the answer. `kubectl create ConfigMap --from-env-file` works on yaml only and cannot be used for JSON file. So this doesn't work – Mikhail_Sam Feb 03 '22 at 12:39