how to pass environment variable in kubectl deployment?

I used envsubst (https://www.gnu.org/software/gettext/manual/html_node/envsubst-Invocation.html) for this. Create a deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: $NAME
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

Then:

export NAME=my-test-nginx
envsubst < deployment.yaml | kubectl apply -f -

Not sure what OS are you using to run this. On macOS, envsubst installed like:

brew install gettext
brew link --force gettext 

Leave a Comment