Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Configure AspNetCore to get settings from environment variables #9522


User avatar
0
shipserv created

Hi AspNetZero,

We have been migrating AspNetZero in docker using Kubernetes.

Currently, our settings like SQL Connection String (i.e.ConnectionStrings in appsettings.json) are hard-coded in these appsettings json files as seen below. What we are currently doing now is we split the appsettings based on the environment and we just set ASPNETCORE_ENVIRONMENT as env variable according to the environment where it's deployed and running.

We would like to move all these settings as environment variables in Kubernetes as we did with our other apps. Please let us know how to properly do it.

Regards, Allan


5 Answer(s)
  • User Avatar
    0
    zony created
    Support Team

    Hi shipserv Do you want to load the configuration items in appsettings.json into environment variables?

  • User Avatar
    0
    shipserv created

    hi Zony,

    Yes, that's right. So that all sensitive data such as database connection string will be stored on environment variables rather than in codes of appsettings.json. We don't want these configs to be hardcoded.

    Regards, Allan

  • User Avatar
    0
    zony created
    Support Team

    hi shipserv, I think you should create an env.yml in which to write environment variables according to the rules. example:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: test-service
      labels:
        app: test-service
    spec:
      selector:
        matchLabels:
          app: test-service
      template:
        metadata:
          labels:
            app: test-service
        spec:
          containers:
          - name: test-service
            env:
              - name: ConnectionStrings__Default
                value: User ID=postgres;Password=XXXX;Host=XXXX;Port=1433;Database=TestDataBase;
              - name: ConnectionStrings__AbpTenantManagement
                value: User ID=postgres;Password=XXX;Host=XXXX;Port=1433;Database=TestDataBase;
              - name: IdentityServer__AuthorityServer
                value: http://localhost:5000
    

    Naming rules: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1#environment-variables

  • User Avatar
    0
    zony created
    Support Team

    You can also separate environment variables. configMap.yml

    apiVersion: v1
    data:
     ConnectionStrings__Default: User ID=postgres;Password=XXXX;Host=XXXX;Port=1433;Database=TestDataBase;
    
    kind: ConfigMap
    metadata:
     labels:
       app: test-service
     name: test-service-config
    

    deploy.yml

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
     name: test-service
     labels:
       app: test-service
    spec:
     selector:
       matchLabels:
         app: test-service
     serviceName: test-service
     replicas: 2
     updateStrategy:
       type: RollingUpdate
     template:
       metadata:
         labels:
             app: test-service
       spec:
         containers:
           - image: xxx:latest
             name: test-service
             envFrom:
               - configMapRef:
                   name: test-service-config # Here
    
  • User Avatar
    0
    ismcagdas created
    Support Team