Kubernetes:管理环境配置

时间:2016-09-21 07:36:35

标签: docker environment-variables yaml kubernetes

管理在pod中运行的环境配置容器的推荐方法是使用configmapSee the docs here

虽然我们有容器需要大量的环境变量,但这很好,这只会在将来扩展。使用规定的configmap方法,这变得不合适且难以管理。

例如,一个简单的deplyment文件变得庞大:

apiVersion: v1
kind: Service
metadata:
  name: my-app-api
  labels:
    name: my-app-api
    environment: staging
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
      name: http
  selector:
    name: my-app-api
    environment: staging
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-app-api
spec:
  replicas: 2
  revisionHistoryLimit: 10
  template:
    metadata:
      labels:
        name: my-app-api
        environment: staging
    spec:
      containers:
      - name: my-app-api
        imagePullPolicy: Always
        image: myapp/my-app-api:latest
        ports:
            - containerPort: 80
        env:
            - name: API_HOST
              value: XXXXXXXXXXX
            - name: API_ENV
              value: XXXXXXXXXXX
            - name: API_DEBUG
              value: XXXXXXXXXXX
            - name: API_KEY
              value: XXXXXXXXXXX
            - name: EJ_API_ENDPOINT
              value: XXXXXXXXXXX
            - name: WEB_HOST
              value: XXXXXXXXXXX
            - name: AWS_ACCESS_KEY
              value: XXXXXXXXXXX
            - name: AWS_SECRET_KEY
              value: XXXXXXXXXXX
            - name: CDN
              value: XXXXXXXXXXX
            - name: STRIPE_KEY
              value: XXXXXXXXXXX
            - name: STRIPE_SECRET
              value: XXXXXXXXXXX
            - name: DB_HOST
              value: XXXXXXXXXXX
            - name: MYSQL_ROOT_PASSWORD
              value: XXXXXXXXXXX
            - name: MYSQL_DATABASE
              value: XXXXXXXXXXX
            - name: REDIS_HOST
              value: XXXXXXXXXXX
      imagePullSecrets:
        - name: my-registry-key 

是否可以轻松注入中央环境配置?

更新

这是针对1.5提出的,虽然没有进行裁剪,看起来它会被包含在1.6中。手指交叉......

1 个答案:

答案 0 :(得分:0)

目前有一个proposal针对1.5,目的是让这更容易。正如所提出的,您可以一次性从ConfigMap中提取所有变量,而无需单独拼出每个变量。

如果实施,它将允许你做这样的事情:

警告:这实际上还没有工作!

ConfigMap:

apiVersion: v1
data:
  space-ships: 1
  ship-type: battle-cruiser
  weapon: laser-cannon
kind: ConfigMap
metadata:
  name: space-config

部署:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: space-simulator
spec:
  template:
    metadata:
      labels:
        app: space-simulator
    spec:
      containers:
      - name: space-simulator
        image: foo/space-simulator
      # This is the magic piece that would allow you to avoid all that boilerplate!
      - envFrom:
        configMap: space-config