Get started with Cloud Code for VS Code for Kubernetes

Cloud Code lets you create a Kubernetes application based on a sample or from an existing project.

Create an application from a template

Cloud Code comes with a collection of code sample templates to get you started quickly. To create a Kubernetes application using an existing sample, follow these steps:

  1. Launch the Command Palette (press Ctrl/Cmd+Shift+P or click View > Command Palette) and then run Cloud Code: New Application.
  2. Select Kubernetes application for the type of sample.
  3. Select a sample based on the language you'd like to use from the available options: NodeJS, Go, Python, Java.
  4. Choose a preferred application location on your local machine and then click Create new application to save.

    Cloud Code clones the sample you chose and opens your new project for use.

Sample application structure

All language sample applications have nearly the same structure. This is not the only supported structure but is recommended when starting.

For example, the Node.js Guestbook application structure looks like:

.
|---- .vscode
|      ---- launch.json
|---- kubernetes-manifests
|     |---- guestbook-backend.deployment.yaml
|     |---- guestbook-backend.service.yaml
|     |---- guestbook-frontend.deployment.yaml
|     |---- guestbook-frontend.service.yaml
|     |---- mongo.deployment.yaml
|     ---- mongo.service.yaml
|---- src
|     |---- backend
|     |     |---- Dockerfile
|     |     |---- index.js
|     |     |---- app.js
|     |     ---- package.json
|     |---- frontend
|           |---- Dockerfile
|           |---- index.js
|     |     |---- app.js
|           ---- package.json
---- skaffold.yaml

Taking a closer look at this Kubernetes Guestbook Node.js sample app, here are some key files and their uses:

  • .vscode
    • extensions.json: prompt requesting download of related extensions when opening this project
    • launch.json: launch configuration (of type cloudcode.kubernetes) to run or debug the Kubernetes application
    • tasks.json: configuration information for Visual Studio Code Tasks
  • kubernetes-manifests
    • guestbook-backend.deployment.yaml: Pod specification for the backend nodes
    • guestbook-frontend.deployment.yaml: Pod specification for the frontend nodes
    • mongo.deployment.yaml: Pod specification for the database
  • src
    • (backend|frontend)/app.js: Node.js code with the web server logic
    • (backend|frontend)/Dockerfile: used to build the container image for our program
  • skaffold.yaml: config file for Skaffold, which Cloud Code uses to build, deploy and debug Kubernetes applications

Use your own application

For steps to use an existing project, see use Cloud Code with an existing Kubernetes application.

Set your Kubernetes context

Before running your application, ensure you're set up to deploy your app to your preferred Kubernetes context. You can specify this in your configuration.

Configuration

When using the Cloud Code: Develop on Kubernetes run configuration, you can customize your deployment by configuring available settings.

To add or edit configurations, go to Run > Open Configurations and then edit or add configurations.

Build settings

Cloud Code supports Docker, Jib, and Buildpacks artifact types. Refer to the Configuring container image build preferences guide to learn about how to set your preferred builder and its relevant settings.

Customize your launch configuration

To configure how your application is run, you can customize your skaffold.yaml file.

You can also configure your launch by editing the cloudcode.kubernetes configuration in your .vscode/launch.json file.

For more information on customizing your launch configuration, see the Kubernetes in Cloud Code.

Run your application

Once you have an application set up, you can run it on a Kubernetes cluster and view it live, by leveraging skaffold dev. You can run your application on a local cluster (like minikube or Docker Desktop), Google Kubernetes Engine, or any other Cloud provider.

  1. Open the Command Palette (press Ctrl/Cmd+Shift+P) and then run the Cloud Code: Run on Kubernetes command.
  2. Confirm whether to use the current Kubernetes context to run the app in (or switch to a preferred one). For more information about setting up a Kubernetes context, see setting up configuration.
  3. If you chose a remote cluster as the context, when prompted, choose an image registry to push the images to. If you're using Container Registry, you can browse to an existing registry or specify the name of a registry to create. If your project has Artifact Registry API enabled and at least one Artifact Registry repository, you can browse to and select an existing Artifact Registry repository.

    The following samples demonstrate how to specify where container images are stored for some common registries:

    Artifact Registry {region}-docker.pkg.dev/{project_id}/{repo-name}
    Container Registry gcr.io/{project_id}
    Docker Hub docker.io/{account}
    Make sure that you're properly authenticated if you're using a private Docker Hub repository.
    AWS Container Repository (ECR) {aws_account_id}.dkr.ecr.{region}.amazonaws.com/{my-app}
    Azure Container Registry (ACR) {my_acr_name}.azurecr.io/{my-app}

    Cloud Code concatenates this image registry with the image name specified in the Kubernetes manifests to generate the final image repository name.

    For more information, see the image registry handling guide.

    This choice is stored in your cloudcode.kubernetes launch configuration (found in .vscode/launch.json).

    Cloud Code builds your containers, pushes them to the registry, applies Kubernetes configurations to the cluster, and waits for the rollout.

View logs

In addition to seeing logs from running pods as a live stream in the terminal output as you're developing and running your application, you can view logs from a specific pod by navigating to the Kubernetes section.

To view logs from a specific pod, follow these steps:

  1. In the Kubernetes section, expand Deployments
  2. Right-click the pod you'd like to see logs from, and then click View Logs.

    The Logs viewer opens.

Make changes, rebuild, and clean up

If you've set watch mode to false in your launch configuration and you want to make changes to your application and then rebuild and redeploy the application:
  1. Make and Save your changes.

  2. In the Debug Toolbar, click Pause (F6) and then click Restart (Ctrl/Cmd + Shift + F5) to rebuild and redeploy the application.

  3. To stop running the application, in the Debug Toolbar, click Stop.

After you stop the application, all deployed Kubernetes resources are deleted from the cluster. You can change this behavior using the cleanUp flag in your launch configuration.

Store secrets

If your code includes potentially sensitive data like API keys, passwords, and certificates, it is recommended you store them as secrets. With Cloud Code, you can securely store these secrets in Secret Manager and programmatically fetch them when you need them.

For a detailed look at how you can create and manage secrets with Cloud Code, see the Secret Manager guide.

View resource details

The Kubernetes section displays clusters, namespaces, nodes, workloads (such as deployments, replicasets, pods and containers), services and ingresses, configurations (such as secrets and config maps) and storage (such as volumes). Using the Kubernetes section, you can perform unique actions on some of these resources.

See the Kubernetes overview for more information on viewing resource details.

What's next

Get Support

To send feedback, report issues on GitHub, or ask a question on Stack Overflow.