This page shows you how to get started quickly with Cloud Code using a remote development environment in Cloud Shell.
In this quickstart, you'll skip setup and clone a project into your remote development workspace with the click of a button, create a cluster, run a Kubernetes app on this cluster, debug your running code, view logs from your live application, and also, connect a terminal to your running container.
Before you begin
To access Cloud Shell, the remote development environment on Google Cloud you'll be using for this quickstart, you'll need to create a GCP project and enable billing.
You'll also need to have the following installed on your machine:
- Visual Studio Code
- Remote - SSH Visual Studio Code extension
Launching Cloud Code
Once you have Visual Studio Code and the Remote - SSH extension installed, click the 'Open with Cloud Code' button below to open Visual Studio Code connected to a remote development environment in Cloud Shell. This environment has all the tools you need to develop Kubernetes applications.
On clicking the 'Open with Cloud Code' button, VS Code will launch and automatically clone a project into your remote development workspace. If you don't already have the prerequisites set up, you'll be prompted to install them at this point.
Choose your preferred language for the cloned project and click on the Open with Cloud Code button to start work in your remote development workplace:
Node.js
Java
Python
Go
Creating a Google Kubernetes Engine cluster
To create a Standard GKE cluster, follow these steps:
- Navigate to the Google Kubernetes Engine explorer using the Cloud Code view
, by clicking its icon in the VS Code Activity bar on the left.
- Authenticate with the Cloud SDK by clicking Click here to log in to Google Cloud SDK.
- In the header of the Google Kubernetes Engine Explorer panel, click the + button (available on mouseover) to create a new GKE cluster.
- Ensure you're creating your cluster in your default project.
Fill out fields in the Create Cluster wizard and then 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 asmy-first-cluster
.Cluster creation takes a couple of minutes. After your cluster is created, it's listed under the GKE Explorer view
.
Running your app
Running a dry-run of your configuration
If you'd like to check the validity of your app's configuration:
- Open the config file you want to validate in your IDE (for the Hello World app, config files are located under the kubernetes-manifests folder).
Use Cloud Code: Dry-run current config for server-side validation or Cloud Code: Dry-run current config for client-side validation from the Command Palette (accessible from the Manage menu
).
This runs
kubectl apply -f dry-run=server
(orkubectl apply -f dry-run=client
, for the Client option) and displays successful validation (or an error message, if your config file isn't valid) as a toast notification.
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:
- Use the Cloud Code status bar and select Run on Kubernetes.
- Confirm whether to use the current cluster context or switch to a different one.
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.
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 get cluster credentials and add
your cluster to kubectl
, run the following command:
gcloud container clusters get-credentials ${CLUSTER} --zone=${ZONE}
Alternatively, you can set your cluster as active and get cluster credentials using the GKE Explorer as follows:
- Navigate to the GKE Explorer (as a panel accessible in the
Kubernetes explorer
under the Cloud Code view
).
- Right-click your cluster and then click Set as Active Cluster.
Debugging your app
To debug your application, follow these steps:
- Use the Cloud Code status bar and then select Debug on Kubernetes.
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 then 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.
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.
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.
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;
forHello, world!
is 13.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
.
Viewing logs
In addition to seeing a live stream of the logs from running pods in 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:
- 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
).
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.
Bonus: Opening a terminal in your container
To open a terminal in your container, follow these instructions:
- Navigate to the Kubernetes Explorer.
- Select the cluster and, from the underlying Pods section, the pod you'd like to connect to.
- Under your specified pod, expand your Containers section.
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!
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:
- In the Kubernetes explorer under the Cloud Code
view
, right-click the cluster you'd like to delete from the Google Kubernetes Engine Explorer pane and then click Delete cluster.
To delete your project (and associated resources, including any clusters):
Go to the Projects page in the Cloud Console:
Select the project you created for this quickstart and then click the trash can icon next to it to delete it.
This shuts down the project and schedules it for deletion.
What's next
- Consider importing an existing application into VS Code and getting set up with Cloud Code.
- Tackle more advanced Google Cloud and Kubernetes configuration with Cloud Code's YAML editing support.
- Discover the language-specific debugging support that Cloud Code provides.
- Explore the types of Kubernetes clusters you can work with while developing with Cloud Code.
- For better insight into and management of your Kubernetes resources, use the Kubernetes Explorer.
- Customize your Cloud Code experience by configuring relevant settings.