Deployment of WordPress and Mysql on the top of K8S Cluster using Amazon EKS Service

Deployment of WordPress and Mysql on the top of K8S Cluster using Amazon EKS Service

INTRODUCTION OF EKS

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

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. 

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
  • IAM for authentication
  • Amazon VPC for isolation
  • Elastic Load Balancing for load distribution

PRERQUISITE:

  • AWS account
  • AWS CLI Configured
  • eksctl and kubectl download

For connecting to aws EKS we have following ways

  • WebUI
  • CLI
  • Terraform Code

Here we are using CLI but for connecting to aws EKS using CLI we required secret key and access key.

So let's start deployment of our WebApp on top of kubernetes using Amazon EKS.

First we install aws CLI and configure it.

No alt text provided for this image


Now we also install eksctl to manage our cluster.

No alt text provided for this image

Now create a cluster using yml file:

No alt text provided for this image

Run this using following command

No alt text provided for this image
No alt text provided for this image

eksctl program internally creating cloud formation stack for cluster automatically and we use cloud formation for auto provisioning.

No alt text provided for this image
No alt text provided for this image

All the instances created in our nodegroup1 are running.

No alt text provided for this image

We have to create config file (.kube) having IP , username and password of kubernetes cluster.

Update config file to allow kubectl to send instructions to master node.

No alt text provided for this image
No alt text provided for this image

We need to install amazon-efs-utils in the node for the instance to be able to connect to efs. So I have installed amazon-efs-utils in each of my nodes.

No alt text provided for this image
No alt text provided for this image

Now we create a namespace

No alt text provided for this image

We set this namespace as default by following command.

No alt text provided for this image
No alt text provided for this image

Now we create a deployment using following command.

No alt text provided for this image

After creating we expose this so that outside world can connect to it.

No alt text provided for this image
No alt text provided for this image

You can see here the load balancer is created and having internet facing. Anyone from outside world having my DNS name can only connect to my webserver.

No alt text provided for this image
No alt text provided for this image

Now we create an EFS provisioner that allows us to mount EFS storage as Persistent Volumes in kubernetes. It is consists of a container that has access to an AWS EFS resource. The container reads a configmap which contains the EFS filesystem ID , the AWS region and the name you want to use for your efs-provisioner . This name will be used later when you create a storage class.

No alt text provided for this image

For creating EFS-provisiner we run following command.

No alt text provided for this image

Then we provide rbas permission . ClusterRole can be used to grant the same permission as a role.

No alt text provided for this image

For providing rbas permission we run following command.

No alt text provided for this image

We create secret for mysql password:

No alt text provided for this image

Then we create a storage classes so that we enable data presistency through EFS.

No alt text provided for this image

We create PVC for both MYSQL and Wordpress deployments.

No alt text provided for this image

Now we create a ELB service to access MYSQL and deploy MYSQL.

apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
  clusterIP: None
---
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-pvc
       


Then we create a ELB service to allow clients to access WordPress and deploy WordPress.

apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend
  type: LoadBalancer
---
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: wordpress-pvc

We run following command in CMD to create the above:

No alt text provided for this image

Our EFS File System

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

To check if all the resources are running use the command:

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Therefore our WordPress site is ready.

FARGATE CLUSTER:-

AWS has one service known as fargate which provide us server-less architecture but only for containers. Fargate manages everything. It creates slaves i.e. worker node on run time. EKS use fargate profile behind the scene which automatically provision the slave as per our demand.

Here is the code for creating the fargate cluster:

No alt text provided for this image

Therefore,our fargate cluster is created!!

Here I conclude my article , Hope you enjoy it.





To view or add a comment, sign in

More articles by Twinkle Purohit

  • Task 6

    Deploy the WordPress application on Kubernetes and AWS using Terraform and RDS What is Kubernetes? Kubernetes is a…

  • AWS-Task 4

    In this task perform task-3 with an additional feature to be added that is NAT Gateway to provide the internet access…

  • AWS-Task 3

    Create a vpc Infrastructure and Host a wordpress Application with Mysql Finally completed!!! . In this task we have to…

  • AWS-Task 2

    Automate AWS cloud using TERRAFORM TASK 2 Description Create Security group which allow the port 80. Launch EC2…

  • AWS-Task 1

    What is Cloud Computing? Cloud computing is the on-demand availability of computer system resources, especially data…

Insights from the community

Others also viewed

Explore topics