Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass all values from multiple Secrets to env variables in Kubernetes?

Tags:

kubernetes

I have multiple Secrets in a Kubernetes. All of them contain many values, as example:

apiVersion: v1
kind: Secret
metadata:
  name: paypal-secret
type: Opaque
data:
  PAYPAL_CLIENT_ID: base64_PP_client_id
  PAYPAL_SECRET: base64_pp_secret
stringData:
  PAYPAL_API: https://api.paypal.com/v1
  PAYPAL_HOST: api.paypal.com

I'm curious how to pass all of the values from all Secrets to a ReplicaSet for example.

I tried this one approach:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: pp-debts
  labels:
    environment: prod
spec:
  replicas: 1
  selector:
    matchLabels:
      environment: prod
  template:
    metadata:
      labels:
        environment: prod
    spec:
      containers:
      - name: integration-app
        image: my-container-image
        envFrom:
        - secretRef:
          name: intercom-secret
        envFrom:
        - secretRef:
            name: paypal-secret
        envFrom:
        - secretRef:
            name: postgres-secret
        envFrom:
        - secretRef:
            name: redis-secret

But when I connected to the pod, and looked on the env variables, I was able to see only values from the redis-secret.

like image 445
Alex Fruzenshtein Avatar asked Nov 28 '18 11:11

Alex Fruzenshtein


People also ask

How do you make multiple secrets in Kubernetes?

Create Kubernetes Secrets 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 encode secrets 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.


Video Answer


1 Answers

Try using one envFrom with multiple entries under it as below:

      - name: integration-app
        image: my-container-image
        envFrom:
        - secretRef:
            name: intercom-secret
        - secretRef:
            name: paypal-secret
        - secretRef:
            name: postgres-secret
        - secretRef:
            name: redis-secret

There's an example at the bottom of this blog post by David Chua

like image 147
Ryan Dawson Avatar answered Sep 18 '22 11:09

Ryan Dawson