Wednesday, August 12, 2020

Deploy microservice in AWS Kubernetes using EKSCTL


In this blog, you will see the magic of eksctl, a simple CLI tool for creating clusters on EKS - Amazon's new managed Kubernetes service for EC2. It is written in Go, and uses CloudFormation.

In my previous blog https://leogether.blogspot.com/2020/08/deploy-net-microservice-on-aws.html, we did all the steps manually to deploy the service in AWS Kubernetes using AWS management console with single command. 

Here we will see that all these steps will be done by one single command using eksctl

Lets get started.


Pre-requisites:

1. AWS management console free tier account.
2. AWS CLI installed and configured.
    Test using the command: aws --version
    
    Once Installed configure your local computer using the command: aws configure

3. Install kubectl and configure.
    Test if it is working using command: kubectl version --short --client
    

4.Install and configure eksctl
    To test run command: eksctl version
    

4. We need a containerized application to deploy to the EKS cluster. I will be using the same application as in my first blog here: https://leogether.blogspot.com/2020/08/containerizing-net-microservice.html?zx=5b6e13926dfa506

5. (Optional) Postman to test the application deployed in the EKS cluster. You can test via browser as well. 


Create your Amazon EKS cluster and compute

Make sure your AWS CLI is configured in your system as part of pre-requisites.

Run the following command:

eksctl create cluster \ --name <name of cluster> \ --version 1.17 \ --region <your region> \ --nodegroup-name <name of worker nodes> \ --node-type <AMI type> \ --nodes <desired nodes> \ --nodes-min <minimum nodes> \ --nodes-max <maximum nodes> \ --ssh-access \ --ssh-public-key <your public kp> \ --managed

Note: --ssh-access is optional, though is it highly recommended, this is used to ssh into the instances and helps to gather diagnostic information in case of any issues.

If you do not have ssh key, you can create one using the command below:

aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pem

To retrieve the public key for your key pair:

ssh-keygen -y -f /path_to_key_pair/myKeyPair.pem > myKeyPair.pub


I already have my key pair, so I used eksctl command as below:

This command will create everything for you, you can read the output to get familiar:

Once the command is completed, you can go into AWS console to view all the resources created.

Now let run these kubectl commands to check what all we have: 

kubectl get svc

kubectl get nodes

kubectl get ns 

kubectl get pods

So we got 3 nodes up and running, there is no service except the default one, there are no pods, there are some default namespaces.

Now, it is time to deploy application so that we can move the service container to the cluster.

I am using same .net sample application as in my last blog. We already created deployment.yml and service.yml file last time. I have already explained in details about these files, please read my last blog if you haven't done https://leogether.blogspot.com/2020/08/deploy-net-microservice-on-aws.html

Run the following command to deploy deployment.yml file.

kubectl apply -f deployment.yml

Now if you run:

kubectl get pods, you will see 3 pods (as defined in yml file)

But you will still need to apply service.yml file to access the service, this will create a load balancer.

If you run kubectl get svc, you still cannot see any load balancer. So we have our application deployed, but have no means to access it.

Now run command to deploy service.yml file:

kubectl apply -y service.yml

Run kubectl get svc again, you will see the load balancer end point created to access the service.

You can also go into AWS Console to see the load balancer created.

Finally test the service in Postman or browser.

Lastly, clean up your resources to avoid billing:

1. Delete Load Balancer

2. Delete Cloudformation template for worker node.

3. Delete Cloudformation template for the cluster.

This should clean up everything. I would still go and check in AWS Console to make sure.

Hope this blog is helpful. Please leave your comments.


2 comments:

  1. Excellent writeup. Please write more

    ReplyDelete
  2. Playtech, the world's best online casino - Ambien Hoppeie
    Playtech is a leading international supplier of online casino games including video slots, bingo, live dealer games, scratch cards, and 온라인 호텔 카지노 video poker.

    ReplyDelete