Defining the Problem
Systems based on a microservice architecture have many advantages like scalability, resilience, fault tolerance and so on. But they are also harder to set up locally as a development environment due to their very nature. You need to run several services with different runtimes on your local workstation. Also running these multiple services will eat away at the available computer resources.
Kubernetes has become the industry standard to run production workloads. But developing in Kubernetes presents some challenges. Typically, you would finish writing your code and package it in a Docker image, push that image to a registry, followed by pulling the container to your cluster for deployment, checking everything works as expected, and then repeating the process for new code. This is not only slow and tedious, but it means that it would be difficult to take advantage of useful features of tools such as debuggers, immediately-visible changes on reload, and the like.
Solution - a Development Platform in the Cloud
Okteto is an answer to your problem. Okteto transforms your Kubernetes cluster into a fully-featured development platform. It allows you to develop and test your code directly in Kubernetes while taking full advantage of tools like Maven and Gradle, popular frameworks, hot reloads, incremental builds, IDE debuggers and so on.
Application Development with Okteto
Let's see how to develop a sample Java application using Okteto.
Before you begin, you will need following things:
- Code Editor/IDE
- kubernetes cluster
KUBECONFIG file pointing to your kubernetes cluster, where the application will be deployed.
- kubectl to communicate with your cluster.
*If you don't have a Kubernetes cluster ready, why not sign up to Civo for the managed Kubernetes to try this out for yourself!
Step 1: Deploying the Java App
Get the source code of our sample Java app.
$ git clone https://github.com/milindchawre/okteto-java-maven-demo $ cd okteto-java-maven-demo
Lets deploy it on Kubernetes.
# Make sure `KUBECONFIG` env is pointing to your kubernetes cluster. $ kubectl apply -f kubernetes.yaml deployment.apps/demo created
Your sample Java application is now running on Kubernetes.
Step 2: Install the Okteto CLI
The Okteto CLI allows you to develop your applications directly in Kubernetes while taking advantage of well-known tooling for your particular choice of language. For Java, these include Maven/Gradle, Spring Boot and your favourite IDE's debugger functionality. It also speeds up our development cycle instead of going through the typical development workflow described above.
Install the Okteto CLI by running the following command:
$ curl https://get.okteto.com -sSfL | sh
You can also install via brew by running:
$ brew install okteto
Download the executable and add it to your
You can also install via scoop by running:
$ scoop install okteto
Alternatively, you can directly download the binary from GitHub.
Step 3: Start your development environment in Kubernetes
With the sample Java application deployed as above, run the following command:
$ okteto up + Development environment activated ✓ Files synchronized Namespace: default Name: demo Forward: 8080 -> 8080 8088 -> 8088 ...
okteto up starts a Kubernetes development environment, which means:
- It connects to your remote kubernetes cluster
- Goes to the deployment that has your application and changes the Docker image with a different one that contains the required tools to build, test, debug and run a Java application.
- Sets up automatic file synchronization in both directions so that to keep your changes up-to-date between your local filesystem and your application pods.
- Sets up port-forwarding in both directions both for your app (8080 :- the application) and for your debugging tools (8088 :- the debugger).
- Sets up a tiny SSH server in the pod to make it easy to work with SSH based tools (i.e. remote workspaces in Visual studio Code). Now you can build, test, and run your application as if you were doing it on your local machine.
You can customize the way Okteto manipulates your code, using okteto.yml manifest file. You can also use the file
.stignore to skip files from file synchronization. This is useful to save you from synchronising data that doesn't need to be moved.
The first time you run the application, Maven will compile your application. Wait for this process to finish.
Okteto automatically forwards port 8080 from your local computer to the remote development environment, making it accessible via localhost. Test your application by running the command below in a local shell:
$ curl localhost:8080 Hello world!
http://localhost:8080 on your browser. You should see the text "Hello World!" like in the image below:
Step 4: Develop directly in Kubernetes
Open src/main/java/com/example/demo/RestHelloWorld.java in your favorite local IDE and modify the response message on line 11 to something else like to Hello world from the cluster!. Now save your changes.
As soon as you save the code, your IDE will auto compile only the necessary
*.class files that will be synchronized by Okteto to your application in Kubernetes. Take a look at the remote shell and notice how the changes are detected by Spring Boot and automatically hot reloaded. Please note that to enable Spring Boot hot reloads you need to import the
spring-boot-devtools dependency in your application, which in the case of this tutorial, we already did here.
Call your application to validate the changes:
$ curl localhost:8080 Hello world from the cluster!
Great! Your code changes are instantly applied to Kubernetes. No commit, build or push required! 😎
The end result is that the remote Kubernetes cluster seems local to your workstation. When you edit your code on your workstation and as soon as you save it, the changes go to the remote cluster and your app instantly updates (taking advantage of any hot-reload mechanism if present like Spring Boot hot swapping in our case). This whole process happens in an instant. There is no need to build any Docker image or apply any Kubernetes manifests. All of this is taken care by Okteto.
Step 5: Debug directly in Kubernetes
Okteto also allows you to debug your applications directly from your IDE. Let’s see how it works in VS Code. To enable debug mode, define the following as a as JVM arguments in your Maven configuration files:
Now, add a breakpoint in your code and click on Run Debug. Go back to the browser and reload the page. The code execution will halt at your breakpoint. You can now inspect the code, trace the function calls, look at available variables, and the like.
So the code runs in remote Kubernetes cluster, but you can debug it from your local workstation. 😉
Step 6: Wrapping Up
Once you are done with the development, run the
okteto down command. It deactivates your development environment, stops the file synchronization service and restores your previous deployment configuration.
$ okteto down + Development environment deactivated i Run 'okteto push' to deploy your code changes to the cluster
Okteto transforms your Kubernetes cluster into a fully-featured development platform. It lets you develop your applications directly in Kubernetes. This way it helps in lot of different ways:
- You don't need to install compilers, runtimes, Docker or Kubernetes locally on your workstation any more.
- No need to build Docker images and create deployments every time you make changes in the code.
- Test your application as fast as you can type, resulting in a faster feedback loop.
- No more compute resources wasted in your machine. The resources are limited only by the power of the cloud.
- While developing with Okteto you can still use your favorite IDE and debugging/testing tools with features like incremental builds and hot-reloads as supported by your programming language.
- The remote Kubernetes clusters can be used as a local filesystem/environment on your workstation.
- You avoid the the "works on my machine" problem as your application is developed inside the cluster in which it is destined to be deployed.
- It solves challenges faced by multiple developers when working simultaneously on a common set of services. Having a separate dedicated Kubernetes namespace for each developer helps in resolving many issues.
- Reduce integration issues by developing in a more production-like environment.
For more information, check out the Okteto project on GitHub and refer to this quickstart guide. Also check out the Okteto samples repository to see how to use Okteto with different programming languages and debuggers.