Deploying WebApps through AWS EKS(Elastic Kubernetes Service)
Amazon EKS :
Though containers have been around since the early days of Linux, Docker introduced the modern version of this technology. Kubernetes is open source software that allows you to deploy and manage containerized applications at scale. It also provides portability and extensibility, enabling you to scale containers seamlessly. But, the downside of Kubernetes is that it takes a lot of time to deploy a cluster with master and worker nodes. A turn around for this is Amazon EKS.
Amazon EKS is a managed service that makes it easy for users to run Kubernetes on AWS without needing to stand up or maintain your own Kubernetes control plane. Since Amazon EKS is a managed service it handles tasks such as provisioning, upgrades, and patching.
It can also be intergrated with various services like Elastic Load Balancer, Iam Authentication, Various Storage Services to provide scalability and securityu for the applications.
There are 2 ways to create a EKS cluster i.e. via AWS Console (WebUI) and AWS CLI.
Using CLI, there are again 2 ways:
1- Using aws eks command.
2- Using eksctl command.
(Advantage of eksctl command is, by using this command we can do a lot of customizations)
Pre - requisites:
- You should have the AWS account or create a new account before getting started
- Download the AWS CLI version2 and configure in your system and add to the path.
- You should have some prior basic knowledge of Kubernetes
Step-by-Step Implementation :
STEP 1: Creating an IAM User
* Login to the AWS Console and go to IAM User. Create one IAM User with Administrator Access.
* We can see Access_Key and Secret_Key in Security Credentials.
* Open CLI in windows with Administrator.
STEP 2: Installing eksctl programme:
Install using chocolatey or go to the Github link and download eksctl.exe file.
After downloading the file, add that file to Path.
STEP 3: Now we have to create Cluster using eksctl.
=> $ Notepad firstcluster.yml
apiVersion: eksctl.io/v1alpha5 kind: ClusterConfig metadata: name: firstcluster region: ap-south-1 nodeGroups: - name: ng1 instanceType: t2.micro desiredCapacity: 2 ssh: publicKeyName: websitekey
=> $ eksctl create cluster -f firstcluster.yml
It takes 10 to 15 minutes to create the cluster.
Check the cluster is created or not
=> $ eksctl get cluster
"The Cluster is created in AWS."
We can see in AWS Console, where cluster is created in EKS service and Nodes are created as instances in EC2 service.
STEP 4: Update the existing kubeconfig file.
Before updating the kubeconfig file, delete the existing config file.
=> C:\Users\name\.kube
We can find the config file in the above folder, delete the config file manually.
After that update using below command.
$ aws eks update-kubeconfig --name cluster1 $ kubectl cluster-info $ kubectl get nodes
STEP 5: Creating a namespace and setting it as default.
$ kubectl create namespace eksns $ kubectl config set-context --current --namespace=eksns $ kubectl config view
STEP 6: Deploying JOOMLA and MySql over the cluster using EBS in the namespace we created.
=> $ Notepad mysqldplymnt.yml
apiVersion: v1 kind: Service metadata: name: joomla-mysql labels: app: joomla spec: ports: - port: 3306 selector: app: joomla tier: mysql clusterIP: None --- # Creating a persistent volume for mysql apiVersion: v1 kind: PersistentVolumeClaim metadata: name: ebs-mysql labels: app: joomla spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi --- # Creating a mysql deployment with Recreate strategy apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 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.6 name: mysql env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-pass key: password - name: MYSQL_DATABASE value: joomla - name: MYSQL_USER value: saurabh - name: MYSQL_PASSWORD valueFrom: secretKeyRef: name: mysql-pass key: upass ports: - containerPort: 3306 name: mysql volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: ebs-mysql
=> $ Notepad joomla.yml,
*This file will launch the joomla app
# Creating a Load Balancer service apiVersion: v1 kind: Service metadata: name: joomla labels: app: joomla spec: ports: - port: 80 selector: app: joomla tier: frontend type: LoadBalancer --- # Creating a persistent volume for joomla apiVersion: v1 kind: PersistentVolumeClaim metadata: name: ebs-joomla labels: app: joomla spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi --- # Creating a joomla deployment with Recreate strategy apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 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_USER value: saurabh - name: JOOMLA_DB_NAME value: joomla - name: JOOMLA_DB_PASSWORD valueFrom: secretKeyRef: name: mysql-pass key: password ports: - containerPort: 80 name: joomla volumeMounts: - name: joomla-persistent-storage mountPath: /var/www/html volumes: - name: joomla-persistent-storage persistentVolumeClaim: claimName: ebs-joomla
=> $ notepad kustomization.yml
* This file is to generate the secret box and to run the commands in an order.
apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization secretGenerator: - name: mysql-pass literals: - password=redhat - upass=redhat resources: - mysqldplymnt.yml - - joomla.yml
This three files should be in a single namespace, which we created.
=> To start the app, run the below command which executes all the files in order.
kubectl apply -k .
Joomla and Mysql pods are deployed in the nodes and load balancer is also created here.
To access the WebApp, go to the
AWS Console -> EC2 Service -> LoadBalancer -> Copy the DNS Name -> Paste that that DNS New name in new Tab.
Step 7: Install and Configure HELM and Tiller
HELM : Helm helps you manage Kubernetes applications — Helm Charts help you define, install, and upgrade even the most complex Kubernetes application. Kubernetes Helm, by making application deployment easy, standardized and reusable, improves developer productivity, reduces deployment complexity, enhances operational readiness, and speeds up the adoption of cloud native apps. These applications can be sourced from open-source community provided repositories, or from an organization’s internal repository of customized application blueprints.
Download the HELM package in windows
Download the Tiller by using below link,
https://download.gettiller.com/releases/Tiller%20Setup%200.92.3.exe
After downlading "helm.exe", "tiller.exe" files, keep both the files in a folder and set path to it.
STEP 8: Configure HELM and Tiller by using following commands.
$ helm init $ helm repo add stable https://kubernetes-charts.storage.googleapis.com/ $ helm repo list $ helm repo update $ kubectl -n kube-system create serviceaccount tiller $ kubectl create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount=kube-system:tiller $ helm init --service-account tiller --upgrade $ kubectl get pods --namespace kube-system
Go to hub.helm.sh, we can see number of kubernetes pre created apps
STEP 9: Installing Prometheus App for metrics monitoring using HELM
Prometheus : Prometheus is a free software application used for event monitoring and alerting. It records real-time metrics in a time series database built using a HTTP pull model, with flexible queries and real-time alerting.
Installing the Prometheus app using HELM chart in a namespace.
$ kubectl create namespace prometheus $ helm install stable/prometheus --namespace prometheus --set alertmanager.persistentVolume.storageClass="gp2" --set server.persistentVolume.storageClass="gp2" $ kubectl get all -n prometheus
Here, if any of the pods are still in pending stage, that means our nodes doesn't have a capacity to run the pods. For this we have to scale the nodes in EKS Cluster
$ eksctl scale nodegroup --cluster firstcluster --name ng1 --nodes=8 --nodes-max=8
Exporting the node port,
kubectl -n prometheus port-forward service/prometheus-1594712385-server 8888:80
Access the Prometheus App with the IP in the browser.
In the above image, we can see the EKS Nodes on Prometheus Server
STEP 10: Installing Grafana for visualizing using HELM
Grafana : Grafana is a multi-platform open source analytics and interactive visualization web application. It provides charts, graphs, and alerts for the web when connected to supported data sources. It is expandable through a plug-in system. End users can create complex monitoring dashboards using interactive query builders.
Installing Grafana using HELM in a new Namespace by the below commands.
$ kubectl create namespace grafana $ helm install stable/grafana --namespace grafana --set persistence.storageClassName="gp2" --set adminPassword=redhat --set service.type=LoadBalancer $ kubectl get all -n grafana
Access the Grafana App in browser using that external IP
LogiIn Credentials :
Username : admin , Password : redhat(which we have mentioned in the above command)
Set the Datasource as Prometheus and give the IP of Prometheus Server.
After that, we can create our Custom Dashboard using Prometheus query according to the requirements. Here we need the Graphical Layout of Kubernetes Cluster Dashboard. It is readily available in Grafana Labs Dashboard Site.
Copy the Dashboard ID (The Dashboard ID is 10000)
Dashboard for Monitoring the metrics of the cluster.
STEP 11 : Delete the setup for Prometheus and Grafana
$ kubectl delete all --all -n prometheus $ kubectl delete all --all -n grafana
STEP 12 : Delete the setup for Joomla App
$ kubectl delete -k .
Use cases:
This kind of Architecture is used in Companies,
a) To easily migrate existing applications to kubernetes.
b) To simplify the deployment and management of microservices based applications.