Sling Academy
Home/DevOps/How to Set Up a Kubernetes Cluster on AWS

How to Set Up a Kubernetes Cluster on AWS

Last updated: January 31, 2024

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.

Next Article: How to Use Kubernetes on DigitalOcean

Previous Article: How to use Minikube for Kubernetes local development

Series: Kubernetes Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide