How to create EKS cluster in AWS Using eksctl
Create Amazon EKS cluster by eksctl
What is Amazon EKS?
Amazon EKS is a fully managed container orchestration service. EKS allows you to quickly deploy a production-ready Kubernetes cluster in AWS, deploy and manage containerized applications more easily with a fully managed Kubernetes service.
EKS takes care of the master node/control plane. We need to create worker nodes.
EKS cluster can be created in the following different ways:
1. AWS console
2. AWS CLI
3. eksctl command
4. using Terraform
We will create an EKS cluster using eksctl command line tool.
Pre-requisites:
Install AWS CLI:
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" sudo apt install unzip unzip awscliv2.zip sudo ./aws/install aws --version
Install eksctl:
curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp sudo mv /tmp/eksctl /usr/local/bin eksctl version
Install kubectl:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl kubectl version
Create IAM Role with Administrator Access:-
You need to create an IAM role with the AdministratorAccess policy.
Go to the AWS console, IAM, and click on Roles. create a role
Select AWS services, Click EC2, and Click on Next permissions.
Now search for AdministratorAccess policy and click
Skip on creating a tag.
Now give a role name and create it.
Assign the role to the EC2 instance:
Go to the AWS console, click on EC2, select EC2 instance, and Choose Security.
Click on Modify IAM Role.
Choose the role you have created from the dropdown.
Select the role and click on Apply.
Let's Create an EKS Cluster
Create EKS Cluster with two worker nodes using eksctl:
eksctl create cluster \
--name MyEKS \
--version 1.22 \
--region us-east-2 \
--nodegroup-name my-worker-nodes \
--node-type t2.micro \
--nodes 2
The above command should create an EKS cluster in AWS, it might take 15 to 20 mins. The eksctl tool uses CloudFormation under the hood, creating one stack for the EKS master control plane and another stack for the worker nodes.
This should confirm that the EKS cluster is up and running.
eksctl get cluster --name MyEKS --region us-east-2
Update the Kube config by entering the below command:
aws eks update-kubeconfig --name MyEKS --region us-east-2
you can view the kubeconfig file by entering the below command:
cat /var/lib/jenkins/.kube/config
Connect to EKS cluster using kubectl commands:
To view the list of worker nodes as part of the EKS cluster.
kubectl get nodes
To view the NameSpaces:
kubectl get ns
Deploy Nginx on a Kubernetes Cluster
Let us run some apps to make sure they are deployed to the Kubernetes cluster. The below command will create a deployment.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
View Deployments:
kubectl get deployments
Delete EKS Cluster using eksctl
eksctl delete cluster --name MyEKS --region us-east-2
OR
Login to AWS console --> AWS Cloud formation --> delete the stack manually.
you can also delete the cluster under AWS console --> Elastic Kubernetes Service---> Clusters --> Click on Delete cluster.
Conclusion
I hope this can help get you started with Kubernetes right away so that you can begin building and deploying applications on the AWS EKS cluster.
In the future, I would like to write articles about using Kubernetes with Terraform (such as Kubernetes provider and helm provider), whether the cluster was created with Terraform or not. This way, you can develop a modular infrastructure script, unladen with unnecessarily complex scripts.
Best of success with your Kubernetes adventures. Enjoy!