How to efficiently build Kubernetes Apps with Google Cloud Code ? (Part-1)
Google cloud recently released Anthos Developer Sandbox to make it easy for developers to build, package, test and debug cloud native apps using nothing but Browser as interface. I plan to write a Part-2 later to cover my learnings on Developer Sandbox, but thought it would be a good idea to first cover the foundations here in Part-1.
In this blog, I will demonstrate how easy it is for developers to do DevTest cycles for a cloud native app using Cloud Code without ever leaving the IDE.
A non-goal is to augment the developer flow with CI/CD, which I will cover in Part-2. So clearly the scope here is local development and test by leveraging Google Cloud.
- VSCode IDE. Note – InteliJ also support Cloud Code extension, but this blog has instructions only for VSCode.
- Docker Daemon installed in your laptop. Download and install the latest version if you don’t have it already.
- Google Cloud Project. You can use any existing, or create a new Project. Note – you will need a Google Cloud account. Subscribe for free with a $300 credits
Step 1 : Setup developer environment.
Launch VSCode Extension page and Install Cloud Code extension.
Step 2 : Clone hello-world node.js app
At this point, clone the hello-world app from this Github repository Along with app source, the repository also has the required Dockerfile and K8s manifest yamls describing the deployment and service. Pretty standard ! In Part-2, I will cover how BuildPacks can be used to further advance this step.
Also note that Dockerfile must be at the root dir, and all Kubernetes yaml should go into a folder named kubernetes-manifests.
After the above extension is successfully installed, the relevant actions will be available in the side palette (marked red in the image below)
Step 3 : Create GKE cluster.
From the side palette within IDE, Click the “Code Cloud – Kubernetes” to launch the cluster options, click the + symbol.
Choose an existing Cluster or create a New GKE Cluster with the configurations required for running the apps.
Step 4 : Review cluster details.
The Cluster can take upto 5 mins for provisioning. After the cluster is created, you can see more details in the “Kubernetes Explorer” window. Notice that the Context name of the Cluster is combination of 3 elements: <gke> + <project-name> + <cluster-name>. The * mark indicates that amongst other clusters that you may have, the IDE is currently fixed on this one as the current-context.
Step 5 : Build Images and Deploy to Kubernetes
You can deploy the app to the chosen Cluster using VSCode command palette – Run on Kubernetes from the newly installed Cloud Code extension.
If the project chosen is a new one, then you are required to provide a Docker-registry, such as gcr.io, to host your images after the app is built and containerised. You don’t have to leave the IDE at any point
Cloud Code output can be seen in the console.
Step 6 : Access the node.js app (with LB URL)
Step 7 : Attach a Debugger.
Bring up the VS Code Command Palette by doing CMD+SHIFT+P (on a Mac), and type in “Debug in Kubernetes”
The below confirmation allows to link the source code with the running container.
You can attach BreakPoint(s) in the source code the ususal way, and hit the application LB URL
Finally, control the flow using the navigation buttons.
It’s often seen that developers have to use multiple systems to develop cloud native apps and live debugging is difficult in a containerised environment. Cloud Code IDE extension is very useful to do the local testing in the Cloud (or with minikube). We only explored handful of VS Code Command Palette commands, there are tonnes of them that will certainly help developers write and debug code faster and smarter.
Outside of the IDE, you can also use command line tool Skaffold to push the app to Kubernetes.
skaffold run --default-repo=gcr.io/YOUR-PROJECT-ID-HERE/cloudcode