AWS EKS TASK
What is AWS EKS?
EKS stands for Elastic Kubernetes Services is a fully managed kubernites services by Amazon.Through EKS, organizations can run Kubernetes without installing and operating a Kubernetes control plane or worker nodes. Simply put, EKS is a managed containers-as-a-service (CaaS) that drastically simplifies Kubernetes deployment on AWS.Amazon will provide and manage everything for us,we just have to specify our requirements.
For more info and resources click on the following link: https://aws.amazon.com/eks/
Task:
To implement different services of AWS EKS and launch a wordpress site along with SQL database for persistent data storage on top of the cluster we have created
Process:
1: Create an IAM User from your AWS WebUI with admin access roles.
Now configure the aws in your CLI by using the login info of the IAM User:
2: Now Download eksctl and add it's path to your environment variables:
you can download eksctl from: https://eksctl.io/introduction/
and add it to your path or paste it in the folder where you have already installed kubernetes and added that to your path
Eksctl: eksctl is a simple CLI tool for creating clusters on EKS - Amazon's new managed Kubernetes service for EC2 and it is more widely used as compared to aws eks command for in CLI.
3: After eksctl is successfully installed and configured, Now we will create a cluster,for that we need a yaml file,create a yaml file called cluster.yaml in your directory and use the following code:
apiVersion: eksctl.io/v1alpha5 kind: ClusterConfig metadata: name: taskOnEKS region: ap-south-1 nodeGroups: - name: ng1 desiredCapacity: 2 instanceType: t2.micro - name: ng2 desiredCapacity: 1
instanceType: t2.small
Now use eksctl create command to create the cluster on top of aws cloud:
eksctl create cluster -f cluster.yaml
Now,update your config file of your kubernetes otherwise we cannot use the cluster from kubernetes,Use the following code for that:
aws eks update-kubeconfig --name taskOnEKS
If config file is present,it will just update it if it is not present than the code will create a new config file for you,you can also check your config file by using the command:
kubectl config view
Now,check all the details whether nodes for the cluster are created or not,use the command:
Kubectl get nodes
You can see that we just specified the requirement we needed in our cluster,AWS EKS services create everything for us,create cluster,nodes,set up load balancer,etc just by using one single command which makes this service of kubernetes very powerful.
4: Now,create a namespace for creating wordpress and sql database,use the command:
kubectl create ns taskns
You can check whether it is created or not by using kubectl get ns command:
To use sql and wordpress we need a pv and pvc ,so we need to create that: create a pvc.yaml file with yaml code:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: lwpvc1 spec: storageClassName: gp2 accessModes: - ReadWriteOnce resources: requests:
storage: 10Gi
Now,create the PVC:
This is dynamic pvc, pv will only be created when we deploy a pod,for that create a yaml file called pvcpending which consists of a generic code to deploy a pod,which makes the pvc status from pending to bound and pv is created.
5: Now,create two yaml files one for deployment of sql code and another for wordress,and then launch these files by creating a kustomization folder
For SQL:
apiVersion: v1 kind: Service metadata: name: wordpress-mysql labels: app: wordpress spec: ports: - port: 3306 selector: app: wordpress tier: mysql clusterIP: None --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-pv-claim labels: app: wordpress spec: accessModes: - ReadWriteOnce resources: requests: storage: 20Gi --- apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: wordpress-mysql labels: app: wordpress spec: selector: matchLabels: app: wordpress tier: mysql strategy: type: Recreate template: metadata: labels: app: wordpress tier: mysql spec: containers: - image: mysql:5.6 name: mysql env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-pass key: password ports: - containerPort: 3306 name: mysql volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: mysql-pv-claim
For Wordpress:
apiVersion: v1 kind: Service metadata: name: wordpress labels: app: wordpress spec: ports: - port: 80 selector: app: wordpress tier: frontend type: LoadBalancer --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: wp-pv-claim labels: app: wordpress spec: accessModes: - ReadWriteOnce resources: requests: storage: 20Gi --- apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: wordpress labels: app: wordpress spec: selector: matchLabels: app: wordpress tier: frontend strategy: type: Recreate template: metadata: labels: app: wordpress tier: frontend spec: containers: - image: wordpress:4.8-apache name: wordpress env: - name: WORDPRESS_DB_HOST value: wordpress-mysql - name: WORDPRESS_DB_PASSWORD valueFrom: secretKeyRef: name: mysql-pass key: password ports: - containerPort: 80 name: wordpress volumeMounts: - name: wordpress-persistent-storage mountPath: /var/www/html volumes: - name: wordpress-persistent-storage persistentVolumeClaim:
claimName: wp-pv-claim
Now,create the kustomization file:
apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization secretGenerator: - name: mysql-pass literals: - password=redhat resources: - deploy-my-SQL.yml
- deploy- - deploy-wordpress.yml
Now,use the kustomization file to launch our wordpress service:
Now,get the DNSserver of wordpress by using kubectl get all -o wide command:
Now use the IP to launch and use wordpress:
6: Our wordpress has successfully launched,now we can also integrate many other technologies like jenkins,prometheus and graphana with EKS by using helm and tiller
helm: Helm helps you manage Kubernetes applications — Helm Charts help you define, install, and upgrade even the most complex Kubernetes application.For more info: https://helm.sh/
tiller: The charts/packages on the server side of helm is known as tiller.
First,install helm and tiller into your system and then add the path to your environment variables
Here,I have simply shown how I initialised helm and used helm and tiller to launch prometheus,You can also use the official site of helm and use services like grafana and jenkins by following the instructions from the official site.Here's how I launched prometheus:
Here I have initialised helm and and configured tiller and laucnhed prometheus pod in a new namespace called prometheus
You can see that prometheus is successfully created:
Now,export the port of prometheus so that we can see it in our webbrowser:
Now,you can also integrate it with grafana for monitoring or launch a jenkins service to automate some jobs.
7: Now,we can also use fargate here,which dynamically launches cluster when needed according to the requirement by EKS.It helps us to make this setup serverless.
fargate: AWS Fargate is a serverless compute engine for containers that works with both Amazon Elastic Container Service (ECS) and Amazon Elastic Kubernetes Service (EKS). Fargate makes it easy for you to focus on building your applications. Fargate removes the need to provision and manage servers, lets you specify and pay for resources per application, and improves security through application isolation by design. For more info: https://aws.amazon.com/fargate/
For this create a fargate.yaml file and then create the create the cluster using fargate.yaml:
apiVersion: eksctl.io/v1alpha5 kind: ClusterConfig metadata: name: fargatecluster region: ap-south-1 fargateProfiles: - name: fargate-default selectors: - namespace: kube-system - namespace: default
With this I have tried to demonstrate some things that I have learnt from LW-AWS EKS classes from LinuxWorld under Vimal Daga Sir,It was an amazing journey and I have learnt a lot of things about kubernetes and AWS EKS service. GitHub link: https://github.com/Pheonix-reaper/EKSTask