<p>A colleague created a K8s cluster for me. I can run services in that cluster without any problem. However, I cannot run services that depend on an image from Amazon ECR, which I really do not understand. Probably, I made a small mistake in my deployment file and thus caused this problem.</p>
<p>Here is my deployment file:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-deployment
  labels:
    app: hello
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
      - name: hello
        image: xxxxxxxxx.yyy.ecr.eu-zzzzz.amazonaws.com/test:latest
        ports:
        - containerPort: 5000
</code></pre>
<p>Here is my service file:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: hello-svc
  labels:
    app: hello
spec:
  type: NodePort
  ports:
  - port: 5000
    nodePort: 30002
    protocol: TCP
  selector:
    app: hello
</code></pre>
<p>On the master node, I have run this to ensure kubernetes knows about the deployment and the service.</p>
<pre><code>kubectl create -f dep.yml
kubectl create -f service.yml
</code></pre>
<p>I used the <a href="https://marketplace.visualstudio.com/items?itemName=ms-kubernetes-tools.vscode-kubernetes-tools" rel="nofollow noreferrer">K8s extension in vscode</a> to check the logs of my pods.</p>
<p>This is the error I get:</p>
<blockquote>
<p>Error from server (BadRequest): container &quot;hello&quot; in pod
&quot;hello-deployment-xxxx-49pbs&quot; is waiting to start: trying and failing
to pull image.</p>
</blockquote>
<p>Apparently, pulling is an issue..... This is not happening when using a public image from the public docker hub. Logically, this would be a rights issue. But looks like it is not. I get no error message when running this command on the master node:</p>
<pre><code>docker pull xxxxxxxxx.yyy.ecr.eu-zzzzz.amazonaws.com/test:latest
</code></pre>
<p>This command just pulls my image.</p>
<p>I am confused now. I can pull my image with <code>docker pull</code> on the master node . But K8s fails doing the pull. Am I missing something in my deployment file? Some property that says: &quot;repositoryIsPrivateButDoNotComplain&quot;? I just do not get it.</p>
<p>How to fix this so K8s can easily use my image from Amazon ECR?</p>

Daan
<p>You should create and use secretes for the ECR authorization.</p>
<p>This is what you need to do.</p>
<ol>
<li>Create a secrete for the Kubernetes cluster, execute the below-given shell script from a machine from where you can access the AWS account in which ECR registry is hosted. Please change the placeholders as per your setup. Please ensure that the machine on which you execute this shell script should have aws cli installed and aws credential configured. If you are using a windows machine then execute this script in Cygwin or git bash console.</li>
</ol>
<pre><code>#!/bin/bash
ACCOUNT=&lt;AWS_ACCOUNT_ID&gt;
REGION=&lt;REGION&gt;
SECRET_NAME=&lt;SECRETE_NAME&gt;
EMAIL=&lt;SOME_DUMMY_EMAIL&gt;

TOKEN=`/usr/local/bin/aws ecr --region=$REGION --profile &lt;AWS_PROFILE&gt; get-authorization-token --output text --query authorizationData[].authorizationToken | base64 -d | cut -d: -f2`

kubectl delete secret --ignore-not-found $SECRET_NAME
kubectl create secret docker-registry $SECRET_NAME \
 --docker-server=https://${ACCOUNT}.dkr.ecr.${REGION}.amazonaws.com \
 --docker-username=AWS \
 --docker-password=&quot;${TOKEN}&quot; \
 --docker-email=&quot;${EMAIL}&quot;
</code></pre>
<ol start="2">
<li>Change the deployment and add a section for secrete which you're pods will be using while downloading the image from ECR.</li>
</ol>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-deployment
  labels:
    app: hello
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
      - name: hello
        image: xxxxxxxxx.yyy.ecr.eu-zzzzz.amazonaws.com/test:latest
        ports:
        - containerPort: 5000
      imagePullSecrets:
        - name: SECRET_NAME
</code></pre>
<ol start="3">
<li><p>Create the pods and service.</p>
</li>
<li><p>IF it succeeds, then still the secret will expire in 12 hours, to overcome that setup a crone ( for recreating the secretes on the Kubernetes cluster periodically. For setting up crone use the same script which is given above.</p>
</li>
</ol>
<p>For the complete picture of how it is happening under the hood please refer to below diagram.</p>
<p><a href="https://i.stack.imgur.com/yOxu5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yOxu5.png" alt="enter image description here" /></a></p>
<p>Regards
Amit Meena</p>
