<p>I want to create a secret for my kubernetes cluster. So I composed following <code>dummy-secret.yaml</code> file:</p>

<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: dummy-secret
type: Opaque
data:
  API_KEY: bWVnYV9zZWNyZXRfa2V5
  API_SECRET: cmVhbGx5X3NlY3JldF92YWx1ZTE=
</code></pre>

<p>When I run <code>kubectl create -f dummy-secret.yaml</code> I receive back following message:</p>

<pre><code>Error from server (BadRequest): error when creating "dummy-secret.yaml": Secret in version "v1" cannot be handled as a Secret: v1.Secret: Data: decode base64: illegal base64 data at input byte 8, error found in #10 byte of ...|Q89_Hj1Aq","API_SECR|..., bigger context ...|sion":"v1","data":{"API_KEY":"af76fsdK_cQ89_Hj1Aq","API_SECRET":"bsdfmkwegwegwe"},"kind":"Secret","m|...
</code></pre>

<p>Not sure why it happens. </p>

<p>As I understood, I need to encode all values under the <code>data</code> key in the yaml file. So I did base64 encoding, but kubernetes still doesn't handle the yaml secret file as I expect.</p>

<p><strong>UPDATE:</strong></p>

<p>I used this command to encode <code>data</code> values on my mac:</p>

<pre><code>echo -n 'mega_secret_key' | openssl base64
</code></pre>

<p>I got the decoded values &quot;mega_secret_key&quot; and &quot;really_secret_value1&quot; from from your encoded data. Seems they are not encoded in right way. So, encode your data in right way:</p>
<pre><code>$ echo &quot;mega_secret_key&quot; | base64
bWVnYV9zZWNyZXRfa2V5Cg==

$ echo &quot;really_secret_value1&quot; | base64
cmVhbGx5X3NlY3JldF92YWx1ZTEK
</code></pre>
<p>Then check whether they are encoded properly:</p>
<pre><code>$ echo &quot;bWVnYV9zZWNyZXRfa2V5Cg==&quot; | base64 -d
mega_secret_key

$ echo &quot;cmVhbGx5X3NlY3JldF92YWx1ZTEK&quot; | base64 -d
really_secret_value1
</code></pre>
<p>So they are ok. Now use them in your <code>dummy-secret.yaml</code>:</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: dummy-secret
type: Opaque
data:
  API_KEY: bWVnYV9zZWNyZXRfa2V5Cg==
  API_SECRET: cmVhbGx5X3NlY3JldF92YWx1ZTEK
</code></pre>
<p>And run <code>$ kubectl create -f dummy-secret.yaml</code>.</p>
<hr />
<p><strong>UPDATE on 11-02-2022:</strong></p>
<p>The newer versions of Kubernetes support the optional <code>stringData</code> property where one can provide the value against any key without decoding.</p>
<blockquote>
<p>All key-value pairs in the <code>stringData</code> field are internally merged into the <code>data</code> field. If a key appears in both the <code>data</code> and the <code>stringData</code> field, the value specified in the <code>stringData</code> field takes precedence.</p>
</blockquote>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Secret
metadata:
  name: dummy-secret
type: Opaque
stringData:
  API_KEY: mega_secret_key
  API_SECRET: really_secret_value1
</code></pre>
<p><strong>UPDATE:</strong></p>
<p>If you use <code>-n</code> flag while running <code>$ echo &quot;some_text&quot;</code>, it will trim the trailing <code>\n</code> (newline) from the string you are printing.</p>
<pre class="lang-sh prettyprint-override"><code>$ echo &quot;some_text&quot;
some_text
$ echo -n &quot;some_text&quot;
some_text⏎
</code></pre>
<p>Just try it,</p>
<pre class="lang-sh prettyprint-override"><code># first encode
$ echo -n &quot;mega_secret_key&quot; | base64
bWVnYV9zZWNyZXRfa2V5
$ echo -n &quot;really_secret_value1&quot; | base64
cmVhbGx5X3NlY3JldF92YWx1ZTE=
# then decode and check whether newline is stripped
$ echo &quot;bWVnYV9zZWNyZXRfa2V5&quot; | base64 -d
mega_secret_key⏎
$ echo &quot;cmVhbGx5X3NlY3JldF92YWx1ZTE=&quot; | base64 -d
really_secret_value1⏎
</code></pre>
<p>You can use these newly (without newline) decoded data in your secret instead. That also should fine.</p>
<pre class="lang-sh prettyprint-override"><code>$ cat - &lt;&lt;-EOF | kubectl apply -f -
apiVersion: v1
kind: Secret
metadata:
  name: dummy-secret
type: Opaque
data:
  API_KEY: bWVnYV9zZWNyZXRfa2V5
  API_SECRET: cmVhbGx5X3NlY3JldF92YWx1ZTE=
EOF
secret/dummy-secret created
</code></pre>
<blockquote>
<p><strong>At the time of update, my kubernetes version is,</strong></p>
<pre class="lang-sh prettyprint-override"><code>Minor:&quot;17&quot;, GitVersion:&quot;v1.17.3&quot;,
GitCommit:&quot;06ad960bfd03b39c8310aaf92d1e7c1 2ce618213&quot;,
GitTreeState:&quot;clean&quot;, BuildDate:&quot;2020-02-11T18:14:22Z&quot;,
GoVersion:&quot;go1.13.6&quot;, Compiler:&quot;gc&quot;, Platform:&quot;l inux/amd64&quot;} Server
Version: version.Info{Major:&quot;1&quot;, Minor:&quot;17&quot;, GitVersion:&quot;v1.17.3&quot;,
GitCommit:&quot;06ad960bfd03b39c8310aaf92d1e7c1 2ce618213&quot;,
GitTreeState:&quot;clean&quot;, BuildDate:&quot;2020-02-11T18:07:13Z&quot;,
GoVersion:&quot;go1.13.6&quot;, Compiler:&quot;gc&quot;, Platform:&quot;l inux/amd64&quot;} ```
</code></pre>
</blockquote>
