referencing a file as configmap in k8s

10/15/2021

Alright, so I have a json file with google cloud credentials. I want to add it to my cluster as a secret so right now I have this

apiVersion: v1
kind: Secret
metadata:
  name: google-credentials
stringData:
  credentials.json: |-
    <GOOGLE-CREDENTIALS>

And I am doing some sed hacks to try and get it to look right, but I am having tons of issues with it.

So I wonder: is it possible to reference a external file in your deployment.yaml?

And to be clear I have found out that I could do this

kubectl create configmap game-config --from-file=configure-pod-container/configmap/

But I would strongly prefer it to be in one command, i.e. kubectl apply -f deployment.yaml

-- munHunger
kubernetes
kubernetes-secrets

1 Answer

10/15/2021

You can create the configmap or secret storing the whole file inside and you can inject it to deployment.

if you have issue with multiple command you can merge the YAML files simply by --- so in single file your configmap will be there and deployment also.

You can apply this YAML file and your both resources will get created.

Something like :

apiVersion: v1
kind: Secret
metadata:
  name: mock
spec:
---
apiVersion: v1
kind: Deployment
metadata:
  name: mock
spec:

Read more at : https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#create-configmaps-from-files

OR

So I wonder: is it possible to reference a external file in your deployment.yaml?

If you have concerns merging files & creating configmap, if your file is external

you can create the Init container into deployment which will run before the main container start and set the file at its place.

If you don't want an init container also you can run command into deployment which will first download a file from a bucket of external place.

https://kubernetes.io/docs/concepts/workloads/pods/init-containers/

-- Harsh Manvar
Source: StackOverflow