Many developers are confused when they hear âgoogle cloud platform,â as I was when I first learned about GCP. Itâs not unexpected, because even if you visit cloud.google.com, you wonât be able to fully comprehend the platformâs massive content and capabilities. On the one hand, this highlights the potential of cloud computing services, but it also makes it nearly impossible for a newcomer to get started using the platform without assistance. Well, why not begin your journey with me today? Weâll work together to clarify all of the steps and procedures, and by the end of the series, youâll have a lot more confidence in your cloud computing skills, and who knows, you could even start guiding other lost souls like yourself!
In this tutorial, we will create a containerized web app, test it locally, and then deploy it to a Google Kubernetes Engine (GKE) cluster. So grab a cup of coffee and brace yourself for a coding session!
What is Kubernetes? Why do we need it?
Kubernetes was introduced in 2014 by Google. Many of you may be curious about the origins of the name Kubernetes, which is also known as K8s, an acronym originating from the eight letters between the letters âKâ and âs.â Itâs derived from a Greek word that means âpilotâ or âorchestratorâ â now its logo already makes sense. According to the official definition, âKubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automationâ. To put it differently, it allows us to manage clusters of containerized applications and services.
Cloud-native apps are self-contained, lightweight containers that can be scaled quickly in response to demand. We can isolate the application and its dependencies from the underlying infrastructure by enclosing everything in a container (such as a Docker container). This allows us to run the containerized application in any environment that has the container runtime engine installed. This is where Kubernetes comes into play. Container orchestrations in Kubernetes are significant because they control the lifetime of the containers. That is why, while dealing with cloud-native apps, Kubernetes is so vital and valuable.
Creating a Web App
To follow along with this guide, youâll need a Google Cloud account first. Google Cloud, fortunately for everyone, offers a 90-day free trial period with $300 in free Cloud Billing credits to explore and evaluate its products and services. Who could ask for anything more? Go ahead and make a new account, then return to this page to catch up.
For this tutorial, weâll be using the Cloud Shell Editor as our development environment. Cloud Shell is a free preloaded online environment, with command-line access for managing your infrastructure and an online code editor for cloud development.
With the default Cloud Shell experience, a preconfigured Compute Engine virtual machine with a Debian-based Linux operating system is created on startup. The environment you work with is a Docker container running on that VM. While your Cloud Shell session is active, the instance persists; after an hour of inactivity, your session is terminated, and the VM is discarded.
To open Cloud Shell, click Activate Cloud Shell button in the upper right part of the page. When you use Cloud Shell for the first time to perform a Google Cloud API request or use a command-line tool that requires credentials, Cloud Shell prompts you with the âAuthorize Cloud Shellâ dialog. To authorize the tool to use your credentials to make calls, click Authorize.
Click Open Editor to open Cloud Shell Editor.
Launch the Cloud Code menu from the status bar and select New Application.
Select the Kubernetes application option as the type of sample app. From the list of sample Kubernetes apps, select the Java: Hello World option, and finally, select a folder for your app location and then click Create New Application.
Cloud Shell Editor creates a new workspace for your program. With the explorer view, you may access your app once it reloads. The structure of the âHello Worldâ project is relatively simple, with only one main class. It consists of a single Deployment and its associated Service. A web server is included in the deployment, which renders a basic webpage. You can take a look at the classes and their basic functionality, however, exploring them in detail is out of the scope of this tutorial.
Building and Testing the App
Now that we have created our app, we can run it in a local Kubernetes cluster in Cloud Shell. In your terminal, run this command to start your local minikube cluster:
minikube start
After your cluster is set up, the following message will be displayed:
Next, we will build and run this app. Launch the Cloud Code menu from the status bar, Select Run on Kubernetes, and then confirm that you want to use the minikube context.
The Output panel displays the progress as your app is built and deployed:
After your app is built, which should take a couple of minutes, you can launch it using the link displayed in your Output panel.
Congratulations! You have just run your first app on Kubernetes using Cloud Code.
Modifying the App
To review what the Hello World app consists of, refer to the diagram in its readme.md file. At a high level, it consists of:
- A basic HelloWorldController web app that returns a templated âItâs running!â response to all received requests.
- A load balancer service hello.service.yaml, that exposes the app by describing a Kubernetes service.
We can modify our HelloWorldController to print âItâs redeployed!â. The file saves automatically.
You can monitor your appâs progress as itâs rebuilt, using the Output panel. After your app finishes building and deploying, launch your app by clicking the link in the Output panel or refresh the tab where you opened your app.
Viewing App Logs
There is a quite handy feature called Log Viewer to monitor your appâs logs while itâs running. You can launch it by opening the Command Palette (Ctrl + Shift + P) and then typing Cloud Code: View Logs.
Specify the Deployment or Pod filters to view the logs for our app, java-hello-world:
Refresh the app in the browser. To view the newly generated logs in the Log Viewer, click the Logs refresh button.
Creating a Google Kubernetes Engine Cluster
Weâve been running our app locally thus far. Itâs time to deploy our application to a remote cluster!
Projects are a way for Google Cloud to group together related resources. We must first create a project before we can create a GKE cluster. You can select an existing project, but letâs create a new one for the sake of this tutorial.
Open the Navigation menu and then click Kubernetes Engine. Click Create and click Configure under the standard cluster.
In Cluster basics, enter a name and zone for the cluster. To create the cluster, click Create. It takes a few minutes for the cluster to provision.
Click the Cloud Code â Kubernetes icon in the navigation bar. Hover over the Kubernetes Explorer and then click + Add a cluster to the KubeConfig.
Select Google Kubernetes Engine. Select your recently created cluster from the list.
Deploying Our App to a GKE cluster
Finally, letâs deploy our app to the new cluster. From the Cloud Code menu, accessible using the status bar, select Run on Kubernetes. Confirm your newly created cluster as the context for your app. Confirm the default option for your image registry.
After the app is successfully deployed, you can launch it with the link displayed in the Output pane.
At the end of this short tutorial, you already know how to create a sample Kubernetes app, build/test/edit this app on a local Kubernetes cluster, view and navigate your appâs logs, create a GKE cluster and deploy an app to GKE. Pretty impressive, isnât it?
Thereâs a lot more to learn about Google Cloud Platform, so stay tuned and donât miss out on my next blogs!