Deployment Of PHP Website On the Top Of AWS EKS Along with Prometheus And Grafana

Introduction

Amazon Elastic Kubernetes Service (Amazon EKS) is a fully managed service. Customers such as Intel, Snap, Intuit, GoDaddy, and Autodesk trust EKS to run their most sensitive and mission-critical applications because of its security, reliability, and scalability.

EKS is the best place to run Kubernetes for several reasons. First, you can choose to run your EKS clusters using , which is a serverless compute for containers. 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. Second, EKS is deeply integrated with services such as Amazon CloudWatch, Auto Scaling Groups, AWS Identity and Access Management (IAM), and Amazon Virtual Private Cloud (VPC), providing you a seamless experience to monitor, scale, and load-balance your applications. Third, EKS integrates with and provides a Kubernetes native experience to consume service mesh features and bring rich observability, traffic controls, and security features to applications. Additionally, EKS provides a scalable and highly-available control plane that runs across multiple availability zones to eliminate a single point of failure.

EKS runs upstream Kubernetes and is certified Kubernetes conformant so you can leverage all benefits of open source tooling from the community. You can also easily migrate any standard Kubernetes application to EKS without needing to refactor your code.

Benefits

  • High Availability
  • Serverless Option
  • secure
  • Built with community

How it works

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 and it welcomes contributions from the community. Install using this git .
  • 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.

Cluster Creation

Create a cluster just with one command and cluster can be created either by YAML file or by using CLI. Now, we will be doing using the YAML file and the requirements for creating a cluster you can put it in YAML file

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: kscluster
region: ap-south-1
nodeGroups:
- name: ng1
desiredCapacity: 4
instanceType: t2.micro
ssh:
publicKeyName: mykey

After creating this file we have to use a single command which will create entire Kubernetes setup.

eksctl create cluster -f cluster.yml
Output of cluster creation
Instances launched by cluster

Creating a PHP website using MYSQL Database

Kubectl supports the management of Kubernetes objects using a kustomization file. You can create a Secret by generators in kustomization.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-claim

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 kustomize 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

All this environment can be set up just by using a single command

kubectl create -k .

After creation, you’ll get output

We use the LoadBalancer External IP to use this environment

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 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 helm repo listhelm repo update

this will initialize the setup for helm and now we can install the charts according to our requirements.

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

It will install the entire setup for the Prometheus environment

kubectl create namespace prometheus
helm install stable/prometheus --namespace prometheus --set alertmanager.persistentVolume.storageClass="gp2" --set server.persistentVolume.storageClass="gp2"
kubectl -n prometheus  port-forward svc/auxiliary-billygoat-prometheus-server  8888:80

Installation Of Grafana Chart

kubectl create namespace grafana
helm install stable/grafana --namespace grafana --set persistence.storageClassName="gp2" --set adminPassword=MyPass --set service.type=LoadBalancer

Deleting Cluster

eksctl delete cluster -f cluster.yml

It will delete you entire setup you have created.