Search code examples
kuberneteskubectl

Creating secrets from env file configured to a certain namespace


So when creating secrets I often will use:

kubectl create secret generic super-secret --from-env-file=secrets

However, I wanted to move this to a dedicated secrets.yaml file, of kind "Secret" as per the documentation: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#-em-secret-generic-em-

However, to do this, I need to base64 encode all of the secrets. Wha! This is bad imho. Why can't kubectl handle this for us? (Another debate for another day).

So, I am back to using the kubectl create secret command which works really well for ease of deployment - however, I am wondering what the process is to apply said creation of secret to a specific namespace? (Again, this probably would involve something tricky to get things to work - but why it's not a standard feature yet is a little worrying?)


Solution

  • Kubectl's standard options include a --namespace or -n option*, which can achieve this with zero fuss:

    kubectl create secret generic super-secret --from-env-file=secrets --namespace=whatever_namespace_you want
    

    *This is not included or mentioned in the documentation for creating a secret, explicitly. It's assumed knowledge for that section.