This blog was originally featured as a guide in civo cloud community.
Introduction
Okteto is an open source tool to develop and test your code directly in Kubernetes. In this guide will walk you through how to develop Java application on a Kubernetes cluster using Okteto.
Defining the Problem
Systems based on microservice architecture have many advantages like scalability, resilience, fault tolerance and so on. But they are also harder to setup 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 the available computer resources.
Kubernetes has become 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 so on.
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
Lets see how to develop a sample Java application using Okteto.
NOTE: This guide focus on developing Java application on Kubernetes, but there are other guides to develop with your favorite programming language such Node, Python, Go, Ruby, etc.
Prerequisite
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 kubernetes cluster ready, sign up to civo for their managed Kubernetes beta which is completely free. You can also check out Okteto Cloud offering that gives you a free kubernetes namespace, explained more in this guide below.
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
Note: The kubernetes.yaml file contains the kubernetes manifest to deploy this app.
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:
MacOS/Linux
$ curl https://get.okteto.com -sSfL | sh
You can also install via brew by running:
$ brew install okteto
Windows
Download the executable and add it to your $PATH
.
You can also install via scoop by running:
$ scoop install okteto
Github
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
...
The command 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 dev 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).
- Setups 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!
Or visit to 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 springboot 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 JVM arguments in your Maven configuration files:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8088
Open launch.json to modify the debug configuration. Add configuration to attach to a remote debugger.
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 so on.
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 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 Cloud
It’s possible to use Okteto for local development even when you don’t have your own kubernetes cluster. Okteto Cloud gives you free access to secure Kubernetes namespaces, fully integrated with remote development capabilities. For more info, refer the official documentation.
There is also a hosted version called Okteto enterprise that companies can use on their own premises instead of Okteto Cloud.
Conclusion
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 anymore.
- No need to build docker images and create deployments everytime 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 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
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.
If you found this guide useful, let me know on twitter at @milindchawre.