How to deploy Laravel with Docker on Kubernetes

Kubernetes is an open-source system for automating deployments, scaling, and management of containerized applications, which are written in languages such as PHP, NodeJS, Python or any other. This tutorial is a personal attempt to package and run Laravel (PHP Framework) using Docker and deploy it on Kubernetes. In this tutorial I’m using Minikube, which is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a Virtual Machine on your laptop or computer. It is intended for users who are looking to try out Kubernetes or develop with it day-to-day. In this tutorial I’m also using Docker Hub, which is a Docker repository to let you share images with other co-workers or developers. The full source code of this repository and the same tutorial can be found over here: https://github.com/pietheinstrengholt/laravel-docker-k8s

Build and run the docker image from the project folder

The first step of this tutorial is cloning the repository that has all the Laravel code and Docker related files. You can do this by issuing the following command:

git clone https://github.com/pietheinstrengholt/laravel-docker-k8s
docker-compose build
docker-compose up -d
http://localhost:8181/

Build the image and push to Docker Hub

The next step is to create a new Docker image, which will be pushed to the Docker Hub. The Docker Hub is a commercial repository that hosts Docker images from many different projects. Building and pushing an image allows us to pull in the image and deploy it to Kubernetes at a later stage. Eventually other users can use this image as a a base and extend it with their own code. Use the following command to build the image:

docker build . -f ./deploy/dockerfile -t laravel-on-k8s:v1
docker login
docker tag laravel-on-k8s:v1 docker.io/pietheinstrengholt/laravel-on-k8s:v1
docker push docker.io/pietheinstrengholt/laravel-on-k8s:v1

Deploy the project using Kubernetes (Minikube)

For the final part of this tutorial we will be using Minikube. Minikube is a local Kubernetes instance. If you don’t have Minikube installed, please use the following link: https://kubernetes.io/docs/tasks/tools/install-minikube/. If you have Minikube installed correctly, start Minikube by using the following command:

minikube start
image: docker.io/pietheinstrengholt/laravel-docker-k8s:v1
kubectl apply -f deploy/app/secret.yml
kubectl apply -f deploy/app/deploy.yml
kubectl apply -f deploy/app/service.yml
minikube dashboard
http://192.168.99.100:30000/#!/overview?namespace=default
kubectl get service
NAME | TYPE | CLUSTER-IP | EXTERNAL-IP | PORT(S) | AGE
hello-minikube | NodePort | 10.98.64.133 | <none> | 8080:30271/TCP | 2d
kubernetes | ClusterIP | 10.96.0.1 | <none> | 443/TCP | 2d
laravel-project | NodePort | 10.105.178.73 | <none> | 80:32469/TCP | 10m
nginx-http | ClusterIP | 10.100.126.245 | <none> | 80/TCP | 20h
curl $(minikube service laravel-project — url)
http://192.168.99.100:32469/
kubectl get events