How to deploy Laravel with Docker on Kubernetes

Piethein Strengholt
5 min readMay 11, 2018

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

After cloning the repository, enter the root of the directory and run the following command from your terminal. This command requires you to have Docker installed. You can install the Docker client by using the instructions from https://docs.docker.com/. The command below builds the Docker image from docker-compose.yml file.

docker-compose build

The next step is to start the project, by starting the Docker image just created. Use the following command to start the image:

docker-compose up -d

If the image works correctly you should be able to open the following webpage. The project uses port 8181, so make sure port 8181 is not used by any other application. If everything works correctly you should see the ‘Laravel’ page. See the url and screenshot below:

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

Before pushing the image make sure you are logged into the Docker Hub. Run the command below to login. If you not have a Docker account please sign up using the this link: https://hub.docker.com/

docker login

Next step is to tag and label the image before pushing it to the Docker Hub repostory. I’m using my own credentials here, so if you want to use your own credentials change the pietheinstrengholt username. The laravel-on-k8s is my repository name. v1 is the tag name. You can use any name you want.

docker tag laravel-on-k8s:v1 docker.io/pietheinstrengholt/laravel-on-k8s:v1

The final and last step is to upload the image. Upload the image by using the following command:

docker push docker.io/pietheinstrengholt/laravel-on-k8s:v1

Go to the https://hub.docker.com and validate that the image has been uploaded correctly. If so, we can continue with Kubernetes.

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

The next step is that we start pulling in the image from the Docker Hub. If you want to use your own image, change the following file: deploy/app/deploy.yml

There should be a line with the following content. Change this line using your own credentials name.

image: docker.io/pietheinstrengholt/laravel-docker-k8s:v1

Next step is to pull in the image. Kubernetes has several concepts, such as Pods, Services, Volumes, Deployments, etc. if you want to know I recommend to have a look at this page: https://kubernetes.io/docs/concepts/

In order to deploy the Laravel image to Kubernetes use the following commands:

kubectl apply -f deploy/app/secret.yml
kubectl apply -f deploy/app/deploy.yml
kubectl apply -f deploy/app/service.yml

Next step is to open the MiniKube dashboard and validate if the image has been pulled in correctly. Use the following command:

minikube dashboard

Your default webbrowser will open a new tab using the following url:

http://192.168.99.100:30000/#!/overview?namespace=default

See the screenshot below:

In the screenshot you can see that the Laravel image is beging deployed. We can also validate the status by using the command line. Run the following command:

kubectl get service

A table should show up with the following conent:

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

The Laravel project should be listed here between the different other images. You can also use the following command to get an detailed overview of the Laravel service:

curl $(minikube service laravel-project — url)

Use the port name and open the browser to validate if the image is working accordingly:

http://192.168.99.100:32469/

See the screenshot below of Laravel running now on Kubernetes:

Congrats! You have now successfully deployed Laravel with Docker on Kubernetes. Next step is to extend it with other microservices, do proper load balancing and scaling, have the database configured correctly and expose it externally. These steps are not part of the tutorial yet, but I really hope this gives you a quickstart when you are interested in experimenting with Kubernetes.

In case you want to debug, you can use the following command to see the events generated by Kubernetes:

kubectl get events

Credits: My repository is a copy of the laravel-docker-k8s repo, but since I’ve adjusted it heavily I personal thought it was better to copy than rather forking it.

--

--

Piethein Strengholt

Hands-on data management professional. Working @Microsoft.