<p>How to add a service_account.json file to kubernetes secrets? I tried</p>
<p><code>kubectl create secret generic service_account.json -n sample --from-file=service_account=service_account.json</code></p>
<p>but it returns an error failed to create secret Secret &quot;service_account.json&quot; is invalid: metadata.name</p>

Tony Stark
<p>You can't use <code>service_account.json</code> as the (metadata) name for a Kubernetes resource. Here's the documentation on permitted <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/names/" rel="nofollow noreferrer">Object Names and IDs</a></p>
<p>You can use:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl create secret generic foo \
--namespace=sample \
--from-file=key.json=service_account.json
</code></pre>
<blockquote>
<p><strong>NOTE</strong> The secret is called <code>foo</code> and it creates a key called <code>key.json</code> whose value is the content of the file called <code>service_account.json</code>.</p>
</blockquote>
<blockquote>
<p><strong>NOTE</strong> If you don't wish to rename the object name in the secret, you can omit it; I renamed the file <code>service_account.json</code> to <code>key.json</code> in the secret. To retain the original name, just use <code>--from-file=service_account.json</code>.</p>
</blockquote>
<p>You should then able to volume mount the secret in the Container where you need to use it:</p>
<pre><code>apiVersion: v1
kind: Namespace
metadata:
  labels:
    control-plane: controller-manager
  name: system
---
apiVersion: apps/v1
kind: Deployment
metadata: {}
spec:
  template:
    spec:
      containers:
        - name: my-container
          volumeMounts:
            - name: bar
              mountPath: /secrets
      volumes:
        - name: bar
          secret:
            secretName: foo
</code></pre>
<blockquote>
<p><strong>NOTE</strong> The container can access the <code>foo</code> secret's content as <code>/secrets/key.json</code>.</p>
</blockquote>
<p>Intentionally distinct names <code>foo</code>, <code>bar</code> etc. for clarity</p>
