AWS EKS Cluster Setup Example: A Comprehensive Guide

Amazon Elastic Kubernetes Service (EKS) simplifies deploying, managing, and scaling containerized applications using Kubernetes. With EKS, you get a fully managed Kubernetes control plane while maintaining full flexibility for workload management. In this article, we’ll demonstrate how to set up an AWS EKS cluster step by step.


What Is Amazon EKS?

Amazon EKS is a managed Kubernetes service that:

  • Handles Kubernetes control plane operations.
  • Ensures high availability with multi-AZ deployment.
  • Integrates seamlessly with AWS services like IAM, CloudWatch, and Elastic Load Balancing (ELB).

AWS EKS Cluster Setup Example

Objective

We’ll create an EKS cluster, configure a worker node group, and deploy a sample application to the cluster.


Step 1: Prerequisites

eksctl: Install the eksctl tool:

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

kubectl: Install the Kubernetes command-line tool:

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/

AWS CLI: Install and configure the AWS CLI:

aws configure

Step 2: Create an EKS Cluster

Using eksctl

    • Cluster Name: example-cluster.
    • Region: us-east-1.
    • Node Group: Creates a managed node group with t3.medium instances.
  1. Wait for the cluster creation to complete. The process includes provisioning the control plane and worker nodes.

Run the following command to create a cluster:

eksctl create cluster \
  --name example-cluster \
  --region us-east-1 \
  --nodegroup-name standard-workers \
  --node-type t3.medium \
  --nodes 2 \
  --nodes-min 1 \
  --nodes-max 3 \
  --managed

Step 3: Configure kubectl

    • You should see a list of the worker nodes in the Ready state.

Verify the cluster connection:

kubectl get nodes

Update the Kubernetes configuration:

aws eks update-kubeconfig --region us-east-1 --name example-cluster

Step 4: Deploy a Sample Application

Create a Deployment

Verify the deployment:

kubectl get deployments

Apply the deployment:

kubectl apply -f deployment.yaml

Create a YAML file (e.g., deployment.yaml) with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
        ports:
        - containerPort: 80

Expose the Application

    • Access the application using the external URL provided by the LoadBalancer.

Get the LoadBalancer URL:

kubectl get services

Apply the service:

kubectl apply -f service.yaml

Create a service YAML file (e.g., service.yaml):

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

Step 5: Monitor the Cluster

  1. View Cluster Metrics:
    • Use CloudWatch Container Insights to monitor resource usage.
  2. Check Logs:
  3. Scale the Deployment:

Scale the number of replicas:

kubectl scale deployment nginx-deployment --replicas=4

Access application logs:

kubectl logs <pod-name>

Best Practices for AWS EKS

  1. Use Managed Node Groups:
    • Simplify lifecycle management with auto-scaling and patching.
  2. Secure Cluster Access:
    • Restrict IAM roles and use Kubernetes Role-Based Access Control (RBAC).
  3. Enable Logging:
    • Enable control plane logging to troubleshoot Kubernetes API issues.
  4. Monitor Costs:
    • Use AWS Cost Explorer to track EKS usage and optimize costs.

Conclusion

AWS EKS provides a scalable, managed Kubernetes environment that simplifies the deployment of containerized applications. This example demonstrated how to set up an EKS cluster, deploy a sample application, and monitor the cluster. With EKS, you can focus on managing workloads while AWS handles the underlying infrastructure.