Amazon Elastic Kubernetes Service (EKS) is a managed service that makes it easier for you to run Kubernetes on AWS without needing to install and operate your own Kubernetes control plane or node infrastructure. One of the simplest tools for creating and managing an EKS cluster is `eksctl`, a command-line tool built by Weaveworks for Amazon EKS. In this blog, we will guide you through the process of creating an EKS cluster using `eksctl`, covering prerequisites, steps, and some basic configurations.
Prerequisites
Before you start, make sure you have the following:
1. “AWS Account:” You need an AWS account. If you don’t have one, you can create it at AWS website.
2. “AWS CLI:” Install and configure the AWS Command Line Interface (CLI). It should be configured with at least the minimum required permissions to create an EKS cluster.
3. “eksctl”: You need to install `eksctl`. It is available for Windows, macOS, and Linux. For installation instructions, refer to the eksctl installation Guide
4. “kubectl”: This is the Kubernetes command-line tool that allows you to run commands against Kubernetes clusters. You can find installation instructions on the [official Kubernetes website](https://kubernetes.io/docs/tasks/tools/).
Step 1: Create an EKS Cluster
Once you have all the prerequisites ready, you can proceed to create your EKS cluster. Open your terminal and type the following command:
eksctl create cluster --name my-cluster --version 1.28 --region us-west-2 --nodegroup-name my-nodes --node-type t3.medium --nodes 3 --nodes-min 1 --nodes-max 4 --managed
Let’s break down what each parameter means:
– `–name`: Specifies the name of your EKS cluster.
– `–version`: The Kubernetes version for your EKS cluster.
– `–region`: The AWS region where your cluster will be created.
– `–nodegroup-name`: The name of your node group.
– `–node-type`: The type of EC2 instances used for your nodes.
– `–nodes`: The initial number of nodes in your cluster.
– `–nodes-min`: The minimum number of nodes your cluster can scale down to.
– `–nodes-max`: The maximum number of nodes your cluster can scale up to.
– `–managed`: Specifies that you want to use AWS-managed nodes.
This command will start the cluster creation process, which might take around 10-15 minutes. It takes care of setting up your cluster control plane, nodes, and a default node group that conforms to the parameters you’ve specified.
Step 2: Configure `kubectl` to Connect to Your Cluster
After your cluster is created, you need to configure `kubectl` to communicate with your new cluster. `eksctl` automatically updates your `kubeconfig` file, which is typically located at `~/.kube/config`. You can ensure it’s correctly configured by running:
kubectl get svc
This command should return the services in your cluster, and if everything is set up correctly, you should see the Kubernetes API server listed.
Step 3: Deploying Applications
Now that your cluster is up and running, you can start deploying applications.
For example, to deploy a simple nginx application, you can use:
kubectl create deployment nginx –image=nginx
To expose your nginx deployment, you can create a service:
kubectl expose deployment nginx –port=80 –type=LoadBalancer
This command creates a load balancer and exposes it to the internet with an AWS Elastic Load Balancer (ELB).
Additional Cluster Configuration Options
When creating an EKS cluster with eksctl, you have several options that can be tailored to suit specific needs. Here are a few:
- VPC Configuration: You can specify a custom VPC and subnets using the
--vpc-public-subnets
and--vpc-private-subnets
flags. This is particularly useful for integrating the cluster into your existing network architecture.eksctl create cluster --name my-cluster --region us-west-2 --vpc-public-subnets subnet-12345678,subnet-87654321
- Security Groups:
eksctl
allows you to specify custom security groups for the control plane and worker nodes, enhancing the security stance of your cluster. - IAM Roles: For enhanced security and fine-grained access control, you can specify custom IAM roles for the cluster and worker nodes.
eksctl
supports IAM with OIDC which allows you to assign IAM roles directly to Kubernetes services.eksctl create cluster --name my-cluster --node-role-name my-custom-role
- Logging: Enable specific logging options for API server, audit, scheduler, etc., by specifying the
--enable-logging
flag. This is crucial for monitoring and troubleshooting.eksctl create cluster --name my-cluster --enable-logging api,audit
Autoscaling
To handle varying loads, eksctl
supports integration with the Kubernetes Cluster Autoscaler. To enable autoscaling, you need to allow the Cluster Autoscaler to modify the EC2 Auto Scaling groups.
eksctl create cluster --name my-cluster --asg-access
After creating the cluster, install the Cluster Autoscaler:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/autoscaler/master/cluster-autoscaler/cloudprovider/aws/examples/cluster-autoscaler-one-asg.yaml
Make sure to modify the manifest file to match your cluster’s specifics.
Cleanup
When you no longer need the cluster, you can delete it to avoid incurring further charges:
eksctl delete cluster –name my-cluster
Conclusion
Using `eksctl` to manage EKS clusters simplifies the process significantly. It handles much of the complexity involved in provisioning, scaling, and managing Kubernetes clusters, allowing you to focus more on deploying and managing your applications. Whether you are experimenting with Kubernetes or managing production environments, `eksctl` is a valuable tool in your DevOps toolkit.