Create EKS Fargate Cluster with EKS Add-Ons & Expose Microservices Using AWS Load Balancer

Listen to this Post

In this article, we explore how to set up an EKS Fargate cluster using Terraform for Infrastructure as Code (IaC). The process includes configuring EKS Add-Ons and exposing a microservice within the cluster using an AWS Load Balancer. Below are the verified commands and code snippets to help you implement this setup.

Terraform Code to Create EKS Fargate Cluster

[hcl]
provider “aws” {
region = “us-west-2”
}

resource “aws_eks_cluster” “example” {
name = “example-eks-cluster”
role_arn = aws_iam_role.example.arn

vpc_config {
subnet_ids = [aws_subnet.example1.id, aws_subnet.example2.id]
}
}

resource “aws_eks_fargate_profile” “example” {
cluster_name = aws_eks_cluster.example.name
fargate_profile_name = “example-fargate-profile”
pod_execution_role_arn = aws_iam_role.example.arn

selector {
namespace = “default”
}
}
[/hcl]

Exposing Microservices Using AWS Load Balancer

[hcl]
resource “aws_lb” “example” {
name = “example-lb”
internal = false
load_balancer_type = “application”
security_groups = [aws_security_group.example.id]
subnets = [aws_subnet.example1.id, aws_subnet.example2.id]
}

resource “aws_lb_target_group” “example” {
name = “example-tg”
port = 80
protocol = “HTTP”
vpc_id = aws_vpc.example.id
}

resource “aws_lb_listener” “example” {
load_balancer_arn = aws_lb.example.arn
port = 80
protocol = “HTTP”

default_action {
type = “forward”
target_group_arn = aws_lb_target_group.example.arn
}
}
[/hcl]

Kubernetes Deployment YAML for Microservice

apiVersion: apps/v1
kind: Deployment
metadata:
name: example-microservice
spec:
replicas: 3
selector:
matchLabels:
app: example
template:
metadata:
labels:
app: example
spec:
containers:
- name: example-container
image: example-image:latest
ports:
- containerPort: 80

What Undercode Say

In conclusion, setting up an EKS Fargate cluster using Terraform and exposing microservices with an AWS Load Balancer is a powerful way to manage containerized applications in the cloud. This approach leverages Infrastructure as Code (IaC) to ensure reproducibility and scalability. Below are some additional Linux, IT, and Windows commands that can enhance your understanding and implementation of cloud solutions:

1. Linux Commands:

– `kubectl get pods` – List all pods in the current namespace.
– `kubectl describe pod ` – Get detailed information about a specific pod.
– `kubectl logs ` – View logs for a specific pod.
– `kubectl apply -f ` – Apply a configuration to a resource.

2. AWS CLI Commands:

– `aws eks list-clusters` – List all EKS clusters in your AWS account.
– `aws eks describe-cluster –name ` – Get detailed information about a specific EKS cluster.
– `aws ecr create-repository –repository-name ` – Create a new ECR repository for Docker images.

3. Windows Commands:

– `docker ps` – List all running Docker containers.
– `docker build -t .` – Build a Docker image from a Dockerfile.
– `docker run -d -p 80:80 ` – Run a Docker container in detached mode and map port 80.

For further reading, you can visit the AWS EKS Documentation and Terraform EKS Module Documentation.

By mastering these commands and techniques, you can efficiently manage and deploy containerized applications on AWS, ensuring high availability and scalability for your microservices architecture.

References:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification ✅Featured Image