Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to source a `.env` file to create Kubernetes secrets?

Tags:

The docs states that

To create a Secret from one or more files, use --from-file. You specify files in any plaintext format, such as .txt or .env, as long as the files contain key-value pairs.

.test-secret

NAME=martin GENDER=male 

Testing to create a secret based on my .test-secret file.

kubectl create secret generic person --from-file .test-secret -o yml  $ kubectl get secret person -o yaml apiVersion: v1 data:   .test-secret: TkFNRT1tYXJ0aW4KR0VOREVSPW1hbGUK kind: Secret metadata:   creationTimestamp: 2018-07-19T09:23:05Z   name: person   namespace: default   resourceVersion: "229992"   selfLink: /api/v1/namespaces/default/secrets/person   uid: 579198ab-8b35-11e8-8895-42010a840008 type: Opaque 

Is it possible to read a list of key / values like that? Is it even possible to do so from an .env file? kubectl get pods returns CreateContainerConfigError

my-app.yml

 77             - name: NAME  78               valueFrom:  79                 secretKeyRef:  80                   name: person  81                   key: NAME 
like image 572
martins Avatar asked Jul 19 '18 09:07

martins


People also ask

How do you create a Kubernetes secret from a file?

To create a Kubernetes secret, apply one of the following methods: Use kubectl for a command-line based approach. Create a configuration file for the secret. Use a generator, such as Kustomize to generate the secret.

How do you use Kubernetes secret as an environment variable?

To use a Secret in an environment variable in a Pod: Create a Secret (or use an existing one). Multiple Pods can reference the same Secret. Modify your Pod definition in each container that you wish to consume the value of a secret key to add an environment variable for each secret key you wish to consume.

Which file system do Secrets use in Kubernetes?

When using definition files, you can add the data in a base64 encoded format or plain text form. Kubernetes encodes the Secret data in base64 format. When you need to reveal a Secret text, you must base64-decode it. To enable containers to access Secrets, you have the option to mount the Secret as a volume.


1 Answers

Yes, use the option --from-env-file

kubectl create secret generic person --from-env-file=.test-secret 

To consume the secrets from the initial .env file in a pod, you can use the following :

apiVersion: v1 kind: Pod metadata:   name: some-meta spec:   containers:   - name: xyz     image: abc     envFrom:     - secretRef:         name: person # <-- 
like image 114
Ignacio Millán Avatar answered May 21 '23 11:05

Ignacio Millán