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 echo s 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.

Step 1: Deploy the PHP Sample App

For this post, we’ll be using a very simple PHP sample application. Run the command below to get a local clone of it:

1

2

git clone https://github.com/okteto/php-getting-started

cd php-getting-started



The 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.

1

kubectl apply -f k8s.yml



1

2

deployment.apps "hello-world" created

service "hello-world" created



One command and we have the application up and running. Pretty cool no 😎?

Step 2: Install the Okteto CLI

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 1

curl https://get.okteto.com -sSfL | sh



Windows Download https://downloads.okteto.com/cli/okteto.exe and add it to your `$PATH`.

Step 3: Activate your development container

With the application deployed, run the following command:

1

okteto up



1

2

3

4

5

6

7

8

9

✓ Development container activated

✓ Files synchronized

Namespace: default

Name: hello-world

Forward: 8080 -> 8080

62309 -> 22

Reverse: 9000 <- 9000



[Tue Jan 7 01:54:59 2020] PHP 7.4.1 Development Server (http://0.0.0.0:8080) started



The 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.

. 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.

1

2

3

4

5

6

7

name: hello-world

image: okteto/hello-world:php-dev

command: [ "php" , "-S" , "0.0.0.0:8080" ]

forward:

- 8080 :8080

reverse:

- 9000 :9000



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 vendor folder.

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:

1

curl localhost:8080



1

Hello world!



Step 4: Develop directly in Kubernetes

Open the 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.

1

2

3



$message = "Hello world from the cluster!" ;

echo ($message);



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:

1

curl localhost:8080



1

Hello world from the cluster!



Cool! Your changes were instantly applied to your application running in Kubernetes. No commit, build or push required 😎!

Step 5: Debug directly in Kubernetes

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:

1

curl localhost:8080



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.

How does it works?

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 🤖.

Conclusions

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.

Accelerate your development and start developing directly in Kubernetes today. Let us know what you think about it on Twitter, or in our #okteto channel in the Kubernetes community Slack.