I have a next js app that I am trying to deploy to a kubernetes cluster as a deployment. Parts of the application contain axios http requests that reference an environment variable containing the value of a backend service.
If I am running locally, everything works fine, here is what I have in my .env.local file:
NEXT_PUBLIC_BACKEND_URL=http://localhost:8080
Anywhere in the app, I can successfully access this variable with process.env.NEXT_PUBLIC_BACKEND_URL.
When I create a kubernetes deployment, I try to inject that same env variable via a configMap and the variable shows as undefined.
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: my-site-frontend
name: my-site-frontend
spec:
replicas: 1
selector:
matchLabels:
app: my-site-frontend
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: my-site-frontend
spec:
containers:
- image: my-site:0.1
name: my-site
resources: {}
envFrom:
- configMapRef:
name: my-site-frontend
imagePullSecrets:
- name: dockerhub
configMap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-site-frontend
data:
NEXT_PUBLIC_BACKEND_URL: backend_service
When I run the deployment and expose the application via a nodePort, I see these environment variables as undefined in my browser console. All api calls to my backend_service (ClusterIP) fail as you can imagine.
I can see the env variable is present when I exec into the running pod.
my-mac:manifests andy$ k get pods
NAME READY STATUS RESTARTS AGE
my-site-frontend-77fb459dbf-d996n 1/1 Running 0 25m
---
my-mac:manifests andy$ k exec -it my-site-frontend-77fb459dbf-d996n -- sh
---
/app $ env | grep NEXT_PUBLIC
NEXT_PUBLIC_BACKEND_URL=backend_service
Any idea as to why the build process for my app does not account for this variable?
Thanks!