Amazon Elastic Kubernetes Service
Naitik Shah
Data Scientist | Machine Learning Engineer | AI & MLOps | Generative AI, NLP & Big Data | Fraud Analytics & Predictive Modeling | AWS | Azure | GCP
So, hey guys, in this tutorial, I will be teaching you some practical on Amazon Elastic Kubernetes Services, popularly known as EKS.
So we will be using CLI version of AWS, so let's set it up first.
Setting CLI of AWS:
Download the software depending upon your base O.S, mine is Windows so it downloaded .msi file, double click and click on next, until it installs, to verify it, type this in cmd:
The above output would be identical, just the version number might change.
Creating IAM user:
Now to use AWS from CLI, you need to have login and password, for that let's create an IAM in AWS, first, create an account on AWS, and then follow these steps:
Click on Users:
In your case the number of users might be different.
Then go on add user:
Put any name you want.
Click on next
And select first option on the next page:
Click on next two times, then click Create User and then on the next page, don't forget to Download .csv file, or else you won't be able to login using CLI.
In command line type:
C:\Users\Naitik>aws configure
AWS Access Key ID [None]: xxxxxxxxxxxxxxX3
AWS Secret Access Key [None]: xxxxxxxxxxxxxxxxxxxxxxxxxxx
Default region name [None]: ap-south-1
Default output format [None]:
Get the access key and secret access key from the csv file you downloaded earlier.
To get more help on what is EKS, run the below command:
To list all the clusters:
You will see the above output as there are no clusters.
Although you can get launch cluster using the normal commands of AWS, but it is a huge topic, and for that, I will be using a specific command with which, a single command will launch whole cluster.
So to setup eksctl, it is an easy topic, any YouTube video or an article with help you with that.
To check whether eksctl command was installed properly, run this command:
To check if any clusters are active or not using eksctl command, run the below command, if there are no clusters, the below output will be identical to yours:
But, before running the above command, make sure you have executed the aws configure command, which shown in the start of the article.
The above command will create a cluster.yml file in the directory you will choose.
This is the format of cluster.yml file:
eksctl command is powerful, that a single command will launch whole cluster within minutes, to create a cluster, run the below command:
It will look like this when you hit enter:
Put the same file name in the command, which you have given to the config file. It might take a few minutes to launch(15-20 Minutes to be accurate).
To download kubectl command, follow the steps in the below:
Click on the first link, and then scroll down, until you find your base O.S, in my case it is windows.
Click on this link and it will download an .exe file.
Then set an path of the folder which is containing the downloaded .exe file.
I suggest you to keep all downloaded files which needs to have specified path in one folder, so that you need to edit path only once, in my case:
For checking whether your cluster was deployed properly, run the following command, the output should be similar, just the name of the cluster will be different, depending upon what you named it.
To verify your cluster in WebUI, Sign in to your AWS console and then:
Then to delete the cluster, as you are charged on hourly basis, and if you are working on new account of AWS, then t2.micro is free, but t2.small is paid, although the charge is less, but it is paid, run the following command:
It will take a little bit of time to delete, and then even crosscheck using WebUI as I explained earlier. After deleting the cluster is successful, you will get this type of output:
If the deletion is Successful, in WebUI, you will be redirected to Creating a cluster.
Ok, so let's see how to connect to the Kubernetes cluster, and for that we need the kubectl command and to set it up, I have added the screenshots above.
First Navigate to the folder path in the image below:
If you don't have the config file, chill, I will show you how to make it.
For the ones who don't have config file from before, follow the steps below:
Open command line and type:
The above output means that you don't have the config file, now run the below command:
Remember that your cluster should be running at that the time when you run the above command, and in my case, my cluster name is naitik-cluster, so my command will contain my cluster name, in yours it will be different.
To crosscheck how many nodes have been launched, type the below command:
To describe a specific nodes, run this command:
So, as you can notice, the above command is kubectl describe nodes (name of the node you want to describe, in your case it will be different, you can get the name from kubectl get nodes.
Let's see how to create your own name space using kubectl command, for that, follow the steps below:
In the above command, in the place of nspace, put a name of your choice.
To check whether the namespace was created, run the following command:
So, let's see the output of kubectl get pods:
But why, we just created a namespace, so the nspace I launched is not the default namespace, let's see how we can change that:
To check whether the namespace was changed:
As you can see above, the namespace is now nspace, the name of my namespace, in your case it will be different.
To check the cluster connectivity:
The command is kubectl cluster-info
To launch any docker image on Kubernetes:
For this I have used Vimal sir's apache image from docker.
To verify the same:
For some extra details about the pod:
The command is kubectl get pods -o wide
To scale your deployment, run the following command:
In the above example, the number of pods will be scaled to 3.
To check number of pods:
How to expose your deployment:
The above command will use LoadBalancer service.
The get a list of all services you are using, the command is:
To delete all the services:
To get pvc storage:
This is how the code in notepad should be:
The above code means:
10GB data, the PVC name would be nspvc1, and the access type would be ReadWriteOnce.
To create the pvc:
To check whether your pvc was successfully launched:
Let's use the feature of auto scaling of AWS, first head to this website below and copy the code below node groups:
And paste the code below in your clusters.yml file, you may make some changes. Your yml should look like this, it may be different depending upon the choices you have made, but the basic outline should be the same:
Let's launch this cluster, I have given the code above to launch the cluster, but here it is again:
To see the nodegroup:
To check the same in WebUI:
Got to EC2
In the left column, you will find spot instances, click it and verify the same.
Okay, so re launch your cluster using the updated yml file, which contains your ssh key, which willbe different for everyone.
Navigate to the folder (in the command prompt) where you have downloaded the ssh key, and then:
Voila, you have logged in to the slave and now run:
sudo yum install amazon-efs-utils -y
So, now go to AWS WebUI, and under the services section, search efs:
And then, click on create file system:
And then:
Click next:
Click next:
Click Create:
You will be greeted with Success:
efs.yml file:
Your values won't match, so it won't be same, if you copy paste from this, you will get some error.
In the first place, select the equivalent vpc as that of the bunch made by the eksctl. You can affirm the vpc from the VPC Dashboard. At that point we have to connect the security bunch ( which resembles a firewall) to the Instances which would be running as the NFS server. So it is mandatory that the Security bunch u append to the Efs examples ought to be equivalent to that of the cases in which all hubs can interface with one another and all ports are permitted inside that security gathering/s as we need the NFS administration port number to be permitted in all the occasion where the EFS should have been mounted.
Creating a RBAC(Role based access control) file:
Now time for mysql.yml:
apiVersion: v1 kind: Service metadata: name: joomla-mysql labels: app: joomla spec: ports: - port: 3306 selector: app: joomla tier: mysql clusterIP: None --- apiVersion: apps/v1 kind: Deployment metadata: name: joomla-mysql labels: app: joomla spec: selector: matchLabels: app: joomla tier: mysql strategy: type: Recreate template: metadata: labels: app: joomla tier: mysql spec: containers: - image: mysql:5.7 name: mysql env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-ns key: naitik1234 ports: - containerPort: 3306 name: mysql volumeMounts: - name: mysql-pvc mountPath: /var/lib/mysql volumes: - name: mysql-pvc persistentVolumeClaim: claimName: mysql-efs
Now time for joomla.yml:
apiVersion: v1 kind: Service metadata: name: joomla labels: app: joomla spec: ports: - port: 80 selector: app: joomla tier: frontend type: LoadBalancer --- apiVersion: apps/v1 kind: Deployment metadata: name: joomla labels: app: joomla spec: selector: matchLabels: app: joomla tier: frontend strategy: type: Recreate template: metadata: labels: app: joomla tier: frontend spec: containers: - image: joomla name: joomla env: - name: JOOMLA_DB_HOST value: joomla-mysql - name: JOOMLA_DB_PASSWORD valueFrom: secretKeyRef: name: mysql-ns key: naitik1234 ports: - containerPort: 80 name: joomla volumeMounts: - name: joomla-pvc mountPath: /var/www/html volumes: - name: joomla-pvc persistentVolumeClaim: claimName: joomla-efs
Now time to run all yml file:
Now you all know how to run a yml file.
Open Joomla installation page from external IP
Now install it and voila, you just learnt Amazon EKS.
Now let's clear out the running tasks, first let's delete EFS, head to EFS and click on Actions:
Then click Delete file system, now it will ask to enter the file system number which will be provided in the above text, just copy and paste:
Remember to delete EFS, or else you will be charged $0.080 every hour.
Now let's get rid of the cluster, if possible, please delete the cluster from CLI using the below command as in some cases, deleting the cluster from WebUI didn't completely delete the cluster, and it kept on charging.
Thank you guys for bearing with me till end, I know this article is too long, sorry for that, but I tried to explain each and every command.