This lab is part of the foundations trainings in place at OCTO Technology Australia.
In this quick lab, we will play with gcloud App Engine.
At the end of the lab you will be able to:
- deploy a simple Go application on Google App Engine - Flexible environment (you don't have to understand Go)
- split traffic between 2 versions of you app
- do basic operations on your application: see logs, ssh, debug, view metrics, etc
This lab should take approximately 30 minutes. You will need to use you own google cloud account.
Google App Engine (GAE) is a web framework and cloud computing platform for developing and hosting web applications in Google Cloud Platform. Applications are sandboxed and run across multiple servers. App Engine offers automatic scaling for web applications.
You can run your applications in App Engine using the flexible environment or standard environment:
- Using the flexible environment means that your application instances run within Docker containers on Google Compute Engine virtual machines.
- Using the standard environment means that your application instances run in a sandbox, using the runtime environment of a supported language listed below.
Let's start with a bit of terminology, your applications will use:
- service, basically one independent piece of software you want to deploy. Except if you are deploying micro-services, most of the time your app will have only 1 service.
- version, a deployable version of your app, your service can have several versions
- instance, when a version of a service is deployed, 1 or several instances are created. This is the running instance where your app is hosted.
In this Lab, we will focus on App Engine Flexible environment.
- Google to the Google Cloud console
- Click on Project List (in the header next to the Google Cloud Platform Logo), the "Create Project"
- Give it a "Name", select a billing account, then use the "Create" button
For more information about this prerequisite step please refer to the official documentation.
- Install the Google Cloud SDK on your machine
- Configure you SDK with you gcloud account, select a project with the command line
gcloud init
- git clone this repository on you machine
- have a look to the helloworld.go file, it is a simple Go app doing an Hello World
- update the string
Hello mate, if you like this lab, please give me a star :)
by any other string, it could beHello World
GCP is using a yaml configuration file to know how to deploy your application, let's create it.
Create an app.yaml file to describe your application, for that you can use the existing app.yaml.template as an example.
Only 2 lines required are:
env: flex
, type of Google App Engine (GAE) project, GCP provided 2 type of environment: Flexible or Standard.runtime: go
, to tell GAE which runtime do you want to use.
I would also recommend setting the service: YOUR_APP_NAME
, if not your application will be deployed in the default one (hope you are alone on your project if it is the case).
Please refer to the official documentation to see all available options you could put in this file.
To do so, just use this simple command line in you shell: gcloud app deploy
.
It will ask you a few questions like in which region you do want to deploy the app.
Note that you must set service's argument of yaml file to default !
service: default
Or you'll get the following error:
ERROR: (gcloud.app.deploy) INVALID_ARGUMENT: The first service (module) you upload to a new application must be the 'default' service (module). Please upload a version of the 'default' service (module) before uploading a version for the 'go-demo-roch' service (module).
Then take a coffee, it could take up to 10 minutes for the app to be created.
GCP App Engine Flexible environment is using Docker under the hood to deploy your application. You can see the built image in the registry.
The build process could fail for many reason: your app is broken, the app.yaml file is not valid... To take a look to the logs of the build process, go the the GCP Cloud Build service.
A service is a kind of application. You should be able to see the created Service from the GCP console with the name you put in the app.yaml file.
From here, you can:
- see the number of version you service have (should be 1 at this stage)
- access application logs from the "Tool" menu
Then click on "Versions"
Notice the Version: status, traffic allocation, #instances (Menu > App Engine > Version >> select your service)
This screen list all versions available for your service. Basically, 1 version is created each time you deploy. If you can't find your version, use the drop-down at the top to select the good service.
When your application is deployed, you should be able to see:
- Status -> Serving
- Traffic Allocation to 100%
- Instances 1 (or more according to your configuration)
- You can also take a look to the config file GCP linked to this version and look at the logs of your application.
You can also check the logs from your terminal: gcloud app logs tail -s YOUR_APP_NAME
This screen is important, this is where you can control the traffic of your app: Stop a version, Start, Migrate or split traffic between several versions.
If your version is in serving state (started), 1 or more instances will be linked to it. This screen allows you do see those instances, ssh on them
Click on the service name from the Service list or use the command gcloud app browse -s YOUR_APP_NAME
Update the message served your helloworld.go file with something else (ie, from "Hello World" (set in pre-requisites) to "GAE is as simple as that")
Run the same command that previously: gcloud app deploy
Redeploying an application will create a new "Version" of it, the process still takes a bit of time but at the end of it you will be able to see your new version listed on the "Version" screen.
Exactly as before, if it is not ready within 2 minutes, wait a bit more :)
gcloud app browse -s go-YOUR_APP_NAME
To do so, go the Versions (Menu > App Engine > Version >> select your service), then:
- start the previous version to have 2 versions with status “Serving” (You can do it on the cloud console or with the CLI)
- then select those 2 versions the click “Split Traffic button”
- select "Random" (easier to test that IP based), then set traffic allocation to 50% 50%
To test if it works:
- go back to the version list and notice the traffic allocation has been updated.
- then take another coffee or wait for 5 to 10 minutes. If you do prefer tea, sorry for you it won't work :)
- open your service in the browser and refresh many times
- After several refreshes, you should see 2 different messages (1 by version) If 1 of those fails (503) => wait more...
That was easy! You made it, congrats!
Don't forget to delete all the ressources you created. Or you can also delete the whole project.