ECR image Deployment on EKS | Part — 2
In my last blog, I discussed the infrastructure of EKS and the step-by-step configuration of the EKS cluster, worker nodes, and accessing the cluster from the local machine using AWS CLI and kubectl tool. If you haven't read that you can read it from here.
Prerequisite:
- You have the docker image of any application which you want to deploy.
- Docker Image is tested and working locally so that should not cause any issues on the cloud.
- AWS CLI is installed and configured with a security credential or STS token.
Steps to Deploy our existing application to EKS:
1. Create an ECR image and push it into the repository:
We will use AWS Elastic Container Registry (ECR) Service to create the repository of our existing application and push the docker image into that repository. In my case, I am going to deploy a static website which is my portfolio.
- Create a Repository in ECR Service using the below command:
aws ecr create-repository --repository-name <repo-name> --region <region-code>
The repository has been successfully created. Let’s confirm from the AWS console as well.
- To deploy the image on the repository, retrieve an authentication token and authenticate your Docker client to your registry.
aws ecr get-login-password --region <region-code> | docker login --username AWS --password-stdin <Account-ID>.dkr.ecr.<region-code>.amazonaws.com
Note: Before running the command make sure Docker is running on your machine.
- For the next step, navigate to your project location where the Docker file exists and execute the below command to build a Docker image:
docker build -t <image-name> .
- The build is successful, now tag your image so you can push the image to this repository:
docker tag <image-name>:latest <Account-ID>.dkr.ecr.<region-code>.amazonaws.com/<image-name>:latest
The docker image has been tagged. According to my command, I have tagged into 2023.21.01. You can choose the latest tag as well.
- Now we will push this image to our created repository:
docker push <Account-ID>.dkr.ecr.<region-code>.amazonaws.com/portfolio:latest
Now our image has been successfully pushed ECR repository.
2. Deploying ECR image to EKS using kubectl:
Now as we already have EKS infrastructure ready and the ECR image is also been pushed into the repository, all we need to do is deploy the image into the EKS orchestration.
- Create a namespace to logically isolate the resources within the cluster.
kubectl create namespace <namespace-name>
Now to deploy the image in the EKS cluster within the namespace we need the Kubernetes manifest file with Service and Deployment resources configuration specific to the application you have to deploy. I am providing the file below. Make changes according to your specifications.
apiVersion: v1
kind: Service
metadata:
name: <service-name>
namespace: <namespace-name>
labels:
app: my-app
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: <deployment-name>
namespace: <namespace-name>
labels:
app: my-app
spec:
replicas: 2
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: beta.kubernetes.io/arch
operator: In
values:
- amd64
- arm64
containers:
- name: web-app
image: <ECR-Image-URL>
ports:
- containerPort: 80
- Navigate where you have this file in your project and run the below command:
kubectl apply -f <menefist-file-name>.yaml
ECR image has been successfully deployed on the EKS cluster within a dev-platform namespace. To get the details of deployed service run the below command:
kubectl -n <namespace-name> describe service <service-name>
Point to note, we selected the spec kind “Load Balancer” within the service resource in the manifest file so that's why Load Balancer automatically has been created to route the traffic towards our application. It means our application only allows requests through the load balance which makes it more robust and secure.
If your application is a static website just like in my case, you can access it through the LoadBalancer Ingress URL.
Note: AWS EKS doesn’t come under the AWS Free Tier Services. If you are creating it for learning purposes then delete the cluster within 1 hour of cluster creation otherwise, you will be charged based on running clusters on an hourly basis.
See you in some other interesting blogs. Happy Reading :)