在Google Cloud上运行容器化应用程序并与之通信

Cloud Computing has completely revolutionized the way we interact with the internet. Today, it’s impractical to maintain personal machines to make your app or website reach the general public around the world. Rather, all you need is write your code, deploy it on the cloud and let it handle everything else for you!

ç响亮计算已经完全与互联网彻底改变了我们的方式进行交互。 如今,维护个人计算机以使您的应用程序或网站覆盖全世界的人们已经不切实际。 相反,您所需要做的就是编写代码,将其部署在云上,然后让它为您处理其他所有事情!

There are numerous cloud providers on the internet today which completely take off the load from your shoulders regarding scaling, maintenance and performance of your applications, so that you as a developer can focus more on what your application does.

如今,互联网上有许多云提供商,这些提供商完全可以减轻应用程序的扩展,维护和性能方面的负担,因此您作为开发人员可以将更多精力放在应用程序的工作上。

I’ve been learning Docker for the past few days and trust me, this is one of the best skills you can develop as it’s an excellent set of platform as a service products that uses OS-level virtualization to deliver software in packages. Here’s what it means:

我过去几天一直在学习Docker,请相信我,这是您可以开发的最佳技能之一,因为它是一组出色的平台即服务产品,使用OS级虚拟化来交付软件包。 这是什么意思:

  • You set up the working environment

    您设置了工作环境
  • Add your dependencies

    添加您的依赖
  • Write your code

    写你的代码
  • Put everything in a box, and ship it to the world! 😀

    把所有东西都放在一个盒子里,然后运送到世界上! 😀
Image for post

To build a real world application, you need to communicate with the cloud to get a complex job done, and get the processed result back to you. Simply put, consider a black box on the cloud which accepts an input from your customers and sends them customized responses based on a logic you wrote. Your backend code is the black box — You make the black box.

要构建真实的应用程序,您需要与云进行通信以完成复杂的工作,并将处理后的结果返回给您。 简而言之,请考虑一下云上的黑盒子,该黑盒子接受客户的输入,并根据您编写的逻辑向他们发送定制的响应。 您的后端代码是黑匣子-您制造了黑匣子。

I’ve been exploring different options on Google Cloud to seamlessly deploy my docker containers on the cloud and communicate with my application whenever required. I explored, and explored, and stumbled upon two beautiful serverless offerings by Google Cloud — App Engine Flex and Cloud Run. In this article, I’ll talk about my observations and step by step guide to deploy your first containerized application on the cloud and communicate with your app. Trust me, it’s super easy once you get the hang of it, and you can migrate all your complex tasks to the Cloud.

我一直在探索Google Cloud上的其他选项,以将我的Docker容器无缝部署在云上,并在需要时与我的应用程序进行通信。 我探索,探索并偶然发现Google Cloud的两个漂亮的无服务器产品-App Engine FlexCloud Run 。 在本文中,我将讨论我的观察和逐步指南,以将您的第一个容器化应用程序部署到云上并与您的应用程序进行通信。 相信我,一旦掌握了它,这将非常容易,并且您可以将所有复杂的任务迁移到云中。

Image for post
Source: https://cloud.google.com/
来源: https//cloud.google.com/

I’ll be demonstrating how to build and depoy a containerized application that accepts a request from the client and sends a suitable response. You can extend it further to develop your REST APIs with microservices running in the cloud.

我将演示如何构建和部署容器化应用程序,该应用程序接受来自客户端的请求并发送适当的响应。 您可以进一步扩展它,以使用云中运行的微服务开发REST API。

To start with the sample examples, you need to write a small Flask app in Python, Create a Dockerfile, and provide a list of dependencies to the Dockerfile. Here are the prerequisite files that you must keep handy:

从示例示例开始,您需要使用Python编写一个小的Flask应用程序,创建一个Dockerfile,并提供对Dockerfile的依赖关系列表。 这是您必须方便使用的必备文件:

先决条件: (Prerequisites:)

  1. Here’s a sample Flask App to handle GET and POST requests separately:

    这是一个示例Flask App,可分别处理GET和POST请求:

from flask import Flask,request


app = Flask(__name__)


@app.route('/', methods=['POST', 'GET'])
def respond_with_result():
    if request.method == 'POST':


        return "Hey there, this is a POST request!"


    else:
        return "Hey there, this is a GET request"




if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0',
            port=int(os.environ.get(
                     'PORT', 8080)))

2. Here’s the Dockerfile content:

2.这是Dockerfile的内容:

FROM python:3.8-slim


COPY . ./


RUN pip install -r requirements.txt


CMD gunicorn --bind :$PORT app:app

3. Dependency file:

3.依赖文件:

Flask
gunicorn
setuptools

Okay, let’s get started!

好的,让我们开始吧!

App Engine: (App Engine:)

Image for post
Source: https://www.todaysoftmag.com/
资料来源: https : //www.todaysoftmag.com/

Google App Engine is a stable and renowned, highly scalable and serverless service which allows you to run your backend code on the cloud. It can handle a humongous amount of requests seamlessly and scale your application as and when required.

Google App Engine是一项稳定而著名的,高度可扩展的无服务器服务,可让您在云上运行后端代码。 它可以无缝处理大量请求,并在需要时扩展您的应用程序。

It is also possible to run your containerized applications inside app engine using App Engine Flex(Flexible). Let’s see how do we do that in the next steps:

也可以使用App Engine Flex (Flexible)在App Engine中运行容器化的应用程序。 让我们看看如何在下一步中做到这一点:

1 Go to your cloud developer console: https://console.cloud.google.com/. Create a new project.

1转到您的云开发人员控制台: https : //console.cloud.google.com/ 。 创建一个新项目。

2 Open Cloud shell and create a new folder. cd inside the folder.

2打开Cloud Shell并创建一个新文件夹。 文件夹中的cd

3 Copy all your project files created above to this directory.

3将上面创建的所有项目文件复制到此目录。

Image for post

4 Create a new file- app.yaml. This file tells GAE about the configuration of your application.

4创建一个新文件app.yaml 。 该文件告诉GAE您的应用程序的配置。

The runtime: custom entry tells App Engine to look for a Dockerfile that will define your runtime’s image

运行时:自定义条目告诉App Engine查找将定义运行时映像的Dockerfile

The env: flex specifies that you are deploying to the flexible environment.

env:flex指定您要部署到灵活环境。

Here’s what your file content should look like:

文件内容应如下所示:

runtime: custom
env: flex

5 Set your project-id as the project property on the cloud shell if not already set:gcloud config set project <project-id>

5如果尚未设置,请在云外壳上将project-id设置为project属性: gcloud config set project <project-id>

6 Run the following command to create your app:gcloud app create

6运行以下命令来创建您的应用程序: gcloud app create

7 Deploy your app:gcloud app deploy

7部署您的应用程序: gcloud app deploy

Done! Open your app using the URL provided by console.

做完了! 使用控制台提供的URL打开您的应用程序。

Image for post
Your application on the browser
您在浏览器上的应用程序

One major drawback in App Engine is that even if your app is not getting any requests, you’re paying for the running instance. So, make sure to disable your test applications after you’re done to avoid any charges.

App Engine的一个主要缺点是,即使您的应用程序未收到任何请求,您仍需为运行中的实例付费。 因此,请确保在完成操作后禁用测试应用程序,以避免产生任何费用。

云跑 (Cloud Run)

App Engine, however would not be suitable for everyone as you pay for the alive instances even if no one is using your application.This brings us to the second elegant service: Cloud Run

但是,即使您没有人在使用您的应用程序,您也需要为活动实例付费,因此App Engine并不适合所有人。这使我们获得了第二种优雅的服务: Cloud Run

Image for post
https://towardsdatascience.com/) https://towardsdatascience.com/ )上的示例项目

Cloud Run is a serverless compute platform that enables you to run stateless containers invocable via HTTP requests.You can deploy your containerized applications handling HTTP requests/responses via docker images on Google Container Registry.Cloud Run is an excellent serverless service to avoid costs as it charges you only on the basis of requests. Unlike App Engine it won’t keep charging you for running instances.

Cloud Run是一种无服务器计算平台,可让您运行可通过HTTP请求调用的无状态容器。您可以在Google Container Registry上通过Docker映像部署处理HTTP请求/响应的容器化应用程序.Cloud Run是一项出色的无服务器服务,可避免成本仅根据请求向您收费。 与App Engine不同,它不会继续向您收取运行实例的费用。

Here are the steps to run your docker image on Cloud Run:

以下是在Cloud Run上运行Docker映像的步骤:

1 Go to your cloud developer console: https://console.cloud.google.com/. Create a new project.

1转到您的云开发人员控制台: https : //console.cloud.google.com/ 。 创建一个新项目。

2 Open Cloud shell and create a new folder. cd inside the folder.

2打开Cloud Shell并创建一个新文件夹。 文件夹中的cd

3 Copy all your project files created above to this directory.

3将上面创建的所有项目文件复制到此目录。

Image for post

4 Okay, here comes the interesting part. Build your image by using the Dockerfile you created. The image will be stored on Google Container registry with the following command:

4好的,这是有趣的部分。 使用您创建的Dockerfile构建映像。 图像将使用以下命令存储在Google Container注册表中:

gcloud builds submit --tag gcr.io/<project_name>/<DEFINE_YOUR_DOCKER_IMAGE_NAME>

gcloud builds submit --tag gcr.io/<project_name>/<DEFINE_YOUR_DOCKER_IMAGE_NAME>

Image for post

If everything goes well, head over to the Container Registry, you’ll find your newly created Docker image:

如果一切顺利,请转到Container Registry ,您将找到新创建的Docker映像:

Image for post
Container Registry
集装箱登记处

5 Great, we’re ready to publish our application to the internet so the whole world can use it:

5太好了,我们已经准备好将我们的应用程序发布到Internet上,以便全世界都可以使用它:

Deploy your image to the internet:

将您的映像部署到Internet:

gcloud run deploy <DEFINE_A_SERVICE_NAME> --image gcr.io/<project_name>/<DEFINE_YOUR_DOCKER_IMAGE_NAME> --platform managed --region us-central1 --allow-unauthenticated

gcloud run deploy <DEFINE_A_SERVICE_NAME> --image gcr.io/<project_name>/<DEFINE_YOUR_DOCKER_IMAGE_NAME> --platform managed --region us-central1 --allow-unauthenticated

Image for post
Successfully deployed the application
成功部署应用程序

Perfect, now head over to the URL provided by the console to see if your app works:

完美,现在转到控制台提供的URL,以查看您的应用程序是否正常运行:

Image for post
Your application on the browser
您在浏览器上的应用程序

Awesome, you’re done.

太好了,您完成了。

You can now customize your flask application to run any code you want, route any type of request accordingly, and send an appropriate response to the client as well.

现在,您可以自定义flask应用程序,以运行所需的任何代码,相应地路由任何类型的请求,以及向客户端发送适当的响应。

Although Cloud Run is highly scalable, As of July 2020, the biggest limitation of Cloud run is that it provides just 2GB of memory. However, based on Google’s public doc — Choosing a Cloud Run platform, the 2 GiB hard limit will not apply to Cloud Run for Anthos since the underlying is a GKE(Google Kubernetes Engine) Cluster, so you will be able to use even GPUs for your containers.

尽管Cloud Run具有高度可扩展性,但截至2020年7月, Cloud Run的最大局限性在于它仅提供2GB的内存。 但是,基于Google的公共文档- 选择Cloud Run平台 ,2 GiB硬限制将不适用于Anthos的Cloud Run,因为底层是GKE(Google Kubernetes Engine)集群,因此您甚至可以将GPU用于您的容器。

结论 (Conclusion)

Google App Engine and Google Cloud Run are two great services to host your application and make it available to your customers without any worries of Infrastructure management or maintenance of your packaged applications. Both have their own pros and cons as discussed above, so feel free to decide what fits best for your use case.

Google App EngineGoogle Cloud Run是两项出色的服务,可托管您的应用程序,并使您的客户可以使用它,而无需担心基础结构管理或打包应用程序的维护。 两者都有各自的优缺点,因此,请随时决定最适合您的用例的情况。

Hope you enjoyed the article. Have a great day! 😊

希望您喜欢这篇文章。 祝你有美好的一天! 😊

翻译自: https://medium.com/@iamadarshkumar/running-and-communicating-with-containerized-applications-on-google-cloud-9d3834e45c54

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值