How to deploy Laravel to Kubernetes

Laravel is an excellent framework for developing PHP applications. Whether you need to prototype a new idea, develop an MVP (Minimum Viable Product) or release a full-fledged enterprise system, Laravel facilitates all of the development tasks and workflows.

How you deal with deploying the application is a different story. Vagrant is very good with setting up a local environment similar to a remote server. However, in production, you will most likely require more than just one web host and one database. You'll probably have separate services for several requirements. You also need to have mechanisms in place to ensure that the application is always online and that the servers can efficiently balance the load.

In this article, I’ll explain how to deal with the simple requirement of running a Laravel application as a local Kubernetes set up.

Kubernetes, Why and What?

Kubernetes is an open-source system initially designed by Google to facilitate the management of containerised applications in a clustered environment. Some refer to it as an orchestration platform, and Kubernetes is not the only available platform of this type. Although, it does have a very high and fast adoption rate. Not to mention that it is quite easy to implement once you get used to it.

If you're still wondering why someone can benefit from using Kubernetes, the answer is simple. Kubernetes makes it easier to set up and manage as many clusters as required across multiple projects.

Deploying a Laravel Application to Minikube

As I've afore-mentioned, I will be showing you how to deploy a straightforward and stateless Laravel application to Kubernetes. I aim to detail the steps involved in accomplishing this while explaining why specific actions are required. Furthermore, I will show you how to quickly scale the application and also make it available on a named host using an Ingress controller.

You can run Kubernetes on several cloud hosting providers such as Google Cloud Engine and Amazon Web Services. In this tutorial, you will run the application on Minikube, a tool that makes it easy to run Kubernetes locally.

Similar to Vagrant, Minikube is merely a Virtual Machine that contains a Kubernetes platform and Docker. You will need both to deploy your application as a Docker container and scale it to three instances using Kubernetes.

The application

I have prepared a simple Laravel application which you can clone from GitHub. It is nothing more than a fresh Laravel installation. Therefore you can follow this example using either the demo application or just create a new Laravel application. To use the demo application clone it in your project's directory.

Kubernetes deploys containerised applications, and therefore as a first step, you will need to build a Docker image of the demo application. Since this tutorial will be run locally on Minikube, you can just build a local Docker Image from the Dockerfile included in the example code.

The above command tells kubectl to run our demo application from the Docker image. The first parameter of the command simply asks kubectl to not pull the image from a registry such as Docker Hub if it exists locally which in this case it does. Do note that you still need to be logged on to Docker's so that kubectl can check if the image is up to date.

You can also use the Minikube GUI dashboard to monitor the cluster. The GUI also helps with visualising most of the discussed concepts. To view the dashboard, just run the following:

minikube dashboard

or to acquire the dashboard's URL address:

minikube dashboard --url=true

Exposing a Service

So far you have created a deployment which is running the application's container. A Pod running in the cluster has a dynamic IP. If you route the traffic directly to it using the IP, you may still need to update the routing table every time you restart the Pod. In fact, on every deployment or container restart, a new IP is assigned to the Pod. To avoid managing IP addresses manually, you need to use a Service. The Service acts as a load balancer for a set of Pods. So even if the IP address of a Pod changes, the service is always pointing to it. And since the Service always has the same IP, you won't need to update anything manually.

will show you a list of running services. You can also view the running service under the "Services" navigation menu within the dashboard. A more exciting way to verify this deployment and the service exposure is obviously seeing the running application in the browser 😊

To obtain the URL of the application (service), you can use the following command:

minikube service --url=true laravel-kubernetes-demo

which will output the IP address and port number similar to:

http://192.168.99.101:31399

or, launch the application directly in the browser:

minikube service laravel-kubernetes-demo

Are you enjoying reading this article?

Subscribe to receive email notifications when I publish new articles and code libraries.

I will not share your email address with anyone, and you can unsubscribe at any time.View the privacy policy for more information.

Scaling

And that is it. You have successfully deployed the application in Kubernetes. It's exciting. But what's the point of doing all of this? Well, you only have one deployment with a single Pod running, provisioned to a Node with the exposed web service. Let's scale this deployment to two more instances of the application.

Done. You have replicated the first Pod to another two, giving you three Pods running this service. Running the get deployment command will verify this.

kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
laravel-kubernetes-demo 3 3 3 3 59m

You can also see this in the Dashboard under Pods or in the Service detail screen.

Now you're running three instances of the applications using three Pods.

Imagine your application becoming even more popular. Thousands of visitors are using your website or software. In the past, you may have been busy writing more scripts to create more instances of your application. In Kubernetes you can scale to multiple instances in a snap:

You can see how convenient it is to use Kubernetes to scale your website.

Ingress

You've already achieved great things, you deployed the application and scaled the deployment. You have already seen the running application in the browser when pointed to the cluster's (Minikube) IP address and node's port number. Now, you will see how to access the application through an assigned URL as you would do when deploying to the cloud.

To use a URL in Kubernetes, you need an Ingress. An Ingress is a set of rules to allow inbound connections to reach a Kubernetes cluster. The Ingress is necessary because, in Kubernetes, resources such as Pods only have IP addresses which are routable by and within the cluster. Meaning that they are not accessible or reachable to and from the world outside.

Among the basic content you would expect from a Kubernetes resource file, this file defines a set of rules to follow when routing inbound traffic. The laravel-kubernetes.demo URL will point to the Service where the application is running, as previously labelled laravel-kubernetes-demo on port 8181.

The Ingress resource is useless without an Ingress controller so you will need to create a new controller or use an existing one. This tutorial uses the Nginx Ingress controller for routing the traffic. Minikube (v0.14 and above) comes with the Nginx setup as an addon which you will need to enable manually:

minikube addons enable ingress

Please note that it may take few minutes for Minikube to download and install Nginx as an Ingress.

Once you have enabled the Ingress addon, you can create the Ingress in this way:

kubectl create -f path-to-your-ingress-file.yaml

You can verify and obtain the Ingress' information by running the following command:

You can now access the application through the minikube IP address as shown above. To access the application through the URL https://laravel-kubernetes.demo, you will need to add an entry in your hosts file.

This is just the beginning

Hopefully, this article has helped you in getting acquainted with Kubernetes. From my own experience, once one has performed similar deployments a couple or more times, things start getting habitual and make a lot more sense. But our Kubernetes journey has only just begun. In future articles, we will walk through more real-life applications using storage volumes to persist state, and we will also learn how to deploy to Cloud providers such as Google's Cloud Platform. Until then, check out these courses to get up to speed and possibly even become a Certified Kubernetes Administrator (CKA).

Special thanks to Daniele Polencic , I couldn't of written this article without your help 🙏

Have you enjoyed reading this article?

Don't be selfish, share it with your friends 😉

Got questions or feedback? Leave a comment, I will reply.

Full-stack development

Continuous results.

Continuous Integration equates to continuous delivery. If your project or team can benefit from an experienced engineer. I'm your go-to person. I enjoy leading by example, and my consultancy tends to stretch beyond the tasks at hand.

Latest articles

Getting started with Docker and Kubernetes on Windows can be daunting when you don't know where to begin.
And it doesn't help that installing the software isn't exactly a walk in the park.
In fact, you should already be a Docker and Kubernetes expert to navigate the options on how to install them. But don't worry! If you're just starting your journey with containers and…

I’ve been applying TDD (Test Driven Development) religiously for the past 5 years and now cannot imagine where I would even start from, in any code if it’s not from a test. The advantages TDD brought along with it are numerous and in my case, vital, for working on projects of a certain calibre. Even though the complexity of any code is now irrelevant to me when it comes to TDD…

I honestly feel obliged to write this article because I feel that most developers think about Event Sourcing as only another PoEAA (Pattern of Enterprise Application Architecture). Maybe reserved to software models which have a certain level of complexity or at least they need to look like they do so that the time and cost is justified. Event Sourcing is a lot more than just an…

Getting started with Docker and Kubernetes on Windows can be daunting when you don't know where to begin.
And it doesn't help that installing the software isn't exactly a walk in the park.
In fact, you should already be a Docker and Kubernetes expert to navigate the options on…

I’ve been applying TDD (Test Driven Development) religiously for the past 5 years and now cannot imagine where I would even start from, in any code if it’s not from a test. The advantages TDD brought along with it are numerous and in my case, vital, for working on projects of a…

I honestly feel obliged to write this article because I feel that most developers think about Event Sourcing as only another PoEAA (Pattern of Enterprise Application Architecture). Maybe reserved to software models which have a certain level of complexity or at least they need to…