无服务器 科特林 with OpenFaaS

无服务器 科特林 on OpenFaaS

用 this 文章, my goal is to demonstrate how 无服务器 科特林 can look like 通过 introducing you to one of the coolest 无服务器 平台s: OpenFaaS. OpenFaaS is an open-source, community-owned project that you may use to run your 功能s and microservices on any public 要么 private 云. You can run your Docker image on OpenFaaS, which runs and scales it for you. As a result, you are free to choose any programming language as long as it can be packaged into a Docker image. Throughout this post, we want to learn about the concepts behind 无服务器 and 服务功能 (FaaS), and how we can deploy 无服务器 科特林 功能s to OpenFaaS.

无服务器 and 服务功能

无服务器 Computing

无服务器 Computing,我们描述了一种云模型,在这种模型中,开发人员不必解决服务器管理和基础架构的决策,而由云提供商自己来照顾。期限"Serverless" describes the fact that we don't have to care much about infrastructure setup, scaling, and maintenance and that we can focus on developing code which can easily be deployed into production. The 无服务器 opportunities seem endless, and so does the landscape map published 通过 the Cloud Native Computing Foundation (CNCF), which you can find 这里.

无服务器景观

无服务器 architecture is said to be the next big thing and somewhat the advancement of microservices: Monolith -> Microservices -> 无服务器 architecture

服务功能

One of the most essential 无服务器 offerings are so-called 计算运行时, also known as 功能 as a service (FaaS) 平台s. Many vendors provide these 平台s, with the most prominent ones being AWS Lambda, Google Cloud 职能, and Microsoft Azure 职能. There are also open-source alternatives, such as Apache OpenWhisk 要么 甲骨文的Fn项目。存在更多工具和平台,它们也是上面显示的景观图的一部分。
FaaS的基本思想是提供一个平台,该平台可用于执行某些事件触发的代码。可以在不维护基础结构的情况下部署代码,而只需将功能上载到云中即可部署代码,这将负责执行和扩展功能。"Functions"就FaaS而言,它们是相当小的单位,通常应该是无状态的,因此可以轻松地水平缩放。 FaaS平台不仅会在代码负载沉重的情况下扩展您的代码,而且还会在一段时间内未多次调用该函数时照顾删除实例。对于初学者来说,这项技术有助于优化成本,但也需要了解 冷启动 situations. 无服务器 overall has relevant positive characteristics and should be part of our discussions around reliable architecture alternatives. If you want to learn a bit more about the concepts and what 无服务器 architecture entails, I recommend watching "无服务器:软件架构的未来" 通过 Peter Sbarski. To be clear, I personally don't believe that you should go 无服务器 no matter what, but rather see it as a valid concept that might solve parts of your problems.

OpenFaaS - Containers as 职能

OpenFaaS,如其网站上所述,"使转弯变得简单 任何东西 into a 无服务器 功能 that runs on Linux 要么 Windows through Docker Swarm 要么 Kubernetes"。他们保证它可以让我们运行 任何规模,任何地方的任何代码。您可以将OpenFaaS描述为"容器功能"平台,因为其抽象功能的形式是Docker映像。该特性是一个出色的特性,因为它允许我们将任何代码打包到Docker映像中,并且OpenFaaS运行它,对其进行缩放并为我们提供指标。老实说,值得一提的是,需要将特定工具添加到您的容器中,这称为 功能看门狗,一个微型Golang HTTP服务器,可将您的功能与外界联系起来。 OpenFaaS本身可以在例如Kubernetes上运行,或者Docker Swarm是MIT许可下的开源软件,并且使用Go编写。您可以找到GitHub项目 这里.

没有供应商锁定

OpenFaaS依赖于用于打包代码的Docker映像,该工具本身可在Kubernetes等平台上运行。社区已经采用了所有这些技术,并且知道如何使用它们。因此,您可以将OpenFaaS实例在任何公共或私有云之间来回移动而不会出现问题。 OpenFaaS不会使您依赖特定的供应商,这与依赖AWS Lambda之类的技术时所得到的相反。依赖于特定供应商的事实称为 供应商锁定.

其他类似项目

将独立容器部署到计算引擎的想法可以在其他几个项目中找到。您可以找到多个类似工具的比较 这里.

无服务器 科特林 deployed on OpenFaaS

Now that we've learned about the general ideas behind 无服务器, FaaS and what OpenFaaS does, we want to get our hands a bit dirty 通过 setting up an OpenFaaS Kubernetes cluster and learning how we can deploy 功能s to it.

使用Kubernetes的OpenFaaS部署

以下基于官方 指南。阅读以获取更多信息并了解替代方法。

第一步,您需要确保设置了Kubernetes集群。如果您想在本地机器上运行Kubernetes,各种工具可以帮助您设置集群(例如 k3s 要么 迷你库)。在Mac和Windows上,您还可以使用Docker的桌面版本在本地运行Kubernetes集群。
设置Kubernetes之后,我们可以开始将OpenFaaS部署到集群。使用起来非常简单 k3sup

# Install k3sup
curl -sLS //get.k3sup.dev | sh
sudo install k3sup /usr/local/bin/ # <- this step might not be necessary
chmod +x k3sup
# applies Openfaas namespaces, create user, applies helm chart
k3sup app install Openfaas

之后,检查您的群集,您会发现几个正在运行的OpenFaaS Pod。

您应该能够通过以下方式查看网关用户界面 http://127.0.0.1:31112/ui/ 使用k3sup生成的凭据登录。您可以通过kubectl获取密码:

PASSWORD=$(kubectl get secret -n Openfaas basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode; echo)

To allow Faas-cli (needs to be installed) to access your newly deployed services, make sure to
1) set OPENFAAS_URL via export OPENFAAS_URL=http://127.0.0.1:31112
2)通过登录 Faas-cli login --password {YOUR_PASSWORD_HERE}

以下脚本将自动登录:

PASSWORD=$(kubectl get secret -n Openfaas basic-auth -o jsonpath="{.data.basic-auth-password}" | base64 --decode; echo)
echo -n $PASSWORD | Faas-cli login --username admin --password-stdin

That's it - We're all set and can start deploying 无服务器 功能s. 🎉

创建和部署OpenFaaS功能

OpenFaaS提供了用于将功能部署到平台的不同方法和界面。我们可能会使用CLI,网关UI或提供的REST API,这些文档通过 这个Swagger Yaml.

范本

The most straightforward way for getting started is via OpenFaaS' 模板 engine. 用 the Faas-cli, we can create new 功能s based on available 模板s existing for various programming languages and tools. To get a list of all existing 模板s, you should run Faas-cli 模板 pull first. Running Faas-cli new --list now, you should see a list of 模板s similar to the following:

csharp csharp-armhf dockerfile dockerfile-armhf go go-armhf java12 java8 node node-arm64 node-armhf node12 php7 python python-armhf python3 python3-armhf ruby

可通过OpenFaaS获得更多模板 商店, which you can examine 通过 running Faas-cli 模板 store list. Neither of both sources currently contains a 科特林模板, which I would love to change. Therefore, a 改变请求 正在等待反馈,并希望将一些官方Kotlin添加到OpenFaaS中。
On the bright side, it is quite easy to add my 科特林模板s to the cli 通过 executing Faas-cli 模板 pull //github.com/s1monw1/openfaas-kotlin. You should find two additional 科特林 variants now via Faas-cli new --list. Let's create a classic 科特林 功能 using the 科特林 模板:

Faas-cli new hello-readers --lang=kotlin
# 功能 can be found in ./hello-readers

按照惯例,OpenFaaS仅向用户公开每个模板的特定部分。如果您想进一步研究,请查看 科特林模板 and see how it is structured. The 功能 is the one we have to deal with after running the Faas-cli new command mentioned above.

修改模板

In the newly generated 功能 hello-readers, we can find a source file Handler.kt, which is the spot where we can implement the HTTP handling for our 功能. It's a simple request-response mapper, and to demo it, we simply change the default body to "您好kotlinexpertise读者".

class Handler : IHandler {
    override fun handle(request: IRequest): IResponse {
        return Response(body = "您好kotlinexpertise读者")
    }
}

构建和部署

建立功能

Next to the hello-readers folder, OpenFaaS should have generated a YAML file called hello-readers.yml. It contains the information the cli needs to build and deploy our 功能. You can learn more about the relevant YAML structure and how you may want to modify it 这里。让我们来构建它。

# 通过  default, OpenFaaS looks for a stack.yml file which can be adjusted using the -f flag
faas-cli build -f hello-readers.yml 

OpenFaaS now runs the Dockerfile contained in the 模板 (not visible to the user) to build the image which we can then deploy. There's also a Faas-cli up command available that wraps the build, push and deploy commands for our convenience. Either way, whether you use Faas-cli up 要么 Faas-cli deploy, the result looks similar to the following output.

成功部署

> Faas-cli deploy -f hello-readers.yml
Deploying: hello-readers.
WARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.

Deployed. 202 接受ed.
URL: http://127.0.0.1:31112/function/hello-readers

单击该URL将显示我们先前配置的消息:"您好kotlinexpertise读者"。您还可以通过网关UI检查已部署的功能。

Why would I use 科特林 to develop 无服务器?

In the previous section, we saw that we could choose from a wide variety of programming languages to write 无服务器 功能s for OpenFaaS. What language works best for you and makes the most sense depends on the use case and what you're trying to achieve. Common 无服务器 languages are, e.g., Node, Python, and Go, but you'll also find C#, Java, and even more in use.
公司中有限的编程语言集可能会导致更好的内部工具,也减轻了由于语言障碍而无法轻松切换团队的问题。我不喜欢用不同的语言编写成百上千的服务,这在某些时候会成为问题。大多数大型科技公司都会限制其语言产品组合,并且仅在情况需要时才添加新的语言组合。
I personally have been using 科特林 for almost four years now, and it was clear that I would also use it to explore the 无服务器 space. We at 布赖特 have started using 科特林 无服务器 功能s not too long ago, and it's working out well so far. Most of our backend services run 科特林, and it's the language we're most experienced with. Nevertheless, we know that it might become necessary to add more languages, e.g., for performance reasons. The JVM footprint cannot be ignored and might impact performance 这里 and there, for instance, when it comes to 冷启动s. To tweak things a bit, Graal VM could be a good option.
在上一篇文章中,我写了关于 服务器即功能工具箱http4k, which is a fantastic library to enable 无服务器 功能s in 科特林. Http4k backs the 功能s we use at 布赖特, and I also proposed a corresponding 模板 适用于OpenFaaS商店。

无服务器 tooling for 科特林

一个相当新的框架,名为 t (Kotlin 无服务器) is currently being developed 通过 Jetbrains with the goal of making it easy to turn 科特林 applications into ready-to-deploy 功能s and even handle the deployment which currently only works with AWS Lambda. The framework has a few very interesting features and I'm looking forward to see where t is heading.

摘要和监视

无服务器方法和相关的体系结构含义是相关的,在考虑采用现代软件体系结构的方法时应注意一些事项。作为无服务器相关主题的服务即服务功能已经存在了一段时间。亚马逊,谷歌和微软等大型云计算提供商提供了最杰出的工具。不过,我们还可以选择选择更多独立的平台来规避供应商锁定,例如我在本文中向您介绍的平台:OpenFaaS。我们看到,设置OpenFaaS并不是火箭科学,它可以在您的本地计算机上运行,​​这是入门的绝佳方法。您可以从大量的编程语言中进行选择,只需要确保您的代码可以打包为Docker映像即可将其部署到OpenFaaS。模板可以帮助快速设置功能,并且在必要时编写自己的功能模板也不太困难。我已经展示了Kotlin模板的示例,以及所产生的HTTP处理程序(表示函数)的外观。除了其他功能外,Kotlin是一个有效的替代方案,您应根据具体情况决定哪种编程语言最适合您的功能需求。正如我所提到的,我已经使用了一段时间的展示技术,目前正在研究一个真实的用例,以利用这些东西来建立一个动态的,高度可扩展的和可扩展的平台,我将在以后的文章中进一步讨论。文章。

关于2个想法“无服务器 科特林 with OpenFaaS

  • ken

    仅供参考,如果您确实要使用类似graalvm的工具’除非您拥有自己的服务器,否则请将任何JVM语言用于无服务器。它’成本是脚本语言成本的4倍,否则,请执行Go’t由于JVM启动时间…
    我在去年的Kotlin服务器框架KotlinConf会话中提到了这一点 ….

发表评论

您的电子邮件地址不会被公开。 必需的地方已做标记 *