环境变量绑定配置和秘钥
本节将介绍 service-binding 运维特征的用法,它能将数据从 Kubernetes Secret 绑定到应用程序所在容器的 ENV 上。
注:这个运维特征默认在
VelaUX处隐藏,你可以在 CLI 侧使用。
如何使用
- 创建一个 Secret
 
$ kubectl create secret generic db-conn-example --from-literal=password=123  --from-literal=endpoint=https://xxx.com --from-literal=username=myname
secret/db-conn-example created
- 将 Secret 绑定到工作负载的环境变量中
 
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
  name: webapp
spec:
  components:
    - name: binding-test-comp
      type: webservice
      properties:
        image: zzxwill/flask-web-application:v0.3.1-crossplane
        ports: 80
      traits:
        - type: service-binding
          properties:
            envMappings:
              # environments refer to db-conn secret
              DB_PASSWORD:
                secret: db-conn-example
                key: password            # 1) 如果 ENV 和 Secret 不一致,则 Secret 必须被设置
              endpoint:
                secret: db-conn-example          # 2) 如果 ENV 和 Secret 一致,则 Secret 可以缺省不写
              username:
                secret: db-conn-example
部署这个 YAML,数据绑定的运维特征会读取名为 db-conn-example 的 Kubernetes Secret 对象,
并注入 binding-test-comp 的这个组件的环境变量 ENV 中。