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

8grams
4 min readMay 13, 2023

--

Laravel PHP Framework

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:

Application 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:

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:

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.

--

--

8grams

We are a DevOps Consulting Firm with a mission to empower businesses with modern DevOps practices and technologies