Recently I made the decision to move my applications to Kubernetes, specifically hosted in an EKS cluster on AWS. Before making this decision, my applications (saintsxctf.com and jarombek.com) were hosted using different methods. saintsxctf.com was hosted on autoscaled AWS EC2 instances and jarombek.com was hosted on AWS ECS. I also had prototypes using different hosting methods and a Jenkins server which was hosted on EC2 instances. Moving all these applications to Kubernetes unifies the deployment process and allows me to take advantage of containerization and container orchestration.
In this article, I'll discuss the process for setting up my EKS cluster with Terraform. I'll also detail my experience deploying ALB Ingress Controller and External DNS pods on the cluster.
One thing I've noticed with EKS is that it's very difficult to create Terraform infrastructure for a cluster from scratch. This difficulty is also true for CloudFormation, so it seems to be an EKS specific weakness. Because of this, I decided to use a community made EKS module from the Terraform registry. I've found this module to be very reliable, and because EKS is updated rather frequently, it helps me avoid spending time fixing my Terraform configuration.
With the community Terraform module, configuration for a cluster is very simple. Besides calling the module, the only resource needed to get the cluster set up is an IAM policy for worker nodes in the cluster (depending on your use case, this can be omitted as well). The following code snippet configures the cluster.
Once the cluster is running, I create Kubernetes objects and resources that can be utilized by all applications. The first resources I create are namespaces, which provide a logical separation of the cluster for different applications and environments. Namespaces act as virtual clusters with object and resource name scoping1. This means there can be two objects (for example, two pods) with the same name in different namespaces.
In my cluster, each application gets at least one namespace (except for prototypes, which all exist in the same namespace). If the application has a development environment along with a production environment, it gets one namespace for each environment. For example, my jarombek.com application has two namespaces -
There are multiple ways to automate the creation of these Kubernetes objects. Some approaches include using the
kubectl CLI or using a high-level programming language API such as the Kubernetes Go client2. The approach I decided to take was to just use Terraform! Terraform has a provider which allows you to provision Kubernetes objects and resources on a cluster.
The biggest benefit of using Terraform is that the same language and CLI commands are used to build the cluster and the Kubernetes objects. The Terraform documentation lists out some other reasons to use Terraform for Kubernetes object management.
Below is an example of a Kubernetes object defined in Terraform. It defines a namespace for my jarombek.com application.
The Kubernetes provider needs to be configured once before building any Kubernetes resources with Terraform. As you can see in the code snippet, I pass parameters to the
provider object from the existing
aws_eks_cluster_auth data sources on AWS. Then I can create a new
kubernetes_namespace resource. For reference, an equivalent YAML file for this resource looks like this:
I often keep the YAML documents in a folder alongside my Terraform infrastructure for reference.
Kubernetes provides multiple ways to configure networking capabilities. Three options are available to make applications on Kubernetes accessible outside the cluster - a
NodePort service, a
LoadBalancer service, or an
LoadBalancer services have limitations -
NodePort services can only use ports 30000-32767 and
LoadBalancer services can only direct traffic to a single application4. Due to these limitations, I use
Ingress objects and ingress controllers for directing traffic to my applications.
Ingress objects take network requests and forward them to services defined in the Kubernetes cluster. In order for an ingress to work, an ingress controller must be running in the cluster. An ingress controller is simply a pod. There are many different ingress controller implementations. AWS has its own implementation called the ALB Ingress Controller. The ALB Ingress Controller watches for Ingress objects and creates AWS infrastructure such as Application Load Balancers (ALBs) if the Ingress objects have the proper tags5. For example, my Jenkins server has the following Ingress object:
This Ingress object routes traffic from the jenkins.jarombek.io and www.jenkins.jarombek.io domains to a Jenkins server on my Kubernetes cluster. Take note of all the tags on the Ingress object with the
alb.ingress.kubernetes.io prefix. The ALB Ingress controller uses these annotations to determine the configuration of the load balancer it builds on AWS6. Also notice there is an additional annotation with the
external-dns.alpha.kubernetes.io prefix. Although the ALB Ingress Controller builds the load balancer AWS infrastructure, it does not build any AWS Route53 DNS records for the domain names that the load balancer takes in traffic from. To build these DNS records, an additional pod called External DNS needs to run on the Kubernetes cluster7. External DNS is not strictly tied to AWS, but in my use cases I ask it to configure AWS Route53 records. To start the ALB Ingress Controller and External DNS, I added more Terraform Kubernetes resources. The configuration is a bit long, so you can view it in main.tf on GitHub.
One of the things I've noticed is that sometimes the ALB Ingress Controller and External DNS don't work as I expect them to. Luckily this is pretty easy to debug. When The Terraform EKS module creates the cluster, it also creates a kubeconfig file which can be used for kubectl authentication. Since the ALB Ingress Controller and External DNS are simply Kubernetes pods, their logs can be viewed with kubectl. Doing this is as simple as the following commands:
I've always enjoyed using Kubernetes to orchestrate application infrastructure, so I'm happy to have my own cluster in AWS. All the code I use to build the EKS cluster is available on GitHub.