【问题标题】:Getting Error while trying to run sample python app尝试运行示例 python 应用程序时出错
【发布时间】:2021-09-26 06:44:46
【问题描述】:

我只想在 1 个虚拟机上运行它以通过 Web 界面 (8080) 访问它: 我所做的事情清单:创建了 vm (debian 9) 默认服务帐户 google。 之后,我按照说明执行了命令 由谷歌提供:

curl -sSO https://dl.google.com/cloudagents/install-logging- agent.sh 
sudo bash install-logging-agent.sh
apt-get update apt-get install -yq git supervisor python python-pip
pip install --upgrade pip
virtualenv useradd -m -d /home/pythonapp pythonapp
export HOME=/root
git clone -b steps https://github.com/GoogleCloudPlatform/getting-started-python.git
/opt/app virtualenv -p python3 /opt/app/gce/env
source /opt/app/gce/env/bin/activate
/opt/app/gce/env/bin/pip install -r /opt/app/gce/requirements.txt 
chown -R pythonapp:pythonapp /opt/app 
# Put supervisor configuration in proper place
cp /opt/app/7-gce/python-app.conf /etc/supervisor/conf.d/python-app.conf
cat >/etc/supervisor/conf.d/python-app.conf << EOF
[program:pythonapp]
directory=/opt/app/7-gce
command=/opt/app/7-gce/env/bin/honcho
start -f ./procfile worker bookshelf autostart=true autorestart=true user=pythonapp environment=VIRTUAL_ENV="/opt/app/7- gce/env",PATH="/opt/app/7-gce/env/bin",\
 HOME="/home/pythonapp",USER="pythonapp" stdout_logfile=syslog stderr_logfile=syslog
EOF
supervisorctl reread
supervisorctl update

我还编辑了文件 config.py:

    import os
    SECRET_KEY = 'secret'
    DATA_BACKEND = 'datastore'
    PROJECT_ID = 'soy-channel-319506'
    CLOUDSQL_USER = 'root'
    CLOUDSQL_PASSWORD = '1111'
    CLOUDSQL_DATABASE = 'bookshelf'(created)
    CLOUDSQL_CONNECTION_NAME = 'soy-channel-319506:us- 
    central1:bookshelfmysql'
    CLOUD_STORAGE_BUCKET = 'bookshelfstorage1'(created)
    MAX_CONTENT_LENGTH = 8 * 1024 * 1024
    ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])`

【问题讨论】:

  • 请在您的问题中添加更多详细信息,说明您在做什么,以便我们提供帮助。书架应用程序包含多项服务,包括 App Engine 和 Firestore。我不清楚你为什么使用supervisord。该错误表明正在使用的凭据未经授权。代码可能在 VM 的服务帐户下运行,如果是这样,默认情况下它没有权限,例如写入 Firestore。但是,目前还不清楚您要做什么以及您是如何做到的,因此很难回答
  • 感谢您的回答,刚刚编辑告诉我实际想要什么
  • 您说您遇到了一个错误,但在此消息中您没有告诉我们错误是什么,也没有告诉我们您在哪里得到它。你认为这可能没有必要吗?
  • 你的脚本一团糟。我试图通过在我认为他们去的地方插入换行符来解开它,但你应该检查一下。
  • 感谢蒂姆的回答。我的脚本直接来自cloud.google.com/python/docs/getting-started/…

标签: python google-cloud-platform grpc


【解决方案1】:

好的,我已经成功了。 Bookshelf 示例使用 Firestore,不使用 Cloud SQL。配置令人困惑 ./gce./bookshelf 等,但以下对我有用。

startup-script.sh:

# Install Stackdriver logging agent
curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
sudo bash install-logging-agent.sh

# Install or update needed software
apt-get update
apt-get install -yq git supervisor python3-pip python3-venv

# Account to own server process
useradd -m -d /home/pythonapp pythonapp

# Fetch source code
export HOME=/root
git clone https://github.com/GoogleCloudPlatform/getting-started-python.git /opt/app

# Python environment setup
python3 -m venv /opt/app/gce/env
source /opt/app/gce/env/bin/activate

# `grpcio` requires upgraded `pip`/`setuptools`
/opt/app/gce/env/bin/python3 -m pip install --upgrade pip
/opt/app/gce/env/bin/python3 -m pip install --upgrade setuptools
/opt/app/gce/env/bin/python3 -m pip install honcho

# Add `requirements.txt` from `bookshelf`
/opt/app/gce/env/bin/python3 -m pip install --requirement /opt/app/bookshelf/requirements.txt

# Move supervisor conf and procfile
cp /opt/app/gce/python-app.conf /opt/app/bookshelf
cp /opt/app/gce/procfile /opt/app/bookshelf

# Edit python-app.conf to reflect `bookshelf` directory
sed --in-place 's|directory=/opt/app/gce|directory=opt/app/bookshelf|g' /opt/app/bookshelf/python-app.conf

# Set ownership to newly created account
chown -R pythonapp:pythonapp /opt/app

# Put supervisor configuration in proper place
cp /opt/app/bookshelf/python-app.conf /etc/supervisor/conf.d/python-app.conf

# Start service via supervisorctl
supervisorctl reread
supervisorctl update

对于 GCP:

BILLING=...
PROJECT=...
REGION=...   # Firestore location
ZONE=...     # VM location
INSTANCE=... # VM instance name
ACCOUNT=...  # Service Account name
FIREWALL=... # Firewall rule name

# Enable services
for SERVICE in "appengine" "compute" "datastore" "firestore"
do
  gcloud services enable ${SERVICE}.googleapis.com \
  --project=${PROJECT}
done

# Or Compute Engine default
gcloud iam service-accounts create ${ACCOUNT} \
--display-name="Python Bookshelf VM" \
--description="Used by Python Bookshelf VM to access DB" \
--project=${PROJECT}

EMAIL=${ACCOUNT}@${PROJECT}.iam.gserviceaccount.com

# Minimally logging, datastore.user
for ROLE in "roles/compute.imageUser" "roles/datastore.user" "roles/logging.logWriter"
do
  gcloud projects add-iam-policy-binding ${PROJECT} \
  --member=serviceAccount:${EMAIL} \
  --role=${ROLE}
done

# Create Firestore
gcloud app create \
--region=${REGION} \
--project=${PROJECT}

gcloud firestore databases create \
--region=${REGION} \
--project=${PROJECT}

# Create VM w/ `startup-script.sh`
gcloud beta compute instances create ${INSTANCE} \
--machine-type=f1-micro \
--image-family=debian-10 \
--image-project=debian-cloud \
--metadata-from-file=startup-script=${PWD}/startup-script.sh \
--service-account=${EMAIL} \
--scopes=userinfo-email,cloud-platform \
--tags=http-server \
--zone=${ZONE} \
--project=${PROJECT}

# Check status
# Either
gcloud compute instances get-serial-port-output ${INSTANCE} \
--zone=${ZONE} \
--project=${PROJECT}

# Or
gcloud compute ssh ${INSTANCE} \
--zone=${ZONE} \
--project=${PROJECT}

# And
sudo journalctl \
--unit=google-startup-scripts.service \
--follow

# Configure Firewall
gcloud compute firewall-rules create ${FIREWALL} \
--allow=tcp:8080 \
--description="Allow port 8080 access to VMs tagged http-server" \
--source-ranges=0.0.0.0/0 \
--target-tags=http-server \
--project=${PROJECT}

然后:

IP=$(\
  gcloud compute instances describe ${INSTANCE} \
  --zone=${ZONE} \
  --project=${PROJECT} \
  --format="value(networkInterfaces[0].accessConfigs[0].natIP)")

curl http://${IP}:8080

【讨论】:

  • 非常感谢您的出色工作和帮助,让我试试,但我确实需要使用 cloud sql 运行“steps”分支
  • 我没有时间重新配置 Kubernetes 示例,以便它可以直接在具有 Cloud SQL 的 VM 上运行。你的config.py 应该使用cloudsql 而不是datastore。您需要配置 VM 的服务帐户以允许 Cloud SQL 访问,并且您还需要使用 Google 的 Cloud SQL 代理将您的 VM 连接到您的实例。祝你好运!
猜你喜欢
  • 2022-10-31
  • 2019-04-02
  • 2017-07-15
  • 2012-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 2014-07-18
相关资源
最近更新 更多