Microservice-based applications make our applications more scalable. But they also make it harder to setup local development environments. You need to run several services that you are not familiar with, with a variety of different runtimes. And you probably need to switch your development environment between different projects. Not to mention that running all these services can eat up all of your computer’s available resources.
In this blog post, I’ll show you how to develop a Hello World application, how to kubernetize it and how to deploy it into Okteto Cloud without having to install anything locally. We will use Grails as our programming language, but the guide applies to any Spring Boot application. And yes, you won’t need to install grails, the JVM, Docker or Kubernetes locally 😍.
Okteto Cloud is a development platform for Kubernetes applications. You can create development environments in Kubernetes, deploy containers, pods, deployments, etc… and code from your favorite local IDE while Okteto takes care of synchronizing the files to your remote development environment to instantly update your application. It’s by far the fastest development experience I’ve ever tried.
Okteto creates your remote development environments using the
okteto.yml file. This file declares the deployment target, the docker image used as the development runtime, the working directory and other information required to activate your development environment with Okteto (more information about the
okteto.yml file is available here).
Create a local folder for your Grails application and add the following
okteto.yml file to it:
This file will instruct Okteto to use a
okteto/grails:latest container as your development environment runtime and to expose two ports in your machine localhost interface. Port 8080 to access the application and 5005 to debug it remotely. Once you have your okteto manifest ready you will create your remote development environment in Okteto Cloud by executing the command below:
Deployment test doesn't exist in namespace jagedn. Do you want to create a new one? [y/n]: y
If all it’s ok, you are now in a remote shell session, with all the tools you need already preinstalled 💥.
Your remote development environment has all the tools you need to create, build and run the application. To create a Hello world application execute the following command on the remote shell:
grails create-app --profile web --inplace
Okteto synchronizes files in both directions, which means that the files created by
grails create-app will automatically appear in your local machine. Create the file
grails-app/controllers/grails4/TestController.groovy in your favorite local IDE, make some changes to it, and save it:
Run the application with the following command:
You can now access the application at http://localhost:8080/test (this port is being automatically forwarded by
okteto up to the remote development environment). You can also use the public HTTPs endpoint created by Okteto Cloud for a true end-to-end experience.
Okteto Cloud uses your Github username as your personal namespace, and it is also used to create automatic HTTPs endpoints for your applications. Since my Github username is
hello-worldwill be accessible at https://hello-world-jagedn.cloud.okteto.net/test
Go back to your local IDE and update the response message in
TestController. Once you modify your code, refresh your browser and see how your changes are applied immediately 😎. This is possible thanks to Grails’ hot reloading capabilities and Okteto’s file synchronization.
To be able to debug the (remote) application we need to configure gradle to run with debug flags. Add the flags below to the
bootRun task in your
Go ahead and restart the application in the remote shell:
and connect your IDE to it using a remote debug configuration on http://localhost:5005 (forwarded by
okteto up to the remote debugger). You will be able to halt the execution at your breakpoints, inspect requests, the available variables, etc.
Now it’s time to deploy your application. Close your remote development environment by exiting the remote shell with
Ctrl + C +
Ctrl + D and execute the following command from your local terminal:
Your remote development environment is now deactivated. The first step is to dockerize your application and push your application docker image to Docker Hub.
Start by creating the following
Dockerfile in the root folder of your application:
# syntax = docker/dockerfile:experimental
We could build and push the docker image using the traditional
docker build -t jagedn/grails-test .
But instead, we are going to use the Okteto CLI to remotely build and push your image (note that you will need to use your DockerHub user instead of
jagedn). This way we don’t need to install docker locally:
okteto build -t jagedn/grails-test .
To deploy your application wee need to create Kubernetes manifests. Create the file
k8s.yml in the root folder of your application with this content:
In this file you instruct Kubernetes to deploy your application and, at the same time, we configure a service to expose your application to the world:
If you don’t have
kubectlinstalled follow this guide.
kubectl apply -f k8s.yml
And finally, the production version of your Kubernetes application will be available in about half a minute at https://hello-world-jagedn.cloud.okteto.net/test (remember to replace
jagedn by your Github ID).
Although this example is a simple app, when you’re working in a more complex solution you soon realize you need to work as close to how it will look in production. You can (you must) write a lot of tests but you can’t mock all services or typical situations that happen in a cluster. Okteto lets you develop close to the production architecture.
Another advantage you’ll gain working directly in a Kubernetes cluster is that you’ll need to define your YAML files in early stages keeping your focus on it, detecting your requirements at the same time you are working in the solution.
Last but not least, with the generous Okteto Cloud free tier of 4 CPUs and 8GB per namespace, you can use Okteto Cloud to put your idea in production in a short time as I did, migrating all my Telegram Bots from Google AppEngine and Heroku to Okteto Cloud. It was great.
Jorge Aguilera is a Principal Software Architect at Puravida Software, where he builds software for self-service devices like ATMs and Sales Kiosks. In his free time, he likes to dabble with Kubernetes, build Telegram Bots, and speak at different meetups and conferences.