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.

If you're running this quickstart on Apple M-series silicon, your application is built using Cloud Build, which can incur charges. For more information, see Cloud Code support for Apple M-series silicon.

Before you begin

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

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

Installing the plugin

To install the Cloud Code plugin:

  1. Install JetBrains IDE version 2020.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 folder should be visible under a directory in your PATH). To check if you already have Docker installed, run docker -v.

    Cloud Code automatically installs and manages the following dependencies in your IDE: kubectl, Skaffold, and Cloud SDK.

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

  4. From the plugins marketplace, search for the Cloud Code plugin and install it.

    The plugins marketplace is accessible through the following menus:

    • Windows or Linux: File > Settings > Plugins
    • macOS: IntelliJ IDEA > Preferences > Plugins

    If you're running Cloud Code on Apple M-series silicon, you're prompted to install Rosetta 2. This allows Cloud Code to install kubectl as a managed dependency. If you prefer not to install Rosetta 2, turn off managed dependencies (IntelliJ IDEA > Preferences > Tools > Cloud Code > Dependencies) and install your own ARM version of kubectl as described in Custom versions of dependencies.

    If necessary, you're prompted to restart your IDE to activate the plugin.

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. Pick a name for your project.

    After you click Finish, Cloud Code clones your chosen template and opens your newly created project for use.

    For Java templates, you're prompted to import required Maven projects to sync your pom.xml. Click Add as Maven project and then click Import Changes.

Creating a cluster

If you already have a cluster configured to work with Kubernetes tools (like kubectl) that you can use for this quickstart, you can move on to developing your application.

In order to develop your application, you'll need an associated Kubernetes cluster (hosted either locally with minikube or using a cloud platform, like a Google Kubernetes Engine (GKE) cluster). If you don't already have a cluster to use for this quickstart, create one using your cloud platform-specific instructions.

If you choose to use Google Cloud, you can create a GKE cluster using either the Cloud Code terminal or the Google Cloud Console:

Using the Cloud Code terminal

  1. With your project window open in your IDE, open the Cloud Code terminal, either using the sidebar at the bottom of your IDE or the Tools menu, Tools > Cloud Code > Tools > Cloud Code Terminal.

  2. If you haven't already, set your default project:

    gcloud config set project PROJECT_ID
    
  3. To create a cluster, run the following command:

    gcloud container clusters create CLUSTER_NAME --zone=COMPUTE_ZONE
    

    For example, to create a cluster called test-cluster, located in zone us-central1-a, you'll run gcloud container clusters create test-cluster --zone=us-central1-a.

    For details on further cluster customization, see the command's reference documentation.

  4. After you create a cluster, add it to your local kubeconfig file by running the following command:

    gcloud container clusters get-credentials CLUSTER_NAME --zone=COMPUTE_ZONE
    

    This also sets your specified cluster to the active context.

Using Cloud Console

  1. Open the Google Kubernetes Engine menu.

    Open the Google Kubernetes Engine menu

  2. Click Create.

  3. Select your preferred cluster type (Standard or Autopilot) and then click Configure.

  4. Choose your preferred cluster name and location.

  5. Click Create to finish creating your cluster.

    For more details on field customization, see Creating a zonal cluster.

  6. After you create a cluster, add the cluster to your local kubeconfig file and set the cluster to the active context by running the following command:

    gcloud container clusters get-credentials CLUSTER_NAME --zone=COMPUTE_ZONE
    

Developing your application

To initiate development of your application on Kubernetes:

  1. If you're running on a cluster hosted on a cloud platform, ensure you have defined where your container images are stored for Develop on Kubernetes.

    If you're logged into Google Cloud in your IDE and you have a default project defined, Cloud Code automatically uses your default container image repository (gcr.io/{project_id} where {project_id} references your default project).

  2. Choose the Develop on Kubernetes run target from the Run/Debug configuration selector in the Navigation bar.

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

    Kubernetes deployment run configurations

  3. To start the development cycle on your Kubernetes cluster, click the run action for Develop on Kubernetes run action icon.

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

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

    Viewing streamed logs from the Kubernetes application

  5. After the deployment is successful, you're notified that your service is accessible using local URLs. Click View to open the Event Log and then click the link to open your browser with your running application.

    Notification stating the URL to access your live app is now available in the Event Log

    Using event log to find port-forwarded services

Debugging your application

To debug your application:

  1. 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

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

    You can now debug against your live Kubernetes cluster.

  3. 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.

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

    Kubernetes debugger session

  5. 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.

    If you're logged into Google Cloud in your IDE and you have a default project defined, Cloud Code automatically uses your default container image repository (gcr.io/{project_id} where {project_id} references your default project).

  2. Ensure you're set up to deploy your app to your preferred Kubernetes context.

    Choose Edit Configurations... from the Run/Debug configuration selector in the Navigation bar. Under the Run tab of your Run on Kubernetes configuration, you can specify your deployment preference.

  3. Now that your application is ready for deployment, choose Run on Kubernetes from the Run/Debug configuration selector in the Navigation bar to deploy the application.

    Kubernetes deployment run configurations

  4. Click Run on Kubernetes run action icon.

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

  6. After the deployment succeeds, you're notified that your service is accessible using local URLs. Click View to open the Event Log.

    In the Event log, find the port where your application is being served. To open your browser with your running application, click the link.

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

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

Viewing logs

In addition to seeing a live stream of the logs from running pods in the terminal output as you're developing and running your application, you can 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 then select Stream Logs. Alternatively, you can stream logs for individual containers running in pods.

    This outputs 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):

  1. Go to the Projects page in the Cloud Console:

    Go to the Projects page

  2. Select the project you created for this quickstart and then click Delete.

    This shuts down the project and schedules it for deletion.

What's Next

Getting support

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