Deploying WordPress Application on Amazon EKS(Elastic Kubernetes Service)
Amazon EKS:
Amazon Elastic Kubernetes Service (Amazon EKS) is a fully managed Kubernetes service, that makes it easy for you to run Kubernetes on AWS without needing to install and operate your own Kubernetes clusters. Kubernetes is an open-source system for automating the deployment, scaling, and management of containerized applications
.Amazon EKS runs Kubernetes control plane instances across multiple Availability Zones to ensure high availability. Amazon EKS automatically detects and replaces unhealthy control plane instances, and it provides automated version upgrades and patching for them. Amazon EKS is also integrated with many AWS services to provide scalability and security for your applications
Benefits of Amazon EKS: High Availability, Serverless option(Fargate), Secure, Built with the community.
COMPANIES ADAPTING AMAZON EKS: HSBC Bank, Pearson, Snapchat, FICO, and so on.
TASK DESCRIPTION
Deploy a WordPress application on Amazon Elastic Kubernetes Service and explain concepts of Helm & Tiller.
STEPS:
Amazon Elastic Kubernetes Service has majorly 3 ways of usage: using WEBUI ,using CLI & using Terraform code. This article shows the way of creating Amazon EKS clusters using CLI.
STEP 1) Create and set up an AWS Free tier Account.
STEP 2) Download the AWS CLI MSI installer for Windows (64-bit) at https://awscli.amazonaws.com/AWSCLIV2.msi. Run the downloaded MSI installer and follow the on-screen instructions. By default, the AWS CLI installs to C:\Program Files\Amazon\AWSCLIV2.
STEP 3) Download minikube-installer.exe and kubectl.exe: https://kubernetes.io/docs/tasks/tools/install-minikube/ https://kubernetes.io/docs/tasks/tools/install-kubectl/. Here, make sure there is a Hypervisor available, in my case its VirtualBox. Also set PATH, in Environment variables. This is my default Kubernetes setup using CLI. (One can also refer https://docs.aws.amazon.com/eks/latest/userguide/install-kubectl.html)
STEP 4) Installing eksctl. It is a simple CLI tool for creating clusters on EKS - Amazon's new managed Kubernetes service for EC2. It is written in Go and uses CloudFormation. One can create a cluster in minutes with just one command – eksctl create cluster! Download eksctl.exe in the same folder as minikube and kubectl(C:\Program Files\Kubernetes\Minikube)
Set the PATH.
Run the following command to check if eksctl is installed successfully.
STEP 5) Create an IAM user in AWS with Administrative Access.Also, provide a custom password for login.
Make sure to download the excel file that contains AWS Access Key ID and AWS Secret Access Key which will be required to log in using AWS CLI. Using the details in this file login using AWS CLI into this IAM User(here EKS1)
aws configure
Now make sure your environment is clean or at least has no resources or services running as in this cluster. For this login to the WEBUI using the IAM User.
STEP 6) Create a key for login via SSH. Download this key as well. The file will be downloaded with .pem extension.
STEP 7) Create a folder on Desktop assume eks_code. Copy the key to this folder. Here also create a cluster.yml file that will contain YML code to launch the cluster. (The name of the key created by me is mykey11 so I provided the same. Make sure to update it with your key name)
apiVersion: eksctl.io/v1alpha5 kind: ClusterConfig metadata: name: mycluster region: ap-south-1 nodeGroups: - name: ng1 desiredCapacity: 1 instanceType: t2.micro ssh: publicKeyName: mykey11 - name: ng2 desiredCapacity: 1 instanceType: t2.small ssh: publicKeyName: mykey11 - name: ng-mixed minSize: 1 maxSize: 3 instancesDistribution: maxPrice: 0.010 instanceTypes: ["t2.micro"] # At least one instance type should be specified onDemandBaseCapacity: 0 onDemandPercentageAboveBaseCapacity: 50 spotInstancePools: 2 ssh:
publicKeyName: mykey11
This code contains the configurations of the node groups, instance type, and about spot instances. You will get charged for t2.small instance and EKS.
Remember its not a free service. Make sure to keep a check on your Bill or create a Budget otherwise to monitor your bill.
Now save and run the following command to create the cluster:
eksctl create cluster -f cluster.yml
Hang on patiently for 15-20mins and let the cluster setup.
Whoah! Our cluster has been launched!
STEP 8) Make or upgrade Kube-config file so for that just run the following command:
Now to view if nodes are successfully launched:
One can also check on WebUI:
Login into one of these nodes using the public IP of any of the nodes and the key(mykey11.pem here):
One can see that we can be easily logged into any node and run various commands.Also, the docker command is running in all 3 nodes launched using cluster.yml file. Now run the following command to check the kubelet and max_nodes:
We can see all the nodes are running successfully. Here are the nodes of the namespace kube-system running successfully.
STEP 9) Create a folder assume efs_code1 in this folder eks_code. Here create 3 YAML files which will create a WordPress-MySQL deployment. This is the WordPress deployment file. To avoid the data loss and make the data permanent I have configured one persistent volume claim which does nothing but internally contacts to dynamically created PV and EKS by default take storage from EBS volumes. One can also configure EFS here. (Here my code file name is deploy-wordpress.yaml)
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
STEP 10) Now, for storing the data of the WordPress application we have to create one MySQL database which works as a back-end for our application. MySQL database deployment is most critical for us since all the necessary services of Kubernetes like secret and all that is included in this code. (Here my file name is deploy-mysql.yaml)
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
Also created a kustomization.yaml code. This will launch the whole setup that is the two deployments at once by scraping them out of the same folder and running them. How cool right?
apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization secretGenerator: - name: mysql-pass literals: - password=redhat resources: - deploy-mysql.yaml
- - deploy-wordpress.yaml
This also contains the password.
Finally:
STEP 11) Run the following command to finally launch the setup.
Check using WebUI-Loadbalancers and EBS Volumes respectively:
Now open the WordPress UI using DNS name provided in Description of the LoadBalancer created:
Open the above-highlighted link on Browser to access the WordPress application launched.
STEP 12) Register and log in using name and password.
Finally, the WordPress application is deployed on the Kubernetes managed by using EKS. Our data will be safe in EBS volumes even if the pod fails or crashes, thus providing security. Here one can also use EFS, but that's a paid service again.
Now here one can also launch a Fargate cluster instead of a normal cluster. Serverless architecture is provided by AWS under the name Fargate.Its a subservice of ECS(Elastic Container Service).In this, we don't have to manage servers or clusters. Everything is done by AWS. It helps in creating a BlackBox.The slave is launched on-demand in Run-time. This is the Fargate service. It can be integrated with EKS. They provision slaves on demand. (Note that its a paid service)
HELM
Helm helps to manage Kubernetes applications. It describes the application's structure through helm charts. Helm is made of two components: CLI binary named Helm and Tiller that lives inside the Kubernetes cluster. We have to provide the repository just like in yum.
---Download helm and tiller & set PATH. Download both in the same folder as minikube,kubectl, and eksctl.
---Run helm init . This will initialize the helm.
Run the following commands:
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 helm init --service-account tiller --upgrade kubectl get pods --namespace kube-system
Prometheus:
kubectl create namespace prometheus helm install stable/prometheus --name my-release --namespace prometheus --set alertmanager.persistentVolume.storageClass="gp2" --set server.persistentVolume.storageClass="gp2" kubectl get svc -n prometheus kubectl -n prometheus port-forward svc/flailing-buffalo-prometheus-server 8888:80
Now we can access Prometheus by using publicIP :8888 and we can now monitor our cluster and obtain the result in graphical form. By executing the query in Prometheus we can obtain the cluster monitoring result.
Grafana can also be launched and many other tools like Jenkins as well to monitor nodes or automate tasks on this cluster. Awesome right!
Github URL: https://github.com/TanyaChetnaVaish/AWSeks1
THANKYOU!