How to set kubernetes secret key name when using --from-file other than filename?

4/30/2020

Is there a way to set a kubernetes secret key name when using --from-file other than the filename?

I have a bunch of different configuration files that I use as secrets.json within my containers. However, to organize my files, none of them are named secrets.json on my host. For example secrets.dev.json or secrets.test.json. My apps only know to read in secrets.json.

When I create a secret with kubectl create secret generic my-app-secrets --from-file=secrets.dev.json, this results in the key name being secrets.dev.json and not secrets.json.

I'm mounting in my secret contents as a file (this is a carry-over from migrating from Docker swarm).

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  template:
    spec:
      volumes:
      - name: my-secret
        secret:
          secretName: my-app-secrets
      containers:
        - name: my-app
          volumeMounts:
            - name: my-secret
              mountPath: "/run/secrets/secrets.json"
              subPath: "secrets.json"

Because secrets.json doesn't exist as a key because it used the filename (secrets.dev.json), it ends up getting turned into a directory instead. I end up getting this mount path: /run/secrets/secrets.json/secrets.dev.json.

I'd like to be able to set the key name to secrets.json instead of using the filename of secrets.dev.json.

-- Joel Magnuson
docker-secrets
kubernetes
kubernetes-secrets

1 Answer

4/30/2020

You can specify key name [--from-file=[key=]source]

kubectl create secret generic my-app-secrets --from-file=secrets.json=secrets.dev.json

Here, secrets.json is key name and secrets.dev.json is source

-- hoque
Source: StackOverflow