Skip to content

NimTechnology

Trình bày các công nghệ CLOUD một cách dễ hiểu.

  • Kubernetes & Container
    • Docker
    • Kubernetes
      • Ingress
      • Pod
    • Helm Chart
    • OAuth2 Proxy
    • Isito-EnvoyFilter
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Vault
    • Longhorn – Storage
    • VictoriaMetrics
    • MetalLB
    • Kong Gateway
  • CI/CD
    • ArgoCD
    • ArgoWorkflows
    • Argo Events
    • Spinnaker
    • Jenkins
    • Harbor
    • TeamCity
    • Git
      • Bitbucket
  • Coding
    • DevSecOps
    • Terraform
      • GCP – Google Cloud
      • AWS – Amazon Web Service
      • Azure Cloud
    • Golang
    • Laravel
    • Python
    • Jquery & JavaScript
    • Selenium
  • Log, Monitor & Tracing
    • DataDog
    • Prometheus
    • Grafana
    • ELK
      • Kibana
      • Logstash
  • BareMetal
    • NextCloud
  • Toggle search form

[AWS] Pull images from ECR

Posted on October 10, 2022October 10, 2022 By nim No Comments on [AWS] Pull images from ECR

Contents

Toggle
  • 1) EKS pull image from ECR.
  • 2) Using imagePullSecrets to pull images from ECR.

1) EKS pull image from ECR.

Chúng ta đã có 1 post hướng dẫn cài EKS thông qua terraform.

[AWS] Create EKS Cluster and EKS Node Groups in Public and Private Subnets

Sau khi đã cài xong EKS thì bạn kiểm tra 1 con Worker (EC2)

Bạn thấy con EC2 đã được gắn sẵn 1 role.
Bạn có thể thấy là Role mà được add vào con worker node
có 1 policy: AmazonEC2ContainerRegistryReadOnly
Action là được pull tất các image trên ECR cùng Account AWS.

Bạn có thể tham khảo thêm link này;
https://devopstales.github.io/home/aws-eks-ecr/

Verify ECR Access to EKS Worker Nodes

  • Go to Services -> EC2 -> Running Instances > Select a Worker Node -> Description Tab
  • Click on value in IAM Role field Role name
  • In IAM on that specific role, verify permissions tab
  • Policy with name AmazonEC2ContainerRegistryReadOnly, AmazonEC2ContainerRegistryPowerUser should be associated

2) Using imagePullSecrets to pull images from ECR.

https://skryvets.com/blog/2021/03/15/kubernetes-pull-image-from-private-ecr-registry/

Bạn có thẻ gen thử secret “docker-registry” để test thử imagePullSecrets

kubectl create secret docker-registry regcred \
  --docker-server=250887682577.dkr.ecr.us-east-1.amazonaws.com \
  --docker-username=AWS \
  --docker-password=$(aws ecr get-login-password) \
  --namespace=default

kubectl create secret docker-registry regcred \
  --docker-server=250887682577.dkr.ecr.us-east-1.amazonaws.com \
  --docker-username=AWS \
  --docker-password=$(cat token-ecr) \
  --namespace=default

Giờ cần tạo ra crontab trên k8s để auto renew token

root@k8s-master:~/ecr# cat rotate-token-ecr.yaml
apiVersion: v1
kind: Secret
metadata:
  name: ecr-registry-helper-secrets
  namespace: production ## <<<< Change it
stringData:
  AWS_SECRET_ACCESS_KEY: "ctR4JSP1rQR7JuTYDd9zLTqlMWSAEBPxQANiL+5s" ## <<<< Change it
  AWS_ACCESS_KEY_ID: "AKIATU2QSHIIZQC525JS" ## <<<< Change it
  AWS_ACCOUNT: "250887682577" ## <<<< Change it
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: ecr-registry-helper-cm
  namespace: production ## <<<< Change it
data:
  AWS_REGION: "us-east-1" ## <<<< Change it
  DOCKER_SECRET_NAME: regcred
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: ecr-registry-helper
  namespace: production ## <<<< Change it
spec:
  schedule: "0 */10 * * *"
  successfulJobsHistoryLimit: 3
  suspend: false
  jobTemplate:
    spec:
      template:
        spec:
          serviceAccountName: sa-production ## <<<< Change it
          containers:
          - name: ecr-registry-helper
            image: odaniait/aws-kubectl:latest
            imagePullPolicy: IfNotPresent
            envFrom:
              - secretRef:
                  name: ecr-registry-helper-secrets
              - configMapRef:
                  name: ecr-registry-helper-cm
            command:
              - /bin/sh
              - -c
              - |-
                ECR_TOKEN=`aws ecr get-login-password --region ${AWS_REGION}`
                NAMESPACE_NAME=production ## <<<< Change it
                kubectl delete secret --ignore-not-found $DOCKER_SECRET_NAME -n $NAMESPACE_NAME
                kubectl create secret docker-registry $DOCKER_SECRET_NAME \
                --docker-server=https://${AWS_ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com \
                --docker-username=AWS \
                --docker-password="${ECR_TOKEN}" \
                --namespace=$NAMESPACE_NAME
                echo "Secret was successfully updated at $(date)"
          restartPolicy: Never
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: sa-production ## <<<< Change it
  namespace: production ## <<<< Change it
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: production ## <<<< Change it
  name: role-full-access-to-secrets
rules:
- apiGroups: [""]
  resources: ["secrets"]
  resourceNames: ["regcred"]
  verbs: ["delete"]
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["create"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: production-role-binding ## <<<< Change it
  namespace: production ## <<<< Change it
subjects:
- kind: ServiceAccount
  name: sa-production ## <<<< Change it
  namespace: production ## <<<< Change it
  apiGroup: ""
roleRef:
  kind: Role
  name: role-full-access-to-secrets
  apiGroup: ""
---
AWS - Amazon Web Service, Kubernetes

Post navigation

Previous Post: [Helm chart] Where will you save or push helm packages?
Next Post: [Teamcity] Why does TeamCity can’t find tag and branch on git

More Related Articles

[AWS] Encrypting your data easily via KMS on AWS AWS - Amazon Web Service
Policies as Code in Kubernetes using jsPolicy Kubernetes
[AWS] Look into Data Transfer on AWS AWS - Amazon Web Service
[Terraform] – Terraform Beginner – Lesson 9: Terraform with AWS – part 2 AWS - Amazon Web Service
[Kubernetes] RBAC Demo Kubernetes
[DevSecOps] Anonymous Authentication in Kubernetes Kubernetes

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Tham Gia Group DevOps nhé!
Để Nim có nhiều động lực ra nhiều bài viết.
Để nhận được những thông báo mới nhất.

Recent Posts

  • [Azure/Loadbalancer] Creating an internal load balancer on Azure Kubernetes Service (AKS). May 13, 2025
  • [Azure] The subscription is not registered to use namespace ‘Microsoft.ContainerService’ May 8, 2025
  • [Azure] Insufficient regional vcpu quota left May 8, 2025
  • [WordPress] How to add a Dynamic watermark on WordPress. May 6, 2025
  • [vnet/Azure] VNet provisioning via Terraform. April 28, 2025

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Categories

  • BareMetal
    • NextCloud
  • CI/CD
    • Argo Events
    • ArgoCD
    • ArgoWorkflows
    • Git
      • Bitbucket
    • Harbor
    • Jenkins
    • Spinnaker
    • TeamCity
  • Coding
    • DevSecOps
    • Golang
    • Jquery & JavaScript
    • Laravel
    • NextJS 14 & ReactJS & Type Script
    • Python
    • Selenium
    • Terraform
      • AWS – Amazon Web Service
      • Azure Cloud
      • GCP – Google Cloud
  • Kubernetes & Container
    • Apache Kafka
      • Kafka
      • Kafka Connect
      • Lenses
    • Docker
    • Helm Chart
    • Isito-EnvoyFilter
    • Kong Gateway
    • Kubernetes
      • Ingress
      • Pod
    • Longhorn – Storage
    • MetalLB
    • OAuth2 Proxy
    • Vault
    • VictoriaMetrics
  • Log, Monitor & Tracing
    • DataDog
    • ELK
      • Kibana
      • Logstash
    • Fluent
    • Grafana
    • Prometheus
  • Uncategorized
  • Admin

Copyright © 2025 NimTechnology.