Deploy a Laravel Project to Kubernetes

Deploy a Laravel Project to Kubernetes

Deploying Laravel Project to Kurbernetes

Prerequisites

Download Docker Desktop, once install open the settings and share the C drive and copy and paste the powershell command into Powershell.

Lets get started

Open git bash and run the following:

PROJECT_DIR=/c/Users/[username]/laravelProject
mkdir $PROJECT_DIR 
//Create your laravel app
docker run --rm -v /$PROJECT_DIR:/app composer/composer create-project --prefer-dist laravel/laravel . 
cd $PROJECT_DIR
//Copy over the env example to the useable env
cp .env.example .env
//generate your app key and optimise
docker run --rm -v /$PROJECT_DIR:/app --entrypoint="//usr/local/bin/php" composer/composer artisan key:generate
docker run --rm -v /$PROJECT_DIR:/app --entrypoint="//usr/local/bin/php" composer/composer artisan optimize

Create the container

Using the open source DevSpace development tool for Kubernetes we will containerise and deploy our Laravel Project.

npm install -g devspace
//Run it in powershell, make sure you select php but use defaults for the rest
devspace init

Open the Dockerfile in a code editor, add the following to the end.

ENV APACHE_DOCUMENT_ROOT=/var/www/html/public 
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf 
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

Kubernetes Cluster

As this is just a practice use the free DevSpace Kubernetes namespaces. Run this command

devspace create space laravel-test

Now register and you will see your powershell update. On the devspace page you will see a new namespace created.

Development

Lets test the Kubernetes cluster:

devspace dev

This builds the Docker image and depoloys the laravel project to the to the Kubernetes cluster. After this should open the Laravel project inside a containerwhich runs in a Kubernetes cluster but we can still access the application via localhost in the browser as long as devspace dev is still running.

Database

Open the devspace.yaml in the project root, change this file to add a MySQL database. Right under the line containing deployments:, add the following:

- name: database
  helm:
    chart:
      name: stable/mysql
    values:
      mysqlDatabase: "homestead"
      mysqlUser: "homestead"
      mysqlRootPassword: ${DB_ROOT_PASSWORD}
      mysqlPassword: ${DB_PASSWORD}
      resources:
        requests:
          memory: 0
          cpu: 0
      initContainer:
        resources:
          requests:
            memory: 0
            cpu: 0

Below it you should have:

- name: laravelproject
  helm:
    componentChart: true
    values:
      containers:
      - image: [username]/laravelproject      
      service:
        ports:
        - port: 80

Change it to this:

- name: laravelproject
  helm:
    componentChart: true
    values:
      containers:
      - image: [username]/laravelproject
      env:
        - name: DB_HOST
          value: database-mysql
        - name: DB_PASSWORD
          value: ${DB_PASSWORD}
      service:
        ports:
        - port: 80

If you receive the error:

[fatal]  deployments[1].helm.componentChart: component values are incorrect: yaml: unmarshal errors:
  line 3: field env not found in type latest.ComponentConfig

Remove the env lines we entered. Now lets redeploy using "devspace dev", this time it will ask you for the passwords and put "root" for both. Check that MySQL is running by searching in the logs for the following:

//Run
devspace logs -f  
//Look for mysqld: ready for connections or MySQL init process done. Ready for start up.

Now run

docker run --rm -v /$PROJECT_DIR:/app --entrypoint="//usr/local/bin/php" composer/composer artisan migrate

Auth

Add Auth to the project by running

docker run --rm -v /$PROJECT_DIR:/app --entrypoint="//usr/local/bin/php" composer/composer artisan make:auth
Authentication scaffolding generated successfully.  
//Redeploy
devspace dev

Public Domain

To make it public and create the production site run:

devspace deploy

Categories: Posts