PHP Laravel Development with Kubernetes using DevSpace — Developer Edition

Image of a PHP stuffed elephant doll sitting on a laptop

Architecture

  • PHP-FPM container, which processes all the PHP assets.
  • Nginx container, which serves static files and acts as a reverse proxy for the PHP assets.
  • MySQL container, as the database.
  • Redis container, as session and cache.

Introduction to DevSpace

Feature Highlights

  • Agile development on local and remote Kubernetes clusters. Execution of entire continuous development and deployment pipeline, and a single command to deploy all components of your application.
  • Declarative configuration kept in source code, in the devspace.yaml file. All of the development, deployment, and pre/post-deployment actions are defined in a single file.
  • Hot Reloading for faster feedback. Instead of building and re-deploying artifacts, DevSpace allows you to use high-performance and bi-directional file synchronization. This allows changes to trigger a hot-reload on the deployed container. All of these features are highly configurable.
  • Extensibility. You can extend the functionality of DevSpace via the plugin system. Hooks and commands are also built-in constructs; you can expand the functionality heavily used in CI/CD pipelines.
  • Easy clean Up. You can delete the resources created via `devspace purge` in a single simple step.
  • Client Only. DevSpace doesn’t require server/cluster side components. A single executable on a local machine is sufficient to develop, troubleshoot and deploy.

Requirements and Setting Up Development Environment

  • Kubectl, documentation here.
  • Helm, documentation here.
  • DevSpace, installation instructions here.

Developing with DevSpace

$ git clone git@github.com:loft-sh/devspace-php-laravel-nginx.git
$ cp .env.example .env
$ devspace run generate-key
$ devspace list vars
Variable              Value                                                
APP_DEBUG true
APP_IMAGE leventogut/php-laravel-nginx-devspace
APP_KEY xxxxxxxxxxxxxxxx
ASSET_VOLUME_NAME static-asset-volume
ASSET_VOLUME_SIZE 1Gi
DB_DATABASE laravel
DB_HOST mysql
DB_MYSQL_VERSION 8.0.23
DB_PASSWORD xxxxxxxxxxxxxxxx
DB_PORT 3306
DB_ROOT_PASSWORD xxxxxxxxxxxxxxxx
DB_USERNAME laravel
NGINX_CONFIG_HASH 740941
NGINX_IMAGE_VERSION 1.9
REDIS_PASSWORD xxxxxxxxxxxxxxxx
REDIS_VERSION 6.0.12

#Running devspace dev

$ devspace use context docker-desktop[info] Your kube-context has been updated to 'docker-desktop' To revert this operation, run: devspace use context maple-staging [done] √ Successfully set kube-context to 'docker-desktop'$ devspace use namespace laravel[info] The default namespace of your current kube-context 'docker-desktop' has been updated to 'laravel' To revert this operation, run: devspace use namespace [done] √ Successfully set default namespace to 'laravel'
$ devspace dev
Route::get('ping', function () {
return "pong";
})

Port Forwarding and Reverse Port Forwarding

$ devspace list commandsName           Command                                                            Description
artisan devspace enter -c php -- php artisan Entry point for artisan commands.
composer devspace enter -c php -- composer Entry point for composer commands.
php devspace enter -c php -- php Entry point for PHP commands.
npm devspace enter -c php -- npm Entry point for NPM commands.
generate-key TMP_FILE=.devspace/app_key.tmp && docker run --rm -v $PWD:/ap... Generate APP_KEY.
mysql devspace enter -c mysql -- mysql -h'mysql' -P'3306' -u'larave... Enter to MySQL shell.
  • Changing MySQL user and password
  • Running npm run watch on the PHP container.
  • Reloading Nginx to re-read the configuration.

Deploying to Production

$ devspace deploy -p production

Troubleshooting with DevSpace

  • Logging
  • Entering into containers
  • Running commands inside the containers
  • Interactive mode

Entering to and Working with Containers

$ devspace enter -c php[info] Using namespace 'default' [info] Using kube context 'docker-desktop' [info] Opening shell to pod:container app-0:php root@app-0:/var/www/html#
? Which pod do you want to open the terminal for?
[Use arrows to move, type to filter]
> redis-master-0:redis app-0:nginx
app-0:php
mysql-0:mysql
$ devspace run php ./vendor/bin/phpunit[info]   Using namespace 'default'
[info] Using kube context 'docker-desktop'
[info] Opening shell to pod:container app-0:php
PHPUnit 9.5.3 by Sebastian Bergmann and contributors.

.F 2 / 2 (100%)

Time: 00:00.122, Memory: 20.00 MB

There was 1 failure:

1) Tests\Feature\ExampleTest::testBasicTest
Expected status code 200 but received 302.
Failed asserting that 200 is identical to 302.

/var/www/html/vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php:187
/var/www/html/tests/Feature/ExampleTest.php:19

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
$ devspace run artisan test[info]   Using namespace 'default'
[info] Using kube context 'docker-desktop'
[info] Opening shell to pod:container app-0:php

PASS Tests\Unit\ExampleTest
✓ basic test

FAIL Tests\Feature\ExampleTest
⨯ basic test

---

• Tests\Feature\ExampleTest > basic test
Expected status code 200 but received 302.
Failed asserting that 200 is identical to 302.

at tests/Feature/ExampleTest.php:19
15▕ public function testBasicTest()
16▕ {
17▕ $response = $this->get('/');
18▕
➜ 19▕ $response->assertStatus(200);
20▕ }
21▕ }
22▕


Tests: 1 failed, 1 passed
Time: 0.19s

Conclusion

Further Reading

--

--

--

>> www.loft.sh << Build Your Internal Kubernetes Platform With Virtual Clusters, Namespace Self-Service & Secure Multi-Tenancy

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Liskov Substitution Principle

Logical Deductions-Maths for Computer Science

Overloading (Compile-Time Polymorphism)

A Project Update — Progress, Onboarding and Looking Forward.

[Git]Front End Developer Interview Questions [Git]

Why Data turns into a Software Engineering Business

AdminLTE — Free Resources

AdminLTE — Free Admin Dashboard.

Events in Entity Component Systems

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Loft Labs

Loft Labs

>> www.loft.sh << Build Your Internal Kubernetes Platform With Virtual Clusters, Namespace Self-Service & Secure Multi-Tenancy

More from Medium

Logging in PHP Cloud Functions

PHP: Making sense of the 8 log levels

Run Symfony commands inside a docker container

Manually implement OAuth Client functionality in Symfony