Quickstart: Running a Kubernetes app with Cloud Code

This page shows you how to get started with Cloud Code.

In this quickstart, you'll set up a new Kubernetes application using a starter 'Hello World' template, create a cluster, run your app on this cluster, kickstart continuous rebuilding to iteratively develop your app, debug your running code, and sit back and view logs streaming from your live application.

Creating a new Hello World app with Cloud Code

Before you begin

If you'd like to use Google Cloud, you'll need to create a Google Cloud project and enable billing.

Alternatively, you can use a cluster hosted on any other Cloud platform or a local Kubernetes cluster with tools such as Minikube or Docker Desktop.

Installing the plugin

To install the Cloud Code plugin:

  1. Install JetBrains IDE version 2019.2+ (such as IntelliJ Ultimate/Community, PyCharm Professional/Community, WebStorm, GoLand), if you haven't already.
  2. For all Cloud platforms, ensure the Docker client (authenticated with your Docker registry) is installed on the PATH of your machine (its respective folder should be visible under a directory within your PATH).

    Cloud Code will automatically install other necessary dependencies.

  3. Open IntelliJ IDEA (or any supported JetBrains IDE).

  4. From your plugins marketplace (File > Settings > Plugins or for Mac OS X, IntelliJ IDEA > Preferences > Plugins), search for the 'Cloud Code' plugin and install it.

    You may be prompted to restart your IDE to activate the plugin.

Creating a cluster

Before you create your application, you'll need an associated Kubernetes cluster (hosted either locally with Minikube or using a Cloud Platform, like a Google Kubernetes Engine cluster). If you don't already have a cluster to use for this quickstart, create one.

If you choose to use Google Cloud, you can create a GKE cluster by following these steps:

  1. Create a new cluster on GKE, either from your terminal or the Cloud Console.

    • To create a cluster using the terminal (accessible within your IDE, using the bottom taskbar), run,

       gcloud container clusters create [CLUSTER_NAME] --zone [COMPUTE_ZONE]
    • To create a cluster using the Cloud Console, visit the Google Kubernetes Engine menu and click Create cluster.

      Visit the Google Kubernetes Engine menu

      Refer to the GKE guide for more details on field customization.

  2. After you create a cluster, you need to add it to your local kubeconfig file. To do so, run:

     gcloud container clusters get-credentials [CLUSTER_NAME]

    This also sets your specified cluster to the active context.

Creating an application

To create a new application using an existing sample:

  1. In your IDE, open File > New Project and select Cloud Code: Kubernetes.
  2. Select a 'Hello World' template from the list of starter applications. List of templates available: Python, Go, NodeJS, Java hello world and guestbook applications
  3. If you're using IntelliJ IDEA, you'll be prompted to specify where your container image is stored.

    Specify your default image repository within the field using the format gcr.io/{gcp-project-name} or docker.io/{account}

    Depending on your choice of container registry, choose an appropriate format:

    • If you are using GKE, you can store your images on Google Container Repository.

      Additionally, the field comes equipped with autocomplete so you can easily find the image associated with your preferred Google Cloud project and currently active cluster. Use the following format:

      gcr.io/{project-name} where {project-name} is the name of your GCP project containing your cluster.

    • If you choose to store your images on Docker Hub (ensure that you are properly authenticated if you are using a private Docker Hub repository), use the following format:

      docker.io/{account} where {account} is the name of your Docker Hub account.

    • If you are running on a local cluster like Minikube or Docker Desktop, you can skip this step and click 'Next'.

  4. Pick a name for your project.

    Once you click Finish, Cloud Code will clone your chosen template and open your newly created project for use.

    • For Java templates, you'll be prompted to import Maven projects to sync your pom.xml. Click 'Add as Maven project'.

      Non-managed pom.xml notification

      Additionally, import necessary Maven projects by clicking 'Import Changes'.

      Import Maven changes notification

Developing your application

To initiate development of your application on Kubernetes:

  1. Ensure you have defined where your container images are stored for Develop on Kubernetes.
  2. Choose the Develop on Kubernetes run target from the Run/Debug configurations dialog on the top taskbar.

    This ensures any code changes to be automatically built, pushed, and deployed to a new version of your application.

    Kubernetes deployment run configurations

  3. In the output window, view incoming application logs.

    Once the deployment has started, you'll see the list of forwarded ports for the deployed application.

    Viewing streamed logs from the Kubernetes application

  4. In the IDE Event log, click on the link to open your browser with your running application.

    Using event log to find port-forwarded services

Debugging your application

To debug your application:

  1. Ensure you have defined where your container images are stored for Develop on Kubernetes.
  2. Click the debug action run action icon for Develop on Kubernetes to start the development cycle in debug mode on your Kubernetes cluster.

    Starting the Kubernetes cluster development cycle in debug mode

  3. Cloud Code will attach a debug session. Once successful, the Debug Tool window opens, confirming connection (within the Console tab).

    You can now debug against your live Kubernetes cluster.

  4. Click the gutter at the executable line of the code where you want to add a breakpoint.

    Red filled circles denote active breakpoints, while red-outlined hollow circles signify disabled breakpoints.

  5. When you send a new request to your application, it will pause at that desired line.

    Kubernetes debugger session

  6. To end the debugging session, click the stop icon on the Develop on Kubernetes run configuration.

Running your application

To run an application on Kubernetes:

  1. Ensure you have defined where your container images are stored for Run on Kubernetes.
  2. Now that your application is ready for deployment, choose Run on Kubernetes from the Run/Debug configurations dialog on the top taskbar to deploy the application.

    Kubernetes deployment run configurations

  3. Click 'Run on Kubernetes' run action icon.

  4. Like developing on Kubernetes, you can view incoming application logs in the output window.

  5. In the IDE Event log, find the port where your application is being served. Click on the link to open your browser with your running application.

  6. Visit http://localhost:{port-number} to access your newly deployed application!

    Application at http://localhost:{port-number} displaying 'Hello World!'

Viewing logs

While you can see a live stream of the logs from running pods within the terminal output as you are developing and running your application, you can also view logs from a specific pod by navigating to the Kubernetes explorer.

To view logs from a specific pod, follow these steps:

  1. Navigate to the Kubernetes Explorer. It can be accessed either from the side panel on the right or using Tools > Cloud Code > Kubernetes > View Cluster Explorer.

    Kubernetes Explorer panel open using the tab on the right side taskbar

  2. Select the pod you'd like to see logs from.

  3. Right click the pod and select Stream Logs. Alternatively, you can stream logs for individual containers running in pods.

    This will output logs to the Kubernetes Explorer Console.

    Streaming logs from a pod using its right-click menu to output logs into the Kubernetes Explorer Console

Cleaning up

To avoid incurring charges to your account for the resources used in this quickstart, be sure to delete the cluster and project you created.

If you're using Google Cloud and would like to delete your project (and associated resources, including any GKE clusters):

  • Go to the Projects page in the Cloud Console:

    Go to the Projects page

  • Select the project you created for this Quickstart and click on the trash can icon next to delete it.

    This shuts down the project and schedules it for deletion.

What's Next

Getting Support

To send feedback, report issues on GitHub, or ask a question on Stack Overflow.