![]() |
Deploying Laravel on Kubernetes with Ingress: A Complete Starter Guide |
Laravel is one of the most popular PHP frameworks, widely used for building scalable web applications. But when it comes to deploying Laravel in modern cloud environments, things can get tricky—especially if you want high availability, scalability, and maintainability. That’s where Kubernetes shines. And to make things easier, I’ve put together a Laravel on Kubernetes (with Ingress) Starter Pack, available on GitHub: Laravel-on-Kubernetes-with-Ingress-Starter-Pack.
This bundle is designed to give you a production-ready setup to containerize, deploy, and scale your Laravel application using Kubernetes, complete with Nginx Ingress support. Let’s walk through everything included and how to get it running.
What’s Inside the Starter Pack
The repository provides a complete Kubernetes-ready package for Laravel. Here’s what you’ll find:
- Dockerfile — A multi-stage build using PHP-FPM 8.3 and Nginx, managed with Supervisor.
- .docker/ — Contains PHP, Nginx, and Supervisor configuration files.
- k8s/ — Kubernetes manifests, including:
- Namespace
- ConfigMap
- Secret
- PersistentVolumeClaim (PVC)
- Deployment
- Service
- Ingress
- Horizontal Pod Autoscaler (HPA)
- kustomization.yaml for easy management
Step 1: Configure Secrets and Environment Variables
Before deploying, configure your Laravel app secrets and environment variables:
- Secrets (sensitive values like database credentials):
- Open
k8s/secret-app.yaml
- Set values for
DB_*
,APP_KEY
, andAPP_URL
- Open
- ConfigMap (non-sensitive configs):
- Open
k8s/configmap-app.yaml
- Add values like cache settings, mail driver, or any other non-secret environment variable
- Open
- StorageClass:
- In
k8s/pvc.yaml
, updatestorageClassName
with your cluster’s available storage class - Or, remove the line to use the default StorageClass
- In
Step 2: Build and Push Your Docker Image
Your Laravel app needs to be containerized. From the project root, run:
docker build -t yourrepo/laravel-app:latest .
docker push yourrepo/laravel-app:latest
Update the image name inside k8s/deployment.yaml
to match your repository.
Step 3: Deploy to Kubernetes
Once everything is set, apply the manifests:
kubectl apply -k k8s/
This will create all required Kubernetes resources: namespace, deployment, service, PVC, Ingress, and HPA.
Step 4: Configure Ingress, DNS, and TLS
The Ingress
resource exposes your Laravel app to the outside world:
- In
k8s/ingress.yaml
, replacelaravel.example.com
with your domain. - If you use cert-manager for TLS:
- Uncomment the TLS section in
ingress.yaml
- Set
cert-manager.io/cluster-issuer
to match your cluster issuer
- Uncomment the TLS section in
- Finally, point your DNS
A
/AAAA
records to your Ingress Controller’s external IP.
Step 5: Handle Laravel Storage and Cache
Laravel needs persistent storage for uploads, logs, and cache:
- A PVC is mounted at
/var/www/html/storage
to persist uploaded files. /var/www/html/bootstrap/cache
usesemptyDir
, which is ephemeral.
This ensures uploads remain intact across pod restarts.
Step 6: Add Health Checks
To ensure Kubernetes can properly manage pod readiness, a health check route is included:
Route::get('/healthz', fn() => response('OK', 200));
The readiness probe in deployment.yaml
will then monitor this endpoint.
Step 7: Enable Auto-Scaling
The Horizontal Pod Autoscaler (HPA) is configured in k8s/hpa.yaml
:
- Scales between 2–10 replicas
- Triggered when CPU utilization exceeds 70%
You can tweak these settings based on your workload.
Advanced Notes & Options
- Asset Delivery via CDN: If you already serve assets via a CDN, adjust cache headers in
.docker/default.conf
. - Redis / Queues / Octane: Add Redis and queue services in your cluster, then configure their envs in
k8s/secret-app.yaml
. - Alternative Setup: If you prefer sidecar Nginx instead of a combined PHP-FPM + Nginx container, update the Deployment spec accordingly.
Why Use This Starter Pack?
Deploying Laravel on Kubernetes can be complex, with many moving parts—Docker, Nginx, PHP-FPM, PVCs, Ingress, scaling, and secrets. This starter pack simplifies it by providing a ready-to-use boilerplate that you can customize for your own projects.
With this setup, you get:
- A production-ready Laravel deployment
- Automatic scaling with HPA
- Persistent storage for user uploads
- Secure Ingress with TLS support
- Health checks for reliable rollouts
Comments from Facebook