skip to Main Content

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",
  }
}

3

Answers


  1. 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
    
    Login or Signup to reply.
  2. 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/.

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search