Configure credentials for AWS Elastic Container Registry using registry-creds addon

How to configure credentials for AWS ECR using the registry-creds addon for a minikube cluster

Overview

The minikube registry-creds addon enables developers to setup credentials for pulling images from AWS ECR from inside their minikube cluster.

The addon automagically refreshes the service account token for the default service account in the default namespace.

Prerequisites

  • a working minikube cluster
  • a container image in AWS ECR that you would like to use
  • AWS access keys that can be used to pull the above image
  • AWS account number of the account hosting the registry

Configuring and enabling the registry-creds addon

Configure the registry-creds addon

Configure the minikube registry-creds addon with the following command:

Note: In this tutorial, we will focus only on the AWS ECR.

  1. minikube addons configure registry-creds

Follow the prompt and enter y for AWS ECR. Provide the requested information. It should look like this -

  1. $ minikube addons configure registry-creds
  2. Do you want to enable AWS Elastic Container Registry? [y/n]: y
  3. -- Enter AWS Access Key ID: <put_access_key_here>
  4. -- Enter AWS Secret Access Key: <put_secret_access_key_here>
  5. -- (Optional) Enter AWS Session Token:
  6. -- Enter AWS Region: us-west-2
  7. -- Enter 12 digit AWS Account ID (Comma separated list): <account_number>
  8. -- (Optional) Enter ARN of AWS role to assume:
  9. Do you want to enable Google Container Registry? [y/n]: n
  10. Do you want to enable Docker Registry? [y/n]: n
  11. Do you want to enable Azure Container Registry? [y/n]: n
  12. registry-creds was successfully configured

Enable the registry-creds addon

Enable the minikube registry-creds addon with the following command:

  1. minikube addons enable registry-creds

Create a deployment that uses an image in AWS ECR

This tutorial will use a vanilla alpine image that has been already uploaded into a repository in AWS ECR.

Let’s use this alpine deployment that is setup to use the alpine image from ECR. Make sure you update the image field with a valid URI.

alpine-deployment.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: alpine-deployment
  5. labels:
  6. app: alpine
  7. spec:
  8. replicas: 1
  9. selector:
  10. matchLabels:
  11. app: alpine
  12. template:
  13. metadata:
  14. labels:
  15. app: alpine
  16. spec:
  17. containers:
  18. - name: alpine
  19. image: <<aws_account_number>>.dkr.ecr.<<aws_region>>.amazonaws.com/alpine:latest
  20. command: ['sh', '-c', 'echo Container is Running ; sleep 3600']

Create a file called alpine-deployment.yaml and paste the contents above. Be sure to replace «aws_account_number» and «aws_region» with your actual account number and aws region. Then create the alpine deployment with the following command:

  1. kubectl apply -f alpine-deployment.yaml

Test your deployment

Describe the pod and verify the image pull was successful:

  1. kubectl describe pods << alpine-deployment-pod-name >>

You should see an event like this:

  1. Successfully pulled image "<<account_number>>.dkr.ecr.<<aws_region>>.amazonaws.com/alpine:latest"

If you do not see that event, look at the troubleshooting section.

Review

In the above tutorial, we configured the registry-creds addon to refresh the credentials for AWS ECR so that we could pull private container images onto our minikube cluster. We ultimately created a deployment that used an image in a private AWS ECR repository.

Troubleshooting

  • Check if you have a secret called awsecr-cred in the default namespace by running kubectl get secrets.
  • Check if the image path is valid.
  • Check if the registry-creds addon is enabled by using minikube addons list.

Caveats

The service account token for the default service account in the default namespace is kept updated by the addon. If you create your deployment in a different namespace, the image pull will not work.

Last modified November 14, 2020: Fix whitespace issues in the site content markdown (ebf37ad15)