I have a config json file in my application. This is single json file which has all configs required for my nodejs application. I have many connections to many db'. I even have bifurcation like stage and production. I don't want to create secret for each variable. I want to dump my complete JSON as secret. So that i can directly pick it up from there and use in my application. Anyway i can achieve it? I think its a generic problem.
Asked
Active
Viewed 874 times
2 Answers
1
The simple solution is to put the entire config file under stringData
in the secret. Something like that:
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
stringData:
config.json: |
<your config goes here>
Now you can just mount the secret onto the pod , and you can read this config file.
While this work, it might be harder to use with GitOps solution, which usually requires you to encrypt the entire secret content. Using Kamus, you can create templated config file with encrypted values, in the following format:
apiVersion: v1
kind: ConfigMap
metadata:
name: encrypted-secrets-cm
data:
key: 4AD7lM6lc4dGvE3oF+5w8g==:WrcckiNNOAlMhuWHaM0kTw== //encrypted
template.ejs: |
<%- secrets["key"] %>
hello
And use the init container to create the config file with the decrypted values. The value with the encrypted items can be safely committed to git.

Omer Levi Hevroni
- 1,935
- 1
- 15
- 33
-
Does it encrypt the values? or is it plain text. – Hacker Apr 23 '20 at 13:55
-
Regular secrets are not encrypted, but base64 encoded. `stringData` is a simple synthetic sugar to save you from encoding the data. – Omer Levi Hevroni Apr 23 '20 at 13:56
-
you don't need to encode, Kubernetes will encode it for you – Omer Levi Hevroni Apr 23 '20 at 14:06
-
When i use as a environment variable, when i use in application do i get it back as JSON object or just string? – Hacker Apr 23 '20 at 17:17
1
You can use the following command:
kubectl -n <namespace> create cm secret --from-file=APPLICATION_CONFIG=./<your-config>
Mount that into certain path and you can read your config from those path.

irvifa
- 1,865
- 2
- 16
- 20