Laravel: How to setup The Most Popular PHP Framework Application on Kubernetes Cluster
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.
Assuming you have an application with the following specifications:
- Laravel Application
- Domain: https://example.com
- 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
- MySQL as the database
- Implements the HTTPS protocol
- Laravel Application running inside a Docker Container
- PHP executing on top of php-fpm, with Nginx serving as a Reverse Proxy to manage HTTP requests and redirect them to php-fpm
- 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.
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
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 setup Redis on Kubernetes Cluster
In the world of databases and data storage, Redis has gained significant popularity in recent years. This article will…
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
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
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()
Great! You have now successfully set up your Laravel Application to run on a Kubernetes Cluster!
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.