loader

Laravel: How to setup The Most Popular PHP Framework Application on Kubernetes Cluster

Laravel: How to setup The Most Popular PHP Framework Application on Kubernetes Cluster

Introduction

Our experience with setting up Laravel on Kubernetes has not been pleasant, even though Laravel is one of the most popular PHP frameworks available. Establishing a Laravel application on a Kubernetes cluster can be quite challenging, intricate, and not as direct as one might hope. This is somewhat expected, given that PHP was not developed during the Cloud Native era, so we need to make some adjustments to ensure it runs smoothly on Kubernetes. In this article, we will explore how to containerize a PHP application, specifically a Laravel application, and then how to deploy it to a Kubernetes cluster.

Prerequisites

Assuming you have an application with the following specifications:

App Architecture
  1. Laravel Application
  2. Domain: https://example.com
  3. Utilizes Redis as a Queue Driver, requiring two Pods for deployment: one for handling HTTP requests and another for the Queue Worker, both sharing the same configuration
  4. MySQL as the database
  5. Implements the HTTPS protocol
  6. Laravel Application running inside a Docker Container
  7. PHP executing on top of php-fpm, with Nginx serving as a Reverse Proxy to manage HTTP requests and redirect them to php-fpm
  8. Supervisord employed as the service controller for Nginx and php-fpm
    You can download all the source code for this example from the following link: https://github.com/8grams/laravel-kubenetes-example

Let's get started!

Create Laravel Application

You have the option to download the example Laravel Application from https://github.com/8grams/laravel-kubenetes-example or to create one from scratch.

~$ composer create-project laravel/laravel:^9.0 laravel-app

The next step involves creating a Dockerfile. This Dockerfile will include the installation of numerous PHP-related libraries, which should be sufficient for most use cases.

Setup Webserver

The Dockerfile mentioned above ensures that our Laravel App runs on port 8000. The subsequent step involves setting up Nginx to receive HTTP requests on port 8000 and forward them to php-fpm on port 9000.

Lastly, we can create a supervisord configuration that manages both Nginx and php-fpm processes effectively.

Installing Database, Redis, and SSL Certificate

You have the option to utilize an external Database and Redis or install them on the Kubernetes Cluster. We will cover the latter in the following in-depth technical guides:

How to install Database MySQL and PostgreSQL on Kubernetes, the better way
How to install Database MySQL and PostgreSQL on Kubernetes using NFS Storage.
How to setup Redis on Kubernetes Cluster
In the world of databases and data storage, Redis has gained significant popularity in recent years. This article will explore what Redis is, why it is so popular, its common use cases, and the differences between Redis cluster and standalone configurations.

Additionally, you will need Cert Manager to generate a free SSL Certificate, enabling your application to run using the HTTPS protocol. You can learn how to accomplish this in the guide provided below:

How to get Free SSL Certificate for Kubernetes Cluster using Let’s Encrypt
Using SSL is essential for any website that wants to protect its users’ sensitive information. Without SSL, data can be intercepted and stolen by hackers, putting users’ privacy and security at risk.

Prepare Deployment

We will create two Deployment Manifests: one for the web application and another for the Queue Worker. Both deployments will utilize a single environment configuration, which can be represented using the ConfigMap resource below:

Install it to Kubernetes

~$ kubectl apply -f configmap.yml

Create a deployment for web application

~$ kubectl apply -f deployment.yml

Create another one for Queue Worker

~$ kubectl apply -f deployment.yml

Now, we can create Service for it

~$ kubectl apply -f service.yml

The last one for Kubernetes installation is creating Ingress resource

~$ kubectl apply -f ingress.yml

Using HTTPS

When running an application in a container, Laravel might still use HTTP instead of HTTPS. It appears that the request protocol detector does not work correctly in this scenario. As a result, we need to programmatically force Laravel to rewrite the base_url variable to use HTTPS.

To achieve this, open the pp/Providers/AppServiceProvider.php file and add the following code snippet within the boot function:

public function boot()
{
    if(!$this->app->environment('local')) {
        \URL::forceScheme('https');
    }
}

Great! You have now successfully set up your Laravel Application to run on a Kubernetes Cluster!


About 8grams

We are a small DevOps Consulting Firm that has a mission to empower businesses with modern DevOps practices and technologies, enabling them to achieve digital transformation, improve efficiency, and drive growth.

Ready to transform your IT Operations and Software Development processes? Let's join forces and create innovative solutions that drive your business forward.

Subscribe to our newsletter for cutting-edge DevOps practices, tips, and insights delivered straight to your inbox!