“Serverless containers” in Kubernetes with Fargate (Part 2) — Hands-on

Mohamed Abukar
Towards AWS
Published in
4 min readSep 17, 2021

--

A hands-on article on deploying an application on Kubernetes with Fargate.

As promised, here is the second part of Fargate profiles on EKS. This article will look into deploying a “serverless” pod on EKS Fargate from scratch.

Note: We will use the eksctl CLI tool to do this for the purpose of this guide. Infrastructure as Code through Terraform or CloudFormation is advised if you are looking to set this up for your production workloads.

Prerequisites:

Create a Kubernetes cluster on AWS

A single eksctl command will create the following resources:

  • A VPC with the suitable subnets and accompanying resources
  • K8s control plane
  • Fargate profile
  • IAM role for pod execution

Note: Feel free to change the name of the cluster and region.

eksctl create cluster --name eks-fargate --region eu-west-1 -- fargate

This process may take several minutes. When complete, you should see a similar output to show that the cluster is ready to go.

Cluster and Fargate profile creation

Verifying the context

kubectl config current-context

Alternatively, you can use “kubectx” to check your current and existing contexts. Install with “brew install kubectx” for Mac and “choco” for Windows.

Current context shows that the cluster has been created

Checking nodes

We can now check if the fargate profile is ready, it should display at least one Fargate node waiting for our deployments.

kubectl get nodes
Fargate nodes ready

Note: the nodes that are running inside our VPC are not visible in the EC2 dashboard nor can you SSH into the nodes; they are fully managed and deployed by Fargate by means of “serverless” but you can SSH into a specific pod.

Also, since we haven’t specified the fargate profile by name, the default name for the fargate profile will be “fp-default”. As well as not specifying a namespace, the default namespace will be used to schedule fargate pods. These settings can be changed during setup by specifying the namespace for the Fargate pods.

Creating a custom Fargate profile

For the purpose of this guide, we can add an additional fargate profile to show that it can be customised. This can be done with the command:

eksctl create fargateprofile --namespace test --cluster eks-fargate --name fp-test

All pods deployed on the “test” namespace will be scheduled as Fargate pods. We must first create the namespace “test”. Now we can deploy some containers using a default nginx image on the “test” namespace.

Creating the namespace and deploying pod onto the Fargate-specified namespace

kubectl create ns test
kubectl create deploy fargate-pod --image=nginx -n test

We can verify the pods are running by running the command:

kubectl get pods -n test -o wide
We can see the pod running on the Fargate node.

Now our pod is running as “serverless”. Well done !

Clean up your environment

Now make sure to delete your environment so that you don’t get a heavy billing from AWS :)

Make sure you delete the Fargate profile first and then the cluster itself.

eksctl delete fargateprofile --cluster eks-fargate --name fp-test
eksctl delete cluster --name eks-fargate --region eu-west-1

Thank you and hope you enjoyed this !

References:

What’s next?

  • Look into exposing the deployment through a service?
  • Look into AWS Load Balancer controller?
  • ALB ingress controllers?
  • Setting up CloudWatch logs in order to obtain application logs?

I’m currently on a journey to learn more about the ever-growing ecosystem of containers, container orchestration and the DevOps culture. Expect to see more articles and open-source contributions. Stay tuned for more !

--

--

AWS Community Builder | Red Hat Accelerator | Platform Engineer @ Trainline