Hosting a PHP Website with MYSQL through Kubernetes Pods and Docker πŸš€

Hosting a PHP Website with MYSQL through Kubernetes Pods and Docker πŸš€

Β·

3 min read

πŸ“ Introduction

In this Blog, we'll walk through the process of Containerization and orchestrate a simple login and registration website using Kubernetes and Docker. Docker is a containerization tool that allows us to package an application and its dependencies into a single container image. This makes it easy to deploy and run the application on any infrastructure. whereas Kubernetes is an orchestration tool that helps to manage multiple containers.

πŸ“ Prerequisites

  1. As I am using my environment so i will be using Minikube to access Kubernetes (Minikube is a tool that runs a local Kubernetes cluster on your machine. It is a lightweight Kubernetes implementation that creates a VM on your local machine and deploys a simple cluster containing only one node)

  2. DockerHub Account (You need a Docker Hub account to push a Docker image to Docker Hub)

  3. Basic concept of MySQL database (creating database, table)

✏️ Steps

Step 1: Create a new directory for the project.

Step 2: Create a Dockerfile and put the code

FROM php:8.0-apache
RUN mkdir /var/www/html -p
RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli
COPY ./register.php /var/www/html
COPY ./logout.php /var/www/html
COPY ./login.php /var/www/html
COPY ./index.html /var/www/html
COPY ./dashboard.php /var/www/html

Step 3: Create two files named mysql.yml and php.yml and put the code :

apiVersion: v1
kind: Pod
metadata:
        name: mysql
        labels:
                app: mysql 
                type: backend
spec:
        containers:
        - name: mysql01
          image: mysql/mysql-server
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: <password>
apiVersion: v1
kind: Pod
metadata:
        name: php01
        labels:
                app: php 
                type: frontend
spec:
        containers:
        - name: php01
          image: username/repo:version #docker username and repo

Step 4: Now you will need to create the pods using this file mysql.yml and php.yml

Kubectl create -f mysql.yml
kubeclt create -f php.yml

Step 5: Create service files for MySQL and PHP so they are accessible from within the node and the website.

apiVersion: v1
kind: Service
metadata:
  labels:
    app: mysql 
  name: mysql01-srv
spec:
  ports:
  - nodePort: 30011
    port: 3306
    protocol: TCP
    targetPort: 3306
  selector:
    app: mysql #should match with mysql.yml
  type: NodePort
apiVersion: v1
kind: Service
metadata:
  name: php-app-service-loadbalancer
spec:
  selector:
    app: php  # This should match the label of your PHP pod
  ports:
    - protocol: TCP
      port: 8088 #your desired port
      targetPort: 80
  type: LoadBalancer

Note: If you are using Minikube the LoadBalancer will not be able to access the Minikube so you need to run this command in a separate terminal.

minikube tunnel

Step 6: Now create the service using the command

kubectl create -f mysqlsrv.yml
kubectl create -f php-srv.yml

Step 7: Now everything is set up with the Kubernetes and docker we will need to set up the MySQL database.

kubectl get pods
kubectl exec -it mysql sh #mysql is the name of the pod
mysql -u root -p <password> #password you setup during creating pod mysql.

Step 8: Now you are inside the MySQL pod now you have to set up the MySQL database.

show databases;
create database database_name;
select database_name;
create table table_name(username varchar(100),password varchar(100));

Step 9: Now you will have to grant all the access to the root user as we are going to add the user records and access those from over the Kubernetes nodes.

mysql -uroot -p<mysql_password>
create user 'root'@'%' identified by '<mysql_password>';
grant all on *.* to 'root'@'%';
flush privileges;

Step 10: You are all set now

kubectl get services

Step 11: Now use the external IP and the target port you mentioned while creating the php-service (8088 in my case).

Step 12: This are the final outcome for my project.

-> You can find all the php code on my github

Github: https://github.com/UjjwalBudha/php-k8s.git

LinkedIn: www.linkedin.com/in/ujwal-budha/

Β