<p>I've created a secret with this command for one pair of public/private keys:</p>
<pre><code>kubectl create secret generic my-keys-secret --from-file=./public.key --from-file=./private.key
</code></pre>
<p>And used it in my pod configuration file:</p>
<pre><code>spec:
  volumes:
    - name: my-keys
      secret:
      secretName: my-keys-secret
</code></pre>
<pre><code>volumeMounts:
  - name: my-keys
    readOnly: true
    mountPath: &quot;/keys&quot;
</code></pre>
<p>So the pod can access <code>keys/public.key</code> and <code>keys/private.key</code>.</p>
<p>But our new requirement is to support multiple pairs of public/private keys in this structure:</p>
<pre><code>.
└── keys
    ├── 1
    │   ├── private.key
    │   └── public.key
    .
    .
    │
    └── n
        ├── private.key
        └── public.key
</code></pre>
<p>Is it possible to create the secret with <code>kubectl create secret generic</code> cmd in the above structure? (the pod should be able to access <code>keys/n/public.key</code>and <code>keys/n/private.key</code>)</p>

Arya
<p>Yes, definintely. You need what is called a <code>generator</code>. The best one is <code>kustomize</code>. You can use it either as a standalone binary, or integrate it with kubectl.</p>
<p>You will simply create a <code>kustomization.yaml</code> file that will take certain <code>resource</code> directories and templates as input and generate a whole bunch of manifests as output. For multi-level directories, you will have a <code>kustomization.yaml</code> file per directory. These will all be consumed by the program in a sequence to generate a complete set of manifests for you. This you can directly apply to your cluster with:</p>
<pre><code>kubectl apply -k .
</code></pre>
<p>This command assumes your current directory has a <code>kustomization.yaml</code> file that you want to use and it will first generate all the manifests and then apply them. If you only want to generate them and not apply them, you can <code>--dry-run</code> your instruction and get <code>-o yaml</code> output and save it to a file, like this:</p>
<pre><code>kubectl apply -k . --dry-run=client -o yaml &gt; my-secrets.yaml
</code></pre>
<p>This will put all of your generated secret manifests in <code>my-secrets.yaml</code> which you can check for correctness when templating.</p>
<p>You can read more about templatization options on the <code>kustomize</code> docs <a href="https://kubectl.docs.kubernetes.io/guides/config_management/secrets_configmaps/" rel="nofollow noreferrer">here</a>. It's very intuitive and simple to use :)</p>
