How To Deploy MySQL and Wordpress On AWS Using EKS.

Atul kumar jha
5 min readAug 20, 2020

What is Elastic Kubernetes Service?

EKS stands for Elastic Kubernetes Service, Amazon offering EKS that helps in running the Kubernetes on AWS without requiring the user to maintain their own Kubernetes control plane. It is a fully managed service by Amazon.

The term “Fully Managed” means, we, as a user, need not to worry about the provisioning of the Kubernetes server. All we have to do is to tell the number of the slave nodes required for the Kubernetes cluster.

There are two ways to create a Kubernetes cluster with slave nodes in Amazon EFS:

- eksctl command. (Download ekctl)- AWS management console. ( Download awscli )

Instead of awscli we are going to use eksctl to manage EKS because it is highly customizable and is dedicatedly designed for EKS.

we require kubectl command too for working in the Kubernetes cluster so download it.

After downloading the commands we have to install it and set the path variable.

Now configure our cmd to work as remote for AWS. After installing awscli and setting up the path variable, open the command prompt and type the below command

aws configure — profile atuljha

create a YAML file to create the cluster. Here, I have created a file name cluster.yml

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: "eks-cluster"
region: ap-south-1
nodeGroups:
- name: ng1
desiredCapacity: 2
instanceType: t2.micro
ssh:
publicKeyName: mykey11
- name: ng2
desiredCapacity: 1
instanceType: t2.small
ssh:
publicKeyName: mykey11

Now, execute the file using the below command and it will configure our EKS cluster on AWS. Note that it may take around 15–20 minutes while doing the same.

eksctl create cluster -f cluster.yml

It will take around 15–20 minutes to create the complete cluster.

Now, we can see that the instances are created in the EC2-Dashboard.

Now we have to update kube config file to run kubectl command

aws eks update-kubeconfig — name eks-cluster

we can also check it through our command line if the cluster is created or not. For this we can use the below command

eksctl get cluster
NAME REGION
eks-cluster ap-south-1

Before deploy webserver on EKS, we need to provision EFS. That will be used as a Persistent Volume for our pods running on the EKS.

An EFS will be used as PVC for both, WordPress site and the MySQL database server.

For WordPress, we mount PVC at /var/www/html and for MySQL , we mount it at /var/lib/mysql.

Creating EFS

Now, Install amazon-efs-utils in all our instances of the EKS Cluster

After that, we can deploy our WordPress and MySQL on the EKS.

Note:- While deploying the pods, for security purposes, we will only expose the WordPress server and not the MySQL servers because WordPress is for our client ( worldwide).

Create a yaml file for deploy MySQL database

apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
---
# Creating persistent volume for mysql
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
---
# Creating a mysql deployment with Recreate strategy
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
value: redhat
- name: MYSQL_DATABASE
value: wordpress
- name: MYSQL_USER
value: atuljha
- name: MYSQL_PASSWORD
value: redhat
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim

Create a yaml file for deploy WordPress database

# Creating a Load Balancer service
apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
spec:
ports:
- port: 80
selector:
app: wordpress
tier: frontend
type: LoadBalancer
---
# Creating a persistent volume for wordpress
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wp-pv-claim
labels:
app: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
# Creating a wordpress deployment with Recreate strategy
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
replicas: 2
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
value: redhat
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-persistent-storage
mountPath: /var/www/html
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: wp-pv-claim

Create Kustomization.yml file

Note:- The name of this file must be Kustomization.yml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- MySQL-deployment.yml
- wordpress-deployment.yml

Now, Run the Kustomization.yml file and it will execute the wordpress-deployment.yml and MySQL-deployment.yml

kubectl create -k

we have successfully deployed a highly fault-tolerant website on a fully-managed Kubernetes cluster.

By using the external-ip we can easily access the WordPress site

This setup is fully fault-tolerant, which means that if any of the pods get down/deleted because of any reason, it will launch automatically by Kubernetes service.

For destroying whole setup we have a command

eksctl delete cluster -f cluster.yml

After 10–15 min, we will see that whole cluster get deleted

THAT ALL

THANK YOU

--

--