'How to read variables from configmaps in kubernetes yml file in Nodejs
We were asked to shift the variables from export key=value to configmaps in the deployment.yml file.
deployment.yml
{% if configmap is defined %}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: "{{ prefix }}-{{ project_name }}"
namespace: "{{ namespace }}"
data:
{% for key, value in configmap.items() %}
{{ key }}: "{{ value }}"
{% endfor %}
{% endif %}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ prefix }}-{{ project_name }}-deployment
namespace: {{ namespace }}
labels:
k8s-app: {{ prefix }}-{{ project_name }}
spec:
progressDeadlineSeconds: 60
revisionHistoryLimit: 1
replicas: 1
selector:
matchLabels:
k8s-app: {{ prefix }}-{{ project_name }}
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
template:
metadata:
labels:
k8s-app: "{{ prefix }}-{{ project_name }}"
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: k8s-app
operator: In
values:
- {{ prefix }}-{{ project_name }}
topologyKey: "kubernetes.io/hostname"
containers:
- name: {{ project_name }}
image: "67567464.dkr.tfr.ap-north-1.amazonaws.com/{{ project_name }}:{{ tag }}"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 4200
livenessProbe:
httpGet:
path: /status
port: 4200
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 2
periodSeconds: 8
successThreshold: 1
failureThreshold: 5
readinessProbe:
httpGet:
path: /status
port: 4200
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 2
periodSeconds: 5
successThreshold: 1
failureThreshold: 20
envFrom:
{% if configmap is defined %}
- configMapRef:
name: "{{ prefix }}-{{ project_name }}"
{% endif %}
- secretRef:
name: "{{ prefix }}-{{ project_name }}"
resources:
limits:
cpu: '200m'
memory: 300Mi
requests:
cpu: '100m'
memory: 150Mi
nodeSelector:
workloadType: {{ workload_type }}
---
apiVersion: v1
kind: Service
metadata:
name: {{ prefix }}-{{ project_name }}-service
namespace: {{ namespace }}
labels:
k8s-svc: {{ prefix }}-{{ project_name }}-service
spec:
ports:
- port: 4200
targetPort: 4200
protocol: TCP
selector:
k8s-app: {{ prefix }}-{{ project_name }}
type: ClusterIP
---
apiVersion: getambassador.io/v2
kind: Mapping
metadata:
name: {{ prefix }}-{{ project_name }}-service-mapping
namespace: {{ namespace }}
spec:
bypass_auth: true
host: {{ fqdn }}
prefix: /
service: {{ prefix }}-{{ project_name }}-service.{{ namespace }}:4200
timeout_ms: 200000
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: {{ prefix }}-{{ project_name }}-hpa
namespace: {{ namespace }}
spec:
scaleTargetRef:
kind: Deployment
name: {{ prefix }}-{{ project_name }}-deployment
apiVersion: apps/v1
minReplicas: {{ min_replicas }}
maxReplicas: {{ max_replicas }}
targetCPUUtilizationPercentage: 95
vars.yml - where we have all the secrets as below
env: staging
project_name: oracle
prefix: staging
namespace: "{{ prefix }}-nexus"
fqdn: "{{ prefix }}-{{ project_name }}.dummy.in"
tag: "{{ prefix }}-{{ build_number }}"
context: development
profile: default
workload_type: general
env_during_build: True
nocache: "no"
min_replicas: 1
max_replicas: 1
configmap:
BASE_URL: ""
IDENTITY_ENDPOINT: ""
NODE_ENV: "production"
But I am unable to access this variables with code
process.env.IDENTITY_ENDPOINT
However when I log into the pods and run env in the terminal the values are present.
Is there another way or code to read the env variables in this case.
P.S: In elixir I had to change to System.get_env from
Application.get_env(:app_name, :env_vars_name)[:key_name]
Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
