# AWS EKS with Fargate

[AWS Fargate](https://docs.aws.amazon.com/eks/latest/userguide/fargate.html) is a technology that provides on-demand, right-sized compute capacity for containers. With AWS Fargate, you no longer have to provision, configure, or scale groups of virtual machines to run containers. This removes the need to choose server types, decide when to scale your node groups, or optimize cluster packing. You can control which pods start on Fargate and how they run with [Fargate profiles](https://docs.aws.amazon.com/eks/latest/userguide/fargate-profile.html), which are defined as part of your Amazon EKS cluster.

Fargate works by letting you deploy containers without having to set up or manage the infrastructure that hosts them. You simply tell Fargate which container images you want to run and provide some details about how much computing and memory resources should allocate to them. Then, Fargate automatically provisions the host servers for you. You pay only for the resources consumed when your containers are running. Fargate will not run on its own. We need to have ECS or EKS handle container orchestration tasks.

With AWS EKS autoscaling, we need to define the min and max number of nodes. But in Fargate, we don't have to do that. So simple yet..!

Using **Fargate** has its drawbacks, but we will discuss those later.

For more info on Fargate, Use the below link.

[**AWS Fargate - Amazon EKS**](https://docs.aws.amazon.com/eks/latest/userguide/fargate.html)

## **Creating Kubernetes Cluster**

> *I am assuming you have already configured aws cli on your system and installed aws cli, eksctl and kubectl*

First, let's create a Kubernetes cluster using the ekcstl command

```bash
eksctl create cluster --name demo-fargate-poc --region ap-south-1 --version 1.23 --nodegroup-name linuxgroup --node-type t2.medium --nodes 2
```

update kubeconfig to access the cluster

```bash
aws eks update-kubeconfig --region ap-south-1 --name demo-fargate-poc
```

check the nodes now. ( you should see 2 nodes ) .

```bash
kubectl get nodes 
```

deploy a sample nginx and verify once.

```bash
kubectl  create deployment nginx --replicas=3 --image=nginx
```

check the pods

```bash
kubectl get pods 
```

Once verified, let's start with Fargate.

Create a namespace to run Fargate pods

```bash
kubectl create namespace fargate-poc 
```

## **Fargate Profile creation**

Before you schedule pods on Fargate in your cluster, you must define at least one Fargate profile that specifies which pods use Fargate when launched.

###### ***To* create a Fargate profile for a cluster with the AWS Management Console**

1. Open the Amazon EKS console at [https://console.aws.amazon.com/eks/home#/clusters](https://console.aws.amazon.com/eks/home#/clusters).
    
2. Choose the cluster to create a Fargate profile for.
    
3. Choose the **Compute** tab.
    
4. Under **Fargate profiles**, choose to **Add Fargate profile**.
    
5. On the **Configure Fargate profile** page, add the following:  **Name**, **Pod execution role (** For more information, see [Amazon EKS pod execution IAM role](https://docs.aws.amazon.com/eks/latest/userguide/pod-execution-role.html) ),  and **Subnets**. (Only private subnets are supported for pods that are running on Fargate),  **Tags** (optional). Choose **Next**.
    
6. On the **Configure pod selection** page, do the following: **Namespace** (can mention multiple namespaces with wild cards like `prod-*` ) I have used `fargate-poc` .  Add Kubernetes labels to the selector (`infrastructure: fargate`) here you are restricting which pods in that namespace to run using the Fargate profile.
    
7. On the **Review and Create** page, review the information for your Fargate profile and choose **Create**.
    

> *For more info on creating a Fargate profile check the below links*

[**Amazon EKS pod execution IAM role - Amazon EKS**](https://docs.aws.amazon.com/eks/latest/userguide/pod-execution-role.html)

For more info on how to create a Role in IAM use the below Link

[**Amazon EKS pod execution IAM role - Amazon EKS**](https://docs.aws.amazon.com/eks/latest/userguide/pod-execution-role.html)

Now that we have a cluster with 2 nodes and a frigate profile to provision nodes. we can go ahead and create pods matching to Fargate profile.

Below is the deployment I have used.

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: fargate-poc
  labels:
    app: nginx

spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
      infrastructure: fargate
      
  template:
    metadata:
      labels:
        app: nginx
        infrastructure: fargate
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
```

Now Check the nodes you should see the Fargate nodes running .

```bash
kubectl get nodes 
```

the output will be something like below

```bash
fargate-ip-10-0-89-244.region-code.compute.internal
```

You can also define the CPU and memory values needed for the pods in yaml file. Fargate will get the CPU and memory values from there and provision the nodes accordingly. Below is an example you can use

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: fargate-pod-poc-
spec:
  containers:
  - name: my-nginx-container
    image: nginx
    resources:
      requests:
        cpu: 256m
        memory: 512Mi
      limits:
        cpu: 512m
        memory: 1Gi
```

This node vanishes as soon as the pod gets deleted.

Now coming to limitations on Fargate. Below are my findings.

1. 20GB disk for each machine - you will get 20GB Ephermel storage for each node provisioned by using the Fargate profile, you cannot increase while using EKS. (You have the option to increase this if you are using ECS )
    
2. Runs only on the private subnet of the EKS cluster.
    
3. You cannot mount volumes on fargate nodes. (its simply not possible now according to aws docs)
    
4. Daemonsets will not run in these nodes.
    
5. For each pod, aws will provision a separate node.
    

You can check below link  for further information and considerations.

[**AWS Fargate - Amazon EKS**](https://docs.aws.amazon.com/eks/latest/userguide/fargate.html)

Thank you for reading ....
