How to pass environment variables in angular application with docker

1/14/2020

I m running my Angular 6 application as a Kubernetes pod/container. In this Angular app, I try to access a JSON config-object, to load my environment variables.

I have used volume to read the config map and volume mount to create a config json file.

My service is throwing a 503 error IF I include the volume creation code in my yaml file.

The pods are in running mode when seen in logs.

Code snippet of my deployment yaml file:

  apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "client-onboard.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "client-onboard.name" . }}
    helm.sh/chart: {{ include "client-onboard.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "client-onboard.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "client-onboard.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          volumeMounts:
          - name: env-vars
            mountPath: usr/share/nginx/html/config/env-vars.json
      volumes:
        - name: env-vars
          configMap:
            name: {{ .Chart.Name }}-configmap
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}

I am using nginx server for this.

-- pulkit arora
angular6
docker
kubernetes
volume
yaml

1 Answer

1/14/2020

For creating a file you need to use subPath.The value of the subPath must match the “key” of the ConfigMap. In the below example the key and subPath is config.yaml.

apiVersion: v1
kind: ConfigMap
metadata:
  name: sherlock-config
  namespace: default
data:
  config.yaml: |
    namespaces:
      - default
    labels:
      - "app"
      - "owner"
---
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config/config.yaml
        subPath: config.yaml
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: sherlock-config
  restartPolicy: Never

kubectl describe cm sherlock-config

Name:         sherlock-config
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"config.yaml":"namespaces:\n  - default\nlabels:\n  - \"app\"\n  - \"owner\"\n"},"kind":"ConfigMap","metadata":...

Data
====
config.yaml: <------ This is the key
----
namespaces:
  - default
labels:
  - "app"
  - "owner"

Events:  <none>
-- Arghya Sadhu
Source: StackOverflow