简体   繁体   English

Kubectl 部署 - 如何从 JSON 文件加载环境变量

[英]Kubectl deployment - how to load env vars from JSON file

I'm using kubectl to deploy ASP.Net core apps to K8S cluster.我正在使用 kubectl 将 ASP.Net 核心应用程序部署到 K8S 集群。 At the current moment I hardcode container PORTs and ConnectionString for DataBase like this:目前,我对数据库的容器端口和 ConnectionString 进行硬编码,如下所示:

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).但我认为好的解决方案 - 使用appsettings.json文件(ASP.Net 配置文件)中的此类变量。 So my question - How to load this vars from JSON file and use in yaml file?所以我的问题 - 如何从 JSON 文件加载这个变量并在 yaml 文件中使用?

JSON file is like this: JSON 文件是这样的:

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

You can use following command to create configmap and then mount it to your contianer您可以使用以下命令创建 configmap,然后将其挂载到您的 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:对于安装,您应该向部署或 sts 添加一个卷,如下所示:

  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:此外,如果您愿意,可以使用配置 map 作为环境变量源,如下所示:

envFrom:
      - configMapRef:
          name: config

I'm putting all the comments as a community wiki answer for better usability.为了更好的可用性,我将所有评论作为社区 wiki 答案。

If you run printenv in a container and see:如果您在容器中运行printenv并查看:

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":"..."而不是类似: "MyApp__ConnectionString":"...""ClientBaseUrls__MyApp":"..."

you should create the configmap using --from-env-file option instead.您应该使用--from-env-file选项创建配置映射。

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/ . 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/ .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM