Java with Kubernetes in VS Code
This tutorial will walk you through the process of deploying a Java application to Kubernetes with Visual Studio Code. Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.
We will continue using the same Spring Boot application we built in our first Java Tutorial. In this tutorial, we will create a Kubernetes cluster, write a Kubernetes manifest file (usually written in YAML) which tells Kubernetes everything it needs to know about the application, and then finally deploy the application to the Kubernetes cluster.
Before you begin
In addition to the Java tools you needed to install for the Java Tutorial, you would also need to have tools for Docker and kubectl. See the Install Docker documentation for details on setting Docker up for your machine and Install kubectl. Before proceeding further, verify you can run Docker and kubectl commands from the shell.
You can create a local Kubernetes cluster with minikube or an Azure Kubernetes cluster in Azure Kubernetes Service (AKS). In this tutorial, we will use Azure Container Service(AKS) and you will need to have your Azure account ready for the deployment steps.
Install the Kubernetes extension
For a fully integrated Kubernetes experience, you can install the Kubernetes Tools extension which lets you quickly develop Kubernetes manifests and HELM charts. With the extension, you can also deploy containerized micro-service based applications to local or Azure Kubernetes clusters and debug your live applications running in containers on Kubernetes clusters. It also makes it easy to browse and manage your Kubernetes clusters in VS Code and provides seamless integration with Draft to streamline Kubernetes development.
To install the Kubernetes extension, open the Extensions view (⇧⌘X (Windows, Linux Ctrl+Shift+X)) and search for "kubernetes". Select the Microsoft Kubernetes extension.
Containerize and publish the application
Create and config a Kubernetes cluster
You can create a Kubernetes cluster running on Azure using the Kubernetes extension in VS Code. Once you have installed the Kubernetes extension, you will see KUBERNETES in the Explorer. Click on More and choose Create Cluster. Follow the instructions to choose the cluster type (here we choose Azure Kubernetes Service), select your subscription, and set up the Azure cluster and Azure agent settings. It will take a few minutes to complete the whole workflow.
Important: To create a Kubernetes cluster on Azure, you need to install the Azure CLI and sign in.
Tip: You will encounter an error if you don't have an available RSA key file. Follow create SSH public-private key to create your key before creating an Azure Kubernetes cluster.
Tip: You might encounter an error indicating conflicting location and VM size when creating an Azure Kubernetes cluster. Pay attention to choose proper location and VM size.
Deploy the application to Azure Kubernetes Service
The Kubernetes extension provides auto-completion, code snippets and verification for the Kubernetes manifest file. For example, once you type
Deployment, a manifest file with fundamental structure is auto-generated for you. You only need to enter your app name, image and port manually.
Below is an example manifest file:
Once your manifest file is ready, you only need one command to start a deployment. Open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and run Kubernetes: Create. It will deploy the application to your Kubernetes cluster and create objects according to the configuration in the open Kubernetes manifest file.
Checking on your deployment
After deployment, the Kubernetes extension can help you check the status of your application. From the Explorer, click on Workloads, right click on Pods and then choose Get to see whether the application has started. To view the status of your app, select Services, right click on your app, and then click Get. The status will be printed in the Integrated Terminal. Once your application has an
EXTERNAL_IP, you can open a browser and see your web app running.
Congratulations! Now your app is successfully running in Azure Kubernetes Service!