<p>I'm trying to get Kubernetes to download images from a Google Container Registry from another project. According to the <a href="http://kubernetes.io/docs/user-guide/images/#specifying-imagepullsecrets-on-a-pod">docs</a> you should create an image pull secret using: </p>

<pre><code>$ kubectl create secret docker-registry myregistrykey --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL
</code></pre>

<p>But I wonder what <code>DOCKER_USER</code> and <code>DOCKER_PASSWORD</code> I should use for authenticating with Google Container Registry? Looking at the <a href="https://cloud.google.com/container-registry/docs/auth">GCR docs</a> it says that the password is the access token that you can get by running:</p>

<pre><code>$ gcloud auth print-access-token
</code></pre>

<p>This actually works... for a while. The problem seems to be that this access token expires after (what I believe to be) one hour. I need a password (or something) that doesn't expire when creating my image pull secret. Otherwise the Kubernetes cluster can't download the new images after an hour or so. What's the correct way to do this?</p>

<p>This is really tricky but after a lot of trail and error I think I've got it working.</p>
<ol>
<li><p>Go to the Google Developer Console &gt; Api Manager &gt; Credentials and click &quot;Create credentials&quot; and create a &quot;service account key&quot;</p>
</li>
<li><p>Under &quot;service account&quot; select new and name the new key &quot;gcr&quot; (let the key type be json)</p>
</li>
<li><p>Create the key and store the file on disk (from here on we assume that it was stored under <code>~/secret.json</code>)</p>
</li>
<li><p>Now login to GCR using Docker from command-line:</p>
<p><code>$ docker login -e your@email.se -u _json_key -p &quot;$(cat ~/secret.json)&quot; https://eu.gcr.io</code></p>
</li>
</ol>
<p>This will generate an entry for &quot;https://eu.gcr.io&quot; in your <code>~/.docker/config.json</code> file.
6. Copy the JSON structure under &quot;https://eu.gcr.io&quot; into a new file called &quot;~/docker-config.json&quot;, remove newlines! For example:</p>
<pre><code></code></pre>
<ol start="7">
<li><p>Base64 encode this file:</p>
<p><code>$ cat ~/docker-config.json | base64</code></p>
</li>
<li><p>This will print a long base64 encoded string, copy this string and paste it into an image pull secret definition (called <code>~/pullsecret.yaml</code>):</p>
</li>
</ol>
<blockquote>
<pre><code>apiVersion: v1
  kind: Secret
  metadata:
    name: mykey
  data:
    .dockercfg: &lt;paste base64 encoded string here&gt;
  type: kubernetes.io/dockercfg
</code></pre>
</blockquote>
<ol start="9">
<li>Now create the secret:</li>
</ol>
<p><code>$ kubectl create -f ~/pullsecret.yaml</code>
10. Now you can use this pull secret from a pod, for example:</p>
<blockquote>
<pre><code>apiVersion: v1
kind: Pod
metadata: 
  name: foo
  namespace: awesomeapps
spec: 
  containers: 
    - image: &quot;janedoe/awesomeapp:v1&quot;
      name: foo
  imagePullSecrets: 
    - name: mykey
</code></pre>
</blockquote>
<p>or add it to a <a href="http://kubernetes.io/docs/user-guide/service-accounts/#adding-imagepullsecrets-to-a-service-account" rel="nofollow noreferrer">service account</a>.</p>
