Overview
In this lab, you create and deploy a simple App Engine application using a virtual environment in the Google Cloud Shell.
Objectives
In this lab, you learn how to perform the following tasks:
Initialize App Engine.
Preview an App Engine application running locally in Cloud Shell.
Deploy an App Engine application, so that others can reach it.
Disable an App Engine application, when you no longer want it to be visible.
Source:
This lab is from Qwiklabs.
Set up your lab environment
Activate Cloud Shell
Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.
In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.
Click Continue.
It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
You can list the active account name with this command:
gcloud auth list
You can list the project ID with this command:
gcloud config list project
After Cloud Shell is activated, you can use the command line to invoke the Cloud SDK gcloud tool or other tools available on the virtual machine instance. Later in the lab, you will use your $HOME directory, which is used in persistent disk storage to store files across projects and between Cloud Shell sessions. Your $HOME directory is private to you and cannot be accessed by other users.
Task 1: Initialize App Engine
1. Initialize your App Engine app with your project and choose its region:
gcloud app create --project=$DEVSHELL_PROJECT_ID
When prompted, select the region where you want your App Engine application located.
2. Clone the source code repository for a sample application in the hello_world directory:
git clone https://github.com/GoogleCloudPlatform/python-docs-samples
3. Navigate to the source directory:
cd python-docs-samples/appengine/standard_python3/hello_world
Task 2: Run Hello World application locally
In this task, you run the Hello World application in a local, virtual environment in Cloud Shell.
Ensure that you are at the Cloud Shell command prompt.
1. Execute the following command to download and update the packages list.
sudo apt-get update
2. Set up a virtual environment in which you will run your application. Python virtual environments are used to isolate package installations from the system.
sudo apt-get install virtualenv
If prompted [Y/n], press Y and then Enter.
virtualenv -p python3 venv
3. Activate the virtual environment.
source venv/bin/activate
4. Navigate to your project directory and install dependencies.
pip install -r requirements.txt
5. Run the application:
python main.py
Please ignore the warning if any.
6. In Cloud Shell, click Web preview (Web Preview) > Preview on port 8080 to preview the application.
To access the Web preview icon, you may need to collapse the Navigation menu.
Result:
7. To end the test, return to Cloud Shell and press Ctrl+C to abort the deployed service.
8. Using the Cloud Console, verify that the app is not deployed. In the Cloud Console, on the Navigation menu, click App Engine > Dashboard.
Task 3: Deploy and run Hello World on App Engine
To deploy your application to the App Engine Standard environment:
1. Navigate to the source directory:
cd ~/python-docs-samples/appengine/standard_python3/hello_world
2. Deploy your Hello World application.
gcloud app deploy
If prompted "Do you want to continue (Y/n)?", press Y and then Enter.
This app deploy command uses the app.yaml file to identify project configuration.
Note: If you get a Gaia propagation related error message, re-run the gcloud app deploy command.
3. Launch your browser to view the app at http://YOUR_PROJECT_ID.appspot.com
gcloud app browse
Copy and paste the URL into a new browser window.
Result:
Congratulations! You created your first application using App Engine.
Task 4: Disable the application
App Engine offers no option to Undeploy an application. After an application is deployed, it remains deployed, although you could instead replace the application with a simple page that says something like "not in service."
However, you can disable the application, which causes it to no longer be accessible to users.
1. In the Cloud Console, on the Navigation menu, click App Engine > Settings.
2. Click Disable application.
3. Read the dialog message. Enter the App ID and click DISABLE.
If you refresh the browser window you used to view to the application site, you'll get a 404 error.
Congratulations!
You created your first application using App Engine!
Reference:
1. Qwiklabs
2. Google Cloud Certification - Associate Cloud Engineer
3. Read the Google Cloud Platform documentation on App Engine.
4. Read the Google Cloud Platform documentation on gcloud tool overview.
最初發表 / 最後更新: 2021.11.01 / 2021.11.01
0 comments:
張貼留言