Containers on AWS - Amazon Elastic Kubernetes Service (EKS) on AWS Fargate
Tags: aws, kubernetes, eks, containers
Published: 2023-03-13 09:30:53

![Architecture](https://cdn-images-1.medium.com/max/2000/1*tQ_vQF_lEd2cfqZXu3rgTg.png)

Hello before, I want to write about how to use **Amazon EKS** on **AWS Fargate**. Before getting started, I want to share my article about Amazon EKS on Amazon EC2 [on this link](https://dev.to/budionosan/containers-on-aws-amazon-elastic-kubernetes-service-eks-on-amazon-ec2-428d). But what is AWS Fargate? [AWS Fargate](https://aws.amazon.com/fargate/) is a serverless container by AWS and can used for Amazon ECS and Amazon EKS.

Before creating the EKS cluster, install several configurations like:

* **eksctl** is a command line for creating an EKS cluster. Install eksctl with the command:

```
    curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
    sudo mv /tmp/eksctl /usr/local/bin
    eksctl version
```

* **kubectl** is a command line for managing Kubernetes worker nodes. Install kubectl with the command:

```
    curl -O https://s3.us-west-2.amazonaws.com/amazon-eks/1.24.7/2022-10-31/bin/linux/amd64/kubectl
    chmod +x ./kubectl
    mkdir -p $HOME/bin && cp ./kubectl $HOME/bin/kubectl && export PATH=$PATH:$HOME/bin
    kubectl version --short --client
```

* **AWS CLI** is a command line from AWS. Install AWS CLI with the command:

```
    curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
    unzip awscliv2.zip
    sudo ./aws/install
    aws --version
```

After several configurations are installed, now create an EKS cluster with AWS Fargate. **NOTE**: EKS cluster created uses CloudFormation to create resources — VPC, NAT gateway, and many more.

    eksctl create cluster --name eksbudionosan --region us-west-2 --without-nodegroup --version 1.24 --fargate

![create EKS cluster with AWS Fargate](https://cdn-images-1.medium.com/max/2136/1*RZ-5iLFvZLXCOqaRPLjm7A.jpeg)

![CloudFormation](https://cdn-images-1.medium.com/max/2484/1*-7TOGgy7K27RxOL_eODPOQ.jpeg)

![EKS cluster](https://cdn-images-1.medium.com/max/2000/1*1hvpQupPXygLyMLVoFvvqw.jpeg)

Click the EKS cluster name that has already been created and click Compute. When creating an EKS cluster with Fargate, also created a [Fargate profile](https://docs.aws.amazon.com/eks/latest/userguide/fargate-profile.html).

Fargate profile for run pods in EKS cluster. If you want to create a Fargate profile again, can create a Fargate profile with the command:

    eksctl create fargateprofile \
        --cluster eksbudionosan \
        --name my-fargate-profile \
        --namespace my-kubernetes-namespace \
        --labels key=value

![Fargate profile](https://cdn-images-1.medium.com/max/2356/1*791w9qmt7Rm-3vka0fKpxQ.jpeg)

When creating an EKS cluster, automatically create VPC, subnet, security groups, NAT gateway, and many more. Click VPC for detail.

![VPC](https://cdn-images-1.medium.com/max/2256/1*10wdoCXSJveimj3CGNbg9Q.jpeg)

Name for VPC in EKS cluster — eksctl-youreksclustername-cluster/VPC.

![VPC](https://cdn-images-1.medium.com/max/2000/1*JYt-APRQF9zOHUCQ4iZmfg.jpeg)

If you want read about what is VPC, you can see [the link](https://dev.to/budionosan/networking-on-aws-amazon-virtual-private-cloud-vpc-4phd).

![Subnet (private and public)](https://cdn-images-1.medium.com/max/2100/1*cJismHkkoytbc1dMGaHCcA.jpeg)

![](https://cdn-images-1.medium.com/max/2000/1*IKBk5hqHRmCzMfmx7NYLkQ.jpeg)

![Route table](https://cdn-images-1.medium.com/max/2000/1*1W17nFbL4coheZRRNgjlyA.jpeg)

![Internet gateway](https://cdn-images-1.medium.com/max/2116/1*FxuDyCgg17Ql_WZe5WXf8g.jpeg)

![Elastic IP](https://cdn-images-1.medium.com/max/2000/1*5n9oXy2RH7na2IRqSM8Oag.jpeg)

![NAT Gateway](https://cdn-images-1.medium.com/max/2000/1*dr7ZZe0jS4u3xsWnicD_GA.jpeg)

Security groups for EKS — control plane security group, shared node security group, load balancer security group and more.

![](https://cdn-images-1.medium.com/max/2000/1*7Z6YKu9M6UJ1NY6OH4z11w.jpeg)

![Security Groups](https://cdn-images-1.medium.com/max/2520/1*Gkux0QR_X4eAjARFaFhK9w.jpeg)

After the EKS cluster is ready to use, create [IAM OIDC](https://docs.aws.amazon.com/eks/latest/userguide/enable-iam-roles-for-service-accounts.html) (Identity and Access Management OpenID Connect) to use IAM roles for service accounts in the EKS cluster.

    eksctl utils associate-iam-oidc-provider --cluster eksbudionosan --region us-west-2 --approve

![](https://cdn-images-1.medium.com/max/2000/1*SPFRUSA5f60v2YpaFqxBHg.jpeg)

![IAM OIDC](https://cdn-images-1.medium.com/max/2000/1*tpSIoogqGN1KgNhruEAM8w.jpeg)

![IAM OIDC and IAM Policy for AWS Load Balancer Controller.](https://cdn-images-1.medium.com/max/2158/1*XAGYZJeu1DPZpjrlEkYNDQ.jpeg)

**AWS Load Balancer Controller** managed [Elastic Load Balancers](https://docs.aws.amazon.com/elasticloadbalancing/latest/userguide/what-is-load-balancing.html) for an EKS cluster.

* An **AWS Application Load Balancer** (ALB) used for create a Kubernetes Ingress.

[Ingress](https://kubernetes.io/docs/concepts/services-networking/ingress/) use HTTP and HTTPS routes from client to [services](https://kubernetes.io/docs/concepts/services-networking/service/) in Kubernetes cluster.

To create Ingress, you must have an ingress controller. For this tutorial, I use AWS Load Balancer Controller for the ingress controller.

* An **AWS Network Load Balancer** (NLB) used for create a Kubernetes service of type LoadBalancer.

![AWS Load Balancer Controller IAM Policy](https://cdn-images-1.medium.com/max/2000/1*JDRnoYW_5_bSxRZYkChkwg.jpeg)

For this tutorial, I use AWS Application Load Balancer (ALB). Create IAM policy for [AWS Load Balancer Controller](https://docs.aws.amazon.com/eks/latest/userguide/aws-load-balancer-controller.html).

    curl -O https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.4.4/docs/install/iam_policy.json
    aws iam create-policy \
        --policy-name AWSLoadBalancerControllerIAMPolicy \
        --policy-document file://iam_policy.json

Create IAM role for EKS service account named aws-load-balancer-controller in the kube-system namespace.

    eksctl create iamserviceaccount \
      --cluster=eksbudionosan \
      --namespace=kube-system \
      --name=aws-load-balancer-controller \
      --attach-policy-arn=arn:aws:iam::<YOUR_AWS_ACCOUNT>:policy/AWSLoadBalancerControllerIAMPolicy \
      --override-existing-serviceaccounts \
      --approve

Installing [Helm](https://helm.sh/). Helm is package manager for Kubernetes.

    curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
    chmod 700 get_helm.sh
    ./get_helm.sh

![IAM Service Account and installing HELM](https://cdn-images-1.medium.com/max/2024/1*oCQUwAvxL4CgwGrZJz3bxg.jpeg)

Install **AWS Load Balancer Controller** using Helm that already installed. When installing AWS Load Balancer Controller on AWS Fargate, add set region and set vpcId.

    helm repo add eks https://aws.github.io/eks-charts
    helm repo update

    helm install aws-load-balancer-controller eks/aws-load-balancer-controller \
      -n kube-system \
      --set clusterName=eksbudionosan \
      --set serviceAccount.create=false \
      --set serviceAccount.name=aws-load-balancer-controller \
      --set region=us-west-2 \
      --set vpcId=YOUR_VPC_EKS

![Install AWS Load Balancer Controller](https://cdn-images-1.medium.com/max/2090/1*-a6L5NoqSkWQm4jaG-OrwA.jpeg)

After installing AWS Load Balancer Controller, check AWS Load Balancer Controller is already installed or not.

    kubectl get deployment -n kube-system aws-load-balancer-controller

Create deployment in the EKS cluster using my Amazon ECR private.

    kubectl create deployment eksbudionosan --image=<YOUR_ECR_IMAGE>

For service, I share my service YAML file for creating service EKS cluster in my GitHub:

[learnaws/service.yaml at main · budionosan/learnaws (github.com)](https://github.com/budionosan/learnaws/blob/main/Amazon%20EKS%20on%20AWS%20Fargate/service.yaml)

Also for ingress, I share my ingress YAML file for creating an ingress EKS cluster in my GitHub:

[learnaws/ingress.yaml at main · budionosan/learnaws (github.com)](https://github.com/budionosan/learnaws/blob/main/Amazon%20EKS%20on%20AWS%20Fargate/ingress.yaml)

When creating an ingress, use variable — load balancer name, scheme (**internet-facing** or internal), target type (**ip** or instance), and ingress class name — ALB (Application Load Balancer).

After creating the service and ingress YAML file, apply two YAML files to create a load balancer in AWS.

    kubectl apply -f service.yaml
    kubectl apply -f ingress.yaml

Check Amazon EC2, scroll and click Load Balancer. See the Load Balancer has 2 URL load balancer — classic (deprecated) and **application** type.

![Load Balancer](https://cdn-images-1.medium.com/max/2506/1*7NjFM0xieyqQs0HGJr7EIw.jpeg)

Click the application load balancer name for detail. This load balancer is associated with VPC which was already created when creating the EKS cluster.

![Load Balancer detail](https://cdn-images-1.medium.com/max/2252/1*UtQoo-M9FtupAt2ryM0_0A.jpeg)

Still on Amazon EC2, then check [Target Groups](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html). Target groups associated with a load balancer. Click the target groups name for detail.

![Target Groups](https://cdn-images-1.medium.com/max/2414/1*IJRA3vNMDYOjHLcR0NuGvw.jpeg)

![](https://cdn-images-1.medium.com/max/2410/1*3S3JVGrVXQjtdPvZrp2phw.jpeg)

![Target groups detail](https://cdn-images-1.medium.com/max/2000/1*w3DKGC2cguweOM5ks5NRzQ.jpeg)

Back to the load balancer, copy the DNS name to a new tab. My web application is running and ready to use. It means the load balancer is successful.

![Load balancer is successful.](https://cdn-images-1.medium.com/max/2640/1*ts_x0n7Y5GYHeG5e7eGYqw.jpeg)

Thank you very much to you when read this tutorial :)