Quickstart: Develop with Java on Kubernetes using Azure Dev Spaces

In this guide, you will learn how to:

  • Set up Azure Dev Spaces with a managed Kubernetes cluster in Azure.
  • Iteratively develop code in containers using Visual Studio Code and the command line.
  • Debug the code in your dev space from Visual Studio Code.

Prerequisites

Create an Azure Kubernetes Service cluster

You need to create an AKS cluster in a supported region. The below commands create a resource group called MyResourceGroup and an AKS cluster called MyAKS.

az group create --name MyResourceGroup --location eastus
az aks create -g MyResourceGroup -n MyAKS --location eastus --node-vm-size Standard_DS2_v2 --node-count 1 --disable-rbac --generate-ssh-keys

Enable Azure Dev Spaces on your AKS cluster

Use the use-dev-spaces command to enable Dev Spaces on your AKS cluster and follow the prompts. The below command enables Dev Spaces on the MyAKS cluster in the MyResourceGroup group and creates a default dev space.

$ az aks use-dev-spaces -g MyResourceGroup -n MyAKS

'An Azure Dev Spaces Controller' will be created that targets resource 'MyAKS' in resource group 'MyResourceGroup'. Continue? (y/N): y

Creating and selecting Azure Dev Spaces Controller 'MyAKS' in resource group 'MyResourceGroup' that targets resource 'MyAKS' in resource group 'MyResourceGroup'...2m 24s

Select a dev space or Kubernetes namespace to use as a dev space.
 [1] default
Type a number or a new name: 1

Kubernetes namespace 'default' will be configured as a dev space. This will enable Azure Dev Spaces instrumentation for new workloads in the namespace. Continue? (Y/n): Y

Configuring and selecting dev space 'default'...3s

Managed Kubernetes cluster 'MyAKS' in resource group 'MyResourceGroup' is ready for development in dev space 'default'. Type `azds prep` to prepare a source directory for use with Azure Dev Spaces and `azds up` to run.

Get sample application code

In this article, you use the Azure Dev Spaces sample application to demonstrate using Azure Dev Spaces.

Clone the application from GitHub and navigate into the dev-spaces/samples/java/getting-started/webfrontend directory:

git clone https://github.com/Azure/dev-spaces
cd dev-spaces/samples/java/getting-started/webfrontend

Prepare the application

Generate the Docker and Helm chart assets for running the application in Kubernetes using the azds prep command:

azds prep --public

You must run the prep command from the dev-spaces/samples/java/getting-started/webfrontend directory to correctly generate the Docker and Helm chart assets.

Build and run code in Kubernetes

Build and run your code in AKS using the azds up command:

$ azds up
Using dev space 'default' with target 'MyAKS'
Synchronizing files...3s
Installing Helm chart...8s
Waiting for container image build...28s
Building container image...
Step 1/8 : FROM maven:3.5-jdk-8-slim
Step 2/8 : EXPOSE 8080
Step 3/8 : WORKDIR /usr/src/app
Step 4/8 : COPY pom.xml ./
Step 5/8 : RUN /usr/local/bin/mvn-entrypoint.sh     mvn package -Dmaven.test.skip=true -Dcheckstyle.skip=true -Dmaven.javadoc.skip=true --fail-never
Step 6/8 : COPY . .
Step 7/8 : RUN mvn package -Dmaven.test.skip=true -Dcheckstyle.skip=true -Dmaven.javadoc.skip=true
Step 8/8 : ENTRYPOINT ["java","-jar","target/webfrontend-0.1.0.jar"]
Built container image in 37s
Waiting for container...57s
Service 'webfrontend' port 'http' is available at http://webfrontend.1234567890abcdef1234.eus.azds.io/
Service 'webfrontend' port 80 (http) is available at http://localhost:54256
...

You can see the service running by opening the public URL, which is displayed in the output from the azds up command. In this example, the public URL is http://webfrontend.1234567890abcdef1234.eus.azds.io/.

If you stop the azds up command using Ctrl+c, the service will continue to run in AKS, and the public URL will remain available.

Update code

To deploy an updated version of your service, you can update any file in your project and rerun the azds up command. For example:

  1. If azds up is still running, press Ctrl+c.

  2. Update line 19 in src/main/java/com/ms/sample/webfrontend/Application.java to:

    return "Hello from webfrontend in Azure!";
    
  3. Save your changes.

  4. Rerun the azds up command:

    $ azds up
    Using dev space 'default' with target 'MyAKS'
    Synchronizing files...1s
    Installing Helm chart...3s
    Waiting for container image build...
    ...    
    
  5. Navigate to your running service and observe your changes.

  6. Press Ctrl+c to stop the azds up command.

Enable Visual Studio Code to debug in Kubernetes

Open Visual Studio Code, click File then Open..., navigate to the dev-spaces/samples/java/getting-started/webfrontend directory, and click Open.

You now have the webfrontend project open in Visual Studio Code, which is the same service you ran using the azds up command. To debug this service in AKS using Visual Studio Code, as opposed to using azds up directly, you need to prepare this project to use Visual Studio Code to communicate with your dev space.

To open the Command Palette in Visual Studio Code, click View then Command Palette. Begin typing Azure Dev Spaces and click on Azure Dev Spaces: Prepare configuration files for Azure Dev Spaces.

Prepare configuration files for Azure Dev Spaces

When Visual Studio Code also prompts you to configure your base images and exposed port, choose Azul Zulu OpenJDK for Azure (Free LTS) for the base image and 8080 for the exposed port.

Select base image

Select exposed port

This command prepares your project to run in Azure Dev Spaces directly from Visual Studio Code. It also generates a .vscode directory with debugging configuration at the root of your project.

Build and run code in Kubernetes from Visual Studio

Click on the Debug icon on the left and click Launch Java Program (AZDS) at the top.

Launch Java Program

This command builds and runs your service in Azure Dev Spaces in debugging mode. The Terminal window at the bottom shows the build output and URLs for your service running Azure Dev Spaces. The Debug Console shows the log output.

Note

If you don't see any Azure Dev Spaces commands in the Command Palette, make sure you have installed the Visual Studio Code extension for Azure Dev Spaces. Also verify you opened the dev-spaces/samples/java/getting-started/webfrontend directory in Visual Studio Code.

Click Debug then Stop Debugging to stop the debugger.

Setting and using breakpoints for debugging

Start your service in debugging mode using Launch Java Program (AZDS).

Navigate back to the Explorer view by clicking View then Explorer. Open src/main/java/com/ms/sample/webfrontend/Application.java and click somewhere on line 19 to put your cursor there. To set a breakpoint hit F9 or click Debug then Toggle Breakpoint.

Open your service in a browser and notice no message is displayed. Return to Visual Studio Code and observe line 19 is highlighted. The breakpoint you set has paused the service at line 19. To resume the service, hit F5 or click Debug then Continue. Return to your browser and notice the message is now displayed.

While running your service in Kubernetes with a debugger attached, you have full access to debug information such as the call stack, local variables, and exception information.

Remove the breakpoint by putting your cursor on line 19 in src/main/java/com/ms/sample/webfrontend/Application.java and hitting F9.

Update code from Visual Studio Code

While the service is running in debugging mode, update line 19 in src/main/java/com/ms/sample/webfrontend/Application.java. For example:

return "Hello from webfrontend in Azure while debugging!";

Save the file. Click Debug then Restart Debugging or in the Debug toolbar, click the Restart Debugging button.

Refresh Debugging

Open your service in a browser and notice your updated message is displayed.

Instead of rebuilding and redeploying a new container image each time code edits are made, Azure Dev Spaces incrementally recompiles code within the existing container to provide a faster edit/debug loop.

Clean up your Azure resources

az group delete --name MyResourceGroup --yes --no-wait

Next steps

Learn how Azure Dev Spaces helps you develop more complex applications across multiple containers, and how you can simplify collaborative development by working with different versions or branches of your code in different spaces.