I've a NextJS app which needs a.env file mounted. I usually do this with providing a configMap:
kind: ConfigMap
apiVersion: v1
metadata:
name: frontend-configmap
namespace: default
data:
.env: |-
NEXT_PUBLIC_API_URL=http://my.domain.com
API_URL=http://my.domain.com
But how to do this with Kustomize?
I try it with envs
, but how do I get the values inside?
configMapGenerator:
- name: frontend-configmap
envs:
- .env
Thank you in advance
You need to have .env
file created first. And ideally even creating configmaps should be based on the existing file (below are examples for kustomize
and kubectl --from-file
).
Then there are two options how to create a configmap:
.env
file with environment variables within (which is your example configmap).env
file (each variable is a separate key)Test structure :
$ tree -a
.
├── .env
└── kustomization.yaml
$ cat .env # same as your test data
NEXT_PUBLIC_API_URL=http://my.domain.com
API_URL=http://my.domain.com
configmap with .env
file with envvars inside:
kustomization.yaml
with an additional option:
$ cat kustomization.yaml
configMapGenerator:
- name: frontend-configmap
files: # using files here as we want to create a whole file
- .env
generatorOptions:
disableNameSuffixHash: true # use a static name
disableNameSuffixHash
- disable appending a content hash suffix to the names of generated resources, see generator options .
And all left is to run it:
$ kustomize build .
apiVersion: v1
data:
.env: | # you can see it's a file with context within
NEXT_PUBLIC_API_URL=http://my.domain.com
API_URL=http://my.domain.com
kind: ConfigMap
metadata:
name: frontend-configmap
The same result can be achieved by running using --from-file
option:
$ kubectl create cm test-configmap --from-file=.env --dry-run=client -o yaml
apiVersion: v1
data:
.env: |
NEXT_PUBLIC_API_URL=http://my.domain.com
API_URL=http://my.domain.com
kind: ConfigMap
metadata:
creationTimestamp: null
name: test-configmap
configmap with envvars as keys within:
$ cat kustomization.yaml
configMapGenerator:
- name: frontend-configmap
envs: # now using envs to create a configmap with envvars as keys inside
- .env
generatorOptions:
disableNameSuffixHash: true # use a static name
Run it to see the output:
$ kustomize build .
apiVersion: v1
data: # you can see there's no file and keys are created directly
API_URL: http://my.domain.com
NEXT_PUBLIC_API_URL: http://my.domain.com
kind: ConfigMap
metadata:
name: frontend-configmap
Same with kubectl
and --from-env-file
option:
$ kubectl create cm test-configmap --from-env-file=.env --dry-run=client -o yaml
apiVersion: v1
data:
API_URL: http://my.domain.com
NEXT_PUBLIC_API_URL: http://my.domain.com
kind: ConfigMap
metadata:
creationTimestamp: null
name: test-configmap
More details:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.