Continuous Delivery Pipeline for Deploying PHP Laravel Application on Kubernetes

Blog Single Post Image

Overview

Running Containers at any real-world scale requires container orchestration, and scheduling platform like Docker SwarmApache MesosAWS ECS but the most popular out of it is Kubernetes. Kubernetes is an open source system for automating deployment and management of containerised applications.

In this post, We’ll share the process how you can Develop and Deploy Microservices based PHP Laravel Application on the Container Environment –  Docker and Kubernetes and adopt DevOps in existing PHPApplications.


Prerequisites For Deploying Laravel Application on Kubernetes

To follow this guide you need –

  • Kubernetes

It is an open source platform that automates container operations, and Minikube is best for testing Kubernetes.

  • Kubectl

Kubectl is command line interface to manage Kubernetes cluster either remotely or locally. To configure kubectl on your machine follow this link.

  • Shared Persistent Storage

Shared Persistent Storage is permanent storage that we can attach to the Kubernetes container so that we don`t lose our data even when container dies. We will be using GlusterFS as the persistent data store for Kubernetes container applications.

  • PHP Application Source Code

Application Source Code is source code that we want to run inside a Kubernetes container.

  • DockerFile

Dockerfile contains a bunch of commands to build PHP Laravel application.

  • Container Registry

The Registry is an online image store for container images.

Below mentioned options are few most popular registries.


Writing a Dockerfile

The below-mentioned code is sample Dockerfile for PHP Laravel applications. In which we are using Apache Maven 3 as the builder for PHP Laravel applications and OpenJDK 8 as a base development environment. Alpine Linux is used due to its very compact size.

FROM ubuntu:14.04

MAINTAINER Xenonstack

# Installing PHP5 and Apache2
RUN apt-get update \
&& apt-get -y install apache2 php5 libapache2-mod-php5 php5-mcrypt php5-json curl git \
&& apt-get clean \
&& update-rc.d apache2 defaults \
&& php5enmod mcrypt \
&& rm -rf /var/www/html \
&& rm -r /var/lib/apt/lists/*

# Installing Composer
RUN curl -sS https://getcomposer.org/installer | php \
&& mv composer.phar /usr/local/bin/composer

# Adding Laravel configurations for apache2
COPY laravel.conf /etc/apache2/sites-available/000-default.conf

# Setting Working Directory
WORKDIR /var/www

# Creating PHP laravel project
RUN composer create-project –prefer-dist laravel/laravel laravel \
&& php laravel/artisan key:generate \
&& chown www-data:www-data -R laravel/storage

# Expose Apache2 Port
EXPOSE 80

# Persistent Data
VOLUME [“/var/www”]

# Starting Apache2 Web Server
CMD /usr/sbin/apache2ctl -D FOREGROUND

 

Below mentioned is the sample Apache2 config file for Laravel application.

Create a file name laravel.conf and add the below mentioned code to it.

<VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www/laravel/public

<Directory /var/www/laravel>
AllowOverride All
</Directory>

</VirtualHost>

 

Building PHP Laravel Application Image

The below-mentioned command will build your application container image.

$ docker build -t <name of your Laravel application>:<version of application>

 

Publishing PHP Laravel Application Container Image

Now we publish our PHP Laravel application container image to any container registry like Docker Hub, AWS ECR, Google Container Registry, Private Docker Registry.

Today I will be using Azure container registry for publishing container images.

You also need to Sign UP to Azure Cloud Platform then create container registry by using the link.

Now use this link to Pull and Push to Azure Container registry.

Similarly, we can push or pull any container image to any of the below-mentioned container registries like Docker Hub, AWS ECR, Google Container Registry, Private Docker Registry etc.


Continue Reading:XenonStack/Blog

Leave a comment