Running a Kubernetes Engine application with Cloud Code

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, build and test the app locally, create a Google Kubernetes Engine cluster, run your app on this cluster, debug your running code, and view your live application.

Interactive tutorial

If you'd prefer to work through this quickstart interactively, launch the Cloud Shell walkthrough using the button below.

Launch interactive tutorial.

Before you begin

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.

    Go to the project selector page

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

Creating an application

  1. Directly launch the Cloud Shell Editor.
  2. From the Cloud Code status bar, select New Application.

    Clicking on the Cloud Code status bar and choosing New Application

  3. Choose 'Kubernetes application' as your preferred application type.

  4. 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 Go: Hello World will create a starter Go Hello World app.

  5. Select a folder as your application location to proceed and click 'Create New Application'.

    Cloud Shell Editor loads the application in a new workspace. Once reloaded, your app is accessible in the explorer view.

Testing your application locally

To run your application in a local Kubernetes cluster in Cloud Shell, follow these steps:

  1. In your terminal, run the following command:

    minikube start
    
  2. When your minikube cluster is ready, click on the Cloud Code status bar and select Run on Kubernetes. Confirm that you want to use the minikube context.

  3. If prompted, authorize Cloud Shell to make Google Cloud API calls.

  4. As your app is being built, you can monitor its progress in the Output Panel. Once it's built, you can launch the app with the link displayed in your Output panel.

Editing your application

To edit your application, follow these steps:

  1. With the Explorer view, find the main.go file under cmd > hello-world folder.
  2. Modify "Hello, world!" to a message of your choosing.
  3. Once you make this change, you'll notice your app being rebuilt by the logs in your Output panel.
  4. When your app finishes building and deploying, launch it from the link in your Output panel to view your updated application.

Viewing application logs

To analyze your application while it's running, you can access its logs using the Log Viewer:

  1. Launch the Log Viewer by typing "Cloud Code: View Logs" using the Command Palette (accessible with Ctrl/Cmd+Shift+P).

    This view allows you to filter and navigate the logs for your app.

  2. Select 'Deployment' or 'Pod' to see the logs for your app.

  3. Refresh your app in the browser, and see the new logs generated by clicking the Logs refresh button.

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 - Kubernetes view Kubernetes explorer icon, by clicking on its icon in the VS Code Activity bar on the left.
  2. In the header of the Google Kubernetes Engine explorer panel, click the '+' button(plus icon) (available on mouseover) to create a new GKE cluster.
  3. Select your project.
  4. 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-c', and cluster name is defined as 'test'.

    Creating a GKE cluster with the cluster creation wizard

    Cluster creation takes a couple of minutes. Once created, details of your cluster and its underlying resources will be available under the Kubernetes Explorer view.

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 that you're using your new cluster as context for your app.
  3. Depending on the context chosen, you may be prompted to choose an image registry to push the images to.
  4. In the Output Panel, you can track the progress of your running application.
  5. Once successful, the output window will display an IP address. Ctrl/Cmd + click to use this linked address to access your application!

    Output window with IP address link to live Hello World application

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'd 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