【问题标题】:Specify an `int` value in Kubernetes ConfigMap?在 Kubernetes ConfigMap 中指定一个“int”值?
【发布时间】:2017-08-15 04:36:16
【问题描述】:

有没有办法在 ConfigMap 中指定 int 值?当我尝试指定端口号时,出现以下错误。

error: error validating "my-deployment.yml": error validating data: expected type int, for field spec.template.spec.containers[0].ports[0].containerPort, got string; if you choose to ignore these errors, turn validation off with --validate=false

这是我正在使用的示例配置文件:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-service
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: poweramp-email-service
    spec:
      containers:
      - name: poweramp-email-service
        env:
          - name: SERVICE_PORT_NUM
            valueFrom:
              configMapKeyRef:
                name: stg-config
                key: poweramp-email-service.port
        image: nginx
        ports:
        - containerPort: my-service.port

这是我用来生成 ConfigMap 的简单 stg.properties 文件,使用以下命令:kubectl create configmap stg-config --from-file stg.properties

my-service.port=9513

【问题讨论】:

    标签: kubernetes


    【解决方案1】:

    我在将端口号设置为环境变量时遇到了类似的问题。 我有两个服务,ServiceB 应该连接到 ServiceA。 错误是

    /opt/service/config.yml 有错误: * 值类型不正确:serviceAPort;类型:字符串,预期:整数 我的 config.yml 包含类似这样的内容 - serviceAPort: "${SERVICEA_PORT!'9091'}"

    Kubernetes 正在创建一个环境变量 SERVICEA_PORT,其格式具有 Protocol:IP:port。 ServiceB 无法将其解析为 int

    我在 ServiceB pod 中使用“printenv”时意识到了这一点,并用以下配置覆盖了它

    env:
      - name : SERVICEA_PORT
        value: 9091
    

    基本上,这是因为 k8s 开箱即用创建的 env 变量造成的冲突。

    【讨论】:

      【解决方案2】:

      您不能在spec.template.spec.containers[0].ports[0].containerPort 中使用configmap 值,恐怕是用于配置值。

      您可以使用它的选项(在this guide 中指定)是

      • 作为环境变量
      • 作为命令行配置标志(使用环境变量)
      • 使用音量插件。

      如果您想为您的部署/pod 配置端口,您可以考虑使用Helm。 Helm 允许您在清单/定义中使用 Go 模板,然后您可以在调用时覆盖它们。

      拿这个MySQL Chart template as an example,你可以在这里将端口设置为配置选项,如下所示:

      ports:
        - name: mysql
        containerPort: {{ default 3306 .Values.mysqlPort }}
      

      然后从那里,在你的values.yaml中设置这个值

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-26
        • 2019-04-13
        • 1970-01-01
        • 2019-02-10
        • 1970-01-01
        • 1970-01-01
        • 2020-01-02
        • 1970-01-01
        相关资源
        最近更新 更多