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' template, create a cluster, run your app on this cluster, debug your running code, view logs from your live application, and also, connect a terminal to your running container.

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.

Installing Cloud Code

  1. Install Visual Studio Code on your machine, 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).

    • To check if you already have Docker installed, run docker -v.
    • If you're using Windows Home, refer to this guide on how to set up your Docker client.
    • If you're using WSL but cannot install Docker for Desktop, use minikube instead for local development with Cloud Code.

    Cloud Code will automatically install kubectl, Skaffold, and Google Cloud SDK.

  3. Install Cloud Code using one of these options:

    • Use this link to 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 available on the left side taskbar), search for the 'Cloud Code' extension and click 'Install'.
  4. Once the installation is complete, you'll see the Cloud Code Welcome page and a collection of built-in templates under Starter Apps which you can use to quickly set up a new application.

    Cloud Code Welcome page

    Additionally, the Cloud Code status bar, stocked with common Cloud Code actions, is now available within the VS Code Status Bar.

    Cloud Code status bar

Creating an application

  1. From the Cloud Code status bar, select New Application. Creating a new app with the Cloud Code status bar
  2. Choose 'Kubernetes application' as your preferred application type.
  3. From the list of templates (Node.js, Python (Flask and Django frameworks), Java, Go), choose a Hello World app in a language of your choosing.

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

  4. Confirm your application location (or modify it, if necessary) to proceed.

    A notification will confirm your application has been created and a new window with your application will open.

Creating a Google Kubernetes Engine cluster

To create a GKE cluster, follow these steps:

  1. Navigate to the Google Kubernetes Engine explorer using the Cloud Code view Kubernetes Explorer icon, by clicking on its icon in the VS Code Activity bar on the left.
  2. Authenticate with the Cloud SDK by clicking on 'Click here to log in to Google Cloud SDK'.
  3. In the header of the Google Kubernetes Engine Explorer panel, click the '+' button(plus icon) (available on mouseover) to create a new GKE cluster.
  4. Ensure you're creating your cluster in your default project.
  5. Fill out fields in the Create Cluster wizard and click 'Create Cluster'.

    In this example, Project ID is set using the default Project ID button, zone is set as 'us-central1-a', and cluster name is defined as 'test'.

    Cluster creation takes a couple of minutes. Once created, your cluster will be listed under the GKE Explorer view Kubernetes Explorer icon.

    Creating a new cluster with the GKE explorer

Running your app

You can now run your application and view it live. Additionally, 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 you'd like to use the current cluster context (or switch to a preferred one).
  3. Depending on the context chosen, you may be prompted to choose an image registry to push the images to.
  4. An output window will appear where you can track the progress of your running application. You'll also see a live stream of the logs from the running pods within the terminal output.
  5. Once successful, the output window will display an IP address. Ctrl/Cmd + click to use this linked address to access your application!

    Running the Hello World app

Troubleshooting tips

  1. If you're using a pre-existing cluster, run gcloud container clusters get-credentials ${CLUSTER} --zone=${ZONE} to get cluster credentials. This will also add your cluster to kubectl.

    Alternatively, you can navigate to the GKE Explorer (as a panel accessible through the Kubernetes explorer under the Cloud Code view Kubernetes Explorer icon). Right-click your preferred cluster and select 'Set as Active Cluster'.

Debugging your app

To debug your application, follow these steps:

  1. Use the Cloud Code status bar and select Debug on Kubernetes.
  2. If prompted, confirm whether you'd like 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.

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

  4. Before your debugger session is attached, you'll be prompted to confirm or enter the directory in the remote container where the program you'd like to debug is found (or press ESC to skip debugging the container).

  5. Open src/app.js and click in the editor margin to add a breakpoint to line #9.

    Red filled circles denote 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, it will pause at that desired line.

    Under 'Local' in the Variables section, note the value of 'res._contentLength'; for 'Hello, world!', this is 13.

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

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

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

    App paused at breakpoint with updated values

Viewing logs

While you can see a live stream of the logs from running pods within the terminal output from your running application, you can also view logs with the Log Viewer that comes with Cloud Code.

To view logs, follow these steps:

  1. Launch the Log Viewer by typing Cloud Code: View Logs using the Command Palette (accessible with Ctrl/Cmd+Shift+P or through the 'Manage' menu marked with a gear icon Manage icon).
  2. Search for the running app, in this case 'nodejs-hello-world', to view logs from using the deployment field in the Log Viewer search box.

    Viewing logs by setting the deployment field within the Log Viewer search box to 'nodejs-hello-world'.

Bonus: Opening a terminal in your container

Additionally, if you'd like to open a terminal in your container, follow these instructions:

  1. Navigate to the Kubernetes Explorer.
  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 and right-click the container in which you'd like to open a terminal.
  4. Select Get Terminal.

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

    Opening a terminal for the specified pod

Cleaning up

Once you terminate your application, all Kubernetes resources deployed during the run will be deleted automatically.

However, 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:

  • Using the Kubernetes explorer under the Cloud Code view Kubernetes Explorer icon, right click the cluster you'd like to delete from the Google Kubernetes Engine Explorer pane and select 'Delete cluster'.

To delete your project (and associated resources, including any 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