If you are building a cloud-based application at scale, running it in Kubernetes is a no-brainer: It’s been adopted by the industry at an incredible rate, it has a wonderful community, and every single cloud vendor offers it as a service.
But the development experience available when building Kubernetes applications is less than ideal: write your code, build a Docker image, push it to the registry, redeploy, validate your changes and repeat. This flow is not only slow and full of friction but it prevents us from benefiting from PHP’s rich toolkit ecosystem. Think about this for a second. When was the last time that you debug with an actual debugger, instead of adding
echos everywhere in your code?
Okteto was created to solve this problem. On this blog post, we will show you how Okteto improves the developer experience in Kubernetes for PHP developers. You will be able to take full advantage of the entire PHP toolkit (debuggers, dependency managers, test frameworks, etc…) while developing your application directly in your cluster.
For this post, we’ll be using a very simple PHP sample application. Run the command below to get a local clone of it:
git clone https://github.com/okteto/php-getting-started
k8s.yml file contains the Kubernetes manifests to deploy the application. Run the application by executing:
You can deploy to your own Kubernetes cluster or give Okteto Cloud a try. Okteto Cloud is a development platform for Kubernetes applications. Sign up today to get a free developer account with 4 CPUs and 8GB of RAM.
kubectl apply -f k8s.yml
deployment.apps "hello-world" created
One command and we have the application up and running. Pretty cool no 😎?
The Okteto CLI is an open-source project that lets you develop your applications directly in Kubernetes while taking advantage of well-known local tooling. We will use it to speed up our development cycle instead of using the typical development workflow based on building docker images and redeploying containers.
Install the Okteto CLI:
MacOS / Linux
curl https://get.okteto.com -sSfL | sh
WindowsDownload https://downloads.okteto.com/cli/okteto.exe and add it to your `$PATH`.
With the application deployed, run the following command:
✓ Development container activated
okteto up command starts a development container, which means:
- The PHP Sample App container is updated with the docker image
okteto/hello-world:php-dev. This image contains the required dev tools to build, test and run the PHP Sample App. Check the Dockerfile to see how it is generated.
- A file synchronization service is created to keep your changes up-to-date between your local filesystem and your development container.
- Port 8080 (the application) is forwarded from localhost to the development container.
- Port 9000 (for Xdebug, more on this later) is reverse forwarded from the development container to localhost.
All of this (and more) can be customized via the
okteto.yml manifest file.
You can also use the file
.stignore to skip files from file synchronization. This is useful to avoid synchronizing binaries, build artifacts, git metadata or your
Okteto automatically forwards port
8080 from your local computer to the development container, making it accessible via
localhost. Test it by running the command below in a local shell:
index.php file in your favorite local IDE nd modify the response message on line 2 to be
Hello world from the cluster!. Save your changes.
Okteto will synchronize your changes to your development container and PHP’s webserver will automatically reload them.
Call your application from a local shell to test the changes:
Hello world from the cluster!
Cool! Your changes were instantly applied to your application running in Kubernetes. No commit, build or push required 😎!
Okteto enables you to debug your applications directly from your favorite IDE. Let’s take a look at how that works with PHPStorm, one of the most popular IDEs for PHP development.
If you haven’t already, fire up PHP Storm and load the sample application code there. Once the project is loaded, open
index.php and set a breakpoint in
line 2. Click on the
Start Listen PHP Debug Connections button on the PhpStorm toolbar.
Go back to your local shell and call the application:
If this is the first time you debug this application, the IDE will ask you to confirm the source mapping configuration. Verify the values and click ok to continue.
The execution will halt at your breakpoint. At this point, you are able to inspect the request object, the current values of everything, the contents of
$_SERVER variable, etc… Just as you would do if you were debugging locally.
The development container we are using already has PHP7 and XDebug installed. We pre-configured XDebug so that it accepts remote debugging requests on
127.0.0.1:9000. Finally, we configured the Okteto manifest to start a reverse tunnel on port 9000.
This configuration allows you to take advantage of PHPStorm’s Zero Configuration Debugging to make debugging extremely simple. And the coolest thing is that since all of this is described in your Okteto manifest, everyone collaborating in your project will get the exact same configuration by simply running
okteto up 🤖.
Kubernetes has the potential to be a great development platform, providing replicable, resource-efficient and production-like development environments. We have shown you how to use Okteto to create a development workflow that also lets you take advantage of features like hot reloaders or debuggers while developing your application directly in Kubernetes.