【问题标题】:Google App Engine run command after deploy部署后 Google App Engine 运行命令
【发布时间】:2020-11-23 03:38:12
【问题描述】:

有没有办法在部署完成后自动在 docker 上运行 linux 命令列表,例如 yaml 文件上的生命周期(对 Kubernetes 有效)?

我不想通过 ssh 连接到实例并运行我的命令。 我需要安装ssh-client 和一些时间vim 和其他。

【问题讨论】:

    标签: google-app-engine deployment app.yaml


    【解决方案1】:

    App Engine 是无服务器解决方案。在产品description 中,您可能会发现:

    完全托管

    完全托管的环境让您可以专注于代码,同时 App Engine 管理基础架构问题。

    这意味着,如果您选择 App Engine,您就不必关心服务器。因此,作为设计,它适用于那些不想访问服务器,而是专注于将所有服务器维护留给 GCP 的代码的人。我认为主要功能是应用程序的自动缩放。

    我们不知道您打算做什么,您可以查看app.yaml reference 以查找所有可用功能。配置因您要使用的语言环境而异。

    如果您想访问环境,您应该使用 Kubernetes 解决方案甚至计算引擎。

    我希望它会有所帮助!

    【讨论】:

      【解决方案2】:

      对于那些正在寻找解决此问题的方法的人。
      App Engine 与 runtime: python 或 app.yaml 中的其他默认源,不会有太多的自定义。
      为了能够创建自己的构建,您必须使用 runtime: custom 并在同一目录(根目录)中添加 Dockerfile 文件。
      这是它的样子:

      app.yaml: 只有第一行改变。

          runtime: custom
      
          # the PROJECT-DIRECTORY is the one with settings.py and wsgi.py
          entrypoint: gunicorn -b :$PORT mysite.wsgi # specific to a GUnicorn HTTP server deployment
          env: flex # for Google Cloud Flexible App Engine
      
          # any environment variables you want to pass to your application.
          # accessible through os.environ['VARIABLE_NAME']
          env_variables:
          # the secret key used for the Django app (from PROJECT-DIRECTORY/settings.py)
            SECRET_KEY: 'lkfjop8Z8rXWbrtdVCwZ2fMWTDTCuETbvhaw3lhwqiepwsfghfhlrgldf'
            DEBUG: 'False' # always False for deployment
            DB_HOST: '/cloudsql/app-example:us-central1:example-postgres'
            DB_PORT: '5432' # PostgreSQL port
            DB_NAME: 'example-db'
            DB_USER: 'mysusername'   # either 'postgres' (default) or one you created on the PostgreSQL instance page
            DB_PASSWORD: 'sgvdsgbgjhrhytriuuyokkuuywrtwerwednHUQ'
      
            STATIC_URL: 'https://storage.googleapis.com/example/static/' # this is the url that you sync static files to
            
          automatic_scaling:
              min_num_instances: 1
              max_num_instances: 2
      
          handlers:
          - url: /static
            static_dir: static
      
          beta_settings:
            cloud_sql_instances: app-example:us-central1:example-postgres
      
          runtime_config:
            python_version: 3 # enter your Python version BASE ONLY here. Enter 2 for 2.7.9 or 3 for 3.6.4
      

      Dockerfile

          FROM gcr.io/google-appengine/python
      
      
          # Create a virtualenv for dependencies. This isolates these packages from
          # system-level packages.
          # Use -p python3 or -p python3.7 to select python version. Default is version 2.
          RUN virtualenv /env -p python3
      
      
          # Setting these environment variables are the same as running
          # source /env/bin/activate.
          ENV VIRTUAL_ENV /env
          ENV PATH /env/bin:$PATH
      
      
          # Install custom linux packages (python-qt4 for open-cv)
          RUN apt-get update -y && apt-get install vim python-qt4 ssh-client git -y
      
      
          # Add the application source code and install all dependencies into the virtualenv
          ADD . /app
          RUN pip install -r /app/requirements.txt
      
          # add my ssh key for github
          RUN mv /app/.ssh / && \
              chmod 600 /.ssh/*
      
      
      
          # Run a WSGI server to serve the application.
          EXPOSE 8080
      
          # gunicorn must be declared as a dependency in requirements.txt.
          WORKDIR /app
          CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 Blacks.wsgi:application --timeout 0 --preload && \
              python3 manage.py makemigrations && \
              python3 manage.py migrate
      

      【讨论】:

        【解决方案3】:

        另一个简单的解决方法是创建一个单独的 URL 处理程序来运行您的 shell 脚本。例如/migrate。部署后,您可以使用 curl 触发该 URL。

        请注意,在这种情况下,任何试图找到您后端的一些秘密 URL 的人都可能会找到它并触发它,只要他们愿意就可以。因此,如果您需要确保只有受信任的人才能触发它 - 您应该:

        • 想出比/migrate更多的秘密网址
        • 检查此视图中的权限(但在这种情况下,通过 curl 调用它会更加困难,因为您还需要传递一些身份验证数据)

        示例基本视图(使用python + django rest框架):

        from io import StringIO
        
        from django.core.management import call_command
        from rest_framework.renderers import StaticHTMLRenderer
        from rest_framework.response import Response
        from rest_framework.views import APIView
        
        
        class MigrateView(APIView):
            permission_classes = ()
            renderer_classes = [StaticHTMLRenderer]
        
            def post(self, request, *args, **kwargs):
                out = StringIO()
                # --no-color because ANSI symbols (used for colors)
                # render incorrectly in browser/curl
                call_command('migrate', '--no-color', stdout=out)
                return Response(out.getvalue())
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-19
          • 2021-04-04
          • 2014-08-03
          • 1970-01-01
          • 2017-05-17
          • 2023-02-15
          相关资源
          最近更新 更多