首页 > 解决方案 > 如何为应用程序 dotnetcore 编写 configmap

问题描述

我有一个用于应用程序 dotnetcore 的 docker-composes,我是 k8s 的新手,我的应用程序有一个很长的 envvariable。“ConnectionStrings__DefaultConnection”如下

      productstudio:
    volumes:
      - ${USERPROFILE}/.aws:/root/.aws
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ConnectionStrings__DefaultConnection=Username=someuser;Password=somepassword;Server=postgres;Port=5432;Database=somedb;Search Path=some
      - EventBus__Enable=true
      - EventBus__HostUri=rabbitmq://eventbus/

我写了configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: be-productstudio-configmap
  labels:
    app: product-builder-be
    tier: backend
data:
  ASPNETCORE_ENVIRONMENT: Development
  EventBus__Enable: true
  EventBus__HostUri: rabbitmq://eventbus/
  ConnectionStrings__DefaultConnection: |-
    Username=someuser;
    Password=somepassword;
    Server=postgres;
    Port=5432;
    Database=somedb;
    Search Path=some

但我有一个错误

Error from server (BadRequest): error when creating "manifect-be.yml": ConfigMap in version "v1" cannot be handled as a ConfigMap: v1.ConfigMap.Data: ReadString: expects " or n, but found t, error found in #10 byte of ...|_Enable":true,"Event|..., bigger context ...|nSearch Path=productstudio\"","EventBus__Enable":true,"EventBus__HostUri":"rabbitmq://eventbus/"},"k|...

谁能帮帮我,谢谢

标签: kubernetes

解决方案


我在这里看到两个问题:

  1. 您看到的错误意味着truefor的值EventBus__Enable没有被引用,它被视为表示布尔值 true 的关键字。环境变量是字符串,必须在您的 yaml 定义中引用。你需要让它看起来更像这样:

  EventBus__Enable: "true"
  1. 你不应该在你的关键定义中使用空格ConfigMap

Search Path=productstudio

作为:

data或字段下的每个键binaryData必须由字母数字字符-_或组成.

您可以使用官方文档作为正确配置的 ConfigMaps 的参考,例如:

apiVersion: v1
kind: ConfigMap
metadata:
  name: game-demo
data:
  # property-like keys; each key maps to a simple value
  player_initial_lives: "3"
  ui_properties_file_name: "user-interface.properties"

  # file-like keys
  game.properties: |
    enemy.types=aliens,monsters
    player.maximum-lives=5
  user-interface.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true

推荐阅读