In this article, you will deploy applications to the Google Cloud services App Engine, Kubernetes Engine, and Cloud Run.
Objectives
- Download a sample app from GitHub
- Deploy to App Engine
- Deploy to Kubernetes Engine
- Deploy to Cloud Run
Task 1. Download a sample app from GitHub
Download a sample application from GitHub and preview it in Cloud Shell.
In the Cloud Console, click Activate Cloud Shell (Activate Cloud Shell icon).
If prompted, click Continue.
To create a new folder, run the following command:
mkdir gcp-course
Change to the folder you just created:
cd gcp-course
Clone a simple Python Flask app from GitHub:
git clone https://GitHub.com/GoogleCloudPlatform/training-data-analyst.git
Change to the deploying-apps-to-gcp folder:
cd training-data-analyst/courses/design-process/deploying-apps-to-gcp
To test the program, enter the following command to build a Docker container of the image:
docker build -t test-python .
To run the Docker image, enter the following command:
docker run --rm -p 8080:8080 test-python
To see the program running, click Web Preview (Web Preview icon) in the toolbar of Google Cloud Shell. Then, select Preview on port 8080.
The program should be displayed in a new browser tab.
In Cloud Shell, type Ctrl+C to stop the program.
Task 2. Deploy to App Engine
App Engine is a completely automated deployment platform. It supports many languages, including Python, Java, JavaScript, and Go. To use it, you create a configuration file and deploy your applications with a couple of simple commands. In this task, you create a file named app.yaml and deploy it to App Engine.
In Cloud Shell, click Open Editor (Cloud Shell Editor icon), then click Open in a new window if required.
Select the gcp-course/training-data-analyst/courses/design-process/deploying-apps-to-gcp folder in the explorer tree on the left.
From the File menu, select New File, and name the file app.yaml. Then click Ok.
Paste the following into the file you just created:
runtime: python37
Save your changes.
Note: There are other settings you can add to the app.yaml file, but in this case only the language runtime is required.
In a project, an App Engine application has to be created. This is done just once using the gcloud app create command and specifying the region where you want the app to be created. Enter the following command:
gcloud app create --region=us-central
Now deploy your app with the following command:
gcloud app deploy --version=one --quiet
Note: This command will take a couple of minutes to complete.
On the Navigation menu (Navigation menu icon), click App Engine > Dashboard. In the upper-right corner of the dashboard is a link to your application.
Note: By default, the URL to an App Engine application is in the form of https://project-id.appspot.com.
Click on the link to test your program.
Make a change to the program to see how easy the App Engine makes managing versions.
In the code editor, expand the training-data-analyst/courses/design-process/deploying-apps-to-gcp folder in the navigation pane on the left. Then, click main.py to open it.
In the main() function, change the title to Hello App Engine as shown below:
@app.route("/")
def main():
model = {"title" "Hello App Engine"}
return render_template('index.html', model=model)
Click File > Save in the code editor toolbar to save your change.
Now, deploy version two with the following command:
gcloud app deploy --version=two --no-promote --quiet
Note: The --no-promote parameter tells App Engine to continue serving requests with the old version. This allows you to test the new version before putting it into production.
When the command completes, return to the App Engine dashboard. Click the link again, and version one will still be returned. It should return Hello GCP. This is because of the --no-promote parameter in the previous command.
On the left, click the Versions tab. Notice that two versions are listed.
Note: You might have to click Refresh to see version two.
Click on the version two link to test it. It should return Hello App Engine.
To migrate production traffic to version two, click Split Traffic at the top. Change the version to two, and click Save.
Give it a minute to complete. Refresh the browser tab that earlier returned Hello GCP. It should now return the new version.
0 Comments