how to pass environment variable in kubectl deployment?

前端 未结 6 478
失恋的感觉
失恋的感觉 2020-12-20 12:44

I am setting up the kubernetes setup for django webapp.

I am passing environment variable while creating deployment as below

kubectl create -f deploy         


        
相关标签:
6条回答
  • 2020-12-20 12:56

    This isn't a right way to use the deployment, you can't provide half details in yaml and half in kubectl commands. If you want to pass environment variables in your deployment you should add those detail in the deployment spec.template.spec:

    You should add following block to your deployment.yaml

    spec:
      containers:
      - env:
        - name: var1
          value: val1
    

    This will export your environment variables inside the container.

    The other way to export the environment variable is use kubectl run (not advisable) as it is going to be depreciated very soon. You can use following command:

     kubectl run nginx --image=nginx --restart=Always --replicas=1 --env=var1=val1
    

    The above command will create a deployment nginx with replica 1 and environment variable var1=val1

    0 讨论(0)
  • 2020-12-20 12:57

    Please try following

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      namespace: kdpd00201
      name: frontend
      labels:
        app: nginx
    spec:
      replicas: 6
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: frontend
            image: ifccncf/nginx:1.14.2
            ports:
            - containerPort: 8001
            env:
               - name: NGINX_PORT
                 value: "8001"
    
    0 讨论(0)
  • 2020-12-20 12:58

    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 
    
    0 讨论(0)
  • 2020-12-20 12:58

    Follow the below steps

    create test-deploy.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: MYAPP
      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
    

    using sed command you can update the deployment name at deployment time

    sed -e 's|MYAPP|my-nginx|g' test-deploy.yaml | kubectl apply -f -
    
    0 讨论(0)
  • 2020-12-20 12:59

    You cannot pass variables to "kubectl create -f". YAML files should be complete manifests without variables. Also you cannot use "-l" flag to "kubectl create -f".

    If you want to pass environment variables to pod you should do like that:

    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: nginx
            image: nginx:1.7.9
            env:
            - name: MY_VAT
              value: MY_VALUE
            ports:
            - containerPort: 80
    

    Read more here: https://kubernetes.io/docs/tasks/inject-data-application/define-environment-variable-container/

    0 讨论(0)
  • 2020-12-20 13:07

    File: ./deployment.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: MYAPP
      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
    

    File: ./service.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: MYAPP
      labels:
        app: nginx
    spec:
      type: ClusterIP
      ports:
      - port: 80
      selector:
        app: nginx
    

    File: ./kustomization.yaml

    resources:
    - deployment.yaml
    - service.yaml
    

    If you're using https://kustomize.io/, you can do this trick in a CI:

    sh '( echo "images:" ; echo "  - name: $IMAGE" ; echo "    newTag: $VERSION" ) >> ./kustomization.yaml'
    sh "kubectl apply --kustomize ."
    
    0 讨论(0)
提交回复
热议问题