How to Set Up a Kubernetes Cluster on AWS

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

Setting up a Kubernetes Cluster on AWS can seem like a daunting task, but with Amazon’s Elastic Kubernetes Service (EKS), the process becomes more streamlined and accessible. In this tutorial, we will walk through setting up a basic Kubernetes cluster on AWS using EKS. We’ll cover all the necessary steps such as setting up an IAM role, creating an EKS cluster, and configuring your kubectl to communicate with your new cluster.

Prerequisites

  • An AWS account
  • AWS Command Line Interface (CLI) installed and configured
  • kubectl installed on your local machine
  • Basic understanding of Kubernetes concepts

Step-by-Step Instructions

Step 1: Setting Up AWS CLI and IAM Role

Before we start, we need to ensure the AWS CLI is installed and configure your AWS credentials. You can do this with the following command:

aws configure

Next, you need to create an IAM role that provides permissions for EKS to make calls to other AWS services on your behalf. Here’s how you can create an IAM role with the necessary permissions:


aws iam create-role --role-name my-eks-role --assume-role-policy-document file://trust-policy.json

Note that trust-policy.json should contain the following policy:


{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "eks.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

Step 2: Creating an Amazon EKS Cluster

Amazon EKS requires a VPC, so if you don’t have one, you’ll need to create it. You can create one manually or use AWS CloudFormation with an Amazon-provided template. Once the VPC is ready, you can go ahead and create an EKS cluster. Use the following command:


aws eks create-cluster --name my-cluster --role-arn arn:aws:iam::YOUR_AWS_ACCOUNT_ID:role/my-eks-role --resources-vpc-config subnetIds=subnet-abcdef12,subnet-ghijk34,subnet-lmnop56,securityGroupIds=sg-789abc123

Replace the subnet IDs and security group IDs with the ones from your VPC. The cluster creation process can take several minutes.

Step 3: Configuring kubectl for EKS

With the cluster created, it’s time to configure kubectl. You will update your kubeconfig file with the following command:


aws eks update-kubeconfig --name my-cluster

This will allow you to start interacting with your Kubernetes cluster using kubectl commands.

Step 4: Worker Nodes Setup

In order to run your applications, you need worker nodes. These are EC2 instances that are registered with the EKS cluster. You can create worker nodes by using an AWS-provided AMI and CloudFormation template, or through the AWS Management Console.

Conclusion

In this tutorial, you learned the basic steps to set up a Kubernetes cluster on AWS using EKS. By now, you should have a functioning Kubernetes environment in the AWS cloud. Remember to delete your cluster if you are done experimenting to avoid unnecessary charges.

This is just the beginning; exploring and mastering services and tools related to Kubernetes helps improve workflow, efficiency, and scalability of your application deployment and management.