<p>I can run this command to create a docker registry secret for a kubernetes cluster:</p>

<pre><code>kubectl create secret docker-registry regsecret \
--docker-server=docker.example.com \
--docker-username=kube \
--docker-password=PW_STRING \
--docker-email=my@email.com \
--namespace mynamespace 
</code></pre>

<p>I would like to create the same secret from a YAML file. Does anyone know how this can be set in a YAML file?</p>

<p>I need this as a YAML file so that it can be used as a Helm template, which allows for a Helm install command such as this (simplified) one:</p>

<pre><code>helm install ... --set docker.user=peter,docker.pw=foobar,docker.email=...
</code></pre>

Rotareti
<p>You can write that yaml by yourself, but it will be faster to create it in 2 steps using <code>kubectl</code>:</p>
<ol>
<li>Generate a 'yaml' file. You can use the same command but in dry-run mode and output mode <code>yaml</code>.</li>
</ol>
<p>Here is an example of a command that will save a secret into a 'docker-secret.yaml' file for <code>kubectl</code> version &lt; 1.18 (check the version by <code>kubectl version --short|grep Client</code>):</p>
<pre><code>kubectl create secret docker-registry --dry-run=true $secret_name \
  --docker-server=&lt;DOCKER_REGISTRY_SERVER&gt; \
  --docker-username=&lt;DOCKER_USER&gt; \
  --docker-password=&lt;DOCKER_PASSWORD&gt; \
  --docker-email=&lt;DOCKER_EMAIL&gt; -o yaml &gt; docker-secret.yaml
</code></pre>
<p>For <code>kubectl</code> version &gt;= 1.18:</p>
<pre><code>kubectl create secret docker-registry --dry-run=client $secret_name \
  --docker-server=&lt;DOCKER_REGISTRY_SERVER&gt; \
  --docker-username=&lt;DOCKER_USER&gt; \
  --docker-password=&lt;DOCKER_PASSWORD&gt; \
  --docker-email=&lt;DOCKER_EMAIL&gt; -o yaml &gt; docker-secret.yaml
</code></pre>
<ol start="2">
<li><p>You can apply the file like any other Kubernetes 'yaml':</p>
<p><code>kubectl apply -f docker-secret.yaml</code></p>
</li>
</ol>
<p><strong>UPD</strong>, as a question has been updated.</p>
<p>If you are using Helm, here is an official <a href="https://helm.sh/docs/howto/charts_tips_and_tricks/#creating-image-pull-secrets" rel="noreferrer">documentation</a> about how to create an <code>ImagePullSecret</code>.</p>
<p>From a doc:</p>
<ol>
<li>First, assume that the credentials are defined in the <code>values.yaml</code> file like so:</li>
</ol>
<pre><code>imageCredentials:
  registry: quay.io
  username: someone
  password: sillyness
</code></pre>
<ol start="2">
<li>We then define our helper template as follows:</li>
</ol>
<pre><code>{{- define &quot;imagePullSecret&quot; }}
{{- printf &quot;{\&quot;auths\&quot;: {\&quot;%s\&quot;: {\&quot;auth\&quot;: \&quot;%s\&quot;}}}&quot; .Values.imageCredentials.registry (printf &quot;%s:%s&quot; .Values.imageCredentials.username .Values.imageCredentials.password | b64enc) | b64enc }}
{{- end }}
</code></pre>
<ol start="3">
<li>Finally, we use the helper template in a larger template to create the <code>Secret</code> manifest:</li>
</ol>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: myregistrykey
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: {{ template &quot;imagePullSecret&quot; . }}
</code></pre>
