Deploy a Kubernetes app with Cloud Code for VS Code

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

You'll set up a new Kubernetes application using a '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.

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 Google Cloud project.

  4. Enable the Google Kubernetes Engine API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Google Kubernetes Engine API.

    Enable the API

  8. Install Git. Git is required for copying samples to your machine.
  9. Install the Cloud Code plugin if you haven't already.

Creating an application

  1. Open the command palette (press Ctrl/Cmd+Shift+P or click View > Command Palette) and then run Cloud Code: New Application.

  2. Choose Kubernetes Application.

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

  4. Save the new application.

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

Create a GKE cluster

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

  1. Click Cloud Code and expand the Kubernetes section.

  2. Click add Add a Cluster to the KubeConfig and then click Google Kubernetes Engine in the Quick pick menu.

  3. When prompted to enable container.googleapis.com, select Yes.

  4. Click + Create a New GKE Cluster.

  5. Choose Standard as the cluster type.

  6. Click Open to permit Cloud Code to open the Google Cloud console.

  7. In Google Cloud console, use the project you created, set the zone to us-central1-a, and set the cluster name to my-first-cluster.

  8. Click Create. Cluster creation takes a few minutes.

  9. After the cluster is created, in the Quick pick menu, click refresh Refresh.

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

Run and view your app

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

To run your application, follow these steps:

  1. In the Cloud Code status bar, click the active project name.

    Active project name in status bar

  2. In the Quick Pick menu that appears, select Run on Kubernetes.

  3. Confirm whether to use the current cluster context or switch to a different one.

  4. If prompted, choose an image registry to push the images to. If you're creating a new image registry using gcr.io/PROJECT_ID, ensure that the image registry is in the same project as your cluster.

    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.

  5. 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 and click the address.

Troubleshooting tips

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

  1. Click Cloud Code and expand the Kubernetes section.

  2. Right-click your cluster name and then click Set as Active Cluster.

Debug your app

To debug your application, follow these steps:

  1. In the Cloud Code status bar, click the active project name.

    Active project name in status bar

  2. In the Quick Pick menu that appears, select Debug on Kubernetes.

  3. If prompted, authenticate your credentials to run and debug an application locally.

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

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

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

  7. Edit the string being sent inline #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

After your debugging session starts, the Development sessions pane displays the structured logging view. As a task begins, the task appears with a spinning semi-circle progress_activity.

If a task succeeds, a check mark check_circle appears next to the step.

To see details of a step, click the step in the Development session pane. The Output pane displays the step in the logging output.

Open a terminal in your container

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

  1. Click Cloud Code and expand the Kubernetes section.

  2. Expand the following sections:

    • Your preferred cluster's section
    • The Namespaces section and then your preferred namespace's section
    • The Pods section and then your preferred pod's section
    • The Containers section

      Cloud Code pods

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

Clean up

After you stop 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 project or delete the cluster you created if you want to reuse the project.

To delete the cluster:

  1. Click Cloud Code and then expand the Kubernetes explorer.
  2. Hold the pointer over your cluster name and then click open_in_new Open in Google Cloud console.
  3. Click Delete and then click Delete.

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

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next