Deploy your first scaleable PHP/MySQL Web application in Amazon EKS

Deploy your first scaleable PHP/MySQL Web application in Amazon EKS

In the previous article, I talk about how to deploy PHP/Mysql Web application on top of Kubernetes. Now I will take forward this article to deploy PHP/MYSQL Application on top of Amazon EKS.

Now, What is Amazon EKS?

Amazon Elastic Kubernetes Service (Amazon EKS) is a fully managed Kubernetes service. 

Through EKS, organizations can run Kubernetes without installing and operating a Kubernetes control plane or worker nodes. Simply put, EKS is a managed containers-as-a-service (CaaS) that drastically simplifies Kubernetes deployment on AWS. With the help of Amazon EKS, we have to no worry about the master node and slave node (if you deploy Amazon EKS with help of fargate). So, Amazon will take care of all the things. Moreover, it also gives easy integration of services like Amazon VPC, Amazon ELB, Amazon EBS.

How to setup Amazon EKS Cluster?

To set up Amazon EKS you need Amazon account having user with all privileges, EKSCTL program which contacts Amazon on behalf of yourself.

Creating Cluster.yml for setting up the cluster.

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

Here I am creating two nodes having a configuration as t2.micro and t2.small for showing the demo how Amazon EKS works behind. After creating this file you have to run the following command.

eksctl create cluster -f cluster.yml

No alt text provided for this image

As you see it will create the cluster on Amazon EKS. It might take 10-15 mins.

After creating clusters now you only need to do deploy your PHP/MYSQL application on top of Amazon EKS.

Creating a PVC resources

  1. Mysql
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: db-pvc
  labels:
    name: dbpvc1
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

  • Create PVC from Kubectl Command
kubectl create -f <file_name.yml>
  • For Confirmation:
kubectl get pvc db-pvc

2. For PHP

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: frontend-pvc
  labels:
    name: frontendpvc1
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

  • Create PVC from Kubectl Command
kubectl create -f <file_name.yml>
  • For Confirmation:
kubectl get pvc frontend-pvc
No alt text provided for this image

Creating Deployment Resource for

  1. Database
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mydb-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      env: production-db
  template:
    metadata:
      name: mydb-pod
      labels:
        env: production-db
    spec:
      volumes:
        - name: db-vol
          persistentVolumeClaim:
            claimName: db-pvc
      containers:
      - name: database
        image: mysql
        env:
         - name: MYSQL_ROOT_PASSWORD
           value: redhat
         - name: MYSQL_USER
           value: raghav
         - name: MYSQL_PASSWORD
           value: redhat
         - name: MYSQL_DATABASE
           value: mydb
        volumeMounts:
          - mountPath: /var/lib/mysql
            name: db-vol

Here we are using a public image for MySQL present on Docker Hub tagged as mysql

  • Create Deployment from Kubectl Command
kubectl create -f <file_name.yml>
  • For Confirmation:
kubectl get deployment mydb-deployment

2. PHP

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myphp-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      env: production-frontend
  template:
    metadata:
      name: myfrontend-pod
      labels:
        env: production-frontend
    spec:
      volumes:
        - name: front-vol
          persistentVolumeClaim:
            claimName: frontend-pvc
      containers:
      - name: frontend
        image: ragh19/phpproject:web_v1
        volumeMounts:
          - mountPath: /var/www/html
            name: front-vol

Here we are using a public image for MySQL present on Docker Hub tagged as ragh19/phpproject:web_v1.

  • Create Deployment from Kubectl Command
kubectl create -f <file_name.yml>
  • For Confirmation:
kubectl get deployment myphp-deployment
No alt text provided for this image

Creating Service Resource for

  1. Database
apiVersion: v1
kind: Service
metadata:
  name: mydb-service
spec:
  type: NodePort
  ports:
    - targetPort: 3306
      port: 3306
      nodePort: 30008
  selector:
    env: production-db

Here Database service is exposed to 30008 port no. You can access this database service by IP and Port No having database username as root and password is redhat.

  • Create Service from Kubectl Command
kubectl create -f <file_name.yml>
  • For Confirmation:
kubectl get svc mydb-service

2. For PHP

kubectl expose deploy myphp-deployment --type=LoadBalancer --port=80

Here PHP service is exposed as Load Balancer service type which will automatically configure LoadBalancer for Deployment

No alt text provided for this image

I am exposing the database as NodePort you can also use ClusterIP for that also.

Now I am creating index.php file which basically connects with the database and prints their values.

<?php
$conn = new mysqli("mydb-service", "newuser", "user_password", "mysql");
// Check connection
if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
}




$sql = "SELECT user FROM user";
$result = $conn->query($sql);




if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
                echo $row['user']."<br>";
        }
} else {
        echo "0 results";
}
$conn->close();


In the MySQL database, I have to do some modifications for making the connection with PHP code. First I have to create a user with all privileges and By default and for some reason, MySQL 8 default plugin is auth_socket. Applications will most times expect to log in to your database using a password.

So I need to run that command

ALTER USER 'newuser'@'%' IDENTIFIED WITH  mysql_native_password 'user_password'

After all this, I click the LoadBalancer external DNS name in the browser

No alt text provided for this image

It showing the users in MySQL database.


This demo shows how to deploy PHP/Mysql top of Amazon EKS.







要查看或添加评论,请登录

Raghav Agarwal的更多文章

社区洞察

其他会员也浏览了