Deploying a Laravel app in Kubernetes on Google cloud

What is Kubernetes? Kubernetes, also known as K8s is an opensource system for production-grade container orchestration. Modern applications stack usually are made up of a minimum of two to three services that interact with each other (web service, database service, and caching service), which run inside containers. Container orchestration, simply put is organizing these respective containers together. K8s is one of many container orchestration tools that allows users to manage containers and group them into clusters.

In this article, we will learn how to create a K8s cluster, deploy a fresh Laravel installation, and expose it publicly.


  • Must have a user account on GCP (google cloud platform)
  • Must have kubectl installed
  • Must have Laravel installed
  • Must have google cloud SDK installed
  • Must have a basic understanding of Laravel
  • Must have a basic understanding of Docker

Once you have the requirements listed above, we can proceed.

In the beginning

Before we get our hands dirty, we will head over to the K8s engine page to create an application or select one if you have already created one. We will then wait for the engine to enable all the related services, which will take a couple of minutes.

In this article, we will be using our local shell with gcloud and kubectl installed. You can also use the cloud console which comes with the above-mentioned packages already installed.

To save yourself some keystrokes, you should run the following commands on your terminal:

$ gcloud config set project PROJECT_ID
$ export PROJECT_ID="$(gcloud config get-value project -q)"
$ gcloud config set compute/zone COMPUTE_ZONE

With all that setup, we can proceed to build a Docker container for our Laravel application. You can click here to view a list of available compute zones.

Building the application image

In this section, we will:

  1. Download a fresh installation of Laravel (version 5.5.43).
  2. Create a Dockerfile inside our project directory.
  3. Run the docker build command.

Once we are done with these steps, we will test run our application locally before it is uploaded to Google Cloud Registry(GCR). These steps will be explained in detail as we go further.

Laravel Installation

To install Laravel version 5.5.43. Run the following command in your terminal:

composer create-project --prefer-dist laravel/laravel app "5.5.43"

This command creates and installs a legacy version of Laravel, by creating a new folder app in the correct directory where the command was run in.

When the installation is done, run the command php artisan serve in the app/ directory. You should see an image like this in your browser when you click on the generated link.

If you see that, you are ready to move on to the next section 👍🏼.

Dockerizing our application

In our application root directory, we are going to create a Dockerfile with the following contents. Open your terminal and run this command:

$ touch Dockerfile

Insert the following content into the Dockerfile:

FROM creativitykills/nginx-php-server:2.0.0
MAINTAINER Neo Ighodaro <>
COPY . /var/www/
RUN chmod -Rf 777 /var/www/storage/

On the first line of our Dockerfile, we are pulling a nginx-php-server with a tag 2.0.0. The second line defines the maintainer while the third line moves our current project directory into the web directory /var/www of the nginx-php-server. On the fourth, line we made the /var/www/storage/ directory readable, writable and executable.

With our Dockerfile inside our application directory, our next course of action is to dockerize our Laravel application. Run this command to dockerize our Laravel application.

 docker build -t${PROJECT_ID}/laravel_k8_app:v1  

Where ${PROJECT_ID} is our project id defined at the beginning of this article. This line is going to build an image of our Laravel application with a v1 tag.

Test running our image
If you run the command docker images you’ll see something along the lines of:

You should see an image which looks like the image name you defined in the image above. After confirming our image was built, we’ll take our image for a spin locally by running the following command:

docker run -p 43211:80${PROJECT_ID}/laravel_k8_app:v1 

When you visit this URL you should see something familiar like so:

Yay! our application works in docker, next step will be to upload our image to GCR.

Pushing our image to GCR
Google cloud registry is a private Docker container registry that runs on the Google cloud platform. Pushing our image to GCR is as easy as running to commands. The first command is to authenticate the Docker CLI (command line interface) tool to Container registry.

 gcloud auth configure-docker 

This command is only needed to be run once. We’ll then push our image to the registry like so:

docker push${PROJECT_ID}/laravel_k8_app:v1 

And that’s it! our image has been uploaded to GCR. In the coming section, we’ll create a Kubernetes cluster on GKE (Google Kubernetes Engine) and deploy our Laravel application app on it.

Deploying our Laravel application

If you’re reading this section, you’ve either travelled far or strolled down here, either way, it is good to have you here. As stated above, we’ll be creating a K8s cluster and deploying our container on it.

Creating a Kubernetes Cluster on GKE
What’s a Kubernetes container cluster? A K8s container cluster basically contains a band of Google compute VM instances which are called Nodes. These Nodes run the Kubernetes processes necessary to make them part of a cluster. Simply put, You deploy your applications to clusters, your applications run on Nodes or the Nodes run your applications. Either way, your applications run in K8s cluster which is made up of Nodes.

Creating a K8s cluster is as simple as running a simple command:

 gcloud container clusters create laravel-k8-cluster --num-nodes=4 

This command creates a cluster named laravel-k8-cluster with four nodes. Give it some minutes, when it is done, you should see something along the lines of:

Deploying our app
To deploy our app onto our cluster we’ll run a simple command like so:

 kubectl run laravel-k8-web${PROJECT_ID}/laravel_k8_app:v1 --port 80 

This command creates a deployment with a name laravel-k8-web and runs our image pointing to the internal port of the container, which in this case is port 80. If successful, you should get a success message along the lines of “deployment.apps laravel-k8-web created”

To expose our application to the outside world (That is, the internet) we’ll run the following command:

 kubectl expose deployment laravel-k8-web --type=LoadBalancer --name=laravel-k8-web-svc 

This command exposes the deployment laravel-k8-web we created earlier and spins up a LoadBalancer with a service name laravel-k8-web-svc. If successful, you should get a success message along the lines of “service laravel-k8-web-svc exposed”

To get an “outside world” link to our application, we’ll run the following command.

 kubectl get services laravel-k8-web-svc 

When this command runs, just below the EXTERNAL-IP section we should see our “outside world” link. If you see the <pending tag, give it a minute and try the command again. If you navigate to the IP provided, you’ll see something along the lines of:

Congrats! you just deployed a Laravel application to Kubernetes 🎉


In this article, you’ve learned how to deploy a Laravel application in Kubernetes, In Part II of this tutorial, we’ll learn how we can scale, update our Laravel application as well as doing a little K8s housekeeping.

This post was originally posted on Pusher.