AWS

How to Setup a Kubernetes Cluster on an AWS EC2 Instance?

Kubernetes cluster is a set of hosts for running the isolated containers which can be utilized to run applications. EC2 instance is a virtual machine running on the cloud that provides a complete infrastructure running on top of the local machine.

This guide will demonstrate the process of configuring a Kubernetes cluster on an AWS EC2 instance.

How to Setup a Kubernetes Cluster on an AWS EC2 Instance?

Follow these simple steps to set up Kubernetes on an AWS EC2 instance.

Step 1: Connect to EC2 Instance

To connect to the instance, it is required to have an EC2 instance created and in the “Running” state. After that, select it and click on the “Connect” button:

Copy the provided command by the platform:

Paste the command on the terminal and change the path of the key pair file from the system:

Update the apt packages:

sudo apt-get update

Step 2: Install AWS CLI

Download the AWS CLI file in the zipped format using the link of the official website:

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

Unzip the AWS CLI file:

unzip awscliv2.zip

Install the AWS CLI:

sudo ./aws/install

Verify the installed version of the AWS CLI:

aws --version

The installed version displayed in the screenshot below is “aws-cli/2.11.2”:

Step 3: Install Kubectl

Download the Kubectl file from the link provided in the following command:

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl

Assign the required permissions to the kubectl:

chmod +x ./kubectl

Move the Kubectl file to the location mentioned in the command below:

sudo mv ./kubectl /usr/local/bin/kubectl

Step 4: Grant Permissions to IAM User

Head into the IAM dashboard and assign the following permissions to the IAM user:

  • AmazonEC2FullAccess
  • AmazonRoute53FullAccess
  • AmazonS3FullAccess
  • IAMFullAccess:

Step 5: Attach IAM User to EC2

Configure the AWS CLI by providing the credentials of the IAM user:

aws configure

To get the complete process of AWS CLI configuration, click here:

Step 6: Install Kops

Download Kops utility from the GitHub:

curl -LO https://github.com/kubernetes/kops/releases/download/$(curl -s https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d '"' -f 4)/kops-linux-amd64

Assign the required permissions to the Kops:

chmod +x kops-linux-amd64

Move Kops to the desired directory:

sudo mv kops-linux-amd64 /usr/local/bin/kops

Step 7: Create Hosted Zone From Route 53

Head into the Route 53 dashboard and click on the “Create hosted zone” button:

Type the name of the hosted zone:

Select the “Private hosted zone” option and provide VPC ID with its Region:

Scroll down to the bottom and click on the “Create hosted zone” button:

Step 8: Create S3 Bucket

Create a bucket using the following command:

aws s3 mb s3://upload31

Note: The name of the bucket should be unique:

Verify the bucket creation by visiting the “Buckets” page on the S3 dashboard:

Allow Kubernetes to store cluster data on the bucket:

export KOPS_STATE_STORE=s3://upload31

Create SSH keys by typing the following command:

ssh-keygen

Running the above command will prompt the user to provide credentials, simply choose default by hitting Enter:

Step 9: Define Cluster to S3 Bucket

Create cluster definitions on the S3 bucket by providing the “Availability Zone” and “Cluster name”:

kops create cluster --cloud=aws --zones=ap-southeast-1a --name=k8s.cluster --dns-zone=private-zone --dns private --state s3://upload31

Step 10: Create Cluster

Now create the cluster using the following command:

kops update cluster k8s.cluster --yes

Verify the cluster creation by visiting the “Instances” page from the EC2 dashboard:

This is all about how to set up a Kubernetes cluster on an EC2 instance.

Conclusion

To set up a Kubernetes cluster on an AWS EC2 instance, create and connect to the EC2 instance. Install AWS CLI on the EC2 instance and configure it with the IAM user containing the required permissions. Install Kubectl and Kops on the instance and then create an S3 bucket to store the cluster’s data on it. Finally, create clusters and verify them from the EC2 dashboard. This guide has explained how to configure a Kubernetes cluster on an AWS EC2 instance.

About the author

Talha Mahmood

As a technical author, I am eager to learn about writing and technology. I have a degree in computer science which gives me a deep understanding of technical concepts and the ability to communicate them to a variety of audiences effectively.