Как заменить значение поля angular config.json, используя переменную окружения в kubernetes и nginx в CI & CD VSTS - PullRequest
0 голосов
/ 01 июля 2019

Я пытаюсь заменить authenticationEndpoint url и другую конфигурацию в config.json углового проекта, используя переменную окружения в kubernetes динамически.Для этого настроено в таблице управления для переменной среды в конвейере CI & CD VSTS.Но не уверен, как поле config.json будет заменено переменной окружения в kubernetes.Не могли бы вы помочь мне в этом?диаграмма

        # 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

Абсолютный путь config.json

Ran shell cmd - kubectl exec -it sample-web-55b71d19c6-v82z4 /bin/sh

path: usr/share/nginx/html/config.json

Ответы [ 2 ]

1 голос
/ 01 июля 2019

Используйте контейнер init для изменения файла config.json при запуске модуля.

Обновлен файл Deployment.yaml

    # 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

Создайте файл /mnt/data.json на узле, с которого запускается модуль

{
      "authenticationEndpoint": "$authenticationEndpoint",
      "authenticationClientId": "my-project",
      "baseApiUrl": "http://localhost:8080/",
      "homeUrl": "http://localhost:4300/"
}
0 голосов
/ 12 июля 2019

Я нашел простое решение.используя сценарий оболочки, я применяю ту же команду для замены содержимого файла 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;'
...