<p>I want to store files in Kubernetes Secrets but I haven't found how to do it using a <code>yaml</code> file.</p>

<p>I've been able to make it using the cli with <code>kubectl</code>:</p>

<pre><code>kubectl create secret generic some-secret --from-file=secret1.txt=secrets/secret1.txt
</code></pre>

<p>But when I try something similar in a <code>yaml</code>:</p>

<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: some-secret
type: Opaque
data:
  secret1.txt: secrets/secret1.txt
</code></pre>

<p>I´ve got this error:</p>

<pre><code>[pos 73]: json: error decoding base64 binary 'assets/elasticsearch.yml': illegal base64 data at input byte 20
</code></pre>

<p>I'm following this guide <a href="http://kubernetes.io/docs/user-guide/secrets/" rel="noreferrer">http://kubernetes.io/docs/user-guide/secrets/</a>. It explains how to create a secret using a <code>yaml</code> but not how to create a secret from a <strong>file</strong> using <code>yaml</code>.</p>

<p>Is it possible? If so, how can I do it?</p>

<p>As answered on previous post, we need to provide the certificate/key encoded as based64 to the file. </p>

<p>Here is generic example for a certiticate (in this case SSL):</p>

<p>The <code>secret.yml.tmpl</code>:</p>

<pre><code>    apiVersion: v1    

    kind: Secret
    metadata:
         name: test-secret
         namespace: default
    type: Opaque
    data:
        server.crt: SERVER_CRT
        server.key: SERVER_KEY
</code></pre>

<p>Pre-process the file to include the certificate/key:</p>

<pre><code>sed "s/SERVER_CRT/`cat server.crt|base64 -w0`/g" secret.yml.tmpl | \
sed "s/SERVER_KEY/`cat server.key|base64 -w0`/g" | \
kubectl apply -f -
</code></pre>

<p>Note that the certificate/key are encoded using base64 without whitespaces (-w0).</p>

<p>For the TLS can be simply:</p>

<pre><code>kubectl create secret tls test-secret-tls --cert=server.crt --key=server.key
</code></pre>
