<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sajjad Zaheri on Medium]]></title>
        <description><![CDATA[Stories by Sajjad Zaheri on Medium]]></description>
        <link>https://medium.com/@sajjadzaheri?source=rss-4a67adfd699f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NmRr9ZU7D9M6DuOu9_KcEA.jpeg</url>
            <title>Stories by Sajjad Zaheri on Medium</title>
            <link>https://medium.com/@sajjadzaheri?source=rss-4a67adfd699f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 21:09:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sajjadzaheri/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Authenticate AWS ECR on Any Kubernetes Cluster — The Right Way]]></title>
            <link>https://medium.com/@sajjadzaheri/how-to-authenticate-aws-ecr-on-any-kubernetes-cluster-the-right-way-26b6ee190125?source=rss-4a67adfd699f------2</link>
            <guid isPermaLink="false">https://medium.com/p/26b6ee190125</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[ecr]]></category>
            <dc:creator><![CDATA[Sajjad Zaheri]]></dc:creator>
            <pubDate>Fri, 19 Jan 2024 10:24:28 GMT</pubDate>
            <atom:updated>2024-01-19T10:24:28.930Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Authenticate AWS ECR on Any Kubernetes Cluster — The Right Way</h3><p>Amazon Elastic Container Registry (Amazon ECR) is a fully managed container registry offering high-performance hosting that can be easily integrated with other Amazon services such as EKS without configuring any extra authentication. To use private container registries like ECR on an on-premise environment, Docker needs to be authenticated with ECR.</p><p>You could use the Docker <a href="https://docs.docker.com/engine/reference/commandline/login/">login</a> command to generate the credentials for you and then use them as <a href="https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/">imagePullSecrets</a>, but the problem is that Docker credentials are valid for 12 hours. So, it should be renewed every 12 hours to get it working.</p><p>I’ve been researching this for a while and there were methods using CronJob to renew docker credentials every 12 hours, but it didn’t seem a nice way to me. I wanted a cleaner way. So, after some digging, I’ve found the right way to do this!</p><p>I will be using K3s with K3sup but this approach applies to all Kubernetes distros. you just need to find a way to pass values into the Kubelet.</p><h3>Kubernetes Credential Provider</h3><blockquote>Starting from Kubernetes v1.20, the kubelet can dynamically retrieve credentials for a container image registry using exec plugins.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OOaKeijnISncckd1FlqWKw.png" /><figcaption>Kubelet Credential Provider Mechanism by <a href="https://kubernetes.io/blog/2022/12/22/kubelet-credential-providers/">Kubernetes</a></figcaption></figure><p>So, this is how it’s going: Every time the Kubelet needs to pull an image, it will ask the credential provider executable to provide credentials. The executable will manage to fetch necessary (and fresh!) credentials and pass them to the Kubelet. Isn’t it great?!</p><p>You can even write your own credential provider but fortunately, the Kubernetes team created the executable for AWS ECR and we can use it right away.</p><h3>Authentication with ECR</h3><p>For the AWS Credential Provider to be able to fetch new credentials, the VPS needs to be authenticated with ECR. Kubernetes uses the <a href="https://docs.aws.amazon.com/sdkref/latest/guide/standardized-credentials.html">AWS SDK credential chain</a> to locate the credential. As I’ve been using AWS EC2 instance for my K8s, I’ve done the authentication using Instance Profiles but you can use whatever suits you. The easiest method may be authenticating through creating <em>~/.aws/config</em><strong> </strong>file and pasting your access key and secret key as below:</p><pre>[default]<br>aws_access_key_id = AKIAIOSFODNN7EXAMPLE<br>aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY</pre><h3>Setting Up K3s</h3><p>The process of setting up K3s using K3sup is very easy. First, install K3sup using the below commands:</p><pre>curl -sLS https://get.k3sup.dev | sh<br>sudo install k3sup /usr/local/bin/</pre><p>Then, let’s set some required variables:</p><pre>export IP=PASTE_IP_HERE<br>export USERNAME=root<br>export K3S_VERSION=v1.29.0+k3s1</pre><p>You need to replace the username and IP of the VPS.</p><p>Create a file named <em>credential-provider-config.yaml</em> and paste the following:</p><pre>apiVersion: kubelet.config.k8s.io/v1<br>kind: CredentialProviderConfig<br>providers:<br>  - name: ecr-credential-provider<br>    matchImages:<br>      - &quot;*.dkr.ecr.*.amazonaws.com&quot;<br>    defaultCacheDuration: &quot;12h&quot;<br>    apiVersion: credentialprovider.kubelet.k8s.io/v1</pre><p>Notice the <em>matchImages </em>section. you can declare whatever image address patterns that need to be authenticated through the credential provider.</p><p>Then, download the credential provider from <a href="https://console.cloud.google.com/storage/browser/_details/k8s-artifacts-prod/binaries/cloud-provider-aws/v1.29.0/linux/amd64/ecr-credential-provider-linux-amd64?pageState=(%22StorageObjectListTable%22:(%22f%22:%22%255B%255D%22))">here</a> and name it <em>ecr-credential-provider</em>.</p><p>Now, you should copy these files into the VPS and make <em>ecr-credential-provider</em> executable:</p><pre>ssh $USERNAME@$IP &quot;mkdir /home/$USERNAME/.kube&quot;<br>scp ./credential_provider_config.yaml $USERNAME@$IP:/home/$USERNAME/.kube/<br>ssh $USERNAME@$IP &quot;mkdir /home/$USERNAME/.aws&quot;<br>scp ./ecr-credential-provider $USERNAME@$IP:/home/$USERNAME/.aws/<br>ssh $USERNAME@$IP &quot;chmod +x /home/$USERNAME/.aws/ecr-credential-provider&quot;</pre><p>We are all set up! now it’s time to install K3s:</p><pre>k3sup install --ip $IP --user $USERNAME --k3s-version $K3S_VERSION --k3s-extra-args &quot;--node-external-ip $IP --image-credential-provider-config /home/$USERNAME/.kube/k8s_credential_provider_config.yaml --image-credential-provider-bin-dir /home/$USERNAME/.aws&quot;</pre><p>And BAAM! Your K3s cluster is ready to pull your private images!</p><p>You can read the source code here: <a href="https://github.com/sajjzah/k8s-ecr-sample">https://github.com/sajjzah/k8s-ecr-sample</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=26b6ee190125" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>