skip to Main Content

How do I use env variable defined inside deployment? For example, In yaml file dow below I try to use env CONT_NAME for setting container name, but it does not succeed. Could you help please with it, how to do it?

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

3

Answers


  1. You can’t use variables to set values inside deployment natively. If you want to do that you have to process the file before executing the kubectl, take a look at this post. The best option to do this which it’s focused on parametrize and standardize deployments is to use Helm

    Login or Signup to reply.
  2. In those situations I just replace the $CONT_NAME in the yaml with the correct value right before applying the yaml.

    sed -ie "s/$COUNT_NAME/$COUNT_NAME/g" yourYamlFile.yaml

    Login or Signup to reply.
  3. If your using fluxcd, it has the ability to do variable interpolation link

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search