简体   繁体   中英

Google App Engine instance shuts down and starts repeatedly

Premise / What you want to achieve

I am creating a web application with Python Flask in the Google App Engine standard environment, and I want to set app.yaml as follows so that one instance is always running.

[app.yaml]
runtime: python37

instance_class: F2

inbound_services:
- warmup

entrypoint: gunicorn -b :$PORT --workers 2 --threads 2 main:app

automatic_scaling:
  min_instances: 1
  max_instances: 2
  min_idle_instances: automatic
  max_idle_instances: automatic

What is happening and what you want to check

If you leave it for a while without any access and check the log, You can see that the instance is repeatedly shut down and started as shown below. (It is often repeated several times an hour.)

/_ah/warmup
[INFO] Starting gunicorn 20.0.4
[INFO] Listening at: http://0.0.0.0:8081 (7)
[INFO] Using worker: threads
[INFO] Booting worker with pid: 15
[INFO] Booting worker with pid: 18
[INFO] Handling signal: term
[INFO] Worker exiting (pid: 15)
[INFO] Worker exiting (pid: 18)
[INFO] Shutting down: Master
/_ah/warmup
[INFO] Starting gunicorn 20.0.4
[INFO] Listening at: http://0.0.0.0:8081 (7)
[INFO] Using worker: threads
[INFO] Booting worker with pid: 15
[INFO] Booting worker with pid: 17

Since min_instances is set, set a warm-up request and set it. You can also see from the log that the application is processing. Since it starts immediately by the warm-up request, the result is that one instance is always started, but in this way, it is correct behavior that the instance shuts down and starts repeatedly without any access. Is not it?

Supplementary information

The following sample app is for questions.

[requirements.txt]
Flask==1.1.1
gunicorn
[main.py]
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

@app.route('/_ah/warmup')
def warmup():
    # Handle your warmup logic here, e.g. set up a database connection pool
    return '', 200, {}

TL;DR : Yes.

There's nothing wrong with the sample application provided. The warmup path is correctly configured as per the documentation and the app.yaml settings are okay. Also, this doesn't seem to be an out of memory scenario since you would see such event reflected in the logs and the monitoring graphs visible in App Engine UI.

Keep in mind that App Engine is a managed service that scales on demand using a variety of heuristics based on application metrics such as memory consumption among others. While the min_instances settings determines that an instance ought to be running at all times there are a variety of reasons that could cause a particular instance to shutdown. The key point here is that, regardless of a particular instance shutting down or spawning, the settings defined in the app.yaml will be respected. Note this fact is reflected in the attached logs, there's always an instance active. Overall, graceful terminations are to be expected in this kind of managed environments and won't have an impact in your applications availability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM