Saturday, 13 January 2024

2048 game in few steps on AWS-EKS

 


Create an EKS cluster and deploy 2048 game into that cluster

==================================================


Task 1: Create an EKS cluster

=============================

Name: <yourname>-eks-cluster-1

Use K8S version 1.25


Create an IAM role 'eks-cluster-role' with 1 policy attached: AmazonEKSClusterPolicy

Create another IAM role 'eks-node-grp-role' with 3 policies attached: 

(Allows EC2 instances to call AWS services on your behalf.)

    - AmazonEKSWorkerNodePolicy

    - AmazonEC2ContainerRegistryReadOnly

    - AmazonEKS_CNI_Policy


Choose default VPC, Choose 2 or 3 subnets

Choose a security group which open the ports 22, 80, 8080

cluster endpoint access: public


# For VPC CNI, CoreDNS and kube-proxy, choose the default versions, For CNI, latest and default are 

# different. But go with default.


Click 'Create'. This process will take 10-12 minutes. Wait till your cluster shows up as Active. 



Task 2: Add Node Groups to our cluster

======================================

Now, lets add the worker nodes where the pods can run


Open the cluster > Compute > Add NodeGrp

Name: <yourname>-eks-nodegrp-1 

Select the role you already created

Leave default values for everything else


AMI - choose the default 1 (Amazon Linux 2)

change desired/minimum/maximum to 1 (from 2)

Enable SSH access. Choose a security group which allwos 22, 80, 8080


Choose default values for other fields 


Node group creation may take 2-3 minutes



Task 3: Authenticate to this cluster

===================================

Reference:

https://docs.aws.amazon.com/eks/latest/userguide/create-kubeconfig.html


Open cloudshell


# Type on your AWS CLI window 

aws sts get-caller-identity

# observe your account and user id details


# Create a  kubeconfig file where it stores the credentials for EKS:

# kubeconfig configuration allows you to connect to your cluster using the kubectl command line.

aws eks update-kubeconfig --region region-code --name my-cluster

ex: aws eks update-kubeconfig --region us-east-1 --name unus-eks-cluster-1 # Use the cluster name you just 

created



# see if you can get the nodes you created

kubectl get nodes


# Install nano editor in cloudshell. We will need this in the next task

sudo yum install nano -y




Task 4: Create a new POD in EKS for the 2048 game

================================================


# clean up the files in cloudshell (Optional)

rm *.* 


# create the config file in YAML to deploy 2048 game pod into the cluster

nano 2048-pod.yaml


### code starts ###

apiVersion: v1

kind: Pod

metadata:

   name: 2048-pod

   labels:

      app: 2048-ws

spec:

   containers:

   - name: 2048-container

     image: blackicebird/2048

     ports:

       - containerPort: 80


### code ends ###



# apply the config file to create the pod

kubectl apply -f 2048-pod.yaml

#pod/2048-pod created


# view the newly created pod

kubectl get pods



Task 5: Setup Load Balancer Service

===================================

nano mygame-svc.yaml  


### code starts ###


apiVersion: v1

kind: Service

metadata:

   name: mygame-svc

spec:

   selector:

      app: 2048-ws

   ports:

   - protocol: TCP

     port: 80

     targetPort: 80

   type: LoadBalancer


### code ends ###


# apply the config file

kubectl apply -f mygame-svc.yaml


# view details of the modified service

kubectl describe svc mygame-svc


# Access the LoadBalancer Ingress on the kops instance

curl <LoadBalancer_Ingress>:<Port_number>

or

curl a06aa56b81f5741268daca84dca6b4f8-694631959.us-east-1.elb.amazonaws.com:80

(try this from your laptop, not from your cloudshell)


# Go to EC2 console. get the DNS name of ELB and paste the DNS into address bar of the browser

# It will show the 2048 game. You can play. (need to wait for 2-3 minutes for the 

# setup to be complete)



Task 3: Cleanup

---------------

# Clean up all the resources created in the task

kubectl get pods

kubectl delete -f 2048-pod.yaml


kubectl get services

kubectl delete -f mygame-svc.yaml


####################################################################


Adding Screenshots of process





Connect with me 😊

Labels: ,

Create Amazon EKS cluster by eksctl | How to create EKS cluster in AWS cloud using eksctl | Create EKS Cluster in command line using IAM Role


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 master node/control plane. We need to create worker nodes.

EKS cluster can be created in following ways:

1. AWS Console
2. AWS CLI
3. eksctl Command
4. using Terraform

We will create EKS cluster using eksctl command line tool.

Pre-requisites:

This Lab is using Jenkins EC2 instance. Jenkins EC2 instance needs to have following configured:


Create IAM Role with Administrator Access

You need to create an IAM role with AdministratorAccess policy.
Go to AWS console, IAM, click on Roles. create a role


Select AWS services, Click EC2, Click on Next permissions.
 

 Now search for AdministratorAccess policy and click



Skip on create tag.
Now give a role name and create it.

Assign the role to EC2 instance
Go to AWS console, click on EC2, select EC2 instance, Choose Security.
Click on Modify IAM Role



Choose the role you have created from the dropdown.
Select the role and click on Apply.

Switch to Jenkins user
sudo su - jenkins

Create EKS Cluster with two worker nodes using eksctl

eksctl create cluster --name demo-eks --region us-east-1 --nodegroup-name my-nodes --node-type t3.small --managed --nodes 2 

the above command should create a 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. 

eksctl get cluster --name demo-eks --region us-east-1

This should confirm that EKS cluster is up and running.

Update Kube config by entering below command:

aws eks update-kubeconfig --name demo-eks --region us-east-1

kubeconfig file be updated under /var/lib/jenkins/.kube folder.

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 EKS cluster.

kubectl get nodes

kubectl get ns

Deploy Nginx on a Kubernetes Cluster
Let us run some apps to make sure they are deployed to Kubernetes cluster. The below command will create deployment:

kubectl create deployment nginx --image=nginx


View Deployments
kubectl get deployments

Delete EKS Cluster using eksctl

eksctl delete cluster --name demo-eks --region us-east-1

the above command should delete the EKS cluster in AWS, it might take a few mins to clean up the cluster.

Errors during Cluster creation
If you are having issues when creating a cluster, try to delete the cluster by executing the below command and re-create it.

eksctl delete cluster --name demo-eks --region us-east-1


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


Labels: ,