This tutorial allows you to get your feet wet with Anthos development practices like continuous/iterative development, local builds and local deployments.
By the end of this tutorial you will have:
- Run this app on a local Kubernetes cluster with minikube
- Use the Cloud Build local builder to run tests locally
- Use Cloud Code to quickly test, debug and iterate on changes
- Build the application with Cloud Buildpacks
- Deploy the app to the Cloud Run Local Emulator
Our sample application is already available to you. Let's run it on minikube
,
a local Kubernetes cluster available by default in Cloud Shell, giving you the
same experience as deploying to Kubernetes without needing infrastructure:
-
In your terminal, run the command below:
minikube start -p cloud-run-dev-internal
-
If prompted, authorize Cloud Shell to make Google Cloud API calls
-
Once your local cluster in Cloud Shell is set-up, you will see the following message
Done! kubectl is now configured to use "cloud-run-dev-internal" by default
Let's now build and run this app:
-
Click Cloud Code in the status bar
-
Select Run on Kubernetes, confirm that you want to use the "cloud-run-dev-internal" context
-
An Output panel will pop-up after a few seconds, displaying the progress as your app is built/deployed
-
Once your app is built (will take a few minutes), launch it with the Web Preview link displayed in your Output panel, near the text:
Forwarded URL from service anthos-sandbox-sample-application-external:
Congratulations! You've just run your first app on Kubernetes using Cloud Code. Next step, let's run our continuous integration tests locally.
Let's now run the tests for our application locally. We'll do this with the
Cloud Build local builder (cloud-build-local
), which allows us to run the
exact same build our continuous integration suite would run, locally:
-
In your terminal, run the following command to run the tests (test_application.py).
cloud-build-local --dryrun=false .
Note: This command will produce some warnings which can safely be ignored.
-
You'll see that the tests have failed:
## FAIL: test_my_app (tests.test_application.TestMyApplication) Traceback (most recent call last): File "/workspace/tests/test_application.py", line 26, in test_my_app assert response.data == b"Hello Anthos" AssertionError ---------------------------------------------------------------------------- Ran 1 test in 0.021s FAILED (failures=1)
Looks like we were expecting our application to say "Hello Anthos" instead of "Hello World". Let's fix that in the next step.
Cloud Code enables iterative development so your deployed app updates as you write code, with no need to redeploy after every change. Our application is composed of the following:
- A basic web app (main.py), which returns "Hello, world!" to all received requests
- A load balancer, "anthos-sandbox-sample-application-external" Kubernetes Service (hello.service.yaml), which exposes our app to the internet.
To modify and automatically rebuild our app:
- Change main.py to print "Hello, Anthos!". The file will save automatically
- You'll notice in your Output panel that your app automatically starts rebuilding
- Once your app finishes building and deploying, view the updated app text by clicking the link in your Output panel or by returning to your open tab and refreshing it
Google Cloud Buildpacks allow you to create production-ready container images directly from source code without having to use a Dockerfile, ready to be deployed anywhere container images can be deployed.
First, now that we've fixed the bug, let's re-run the tests:
-
In your terminal, run the following command from before:
cloud-build-local --dryrun=false .
Note: This command will produce some warnings which can safely be ignored.
-
You'll see that the tests are now passing:
Ran 1 test in 0.020s OK
-
As part of the build process, the
cloud-build-local
tool has also used Google Cloud Buildpacks to produce a container image for your application. -
You can verify that your image is built by entering the following command and viewing the details under the
LOCAL
output:pack inspect-image anthos-sandbox-sample-application
Next, we'll deploy the app to the Cloud Run Emulator.
Now that we've built an image with Cloud Buildpacks, we can choose to deploy it directly to Cloud Run to take advantage of it's ability to scale up whenever traffic increases, and scale to zero when it's not being used, eliminating any idle costs.
We can get the same experience as deploying to Cloud Run by redeploying our app locally on the Cloud Run Emulator:
-
Launch Cloud Code from the status bar
-
Select Run on Cloud Run Emulator
-
Your app will now be built and deployed again
-
Once your app is deployed, launch it with the link displayed in your Output panel, near the text
http://localhost:8080 Update successful
You can open the web preview by clicking the button and selecting "Preview on port 8080".
Congratulations! You've just created and run your first Anthos application.
Here are some additional tutorials for using Cloud Code to develop your apps on Google Cloud:
Cloud Code IDE extension Install the Cloud Code extension on your local IDE (VSCode/IntelliJ)
Cloud Run Create a serverless app from your browser