Deployment Of PHP Website On the Top Of AWS EKS Along with Grafana And Prometheus
Introduction
Amazon Elastic Kubernetes Service (Amazon EKS) is a managed service that makes it easy for you to run Kubernetes on AWS without needing to stand up or maintain your own Kubernetes control plane. 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, including the following:
- Amazon ECR for container images
- Elastic Load Balancing for load distribution
- IAM for authentication
- Amazon VPC for isolation
Amazon EKS runs up-to-date versions of the open-source Kubernetes software, so you can use all the existing plugins and tooling from the Kubernetes community. Applications running on Amazon EKS are fully compatible with applications running on any standard Kubernetes environment, whether running in on-premises data centres or public clouds. This means that you can easily migrate any standard Kubernetes application to Amazon EKS without any code modification required.
Benefits
- High Availability
- Serverless Option
- secure
- Built with community
Amazon EKS -Managed Kubernetes Service
they are lot of companies adopted Amazon EKS
Prerequisites
- Installing AWS CLI -The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.
- Kubernetes uses a command-line utility called kubectl for communicating with the cluster API server. The kubectl binary is available in many operating system package managers, and this option is often much easier than a manual download and install process.
eksctl
is a simple CLI tool for creating clusters on EKS - Amazon's new managed Kubernetes service for EC2. It is written in Go, uses CloudFormation, was created by Weaveworks and it welcomes contributions from the community. Install using this git URL.- AWS CLI must be configured with you IAM or ROOT user and it also has the power to use the Roles service in AWS, for this, I suggest you create an IAM user with administrative access.
Things we do here:
- Create Eks Cluster
- Create namespace
- Deploy or Launch the container
- Scale-up our Deployment
- Create LoadBalancer and exposes pod to connect to the client.
- Creating EBS volume to store data permanently and mount to deployment.
- For Creating EBS create PVC and PVC will create PV and PV will get volume from storage class EBS.
- EBS has default storage class gp2 we create a storage class io1 then create a new PVC then mount to our deployment.
- Then change gp2 annotation default “true” to default “false”.
let’s get started
- Create an account on AWS.
- Create an IAM user with the administrator power and download the .csv file
- Install Kubectl >>> https://kubernetes.io/docs/tasks/tools/install-kubectl/
- Install eksctl
- Create a yml file for the cluster You can write any name but extension must be .yml in my case I created cluster.yml.
>>Codes for creating cluster.
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfigmetadata:
name: nischal-cluster
region: ap-south-1nodeGroups:
- name: ng-1
instanceType: t2.micro
desiredCapacity: 3
ssh:
publicKeyName: nischal
if required you can create or add extra node gropes but in my case I am using only one node group and attach your own key if you created if not then create and attach if you want to access the instance through ssh.
creating key in aws:
services>>EC2>>key Paris
NOTE: download pem to do ssh
here is an example if you want to give additional node gropes
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfigmetadata:
name: nischal-cluster
region: ap-south-1nodeGroups:
- name: ng-1
instanceType: t2.micro
desiredCapacity: 3
ssh:
publicKeyName: nischal
- name: ng-1
instanceType: t3.small
desiredCapacity: 1
ssh:
publicKeyName: nischal
- Now Create cluster >>> eksctl create cluster -f cluster.yml
- Cluster will be created in 15–20 min
- Finally, Cluster is created.
- Our instance is created and ready you can check on AWS portal and from CLI also.
- Now update KubeConfig file so that you can work on your created cluster. Before updating must have installed kubectl in your pc because we use kubectl command now.
- TO Check kubeConfig file >>> kubectl config view.
- Check instance or nodes using CLI >>> kubectl get nodes
- if you want to Check nodes in more details. >>> kubectl get nodes -o wide
- Check pods
- No pods is running because we haven’t deploy any container.
- Check Namespaces
- now you have to create your own namespace
- to create namespace>>>kubectl create namespace <yourname>
- you can give any name in my case it is nischal
- Set you namespace to default so that every pods deploy in your namespace
- to make it default>>>
kubectl config set-context -- current -- namespace=nischal
- To check cluster-info>>>kubectl cluster-info
- Now deploy a container.
- You can use any Docker image but I am using this image >>> vimal13/apache-webserver-PHP this container has some PHP code which will give you IP of the pods where your image is running when we do load-balancing this will us to check.
- To deploy container
- now if you check the pose .you can see that one pod is running
- to check pod in detail>>kubectl get pods -o wide
- To scale up your pod
- replica=4 that means total 4 pods we need and one is already there then 3 more will launch
- hear you can see that 3 more pods are created and age is 42 second
- Now We can create a LoadBalancing and expose or pod to outside So that out customer can visit our web-page.
- Here we use LoadBalancing type to do outside connectivity this type will do LoadBalancing as well as give a single public IP for all pods.
*To describe LoadBalancer (in that You will get public IP of load-balancer)
- You can get IP from this command also >>> kubectl get all
- Selected part is public IP of LoadBalancer.
- Copy the IP and paste it on any browser and you can access your web-page
- Here you can observe that I have four pods and every time I refer it get another IP because LoadBalancer will balance the load on pods. You may not get different IP(sometimes) because our system make cashes of site but inside load is balanced.
- If you delete your pods it will start automatically because we set 4 replicas and Kubernetes will keep on checking if any pod is deleted Kubernetes will start and your web-page will not get any downtime.
Creating a PHP website using MYSQL Database
Kubectl supports the management of Kubernetes objects using a customization file. You can create a Secret by generators in kustomization.yaml.
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: mysql-pass
literals:
- password=redhat
resources:
- mysql-deployment.yaml
- wordpress-deployment.yaml
Manifest for SQL-deployment
the manifest describes a single-instance MySQL Deployment. The MySQL container mounts the PersistentVolume at /var/lib/mysql. The MYSQL_ROOT_PASSWORD environment variable sets the database password from the Secret.
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: 5Gi
---
apiVersion: apps/v1
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-claiml
Manifest for WordPress Deployment
the manifest describes a single-instance WordPress Deployment. The WordPress container mounts the PersistentVolume at /var/www/html for website data files. The WORDPRESS_DB_HOST environment variable sets the name of the MySQL Service defined above, and WordPress will access the database by Service. The WORDPRESS_DB_PASSWORD environment variable sets the database password from the Secret customize generated.
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: 5Gi
---
apiVersion: apps/v1
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 All this environment can be set up just by using a single command
kubectl create -k .
kubectl get all
copy the load balancer URL now you can access our webpage in any browser
kubectl get pv
kubectl get pvc
After pasting the URL that we copied we will get like this
We just developed a fully managed PHP Website without any of our local environments, the nodes are in the AWS Cloud.
Helm
Helm is a tool for managing Charts. Charts are packages of pre-configured Kubernetes resources.
Uses of Helm
- Find and use popular software packaged as Helm Charts to run in Kubernetes
- Share your own applications as Helm Charts
- Create reproducible builds of your Kubernetes applications
- Intelligently manage your Kubernetes manifest files
- Manage releases of Helm packages
Simply, you can assume this as we have created our website environment. And somebody also wants to use this environment so the second person also has to do this same setup again and again. To reduce this task helm provides Kubernetes help. We can install the popular environment within a second because of the power of container and Kubernetes.
So using the power of helm we are going to install two powerful environments one is Prometheus and the other one is Grafana. Prometheus is used to collect the metrics from the system and Grafana is a very popular Visualizing tool for Prometheus to see the system usage and many more things.
- Download and install Helm and Tiller
helm inithelm repo add stable https://kubernetes-charts.storage.googleapis.com/helm repo listhelm repo update
kubectl -n kube-system create serviceaccount tillerkubectl create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount=kube-system:tillerhelm init --service-account tiller --upgradekubectl get pods --namespace kube-system
Installation Of Prometheus Chart
by using this it will install the entire setup for the Prometheus environment
kubectl create namespace prometheushelm install stable/prometheus --namespace prometheus --set alertmanager.persistentVolume.storageClass="gp2" --set server.persistentVolume.storageClass="gp2"kubectl get all -n prometheus
kubectl -n prometheus port-forward svc/service/prometheus-<your prometheus>-server 8888:80
NOTE: everyone will have there unique id for prometheus
Installation Of Grafana Chart
kubectl create namespace grafanahelm install stable/grafana --namespace grafana --set persistence.storageClassName="gp2" --set adminPassword=MyPass --set service.type=LoadBalancer
kubectl get all -n grafana
- copy the external IP so that you can open it in browser
Get your ‘admin’ user password by running>>>
kubectl get secret — namespace grafana grafana-1594403593 -o jsonpath=”{.data.admin-password}” | base64 — decode ; echo
- by default username will be admin
- congratulation, now you have created a webpage which will not have downtime most of the time with the support of grafana
Deleting Cluster
eksctl delete cluster -f cluster.yml
this will delete all the setup that you have created till now.
Connect me on my LinkedIn as well.