如何将值动态填充到Kubernetes yaml文件中 [英] How to dynamically populate values into Kubernetes yaml files

查看:1029
本文介绍了如何将值动态填充到Kubernetes yaml文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在运行时传递kubernetes yaml文件中的某些值,例如从config/properties文件中读取.

I would like to pass in some of the values in kubernetes yaml files during runtime like reading from config/properties file.

什么是最好的方法?

在下面的示例中,我不想对端口值进行硬编码,而是从配置文件中读取端口号.

In the below example, I do not want to hardcode the port value, instead read the port number from config file.

例如:

logstash.yaml


apiVersion: v1
kind: ReplicationController
metadata:
name: test
namespace: test
spec:
replicas: 1
selector:
app: test
template:
metadata:
  labels:
    app: test
spec:
  containers:
  - name: test
    image: logstash
    ports:
    - containerPort: 33044 (looking to read this port from config file)
    env:
    - name: INPUT_PORT
      value: "5044"

config.yaml

logstash_port: 33044

推荐答案

您可以为此使用Kubernetes ConfigMaps.引入了ConfigMap,以包括诸如属性文件之类的外部配置文件.

You can use Kubernetes ConfigMaps for this. ConfigMaps are introduced to include external configuration files such as property files.

首先从您的属性中创建一个ConfigMap工件,如下所示:

First create a ConfigMap artifact out of your property like follows:

kubectl create configmap my-config --from-file=db.properties

然后在您的Deployment Yaml中,您可以将其提供为卷绑定或环境变量

Then in your Deployment yaml you can provide it as a volume binding or environment variables

音量绑定:

apiVersion: v1
kind: ReplicationController
metadata:
  name: test
  labels:
    app: test
spec:
  containers:
  - name: test
    image: test
    ports:
    - containerPort: 33044
    volumeMounts:
    - name: config-volume
      mountPath: /etc/creds <mount path> 
  volumes:
    - name: config-volume
      configMap:
        name: my-config

mountPath下,您需要提供您的属性文件应位于的容器的位置.在configMap name下,您应该定义所创建的configMap的名称.

Here under mountPath you need to provide the location of your container where your property file should resides. And underconfigMap name you should define the name of your configMap you created.

环境变量的方式:

apiVersion: v1
kind: ReplicationController
metadata:
  name: test
  labels:
    app: test
spec:
  containers:
  - name: test
    image: test
    ports:
    - containerPort: 33044
    env:
    - name: DB_PROPERTIES
      valueFrom:
        configMapKeyRef:
          name: my-config
          items:
          - key: <propert name>
            path: <path/to/property>

name下的configMapKeyRef部分下,您应该定义您创建的配置映射名称.例如我的配置.在items下,您应该定义属性文件的密钥以及每个密钥的路径,Kubernetes会在内部自动解析属性的值.

Here under the configMapKeyRef section under name you should define your config map name you created. e.g. my-config. Under the items you should define the key(s) of your property file and path to each of the key, Kubernetes will automatically resolve the value of the property internally.

您可以在此处找到有关ConfigMap的更多信息. https://kubernetes-v1-4.github.io/docs /user-guide/configmap/

You can find more about ConfigMap here. https://kubernetes-v1-4.github.io/docs/user-guide/configmap/

这篇关于如何将值动态填充到Kubernetes yaml文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆