Cloud Code is a set of IDE plugins for popular IDEs that make creating, deploying, and integrating Google Cloud applications easier. You probably know about cloud code this much. But how to deploy a service in cloud code is more fun and confusing part.
In this article, we will do an overview of cloud code for cloud shell, where we will introduce you first to the topic of how to deploy a service to cloud run. Afterward, we will discuss managing the Kubernetes cluster and debugging a Kubernetes application. Ultimately, we will talk about working and managing cloud APIs and libraries. So let’s get started!
To do an overview of cloud code for cloud shell, it is important to understand how to deploy a service in cloud code for cloud shell.
Deploying a Service to Cloud Run
To deploy your service to Cloud Run, follow these steps: Step 1:Run the Deploy to Cloud Run command after opening the command palette (Ctrl/Cmd+Shift+P or View > Command Palette).
Step 2: Set your Google Cloud project in the Deploy to Cloud Run pane.
Step 3: Choose an existing service or create a new one.
Step 4: If you're creating a new service, under Deployment Platform, select either Cloud Run (fully managed) or Cloud Run for Anthos.
Step 5: Choose a region to deploy to if you selected Cloud Run (fully managed). Configure the Kubernetes cluster information if you chose Cloud Run for Anthos.
Step 6: (Optional) Expand the Advanced revision settings section to specify additional settings such as service account, environment variables, and SQL connections.
Step 7: Select whether to build locally or use Cloud Build under the Build environment.
Step 8: Enter the name of your builder (Docker or Buildpack) and its settings.
Step 9: After you've chosen your settings, click Deploy. Cloud Code for Cloud Shell creates your image, registers it, and deploys it to Cloud Run. Your service is now operational!
Step 10: Follow the URL displayed in the web view to view your running service. You can also view details about your running service by clicking the Show Detailed Logs button and scrolling through the output window's verbose logs.
Step 11: When your session is finished, right-click and use the following commands:
a.) View Logs: Use the Cloud Code Logs to view the application logs for a specific deployment. Explorer
b.) Open URL: In a web browser, open the application service URL of a specific service.
Step 12: If you disabled watch mode in your launch configuration and want to make changes to your application as well as rebuild and redeploy it, click the Cloud Code status bar and then click Turn on watch mode.
Manage Kubernetes clusters in Cloud Code for Cloud Shell
While doing an overview of cloud code for cloud shell, it is necessary to know how to manage Kubernetes clusters in cloud code for cloud shell.
Creating a GKE cluster and adding it to your KubeConfig
To make a cluster:
Click Cloud Code - Kubernetes to launch the Kubernetes Explorer.
Add a Cluster to the KubeConfig by clicking the Add a Cluster button.
Choose Google Kubernetes Engine, then Yes to enable container GCP API in your project.
Select either Standard or Autopilot.
When prompted, click Open to allow Cloud Code to launch the console.
Select any configuration options you want to change in the console and then click Create.
After you've created the cluster, go to VS Code and click Refresh Kubernetes cluster view Refresh.
When the name of your new cluster appears in the list, click it. Your new cluster has been added to the configuration and designated as the default context.
Working with an existing GKE cluster
If you already have a Standard or Autopilot GKE cluster, you can make it your active cluster and work with it through Cloud Code.
Click Cloud Code - Kubernetes to launch the Kubernetes Explorer.
Add a Cluster to the KubeConfig by clicking the Add a Cluster button.
Navigate to Google Kubernetes Engine.
Click the name of your cluster. Your cluster has been added to KubeConfig.
Setting a cluster as the default context
The active cluster is the cluster to which your application is automatically deployed.
Click Cloud Code - Kubernetes to launch the Kubernetes Explorer.
Set as Active Cluster by right-clicking a cluster.
Removing a cluster from your KubeConfig
When you remove a cluster from your KubeConfig, it is no longer available for deployment.
Click Cloud Code - Kubernetes to launch the Kubernetes Explorer.
Remove Context Under Cluster by right-clicking a cluster.
Debugging a Kubernetes Application in Cloud Code for Cloud Shell
In our journey to do an overview of cloud code for cloud shell, it is important to know to debug a Kubernetes application using skaffold debug. Cloud Code allows you to easily debug an application deployed to a Kubernetes cluster.
Your application can be debugged on a local cluster (such as minikube or Docker Desktop), Google Kubernetes Engine (GKE), or any other cloud provider.
You don't have to do any manual setup with Cloud Code's debugging support, such as configuring port forwarding or injecting language-specific debug arguments. A Cloud Code-ready Kubernetes application with a skaffold.yaml configuration file and a cloudcode.kubernetes launch configuration is required for debugging.
Debugging an Application
Select the Cloud Code status bar, followed by Debug on Kubernetes.
If your application lacks the necessary Skaffold configuration or cloudcode.kubernetes launch configuration, Cloud Code can assist you in obtaining these.
Confirm whether the app should be run in the current Kubernetes context (or switch to a preferred one).
When prompted, select an image registry to push the images to if you chose a remote cluster as the context. If you're using Container Registry, you can browse to an existing registry or enter a name for a new registry. You can browse to and select an existing Artifact Registry repository if your project has Artifact Registry API enabled and at least one Artifact Registry repository.
Confirm or enter the directory in the remote container where the program you want to debug is located for each debuggable container in your application.
You can also skip debugging the container by pressing ESC.
Each debuggable container in the application is assigned a debug session by Cloud Code.
Setting breakpoints and stepping through code, as you would when debugging local code, is now possible against a live Kubernetes cluster.
When you save a change to your application, Cloud Code redeploys it and starts a new debug session by default. The watch flag in your project's launch configuration can be used to enable or disable this feature.
The Debug Sidebar can be used to inspect variables and stack information. Use the Debug Console in the bottom pane debugger to interact with the debugging session.
After your session is finished, you can use the contextual menu commands listed below:
Open Deployment Logs: Using the Cloud Code Logs Explorer, navigate to the application logs for a specific deployment.
Open Service URL: In a web browser, open the application service URL of a specific service.
If you disabled watch mode in your launch configuration and want to make changes to your application and rebuild and redeploy it, pause on the run action in the Development sessions pane and then click the Rebuild and redeploy icon.
To end the debugging session, click the Debug Toolbar's Debug stop icon Stop. When you finish debugging, all deployed Kubernetes resources are removed from the cluster.
Work with Google Cloud and Kubernetes YAML in Cloud Code for Cloud Shell
In our journey to do an overview of cloud code for cloud shell, we will now learn the working of Google cloud and Kubernetes YAML in cloud code for cloud shell.
Cloud Code for Cloud Shell is intended to simplify Kubernetes and Cloud Build configuration by the linting schema for structure and valid values and reporting descriptive errors.
Configuring with Snippets
Out-of-the-box snippets for common YAML schema (view options with Command/Ctrl+Space) make it simple to start a new YAML file or add to an existing one without errors while adhering to best practices. Cloud Code makes working with repetitive fields easier by filling out the remaining instances after you fill out the first.
Completing with Context
Cloud Code provides contextual completions and relevant docs based on the current schema to assist you in selecting the best option.
Validating YAML Schema
Cloud Code provides schema validation by highlighting invalid tags and values in your YAML files and recommending fixes when possible.
Discovering Documentation on hover
When you hover the pointer over a value in the schema, Cloud Code displays relevant documentation.
Accessing Resource Definitions
To view a resource's definitions, right-click it and select Go to Definition or Peek Definition.
Applying a YAML file
To use the current file to make a configuration change, open the command palette (Ctrl/Cmd+Shift+P or View > Command Palette) and then run Cloud Code: Apply the most recent JSON/YAML file to a K8s deployed resource.
This command displays a diff view so you can review changes. When prompted to apply this change, click Apply. This command executes kubectl apply -f.
Migrating an Application from Cloud Shell Editor to a local IDE
While doing an overview of cloud code for cloud shell, we now have to learn how to migrate an application from cloud shell editor to local IDE.
Migrate your app from Cloud Code to a local IDE
Follow these steps to migrate your application:
Install the Cloud Code IDE extension. Cloud Code is a JetBrains IDE (IntelliJ Ultimate/Community, PyCharm Professional/Community, WebStorm, and GoLand) that can be installed on VS Code.
Navigate to File > Download to obtain the required source code.
You can also commit your source code to a Git repo by using the Source Control: Git view from the Activity bar Source Control icon or by using Git commands in the Cloud Code terminal Open terminal icon, which is accessible from the Cloud Code top menu bar.
If you downloaded your Cloud Code workspace, you can now access it from your local IDE via File > Open Workspace (VS Code)/Open (IntelliJ), or via your local Source Control: Git view if you committed your source code to a Git repo.
Managing Cloud APIs and Cloud Client Libraries in Cloud Code for Cloud Shell
Now the last thing that we are going to cover while doing an overview of cloud code for cloud shell, is managing cloud APIs and cloud client libraries in cloud code for cloud shell.
Cloud APIs are used to programmatically access Google Cloud products and services. These APIs provide a basic JSON REST interface. The Cloud Client Libraries are the recommended method for accessing the Cloud APIs.
Cloud Code makes it simple to add Cloud Client Libraries for the Cloud APIs and language you're working with to your project. You can search for samples for each API in the same view and easily incorporate samples into your application.
Browsing Cloud APIs
To investigate all Google Cloud APIs:
Click Cloud Code - Cloud Run to open the Cloud APIs Explorer. The Cloud APIs view categorizes Cloud APIs.
Click the API name to view its details. Details such as the service name, status, client library installation instructions, documentation, and code samples are displayed.
Enabling Cloud APIs
Follow these steps to enable Cloud APIs for a project via the API details page:
Select the project for which you want to enable the Cloud API on the Cloud API details page.
Click the Enable API button. After enabling the API, a message is displayed to confirm the change.
Using API samples
In the API browser, you can find and use code samples for each API.
Click Cloud Code - Cloud Run to open the Cloud APIs Explorer.
Click the name of an API to open the detail view.
Click Code Samples to view API code samples.
Type text to search for or select a programming language from the Language list to filter the list of samples.
To view a sample, click the sample's name. You can also copy the sample to your clipboard or view the sample on GitHub.
Frequently Asked Questions
What is the Google Cloud shell?
Cloud Shell is a Google Cloud interactive shell environment that allows you to learn and experiment with Google Cloud while also managing your projects and resources from your web browser.
What is the process for creating a Cloud Shell file?
To create a new file in the cloud shell editor, type code filename> to open the editor without the file explorer and create a new file. If you want to delete the file, use the BASH command rm -r filename>.
How do I get started with Google Cloud Shell?
Step 1: To launch a cloud shell, click the Activate Cloud Shell button at the top of the Google Cloud Console window.
Step 2: You can now use your Google Cloud Shell session.
Step 3: When you launch Google Cloud Shell, the active project in Console is synced with your gcloud configuration inside Cloud Shell.
Conclusion
In this article, we have extensively discussed the overview of cloud code for cloud shell, how to deploy different services that will run on cloud code for sample applications, setting up, managing, and working with Google Clouds along with debugging them. We had also discussed how to develop a server locally in the cloud for Cloud Shell.
But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!