Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubectl run with env var from a secret config

Tags:

kubernetes

How can I issue a kubectl run that pulls an environment var from a k8s secret configmap?

Currently I have:

kubectl run oneoff -i --rm NAME --image=IMAGE --env SECRET=foo

like image 358
Alex Flint Avatar asked Sep 24 '18 22:09

Alex Flint


People also ask

How do I pass an env file in Kubernetes?

There are two ways to define environment variables with Kubernetes: by setting them directly in a configuration file, from an external configuration file, using variables, or a secrets file. This tutorial shows both options, and uses the Humanitec getting started application used in previous tutorials.

How do I get data from secret Kubernetes?

If you want to access data from a Secret in a Pod, one way to do that is to have Kubernetes make the value of that Secret be available as a file inside the filesystem of one or more of the Pod's containers. To configure that, you: Create a secret or use an existing one. Multiple Pods can reference the same secret.

How do you pass environment variables in pod?

When you create a Pod, you can set dependent environment variables for the containers that run in the Pod. To set dependent environment variables, you can use $(VAR_NAME) in the value of env in the configuration file.


2 Answers

Look into the overrides flag of the run command... it reads as:

An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field.

https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#run

So in your case I guess it would be something like:

kubectl run oneoff -i --rm --overrides='
{
  "spec": {
    "containers": [
      {
        "name": "oneoff",
        "image": "IMAGE",
        "env": [
          {
            "name": "ENV_NAME"
            "valueFrom": {
              "secretKeyRef": {
                "name": "SECRET_NAME",
                "key": "SECRET_KEY"
              }
            }
          }
        ]
      }
    ]
  }
}
'  --image= IMAGE
like image 144
Charlino Avatar answered Nov 15 '22 07:11

Charlino


This is another one that does the trick:

 kubectl run oneoff -i --rm NAME --image=IMAGE --env SECRET=$(kubectl get secret your-secret -o=jsonpath="{.server['secret\.yml']}")
like image 29
Rico Avatar answered Nov 15 '22 09:11

Rico