Running a Kubernetes app

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

You'll set up a new Kubernetes application using a starter 'Hello World' sample application, create a cluster, run your app on this cluster, debug your running code, view logs from your live application, and connect a terminal to your running container.

Creating a new Hello World app with Cloud Code

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

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.

  4. Enable the Google Kubernetes Engine API.

    Enable the API

  5. Install Git. Git is required for copying samples to your machine.

Installing Cloud Code

  1. Install Visual Studio Code on your machine, if you haven't already.

  2. For all Cloud platforms, install the Docker client (authenticated with your Docker registry) on the PATH of your machine (its installation folder should be visible under a directory in your PATH).

    • To check if you already have Docker installed, run docker -v.
    • If you're using Windows Home, see Install Docker Desktop on Windows Home.
    • If you're using WSL but can't install Docker for Desktop, use minikube instead for local development with Cloud Code.

    Cloud Code automatically installs kubectl, Skaffold, and Cloud SDK.

  3. Install Cloud Code using one of the following options:

    • Open VS Code and install Cloud Code.
    • Open Visual Studio Code on your machine. Using the Extensions view Extension icon in VS Code (its square icon should be available on the left side taskbar), search for and then click the Cloud Code extension and then click Install.

    If you're running Cloud Code for VS Code on Apple M-series silicon, you're prompted to install Rosetta 2 or to disable Auto dependencies, which require Rosetta 2 to work on your system.

    After the installation is complete, the Cloud Code Welcome page and a collection of built-in templates that allow you to quickly set up a new application appear under Starter Apps.

    Cloud Code Welcome page

    The Cloud Code status bar, stocked with common Cloud Code actions, is available in the VS Code Status Bar.

    Cloud Code status bar

Creating an application

  1. From the Command Palette (press Cmd/Ctrl+Shift+P), run Cloud Code: New Application, choose Kubernetes Application.

  2. Choose a Hello World app in the language you prefer.

    For example, choosing Node.js: Hello World creates a starter Node.js Hello World app.

  3. Save the new application.

    A notification confirms that your application has been created and a new window with your application opens.

Creating a GKE cluster

To create a Standard Google Kubernetes Engine (GKE) cluster, follow these steps:

  1. To open the Kubernetes Explorer, click Kubernetes Explorer icon Cloud Code - Kubernetes.
  2. In the header of the Kubernetes Explorer pane, click + Add a Cluster to the KubeConfig.
  3. Choose Google Kubernetes Engine and then click + Create a new GKE Cluster.
  4. Choose Standard as the cluster type.
  5. Click Open to permit Cloud Code to open the Cloud Console.
  6. In Cloud Console, use the project you created, set the zone to us-central1-a, and set the cluster name to my-first-cluster.
  7. Click Create. Cluster creation takes a couple minutes.
  8. After the cluster is created, in Kubernetes Explorer, click Refresh Kubernetes cluster view Refresh.
  9. After the name of your new cluster appears in the list, click the cluster name. Your new cluster is added to the configuration and configured to be the active context.

Running and viewing your app

Now that you're all set up, you can run your application and view it live. Cloud Code watches your filesystem for changes so that you can edit and rerun your app in near real time.

To run your application, follow these steps:

  1. Use the Cloud Code status bar and select Run on Kubernetes.
  2. Confirm whether to use the current cluster context or switch to a different one.
  3. If prompted, choose an image registry to push the images to.

    An output window appears where you can track the progress of your running application. You'll also see a live stream of the logs from the running pods in the terminal output.

  4. After your application is running on Kubernetes, the output window displays an IP address. To use this linked IP address to access your application, press Ctrl/Cmd + click.

Troubleshooting tips

If you're using a pre-existing cluster, to set your cluster as active and get cluster credentials using the Kubernetes Explorer, follow these steps:

  1. To open the Kubernetes Explorer, click the Cloud Code - Kubernetes Kubernetes Explorer icon icon in the VS Code Activity bar on the left.
  2. In the Kubernetes Explorer, right-click your cluster name and then click Set as Active Cluster.

Debugging your app

To debug your application, follow these steps:

  1. Use the Cloud Code status bar and then select Debug on Kubernetes.
  2. If prompted, confirm whether to use the current cluster context or switch to a preferred one.

    Cloud Code uses the cloudcode.kubernetes configurations in your .vscode/launch.json file to run your application and attach a debugger session to it.

    Cloud Code builds your containers, pushes them to the registry, applies Kubernetes configurations to the cluster, and returns the IP address that you can use to browse your live application.

  3. Before your debugger session is attached, you're prompted to confirm or enter the directory in the remote container where the program to debug is found or press ESC to skip debugging the container.

  4. To add a breakpoint to line #9, open src/app.js and then click in the editor margin.

    Red filled circles signify active breakpoints, while gray hollow circles signify disabled breakpoints. For finer breakpoint control, you can use the Breakpoints section in VS Code's Debug view.

    Breakpoints section in the left hand panel of Debug View that allows adding, removing, and disabling breakpoints

    When you send a new request to your application, the debugger pauses at the first active breakpoint.

    In the following sample, under Local in the Variables section, note that the value of res._contentLength; for Hello, world! is 13.

    App paused at breakpoint and variables and call stack sections populated with values in scope

  5. Edit the string being sent in line #8 to Hello, goodbye! and then restart the Debug on Kubernetes action.

    After the app is rebuilt and redeployed, note the updated value of res._contentLength.

    App paused at breakpoint with updated values

Viewing logs

Bonus: Opening a terminal in your container

To open a terminal in your container, follow these instructions:

  1. To open the Kubernetes Explorer, click Kubernetes Explorer icon Cloud Code - Kubernetes.
  2. Select the cluster and, from the underlying Pods section, the pod you'd like to connect to.
  3. Under your specified pod, expand your Containers section.
  4. Right-click the container in which you'd like to open a terminal and then click Get Terminal.

    This launches a terminal; you now have access to a shell inside the running container!

    Opening a terminal for the specified pod

Cleaning up

After you terminate your application, all Kubernetes resources deployed during the run are deleted automatically.

To avoid incurring charges to your account for other 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 just your cluster, you can do so by following these steps:

  1. In the Kubernetes Explorer, hover over your cluster name and then click Open in Cloud Console icon Open in Cloud Console.
  2. Click Delete and then click Delete.

To delete your project (and associated resources, including any clusters):

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

    Go to the Projects page

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

  3. Type the project ID to confirm and then click Shut down.

    This shuts down the project and schedules it for deletion.

What's next