Docker 如何在CI&;中使用kubernetes和nginx中的环境变量替换angular config.json的字段值;CD-VST

Docker 如何在CI&;中使用kubernetes和nginx中的环境变量替换angular config.json的字段值;CD-VST,docker,nginx,kubernetes,angular6,kubernetes-helm,Docker,Nginx,Kubernetes,Angular6,Kubernetes Helm,我正在尝试使用kubernetes中的环境变量动态替换angular项目的config.json中的authenticationEndpoint url和其他配置。用于VSTS CI&CD管道中环境变量的helm图表中配置的。但不确定如何用kubernetes中的环境变量替换config.json字段。你能帮我一下吗 pods中的环境(kubernetes)运行printenv cmd config.json 从头盔图表生成的yaml文件 config.json的绝对路径 当pod启动时,使用i

我正在尝试使用kubernetes中的环境变量动态替换angular项目的config.json中的authenticationEndpoint url和其他配置。用于VSTS CI&CD管道中环境变量的helm图表中配置的。但不确定如何用kubernetes中的环境变量替换config.json字段。你能帮我一下吗

pods中的环境(kubernetes)运行printenv cmd config.json 从头盔图表生成的yaml文件 config.json的绝对路径
当pod启动时,使用init容器修改config.json

更新了您的部署.yaml 在pod启动的主机中创建
/mnt/data.json
文件
我找到了简单的解决办法。我使用shell脚本应用相同的命令替换config.json的内容,然后启动Nginx以运行应用程序。它起作用了

Config.json

{
  "authenticationEndpoint": "$AUTHENTICATION_ENDPOINT",
  "authenticationClientId": "$AUTHENTICATION_CLIENT_ID",
  "baseApiUrl": "http://localhost:8080/",
  "homeUrl": "http://localhost:4300/"
}
setup.sh

sed -i -e 's#$AUTHENTICATION_ENDPOINT#'"$AUTHENTICATION_ENDPOINT"'#g' usr/share/nginx/html/config.json
sed -i -e 's#$AUTHENTICATION_CLIENT_ID#'"$AUTHENTICATION_CLIENT_ID"'#g' /usr/share/nginx/html/config.json
nginx -g 'daemon off;'

我会采取的方法是将K8s安装转换为Helm图表-然后您可以将
config.json
包含到模板中。您能提供一些示例代码吗,我们是否需要在config.json中放置任何占位符,或者将自动替换为key。?共享kuberenetes应用程序yaml文件,必需的config.json文件。嗨,我已经更新了从helm chart和必需的config.json文件生成的yaml文件。请在这方面帮助我。在容器内共享config.json-absolute路径。感谢您的及时回复。创建/mnt/data.json给我留下了深刻的印象,因为我使用的是helm chart,我不知道如何创建包含内容的文件。如果我能够创建此文件,那么我可以更改舵图并尝试部署它。
        # Source: sample-web/templates/service.yaml
        apiVersion: v1
        kind: Service
        metadata:
          name: cloying-rattlesnake-sample-web
          labels:
            app.kubernetes.io/name: sample-web
            helm.sh/chart: sample-web-0.1.0
            app.kubernetes.io/instance: cloying-rattlesnake
            app.kubernetes.io/managed-by: Tiller
        spec:
          type: ClusterIP
          ports:
            - port: 80
              targetPort: 80
              protocol: TCP
              name: http
          selector:
            app.kubernetes.io/name: sample-web
            app.kubernetes.io/instance: cloying-rattlesnake
        ---
        # Source: sample-web/templates/deployment.yaml
        apiVersion: apps/v1
        kind: Deployment
        metadata:
          name: cloying-rattlesnake-sample-web
          labels:
            app.kubernetes.io/name: sample-web
            helm.sh/chart: sample-web-0.1.0
            app.kubernetes.io/instance: cloying-rattlesnake
            app.kubernetes.io/managed-by: Tiller
        spec:
          replicas: 1
          selector:
            matchLabels:
              app.kubernetes.io/name: sample-web
              app.kubernetes.io/instance: cloying-rattlesnake
          template:
            metadata:
              labels:
                app.kubernetes.io/name: sample-web
                app.kubernetes.io/instance: cloying-rattlesnake
            spec:
              containers:
                - name: sample-web
                  image: "sample-web:stable"
                  imagePullPolicy: IfNotPresent
                  ports:
                    - name: http
                      containerPort: 80
                      protocol: TCP
                  livenessProbe:
                    httpGet:
                      path: /
                      port: http
                  readinessProbe:
                    httpGet:
                      path: /
                      port: http
                  env:
                    - name: authenticationEndpoint
                      value: "http://localhost:8080/security/auth"
                  resources:
                    {}
        ---
        # Source: sample-web/templates/ingress.yaml
        apiVersion: extensions/v1beta1
        kind: Ingress
        metadata:
          name: cloying-rattlesnake-sample-web
          labels:
            app.kubernetes.io/name: sample-web
            helm.sh/chart: sample-web-0.1.0
            app.kubernetes.io/instance: cloying-rattlesnake
            app.kubernetes.io/managed-by: Tiller
          annotations:
            kubernetes.io/ingress.class: nginx
            nginx.ingress.kubernetes.io/rewrite-target: /$1
            nginx.ingress.kubernetes.io/ssl-redirect: "false"

        spec:
          rules:
            - host: ""
              http:
                paths:
                  - path: /?(.*)
                    backend:
                      serviceName: cloying-rattlesnake-sample-web
                      servicePort: 80
Ran shell cmd - kubectl exec -it sample-web-55b71d19c6-v82z4 /bin/sh

path: usr/share/nginx/html/config.json
    # Source: sample-web/templates/deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: cloying-rattlesnake-sample-web
      labels:
        app.kubernetes.io/name: sample-web
        helm.sh/chart: sample-web-0.1.0
        app.kubernetes.io/instance: cloying-rattlesnake
        app.kubernetes.io/managed-by: Tiller
    spec:
      replicas: 1
      selector:
        matchLabels:
          app.kubernetes.io/name: sample-web
          app.kubernetes.io/instance: cloying-rattlesnake
      template:
        metadata:
          labels:
            app.kubernetes.io/name: sample-web
            app.kubernetes.io/instance: cloying-rattlesnake
        spec:
          initContainers:
            - name: init-myconfig
              image: busybox:1.28
              command: ['sh', '-c', 'cat /usr/share/nginx/html/config.json | sed -e "s#\$authenticationEndpoint#$authenticationEndpoint#g" > /tmp/config.json && cp /tmp/config.json /usr/share/nginx/html/config.json']
              env:
                - name: authenticationEndpoint
                  value: "http://localhost:8080/security/auth"
          containers:
            - name: sample-web
              image: "sample-web:stable"
              imagePullPolicy: IfNotPresent
              ports:
                - name: http
                  containerPort: 80
                  protocol: TCP
              livenessProbe:
                httpGet:
                  path: /
                  port: http
              readinessProbe:
                httpGet:
                  path: /
                  port: http
              env:
                - name: authenticationEndpoint
                  value: "http://localhost:8080/security/auth"
              volumeMounts:
                - mountPath: /usr/share/nginx/html/config.json
                  name: config-volume
          volumes:
            - name: config-volume
              hostPath:
                path: /mnt/data.json # Create this file in the host where the pod starts. Content below.
                type: File
{
      "authenticationEndpoint": "$authenticationEndpoint",
      "authenticationClientId": "my-project",
      "baseApiUrl": "http://localhost:8080/",
      "homeUrl": "http://localhost:4300/"
}
{
  "authenticationEndpoint": "$AUTHENTICATION_ENDPOINT",
  "authenticationClientId": "$AUTHENTICATION_CLIENT_ID",
  "baseApiUrl": "http://localhost:8080/",
  "homeUrl": "http://localhost:4300/"
}
sed -i -e 's#$AUTHENTICATION_ENDPOINT#'"$AUTHENTICATION_ENDPOINT"'#g' usr/share/nginx/html/config.json
sed -i -e 's#$AUTHENTICATION_CLIENT_ID#'"$AUTHENTICATION_CLIENT_ID"'#g' /usr/share/nginx/html/config.json
nginx -g 'daemon off;'