AWS EKS User Guide
Introduction
Amazon Elastic Kubernetes Service (AWS EKS) is a managed Kubernetes service on the AWS cloud platform. Instead of having to install, operate, and maintain a Kubernetes cluster from scratch yourself, AWS takes over the management of the system's "brain" (Control Plane), helping you focus entirely on deploying and running applications.
Advantages
- Reduced administrative burden: AWS automatically performs difficult tasks such as version updates, security patching, and ensuring high availability for the Control Plane across multiple Availability Zones.
- Optimal security: EKS integrates tightly with AWS security services such as IAM (for granular permissions for Pods), VPC (for network isolation), and AWS KMS (for data encryption).
- Flexible scalability: You can easily increase or decrease the number of resources (nodes) based on the actual traffic of the application, helping to optimize costs.
- Rich ecosystem: Easily connect with other AWS services such as CloudWatch (monitoring), ELB (load balancing), and Fargate (running containers without managing servers).
- Full compatibility: Since EKS runs a standard Kubernetes version, you can easily migrate applications from On-premise environments or other clouds to AWS without changing the source code.
Prerequisites
In this article, I will use NestJS to create a simple API to get a list of files in an S3 Bucket. After that, I will build a docker image and push this image to AWS ECR and use this docker image in k8s; if you have any questions about any content, please review the previous articles I mentioned.
The content of the s3.service.ts file is as follows:
- The content is simply to allow uploading via presigned urls and listing the urls to access uploaded files
- Pay attention to the BUCKET field; you will need to create this value in the .env file and in the .yaml file that will be created in the content below
- As for the content regarding using CDK to create the S3 Bucket, controller, and how to use presigned urls, please review the previous article I shared
Detail
Use AWS CDK to create the file lib/eks-public-stack.ts as follows:
- natGateways: 0, here I create a VPC without using a NAT gateway because this is a service with quite high costs
- defaultCapacity: 0, the initial number of nodes (EC2); I will configure addAutoScalingGroupCapacity below, so it is not needed here
- eks.KubernetesVersion.V1_34: this is the current latest version corresponding to the library @aws-cdk/lambda-layer-kubectl-v34; when you use it, please check again to use it with the appropriate kubectlLayer
- currentUserName: this is the username of the account you use to deploy this CDK; you can get the information as follows (the purpose here is to grant permissions for your current account to operate with the Cluster after it is created)
- ec2.Peer.anyIpv4(): I am allowing all IPs to connect to this cluster; if you want more security, you can use ec2.Peer.ipv4('<IP address>')
- instanceTypes: [new ec2.InstanceType("t3.small")]: this is the minimum instance for nodes to run; if you use micro, there may be too few resources to use
- PolicyStatement: because the NestJS docker image uses the S3 service, I will authorize this cluster to have permission to use the S3 service
- the output part of ConfigCommand is used to connect to k8s
Next is the k8s.yml file
- serviceAccountName: note that this part must have the same name as the serviceAccount created in the CDK
- image: please replace it with the image URI that you pushed to your ECR accordingly
- LoadBalancer: to connect to the external internet
After deploying, the result is as follows
Resources have been created on the AWS Console
Update the k8s config with the created cluster; you only need to execute the command outputted after deploying the CDK
Next is to apply the content of the k8s.yml file
Wait a moment for the resources to be created; when you check and see the status of the resources as ready like this, it is successful
Pay attention to the LoadBalancer EXTERNAL-IP part, which is the endpoint you can use to access the API
Happy coding!
Comments
Post a Comment