DEV Community

Cover image for Bash Script : How to Create a k8s Cluster on AWS EKS using eksctl.
Israel .O. Ayanda
Israel .O. Ayanda

Posted on • Updated on

Bash Script : How to Create a k8s Cluster on AWS EKS using eksctl.

This article is a simple shell script tutorial about automating your kubernetes cluster setup on Amazon Elastic Kubernetes Service (EKS).

AWS EKS: Amazon Elastic Kubernetes Service (EKS) is a managed service and certified Kubernetes conformant to run Kubernetes on AWS and on-premises.

Eksctl: is a simple CLI tool for creating and managing clusters on EKS - Amazon's managed Kubernetes service for EC2.

Prerequisite

Create an access key for your IAM user

  • Login into your AWS account
  • Go to IAM dashboard
  • Select Users and click on your IAM user name
  • Click on Security Credential Tab
  • Scroll down to Access keys and Click on Create access key
  • Select Command Line Interface (CLI), agree with recommendation click Next and follow the prompts.

access key

Configure AWS CLI

# In your CLI, configure AWS cli with your access keys
aws configure
Enter fullscreen mode Exit fullscreen mode

aws configure

Create a file with permission

# Create file
vi scriptfile.sh

# Make execute permission
chmod +x scriptfile.sh
Enter fullscreen mode Exit fullscreen mode

Copy and paste the code snippet below. Change the variables to your preferred names and values. Also change the values under the Creation of EKS cluster to preferred cluster requirements.

# Variables
CLUSTER_NAME=my-cluster
REGION=us-east-1
NODE_NAME=Linux-nodes
KEY_NAME=instance

# Set AWS credentials before script execution

aws sts get-caller-identity >> /dev/null
if [ $? -eq 0 ]
then
  echo "Credentials tested, proceeding with the cluster creation."

  # Creation of EKS cluster
  eksctl create cluster \
  --name $CLUSTER_NAME \
  --version 1.22 \
  --region $REGION \
  --nodegroup-name $NODE_NAME \
  --nodes 2 \
  --nodes-min 1 \
  --nodes-max 4 \
  --node-type t3.micro \
  --node-volume-size 8 \
  --ssh-access \
  --ssh-public-key $KEY_NAME \
  --managed

  if [ $? -eq 0 ]
  then
    echo "Cluster Setup Completed with eksctl command."
  else
    echo "Cluster Setup Failed while running eksctl command."
  fi
else
  echo "Please run aws configure & set right credentials."
  echo "Cluster setup failed."
fi

Enter fullscreen mode Exit fullscreen mode

Make sure the key pair is in the same directory with the script or you specify.

script

aws sts get-caller-identity - checks if the AWS identity is created correctly. if [ $? -eq 0 ] - check if the last command, in this case aws sts get-caller-identity succeeded without no error. If it equals 0, it should go ahead and create the cluster. After the cluster is created, print a created message else inform us the AWS credential was not configured correctly.

eks cluster

Cluster Nodes on EC2 dashboard

ec2

Clean Up

  # Delete EKS Cluster
  eksctl delete cluster my-cluster
Enter fullscreen mode Exit fullscreen mode

delete cluster

As always, I look forward to getting your thoughts on this article. Please feel free to leave a comment!

Top comments (0)