Prerequisites

This article assumes you have a basic understanding of Docker and Kubernetes, Gitlab CI and that you have already set up a Kubernetes Cluster.

Start a Laravel Project

The first thing you’ll need is a Laravel application, use composer to start a new project.

composer create-project --prefer-dist laravel/laravel blog

Dockerize your Laravel Project

There’s a number of ways to dockerize your Laravel project. You may use the official Nginx and PHP images form dockerhub, but I found it’s a bit troublesome to set them up.

So instead of messing around with all the different kinds of docker images, I came across thecodingmachine/docker-images-php, a set of production-ready docker images.

To build a production-ready image, we will use thecodingmachine/php:7.3-v2-slim-apache as our base image. The Dockerfile looks like this:

We will configure Gitlab CI to build the docker image automatically later.

Create Kubernetes Deployment Files

Here are all the yaml files we need to deploy our Laravel Application.

Deployment

Our deployment.yaml contains two deployments actually. One is for our main Laravel application, while the other one is for Laravel Horizon. If you do not plan to use Horizon, you can simply remove it.

There’s an init container to optimize configuration and route loading. To share the application code between init container and app container, it uses an emptyDir.

There’s also affinity setting to tell Kubernetes to try it best to schedule the pods among different nodes as to avoid downtime.

CronJob

We use cronjob.yaml that leverage Kubernete’s CronJob to run php artisan schedule:run every minute. We feel like this is a more robust way of scheduling cron by fine-tuning activeDeadlineSeconds , backoffLimit and startingDeadlingSeconds to make sure our cron gets scheduled.

ConfigMap, Ingress & Service

Our ingress.yaml and service.yaml is pretty standard, we use CloudFlare DNS verification to obtain HTTPS certificates from Let’s Encrypt (It’s commented).

As for configmap, it’s recommended to use secret to store sensitive information like the database password.