GCP-分别在子域和域上部署API和前端 [英] GCP - Deploy API and frontend on subdomain and domain respectively

查看:95
本文介绍了GCP-分别在子域和域上部署API和前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我创建一个Google App Engine并将其部署在API和前端上的自定义域(我们将其称为:mysite.ms)上. API是用nodejsExpress编写的,前端是React应用程序.这是我用于部署的app.yml文件:

To get started, I create an Google App Engine where I deploy on my custom domain (which we will refer to as: mysite.ms) both the API and the frontend. The API are written in nodejs with Express ant the frontend is a React application. This is my app.yml file that I use for the deploy:

runtime: nodejs
env: flex

manual_scaling:
  instances: 1

resources:
  cpu: .5
  memory_gb: 0.5
  disk_size_gb: 10

handlers:
  - url: /
    static_files: www/build/index.html
    upload: www/build/index.html

  - url: /
    static_dir: www/build

现在,我要分割元素.在mysite.ms域上仅部署React应用程序,在子域sub.mysite.ms上部署API.由于该域是在freenom上接管的,因此要创建一个子域,我添加一个新的DNS类型为CNAME,其值为sub.mysite.ms并定位到原始域mysite.ms.

Now, what I want is to separte the element. On the mysite.ms domain deploy only the React application and on a subdomain sub.mysite.ms the API. Since the domain was taken over on freenom, to create a subdomain I add a new DNS of type CNAME with value sub.mysite.ms and target the original domain mysite.ms.

是否可以仅使用Google App Engine和单个app.yml文件来创建这些单独的部署,还是需要使用其他工具来分离文件?

Is it possible to create these separate deployments using only the Google App Engine and a single app.yml file or do you need to use some other tool and separate the files?

您如何建议我继续?由于我在网上找不到任何清晰的信息,您能给我一些解决这些问题的提示吗?

How do you advise me to proceed? Since I can't find anything clear online, could you give me some tips to solve these problems?

我已经阅读了您提供给我的文档,对此我有些怀疑.首先,如何创建不同的服务?因为我创建了这个(但很可能是错误的)dispatch.yml:

I have read the documentation that you provided me and I some doubts regarding it. First of all, how can I create different services? Because I create this (but most probably wrong) dispatch.yml:

dispatch:
  - url: "mysite.ms/*"
    service: default

  - url: "sub.mysite.ms/*"
    service: api

但是当我使用此命令gcloud app deploy dispatch.yaml进行部署时,出现错误,因为它找不到模块'api'. 在上一个版本中,在我的server.js中,我有这段代码可以处理React:

but when I deploy with this command gcloud app deploy dispatch.yaml, I get an error because it can't find the module 'api'. In the previus version, in my server.js I have this code to handle the React:

app.use(express.static(path.resolve(__dirname, 'www', 'build')));
app.get('*', (req, res) => { res.sendFile(path.resolve(__dirname, 'www', 'build', 'index.html')); });

  1. 即使将前端和api分配在不同的域上,我也应该保留这两行代码吗?

  1. Should I keep these two lines of code even if I split the frontend and the api on different domain?

我应该将sub.mysite.ms添加到Google App Engine部分中的自定义域区域吗?

Shoud I add the sub.mysite.ms to the custom domain area in the section on Google App Engine?

即使我有dispath.yaml,我也应该保留文件app.yml吗?

Should I keep the file app.yml even if I have the dispath.yaml?

推荐答案

目前,使用同一yaml文件不能部署多个服务.假设您可能要部署两个服务:apifrontend.假设您希望frontend服务是默认服务,因此每次每个人访问mysite.ms时,他们都会看到frontend服务.

For now, it is not possible to deploy more than one service using the same yaml file. Let's suppose you may want to deploy two services: api and frontend. Let's say you want the frontend service to be default one so everytime everybody access to mysite.ms, they will see the frontend service.

假设您具有frontend服务的以下app.yaml文件:

Let's say you have the app.yaml file as follows for the frontend service:

runtime: nodejs
env: flex

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

如您所见,

app.yaml中没有service属性.在app.yaml文件参考文档您将看到以下内容:

as you can notice, there is no the service property in your app.yaml. In the app.yaml file reference doc you will see the following:

service: service_name

如果创建服务,则为必需.对于默认服务是可选的.每个服务和每个版本都必须有一个名称.名称可以包含数字,字母和连字符.在灵活的环境中,服务和版本的总长度不能超过48个字符,并且不能以连字符开头或结尾.为每个服务和每个版本选择唯一的名称.不要在服务和版本之间重复使用名称.

Required if creating a service. Optional for the default service. Each service and each version must have a name. A name can contain numbers, letters, and hyphens. In the flexible environment, the combined length of service and version cannot be longer than 48 characters and cannot start or end with a hyphen. Choose a unique name for each service and each version. Don't reuse names between services and versions.

由于没有service属性,因此部署将设置为default服务.现在,假设您还有另一个yaml文件,尤其是api.yaml文件,用于部署api服务.这是一个示例:

Because there is not the service property, the deploy will be set to the default service. Now let's say you have another yaml file, in particular the api.yaml to deploy the api service. Here's an example:

runtime: nodejs
env: flex
service: api

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

您将看到我已经添加了service属性,并且当您使用gcloud app deploy api.yaml进行部署时,部署将创建服务api.

You will see that I've added the service property and when you deploy using gcloud app deploy api.yaml, the deploy will create the service api.

最后,在创建服务后,您将能够部署您创建的dispatch.yaml文件.

Finally, after creating the services you will be able to deploy the dispatch.yaml file you've created.

一些为您提供的建议:

  • app.yaml文件分配给默认服务是一种很好的做法.对于其他服务,您可能要根据要使用该文件进行部署的服务来命名文件,例如api.yamlbackend.yamlapi_external.yaml等.
  • 您可以使用gcloud app deploy path/to/service1 path/to/service2 path/to/service3 ...部署部署这两种服务,也可以单独进行部署以便更好地进行调试,以防出现某些问题.
  • 由于您使用的是Flex环境,因此handlers属性为 app.yaml
  • Is a good practice to assign the app.yaml file to the default service. For the other services, you may want to name the files according to the service to deploy with such file i.e. api.yaml, backend.yaml, api_external.yaml, etc.
  • You can deploy deploy both services using gcloud app deploy path/to/service1 path/to/service2 path/to/service3 ... or you can do it individually for better debugging in case there could be some issues.
  • Since you're using the Flex environment, the handlers property are not supported. If you add them, those are ignored.
  • Check the right documents for the environment you use. app.yaml, dispatch.yaml, general docs.

这篇关于GCP-分别在子域和域上部署API和前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆