|
1 |
| -# AWS ECR - Elastic Container Registry Integration with EKS |
| 1 | +# AWS ECR - Elastic Container Registry Integration |
| 2 | + |
| 3 | +## Step-01: Introduction to ECR |
| 4 | +- For introduction slides refer the [presentation slides](/presentation/AWS-Fargate-and-EKS-Masterclass.pptx). |
| 5 | + |
| 6 | +## Step-02: ECR Terminology |
| 7 | + - **Registry:** An ECR registry is provided to each AWS account; we can create image repositories in our registry and store images in them. |
| 8 | +- **Repository:** An ECR image repository contains our Docker images. |
| 9 | +- **Repository policy:** We can control access to our repositories and the images within them with repository policies. |
| 10 | +- **Authorization token:** Our Docker client must authenticate to Amazon ECR registries as an AWS user before it can push and pull images. The AWS CLI get-login command provides us with authentication credentials to pass to Docker. |
| 11 | +- **Image:** We can push and pull container images to our repositories. We can use these images locally on your development system, or we can use them in Amazon ECS task definitions. |
| 12 | + |
| 13 | +## Step-03: Pre-requisites |
| 14 | +- Install required CLI software on your local desktop |
| 15 | + - **Install AWS CLI V2 version** |
| 16 | + - We have taken care of this step as part of [01-EKS-Create-Clusters](/01-EKS-Create-Clusters/README.md) |
| 17 | + - Documentation Reference: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html |
| 18 | + - **Install Docker CLI** |
| 19 | + - We have taken of Docker local desktop installation as part of [Docker Fundamentals](https://github.com/stacksimplify/docker-fundamentals/tree/master/02-Docker-Installation) section |
| 20 | + - Docker Desktop for MAC: https://docs.docker.com/docker-for-mac/install/ |
| 21 | + - Docker Desktop for Windows: https://docs.docker.com/docker-for-windows/install/ |
| 22 | + - Docker on Linux: https://docs.docker.com/install/linux/docker-ce/centos/ |
| 23 | + |
| 24 | + - **On AWS Console** |
| 25 | + - We have taken care of this step as part of [01-EKS-Create-Clusters](/01-EKS-Create-Clusters/README.md) |
| 26 | + - Create Authorization Token for admin user if not created |
| 27 | + - **Configure AWS CLI with Authorization Token** |
| 28 | +``` |
| 29 | +aws configure |
| 30 | +AWS Access Key ID: **** |
| 31 | +AWS Secret Access Key: **** |
| 32 | +Default Region Name: us-east-1 |
| 33 | +``` |
| 34 | + |
| 35 | +## Step-04: Create ECR Repository |
| 36 | +- Create simple ECR repository via AWS Console |
| 37 | + - Repository Name: aws-ecr-kubenginx |
| 38 | + - Tag Immutability: Enable |
| 39 | + - Scan on Push: Enable |
| 40 | +- Explore ECR console. |
| 41 | +- **Create ECR Repository using AWS CLI** |
| 42 | +``` |
| 43 | +aws ecr create-repository --repository-name aws-ecr-kubenginx --region us-east-1 |
| 44 | +aws ecr create-repository --repository-name <your-repo-name> --region <your-region> |
| 45 | +``` |
| 46 | + |
| 47 | +## Step-05: Create Docker Image locally |
| 48 | +- Navigate to folder **05-ECR-Elastic-Container-Registry\01-aws-ecs-kubenginx** from course github content download. |
| 49 | +- Create docker image locally |
| 50 | +- Run it locally and test |
| 51 | +``` |
| 52 | +# Build Docker Image |
| 53 | +docker build -t <ECR-REPOSITORY-URI>:<TAG> . |
| 54 | +docker build -t 180789647333.dkr.ecr.us-east-1.amazonaws.com/aws-ecr-kubenginx:1.0.0 . |
| 55 | +
|
| 56 | +# Run Docker Image locally & Test |
| 57 | +docker run --name <name-of-container> -p 80:80 --rm -d <ECR-REPOSITORY-URI>:<TAG> |
| 58 | +docker run --name aws-ecr-kubenginx -p 80:80 --rm -d 180789647333.dkr.ecr.us-east-1.amazonaws.com/aws-ecr-kubenginx:1.0.0 |
| 59 | +
|
| 60 | +# Access Application locally |
| 61 | +http://localhost |
| 62 | +
|
| 63 | +# Stop Docker Container |
| 64 | +docker ps |
| 65 | +docker stop aws-ecr-kubenginx |
| 66 | +docker ps -a -q |
| 67 | +
|
| 68 | +# Optional (Delete) |
| 69 | +docker build -t 411686525067.dkr.ecr.us-east-1.amazonaws.com/aws-ecr-kubenginx:1.0.0 . |
| 70 | +docker run --name aws-ecr-kubenginx -p 80:80 --rm -d 411686525067.dkr.ecr.us-east-1.amazonaws.com/aws-ecr-kubenginx:1.0.0 |
| 71 | +``` |
| 72 | + |
| 73 | +## Step-06: Push Docker Image to AWS ECR |
| 74 | +- Firstly, login to ECR Repository |
| 75 | +- Push the docker image to ECR |
| 76 | +- **AWS CLI Version 2.x** |
| 77 | +``` |
| 78 | +# Get Login Password |
| 79 | +aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <ECR-REPOSITORY-URI> |
| 80 | +aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 180789647333.dkr.ecr.us-east-1.amazonaws.com/aws-ecr-kubenginx |
| 81 | +
|
| 82 | +# Push the Docker Image |
| 83 | +docker push <ECR-REPOSITORY-URI>:<TAG> |
| 84 | +docker push 180789647333.dkr.ecr.us-east-1.amazonaws.com/aws-ecr-kubenginx:1.0.0 |
| 85 | +
|
| 86 | +# Optional (Delete) |
| 87 | +aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 411686525067.dkr.ecr.us-east-1.amazonaws.com/aws-ecr-kubenginx |
| 88 | +docker push 411686525067.dkr.ecr.us-east-1.amazonaws.com/aws-ecr-kubenginx:1.0.0 |
| 89 | +``` |
| 90 | +- Verify the newly pushed docker image on AWS ECR. |
| 91 | +- Verify the vulnerability scan results. |
| 92 | + |
| 93 | +## Step-07: Using ECR Image with Amazon EKS |
| 94 | +- Understand the Deployment and Service kubernetes manifests present in folder **05-ECR-Elastic-Container-Registry\02-kube-manifests** |
| 95 | +- **Important Note:** We have two objects deployment and service present in single yaml file separated by `---` |
| 96 | +- Deploy the kubernetes manifests |
| 97 | +``` |
| 98 | +cd 05-ECR-Elastic-Container-Registry\02-kube-manifests |
| 99 | +kubectl apply -f ecr-kube-deployment-and-service.yml |
| 100 | +kubectl get deploy |
| 101 | +kubectl get svc |
| 102 | +kubectl get po |
| 103 | +``` |
| 104 | +- Access Application |
| 105 | +``` |
| 106 | +# Get external ip of EKS Cluster Kubernetes worker nodes |
| 107 | +kubectl get nodes -o wide |
| 108 | +
|
| 109 | +# Access Application |
| 110 | +http://<External-IP-of-EKS-Cluster-Nodes>:31031 |
| 111 | +``` |
| 112 | +- **ecr-kube-deployment-and-service.yml** |
| 113 | +```yml |
| 114 | +apiVersion: apps/v1 |
| 115 | +kind: Deployment |
| 116 | +metadata: |
| 117 | + name: kubeapp-ecr |
| 118 | + labels: |
| 119 | + app: backend-restapp |
| 120 | +spec: |
| 121 | + replicas: 3 |
| 122 | + selector: |
| 123 | + matchLabels: |
| 124 | + app: kubeapp-ecr |
| 125 | + template: |
| 126 | + metadata: |
| 127 | + labels: |
| 128 | + app: kubeapp-ecr |
| 129 | + spec: |
| 130 | + containers: |
| 131 | + - name: kubeapp-ecr |
| 132 | + image: 411686525067.dkr.ecr.us-east-1.amazonaws.com/aws-ecr-kubenginx:1.0.0 |
| 133 | + resources: |
| 134 | + requests: |
| 135 | + memory: "128Mi" |
| 136 | + cpu: "500m" |
| 137 | + limits: |
| 138 | + memory: "256Mi" |
| 139 | + cpu: "1000m" |
| 140 | + ports: |
| 141 | + - containerPort: 80 |
| 142 | +--- |
| 143 | +apiVersion: v1 |
| 144 | +kind: Service |
| 145 | +metadata: |
| 146 | + name: kubeapp-ecr-service |
| 147 | + labels: |
| 148 | + app: kubeapp-ecr |
| 149 | +spec: |
| 150 | + type: NodePort |
| 151 | + selector: |
| 152 | + app: kubeapp-ecr |
| 153 | + ports: |
| 154 | + - port: 80 |
| 155 | + targetPort: 80 |
| 156 | + nodePort: 31031 |
| 157 | + |
| 158 | +``` |
| 159 | + |
| 160 | +## Step-08: Clean Up |
| 161 | +``` |
| 162 | +cd 05-ECR-Elastic-Container-Registry\02-kube-manifests |
| 163 | +kubectl delete -f ecr-kube-deployment-and-service.yml |
| 164 | +kubectl get deploy |
| 165 | +``` |
0 commit comments