Deploying a Laravel app in Kubernetes
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 8 letters “ubernete” with “8”. 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( 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.

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

Requirements

  • 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).
  1. Create a Dockerfile inside our project directory.
  1. 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.5.43  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/