Deploying a Scalable Laravel Application on Microsoft Azure

We build a lot of applications but making that scalable is very important as you don’t know when the users are going to increase and if the application crashes at that time it can impact the whole business.

To deploy the application on Kubernetes, we have 2 options: we can directly add the application on Azure Compute service and next, we can do that with the native approach of deploying it on Azure Kubernetes service of any cloud provider.

Some of the prerequisites to have are:

  • Docker
  • Kubernetes
  • kubectl

Let’s break down the process in smaller steps:

Step 1. Create the docker image of our Laravel Application.

Here’s an example of DockerFile.

# Use the official PHP image as the base image
FROM php:7.4-apache

# Install the required packages and extensions
RUN apt-get update \
   && apt-get install -y git zip unzip libpng-dev libonig-dev libxml2-dev \
   && docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd \
   && pecl install redis \
   && docker-php-ext-enable redis

# Copy the application code to the container
COPY . /var/www/html

# Set the document root
ENV APACHE_DOCUMENT_ROOT /var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Install dependencies
RUN composer install --no-interaction --no-scripts --no-progress --prefer-dist

# Set the working directory
WORKDIR /var/www/html

# Expose port 80
EXPOSE 80

# Start Apache
CMD ["apache2-foreground"]

This Dockerfile installs the necessary packages and extensions and starts with the official PHP image. After that, it installs Composer, installs dependencies, establishes the working directory, exposes port 80, moves the application code to the container, and sets the document root. At last, Apache is launched.

To build the docker image we need to run:

$ docker build -t my-laravel-app .

Step 2. Configure the Kubernetes Configuration Files for Laravel.

We need to create 3 major files deployment manifest, service manifest, and ingress manifest for networking.

The key major thing in the deployment manifest is a replica it will help to create as many replicas as we need to scale at the times of high traffic.

Sample Deployment manifest file:

# laravel.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: my-laravel-app
 labels:
   app: my-laravel-app
spec:
 replicas: 1
 selector:
   matchLabels:
     app: my-laravel-app
 template:
   metadata:
     labels:
       app: my-laravel-app
   spec:
     containers:
     - name: my-laravel-app
       image: my-laravel-app
       ports:
       - containerPort: 80
       env:
       - name: DB_HOST
         value: mysql
       - name: DB_PORT
         value: "3306"
       - name: DB_DATABASE
         value: my_database
       - name: DB_USERNAME
         valueFrom:
           secretKeyRef:
             name: mysql-secret
             key: username
       - name: DB_PASSWORD
         valueFrom:
           secretKeyRef:
             name: mysql-secret
             key: password

Service Manifest

It helps to expose the application within the cluster to other pods

There are 2 major things here “Port” and “Target Port”. Port helps to decide on which port we can access the service and target port defines on which port the service is expose to other pods.

Sample service manifest file:

#laravel-service.yaml
apiVersion: v1
kind: Service
metadata:
 name: my-laravel-app
 labels:
   app: my-laravel-app
spec:
 ports:
 - name: http
   port: 80
   targetPort: 80
 selector:
   app: my-laravel-app

Ingress Manifest

It helps to expose the application to the external world.

Sample Ingress manifest file:

#laravel-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
 name: my-laravel-app
 annotations:
   nginx.ingress.kubernetes.io/rewrite-target: /
spec:
 rules:
 - host: my-laravel-app.example.com
   http:
     paths:
     - path: /
       pathType: Prefix
       backend:
         service:
           name: my-laravel-app
           port:
             name: http

Now, let’s deploy all the manifest files to the cluster.

$ kubectl create secret generic mysql-secret --from-literal=username=<username> --from-literal=password=<password>

$ kubectl apply -f laravel.yaml

$ kubectl apply -f laravel-service.yaml

$ kubectl apply -f laravel-ingress.yaml

Hurrah, we have deployed the Laravel application on Kubernetes successfully. Now our application can handle the traffic seamlessly.

Up Next
    Ebook Download
    View all
    Learn
    View all