Kubernetes Application Deployment with AWS EKS and ECR

Tram Ho

Question

Recently a lot of people talk about Docker, Kubernetes (K8s) making a guy just backend code as I noticed. After a while of learning and losing money for AWS, finally came out the following article.

In this article I will talk about automatically deploying an application to AWS EKS (Elastic Kubernetes Service) using Gitlab-CI, docker images will be stored in ECR (Amazon Elastic Container Registry).

For those who are new to AWS, you should also pay attention to the price of the service. While AWS has a 1-year Free Tier package, not all are free. Remember to read carefully to avoid unjust money.

The content includes:

  • Setup Kubernetes cluster
  • Dockerize
  • Push image to ERC
  • Kubernetes deployments
  • Auto deploy via Gitlab-CI
  • Clean up

The steps I deploy: create k8s cluster => dockerize => push image to ERC => deploy app with EKS => Gitlab auto deploy.


Setup Kubernetes cluster

There are many ways to create a K8s cluster, you can go to the AWS console to create via the intuitive interface. As for me, I will create through eksctl , this is a CLI provided by AWS.

Note a little bit that I am using macOS so the commands below are for macOS. You can access the AWS guide to install the operating system you are using.

Install the AWS CLI

Configure your AWS CLI credentials

To use this CLI, you need AWS access keys including access key ID and secret access key. Visit IAM of AWS to generate offline. Next use the command:

Then enter the Access keys information obtained from AWS.

For example:

Install eksctl

I installed it with brew

Check if the installation was successful with the command:

Create Kubernetes cluster

The command creates a cluster as follows:

Above I created a cluster called app-demo , there is 1 node, note type is t2.micro this node type will be free under AWS Free Tier package. Without the –node-type flag, eksctl will default to m5.large at a cost of $ 0.12 per hour. It sounds like a little, but if you try to watch for a few days, it will be:

Cluster creation time usually takes 15-20 minutes. Once done, run the command:

To test. If there is a problem with credentials, you should check the aws configuration with the command above aws configure .

Dockerize

The next step, we need to dockerize the application. Depending on your application, you write Dockerfile accordingly. If you do not have one then use my app. App written in Vuejs, clone at the link .

Build docker images with the command:

The output of dockerize is that you must be able to run the application via docker. We run the app with the command:

Access localhost:8080 if the app displays normally that we have succeeded.

Push image to ERC

Next step we will push images onto ERC First we need to login:

Replace region, aws_account_id with your AWS account information.

Next create a responsitory

You can also create responsitory through the interface at ERC responsitory

Finally, we add the tag and push the image onto ERC via:

Note that the Free Tier package will free you 500Mb when saving on ERC, If your total image file> 500Mb, you should consider deleting.

Kubernetes deployments

Now it’s time to deploy the app to the cluster we created earlier. Create a file named deployment.yaml with the following content:

  • We label the app as app:web
  • Image is taken from aws_account_id.dkr.ecr.region.amazonaws.com/demo-app .
  • The number of replicas is 2.

Next, run the command:

Check by command:

Next we need to expose an External IP to access the app from the internet. Use an ELB (Elastic Load Balancer)

Check ELB information by command:

Check at EXTERNAL-IP we will have a domain . Access domain:8080 to check if the application has been deployed.

Auto deploy via Gitlab-CI

The steps above are just padding for this last step. The result we need to achieve is to automatically deploy the app every time a new commit is made.

First, go to Gitlab.com => Settings => CI-CD => Variables and add the following variables:

Create a new .gitlab-ci.yml file

The commands in the .gitlab-ci.yml file we have done above. There are some other points:

  • Use the variable ${CI_BUILD_REF} to deploy the exact image that was built upon commit.
  • kubectl set image command kubectl set image to update the image and deploy.

Commit the code to your repository and enjoy the results.

Cleaning Up

After achieving the results, you can look for a while, take a screenshot but do not forget to delete it if you do not intend to use it for a long time.

Conclude

The above article is a journey to Docker, k8s, and AWS for my $ 21.53 tuition. If you have any questions, comment. References:

Share the news now

Source : Viblo