Deploying a Laravel app in Kubernetes on Google Cloud

Deploying a Laravel app in Kubernetes on Google Cloud

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

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

Kubernetes, also known as K8s, is an opensource system for production-grade container orchestration. The word Kubernetes originates from a Greek word which means helmsman or pilot. K8s is an abbreviation derived by replacing the eight letters “ubernete” with “8”. Modern application stacks 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 (software engineers or DevOps engineers) to effectively manage containers and group them into clusters. Why should I use Kubernetes? K8s make complicated concepts such as horizontal scaling, load balancing, automated rollouts and rollbacks as painless as writing a line of command. Companies currently taking advantage are Evernote, Intel, Shopify and many more.

  • Must have a user account on GCP (Google Cloud Platform)
  • Must have a basic understanding of Google Cloud Platform
  • Must have kubectl installed on your local machine (latest version)
  • Must have a basic understanding of Kubernetes
  • Must have Composer installed on your local machine (version >= 1.8.0)
  • Must have PHP setup on your local machine (version >= 7.1.3)
  • Must have Google Cloud SDK installed (version >= 206.0.0)
  • Must have a basic understanding of Laravel
  • Must have Docker installed on your local machine (version >= 18.09.0)
  • 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 tutorial, 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:

    // remember to change PROJECT_ID to your desired project name

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

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

Building the application image

In this section, we will:

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

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

Laravel installation

To install Laravel version 5.7 we will run the following command:

    composer create-project --prefer-dist laravel/laravel app "5.7.*"

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, we’ll navigate into our project directory like so:

$ cd app/

The inside our project directory we’ll run our Laravel application like so:

$ php artisan serve

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 <[email protected]>
    COPY . /var/www/
    RUN chmod -Rf 777 /var/www/storage/

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

With our Dockerfile inside our application directory, we will run the following command to dockerize our Laravel App.

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 two commands. First one to authenticate the Docker CLI (command line interface) tool to the container registry.

gcloud auth configure-docker

This command only needs 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 (our Laravel app) on it.

Before we continue, let’s briefly talk about what Laravel is. Laravel is a super fast PHP framework for web artisans. Performance should be a key feature to be considered when building applications. Deploying a Laravel application in K8s is going to give our application an edge over other applications running on bare metal when it comes to performance (deployment, scaling, load balancing, logging and monitoring) and flexibility.

Creating a Kubernetes cluster on GKE

What is 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 a 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 will 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 on a web browser, you will see this:

Congrats! you just deployed a Laravel application to Kubernetes 🎉


In this tutorial, we have learned what Kubernetes is, why we need to use it and what it means to deploy a Laravel application in a K8s cluster.

Learn More

Learn Kubernetes: A Detailed Guide to Orchestrating Containers

An illustrated guide to Kubernetes Networking

An Introduction to Kubernetes

Laravel 5.8 Tutorial for Beginners

Build a live commenting system with Laravel and Vue.js

Build a CMS with Laravel and Vue

Learn Kubernetes from a DevOps guru (Kubernetes + Docker)

Learn DevOps: The Complete Kubernetes Course

Kubernetes for the Absolute Beginners - Hands-on

How Kubernetes Helps to Enable DevOps

How Kubernetes Helps to Enable DevOps

The automation and infrastructural capabilities of Kubernetes makes it an ideal technological partner for DevOps. In this article, you'll see 10 ways Kubernetes enables DevOps

The automation and infrastructural capabilities of Kubernetes makes it an ideal technological partner for DevOps. In this article, you'll see 10 ways Kubernetes enables DevOps

DevOps was an idea before its time.

It actually took a while for technology to catch up and fully implement the principles and vision of DevOps, but that is how innovation works.

A new set of tools in our toolboxes (though it's now taking up more and more mindshare) is end-to-end automation, and Kubernetes helps you manage that. Kubernetes is an open source framework for "automating deployment, scaling, and management of containerized applications." Originally introduced by Google, Red Hat, and others pushed it forward the last 3-4 years.

At the 2017 All Day DevOps conference, Siamak Sadeghianfar laid out 10 ways Kubernetes enables DevOps. We thought it was worth revisiting what he had to say as **Kubernetes **only becomes more prominent.

**So, how exactly does Kubernetes enable DevOps? Let's dive into what Siamak had to say: **

1. Deployment automation.

You should automate every step of your delivery pipeline. Kubernetes automates the deployment of containers. Each component becomes a container image.

**2. Infrastructure as code. **

With **Kubernetes **your entire infrastructure is code. This means any part in your application (databases, ports, access controls, etc.) can be described in a way **Kubernetes **can use. For example, you store your infrastructure code in a version-control repository. **Kubernetes **takes the code, and based on instruction, deploys, and maintains your infrastructure. This happens automatically and consistently.

3. Configuration as code.

**Kubernetes **allows you to "configure as code." Traditionally, an admin had to run configuration scripts manually to make sure they get the right one. **Kubernetes **keeps the file in the source repo. This allows you to describe where the file needs to go in the container and how the application consumes it. Additionally, it can be version controlled.

4. Immutable infrastructure.

Since the inception of servers, there's been one problem: every time you put out a fire you change the state of the virtual machine. Eventually, you don't know exactly what the server looks like and you can't recreate it, even though you know it works. (These virtual machines are referred to as snowflakes.)

In Kubernetes, new containers are immutable; they are created when there is a problem with the original state. So, you can be confident that you know exactly how the new environment is configured.

5. On-Demand Infrastructure.

Developers can create hybrid services and infrastructure on-demand from the self-service catalog. This gives control to developers to get the resources they need, yet allows operations to control the configuration of the services. Kubernetes follows open service and API standards so you can expose cloud services.

6. Environment consistency.

Build once, and deploy into production-like environments everywhere. Kubernetes allows you to build a golden image and use the exact same image for every single place you deploy your container. This gives you a consistent, production-like environment so it is the exact same whether you are on your local Windows or Mac development machine, a test server, etc. This helps you "shift left" so that you can see issues when you are in development instead of in production.

7. Continuous Delivery pipeline.

This is a series of automated steps to test code before it goes in production. Jez Humble says the role of Continuous Delivery is, "an automated process to prove to you a change is a bad change and it should not go into production." Continuous Integration (CI) means every change is tested. Continuous Delivery (CD) automates all the way to production.

How can you tell if you have fully implemented Continuous Delivery? Siamak says that the test is if someone walks in and says, "Can you go to production right now?" can you do it without breaking a sweat?

8. Zero downtime deployments.

When you have several deployments a day, you can't pull down production to deploy. You must have safe, rolling updates without disrupting the production traffic. Kubernetes helps with blue/green deployments so that you can set up a new environment and switch to the new one without downtime.

9. A/B Testing.

How can you evaluate proposed changes, such as changing copy or the color of buttons? A/B testing delivers different versions to different customers so you can get real-world test results. Kubernetes manages routing traffic to different versions. The same version with slight modifications are possible.

10. Cross-functional collaboration.

This is shared access to environments with granular control. Kubernetes goes beyond the silos so everyone has the same set of environments, but you can grant access to different roles and allow different roles to do different things. For instance, developers may be able to push to production, while Infosec may have view access, and QA may have access to live containers, but not building or deploying. The entire team can access production, but operations is the only one who can make containers.

The growing popularity of Kubernetes is undeniable. Luckily, with these 10 capabilities, Siamak has laid out a great path for us all to follow as they continue to not only make their way into the DevOps pipeline, but in many ways enable it.

What is Kubernetes | Kubernetes Tutorial For Beginners

What is Kubernetes | Kubernetes Tutorial For Beginners

This video on "What is Kubernetes | Kubernetes Tutorial For Beginners" will give you an introduction to one of the most popular Devops tool in the market - Kubernetes, and its importance in today's IT processes. This tutorial is ideal for beginners who want to get started with Kubernetes & DevOps

What Is Kubernetes | Kubernetes Introduction | Kubernetes Tutorial For Beginners

The following topics are covered in this training session:

  1. Need for Kubernetes
  2. What is Kubernetes and What it's not
  3. How does Kubernetes work?
  4. Use-Case: Kubernetes @ Pokemon Go
  5. Hands-on: Deployment with Kubernetes

Building and Managing Kubernetes with Kubernetes

Building and Managing Kubernetes with Kubernetes

Building and Managing Kubernetes with Kubernetes: Kubernetes as a declarative and portable system can be used to do many things in different ways.

Kubernetes as a declarative and portable system can be used to do many things in different ways.

At eBay we built a fleet management system based on k8s. Everything(server, subnet, OS, package and state) is declarative and can be modeled as CRDs in k8s, or referred to as a commit id in git from the objects. By running various controllers on top of these CRD objects, we use k8s to manage k8s, and the entire eBay data center. - Our system provisions hosts the same way k8s creates and manages pods. - We build k8s clusters with Salt. each host has a set of states defined in its salt CRD object. controllers pull states from git based on commit ids to apply. - We build both schedulers and deployment transactions to manage the k8s clusters for both config deployments and upgrades. This declarative, highly scalable, auto healing, and cloud native system is what we think can unify eBay’s fleet.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about Kubernetes

An illustrated guide to Kubernetes Networking

Google Kubernetes Engine By Example

An Introduction to the Kubernetes DNS Service

Deploying a Laravel app in Kubernetes on Google Cloud

How to build a Microservice Architecture with Spring Boot and Kubernetes?