项目作者: markhilton

项目描述 :
Laravel app deployment for auto scaled Kubernetes cluster
高级语言: Dockerfile
项目地址: git://github.com/markhilton/larakube.git
创建时间: 2017-12-30T00:05:55Z
项目社区:https://github.com/markhilton/larakube

开源协议:MIT License

下载


Laravel

Things evolve faster than my commitment to this documentation, but may still be helpful…

Walk through deploying a Laravel application on auto scaled Kubernetes cluster.

Goal: Deploy Laravel PHP app that will auto scale based on average CPU utilization across cluster nodes.

Install Laravel with composer

  1. composer global require "laravel/installer"

Create new Laravel project

  1. composer create-project --prefer-dist laravel/laravel laravel-project

Local development

I have created docker-compose.yml with a typical infrastructure stack that includes:

  • MySQL (default database)
  • Redis (default cache engine)
  • Node (for auto compiling VUE components into app.js & css.js using webpack)

Linked following folders to Docker volumes:

  • MySQL data storage
  • Laravel vendors/
  • Laravel storage/
  • Laravel storage/public
  • Laravel node_modules

Therefore it’s important to remember that while you work with this stack, the Laravel logs, compiled templates, cache files, vendor libraries, user uploads and application storage files, as well as MySQL database files will be saved in Docker volumes.

If you installed docker-compose already, simply run:

  1. docker-compose up

and watch logs as Docker will pull all required stack images and provision application environment:

  • install required Laravel storage folders
  • run pending database migrations
  • run composer to install all vendor libraries
  • run node to install all dependencies to compile: app.js & app.css

Both composer and node container will remain running. Node will monitor (watch) for changes in resourse/js/ files to re-compile them on the fly.

Composer will remain running if you need to install additional vendor libraries, simply enter composer container shell with and for example install Redis support libs:

  1. docker exec -ti composer bash
  2. composer require predis/predis
  3. composer require laravel/horizon

Node container:

  1. docker exec -ti node bash
  2. npm install

Laravel Horizon is a great package to manage Laravel queues with Redis.

Docker

For the purpose of this demo I’ve built a public Docker image and pushed to my account. Dockerfile adds Laravel app into a lightweight alpine image.

build application docker image

  1. docker build -t crunchgeek/laravel-project .

push image to the repository

  1. docker push crunchgeek/laravel-project

Kubernetes

Having to work with Kubernetes for the last year I’ve got some experience with both Google Cloud and AWS services providers.

I installed K8s on AWS using KOPS and do not recommend this for production workloads. I run into multiple issues around I/O performance with Docker overlay. AWS EKS does not appeal to me like GKE. Google K8s is real hands off managed master node, with easy upgrades and it ended up as my go to solution.

I also learned about Helm, Kubernetes package manager. So once you get your K8s cluster up on GKE I recommend this tutorial to install Helm.

test dry run helm chart

  1. helm install --name laravel-labs --dry-run --debug ./laravel

install or upgrade current helm chart

  1. helm upgrade laravel-labs ./laravel -i

delete helm chart

  1. helm del --purge laravel-labs

That’s it! Now the infrastructure will auto adjust itself to the app resources demand.

Ingress / Load Balancing

Every time you deploy a completely independent application on Kubernetes exposed to the public network - it will probably come with a Load Balancer service. This may be costly if you have to deploy several of them.

Instead better solution is to use Nginx Ingress Helm Chart with a single Load Balancer and IP address. You can deploy as many new services as you need and use Ingress to route the traffic to a specific app.

SSL support

The easiest way to deploy SSL protected application is to use Cert Manager Helm Chart, which will provision SSL certificates using free Let’s Encrypt service.

Deploying SSL protected sites is as simple as adding a single line in your ingress.yaml