如何在Silicon Cloud Kubernetes上使用Knative运行无服务器工作负载
作者选择将开放互联网/言论自由基金作为“为捐助而写作”计划的受捐机构。
简介
Kubernetes是一个功能强大的容器编排工具,它允许您部署和管理容器化应用程序,但有时需要花费时间来管理底层基础设施。无服务器范式帮助用户在不必担心底层基础设施的情况下部署应用程序。随着无服务器2.0的出现,许多平台和工具现在允许您在Kubernetes上部署无服务器应用程序。
Knative是一个基于Kubernetes的平台,提供部署和管理无服务器工作负载的组件。Knative提供开源的Kubernetes集成、云无关性、构建块和可扩展性。像红帽的Openshift等工具也使用Knative,使用户可以在Kubernetes上部署他们的无服务器工作负载。
Knative拥有两个主要组件:Eventing和Serving。Eventing用于管理触发无服务器工作负载的事件。Serving是一组组件,用于部署和管理无服务器工作负载。Knative Serving使开发人员能够在Kubernetes上部署和管理无服务器应用程序。借助Knative Serving,开发人员可以快速轻松地部署新服务,按需扩缩容,并将其连接到其他服务和事件源。该功能使开发人员能够构建和部署具有灵活性、可扩展性和易于维护的现代云原生应用程序。
在这个教程中,您将使用Knative Serving在Silicon Cloud Kubernetes集群上以无服务器方式部署一个Node.js应用程序。您将使用doctl(Silicon Cloud CLI)和kn(Knative CLI)来创建Kubernetes集群并部署应用程序。
先决条件
完成这个教程,您需要以下物品:
- A Silicon Cloud account that you will use to start a Kubernetes cluster with at least 4GB RAM and 2 CPU cores. If you do not have one, sign up for a new account. This setup will use a Silicon Cloud Kubernetes cluster. To create a Kubernetes cluster in the Silicon Cloud Cloud Panel, see our Kubernetes Quickstart.
- To follow this tutorial from a remote server, you can set up an Ubuntu 22.04 server with a non-root user and a firewall by following our Initial Server Setup. To get started with a Silicon Cloud vServer, you can use our vServer Quickstart.
- The Silicon Cloud command-line client, doctl, installed on your machine. The GitHub Download option is recommended. See How To Use doctl for more information on using doctl.
- kubectl installed on your machine, which you can set up with the Kubernetes installation docs.
- Docker installed on your machine, which you can set up by following Steps 1 and 2 of our tutorial on How To Install and Use Docker.
- A sample Node.js application set up by following the How To Build a Node.js Application with Docker tutorial for creating the application and pushing its container image to Docker Hub.
- An account at Docker Hub for storing Docker images you’ll create during this tutorial.
第一步 – 启动Silicon Cloud Kubernetes集群
由于Knative是基于Kubernetes的平台,你将在Silicon Cloud上的Kubernetes集群中使用它。有多种方法可以在Silicon Cloud上启动Kubernetes集群,你可以使用Silicon Cloud云界面、Silicon Cloud命令行界面或者Terraform提供程序。
在这个教程中,您将使用doctl,即Silicon Cloud的命令行客户端,来启动Kubernetes集群。如果您还没有安装doctl,请按照官方安装指南的步骤进行安装。
为了在本教程中有效地使用Knative,您需要一个具有至少4GB RAM和2个 CPU 内核可用的 Kubernetes 集群。您可以通过使用以下标志运行 doctl 命令来启动一个名为 knative-tutorial 的集群,以满足这些规格要求。
- –size specifies the size of the remote server.
- –count specifies the number of nodes that will be created as part of the cluster.
要创建Silicon Cloud Kubernetes集群,请运行以下命令:
- doctl kubernetes cluster create knative-tutorial –size s-2vcpu-4gb –count 3
在这个命令中,你创建一个名为knative-tutorial的集群,大小设置为4GB内存和2个CPU核心,并且节点数为3个。
Note
这个命令需要几分钟完成。一旦完成,您将收到类似以下消息的通知:
Notice: Cluster is provisioning, waiting for cluster to be running ………………………………………………….. Notice: Cluster created, fetching credentials Notice: Adding cluster credentials to kubeconfig file found in “/home/sammy/.kube/config” Notice: Setting current-context to do-nyc1-knative-tutorial ID Name Region Version Auto Upgrade Status Node Pools d2d1f9bc-114b-45e7-b109-104137f9ab62 knative-tutorial nyc1 1.24.4-do.0 false running knative-tutorial-default-pool
集群现在可以使用了。
您现在可以使用以下命令验证您的系统是否已设置好kubectl,并且是否能够连接到Silicon Cloud Kubernetes集群。
- kubectl cluster-info
你应该获取到类似的输出。
Kubernetes control plane is running at https://69de217e-0284-4e18-a6d7-5606915a4e88.k8s.ondigitalocean.com CoreDNS is running at https://69de217e-0284-4e18-a6d7-5606915a4e88.k8s.ondigitalocean.com/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
由于输出中列出了控制平面和CoreDNS服务的URL(在输出块中突出显示),所以您知道kubectl在您的系统中已正确配置,并且可以访问集群。
作为群集创建过程的一部分,doctl会自动配置kubectl上下文以使用新的群集。您可以通过运行以下命令来验证这一点。
- kubectl config current-context
这个命令将返回当前上下文的名称。
你应该收到以下的输出结果。
do-nyc1-knative-tutorial
输出表明当前的上下文是do-nyc1-knative-tutorial,它是您在区域(nyc1)中创建的集群的名称。
您还可以使用此命令验证集群是否运行,并且节点已准备好接受工作负载。
- kubectl get nodes
kubectl get nodes命令会列出集群中所有节点的状态以及其他信息。
你应该收到以下的输出结果。
NAME STATUS ROLES AGE VERSION do-nyc1-knative-tutorial-159783000-0v9k5 Ready <none> 2m52s v1.24.4 do-nyc1-knative-tutorial-159783000-1h4qj Ready <none> 2m52s v1.24.4 do-nyc1-knative-tutorial-159783000-1q9qf Ready <none> 2m52s v1.24.4
输出显示集群有三个节点准备接受工作负载。
在这一步中,您在Silicon Cloud上启动了一个Kubernetes集群。现在您可以安装Knative,在Kubernetes上部署您的无服务器工作负载。
步骤2 — 安装Knative服务
在这一步中,您将在您的Kubernetes集群上安装Knative Serving。Knative Serving负责部署和管理您的无服务器工作负载。
要安装Knative Serving,您需要Knative核心组件和自定义资源。运行以下命令来安装核心组件:
- kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.8.0/serving-crds.yaml
- kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.8.0/serving-core.yaml
kubectl apply命令在您的集群上安装Knative核心组件和自定义资源。-f标志指定包含配置更改的文件。在这种情况下,配置更改位于您从Knative存储库下载的YAML文件中。
这些命令将花费几分钟来完成。你将收到以下输出(以下输出被截断以保持简洁)。
customresourcedefinition.apiextensions.k8s.io/certificates.networking.internal.knative.dev created customresourcedefinition.apiextensions.k8s.io/clusteringresses.networking.internal.knative.dev created customresourcedefinition.apiextensions.k8s.io/configurations.serving.knative.dev created customresourcedefinition.apiextensions.k8s.io/images.caching.internal.knative.dev created customresourcedefinition.apiextensions.k8s.io/ingresses.networking.internal.knative.dev created …
此输出表明您的集群上存在Knative核心组件和自定义资源。所有组件都位于knative-serving命名空间中。
一旦下载完成,您可以验证Knative Serving是否已安装。
- kubectl get pods –namespace knative-serving
通过执行kubectl get pods命令,可以检索位于命名空间knative-serving中的集群中所有已启动的pod列表。该命令会识别集群中的pod、它们当前的状态、每个pod中容器的数量以及特定命名空间中容器的名称。
你应该会得到一个类似的输出。
NAME READY STATUS RESTARTS AGE activator-5f6b4bf5c8-kfxrv 1/1 Running 0 4m37s autoscaler-bc7d6c9c9-v5jqt 1/1 Running 0 4m34s controller-687d88ff56-9g4gz 1/1 Running 0 4m32s domain-mapping-69cc86d8d5-kr57g 1/1 Running 0 4m29s domainmapping-webhook-65dfdd9b96-nzs9c 1/1 Running 0 4m27s net-kourier-controller-55c99987b4-hkfpl 1/1 Running 0 3m49s webhook-587cdd8dd7-qbb9b 1/1 Running 0 4m22s
输出显示了在 knative-serving 命名空间中运行的所有 pod。这些 pod 负责 Knative Serving 的不同组件。
Knative要求一个网络层来路由传入的流量到你的服务。Knative中的网络层可以在分布式环境中部署和通讯微服务。Knative Serving支持使用Istio、Contour和Kourier作为网络层。
在这个教程中,你将使用Kourier作为网络层,因为它能够与Knative无缝集成。Kourier使用与Knative生态系统的其他部分相同的API和标准,对于已经使用Knative并希望从其强大的网络能力中获益的开发者和组织来说,这是一个很好的选择。
使用以下命令安装Kourier:
- kubectl apply -f https://github.com/knative/net-kourier/releases/download/knative-v1.8.0/kourier.yaml
你应该收到以下输出:
namespace/kourier-system configured configmap/kourier-bootstrap configured configmap/config-kourier configured serviceaccount/net-kourier configured clusterrole.rbac.authorization.k8s.io/net-kourier configured clusterrolebinding.rbac.authorization.k8s.io/net-kourier configured deployment.apps/net-kourier-controller configured service/net-kourier-controller configured deployment.apps/3scale-kourier-gateway configured service/kourier configured service/kourier-internal configured
输出列出了作为Kubernetes集群中Kourier安装过程的一部分创建的所有资源,例如命名空间和配置地图。
要将Knative配置为使用Kourier作为网络层,您需要编辑config-network的ConfigMap。
为此,您需要使用kubectl patch命令来更新Kubernetes集群中的对象字段。您还需要在此命令中加入一些标志。
- –namespace specifies where you can find the object you want to patch.
- –type specifies which patch to perform when applying configs with the patch command. The available types are json, merge, and strategic.
- –patch specifies the patch data directly on the command line rather than in a file.
使用相应的标志运行此命令。
- kubectl patch configmap/config-network \
- –namespace knative-serving \
- –type merge \
- –patch ‘{“data”:{“ingress-class”:”kourier.ingress.networking.knative.dev”}}’
使用kubectl patch命令,将configmap/config-network的命名空间设置为knative-serving,并将类型设置为merge。
合并补丁类型允许更有针对性的更新,而json或策略补丁类型则允许更全面的更新。合并补丁类型指定要更新的个别字段,而不包括在补丁命令中的整个资源配置,这与其他类型不同。要修补的数据可以通过补丁标志进行标识。
你应该接收到以下输出:
configmap/config-network patched
该命令的输出确保在集群中正确设置了Kourier。
最后,使用以下命令获取Kourier负载均衡器的外部IP地址。
- kubectl get svc kourier –namespace kourier-system
kubectl get svc命令用于检索运行在Kubernetes集群中指定命名空间(在这种情况下是kourier-system)中的服务信息。该命令将列出集群中所有服务及其关联的IP地址和端口号。
你应该收到以下输出:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kourier LoadBalancer 10.245.186.153 138.197.231.61 80:31245/TCP,443:30762/TCP 2m33s
该命令的输出包括服务名称、服务类型(如ClusterIP、NodePort等)、集群IP地址和端口号以及外部IP地址和端口号。这里列出的数字仅为示例,所以您的输出会显示不同的数字。
负载均衡器的配置可能需要几分钟的时间。在 EXTERNAL-IP 字段中,您可能会看到一个空值或 。如果是这种情况,请等待几分钟然后重新运行命令。
Note
您还可以配置域名的DNS指向负载均衡器的外部IP地址。Knative提供了一个名为default-domain的Kubernetes作业,它会自动配置Knative服务将sslip.io作为默认的DNS后缀。
sslip.io是一个动态DNS(域名系统)服务,允许用户使用自定义域名访问他们的设备,而不是使用IP地址。使用sslip.io可以提高用户远程访问设备的便利性,无需记住复杂的IP地址。
要配置默认的 DNS 后缀,您需要运行以下命令:
- kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.8.0/serving-default-domain.yaml
你将会收到以下的输出结果。
job.batch/default-domain configured service/default-domain-service configured
成功配置了运行 Magic DNS 服务所需的资源。
Note
您现在已成功在Kubernetes集群上安装了Knative Serving。您现在可以使用Knative Serving在Kubernetes集群上部署无服务器工作负载。
步骤三 — 部署无服务器工作负载
在这一步中,您将在当前运行在Kubernetes集群中的Knative之上部署一个无服务器工作负载。您将使用之前准备工作中创建的Node.js应用程序。
在继续之前,您需要创建一个新的命名空间来部署无服务器工作负载。您可以通过运行以下命令来完成此操作:
- kubectl create namespace serverless-workload
这个命令将创建一个名为serverless-workload的新命名空间。
你应该收到以下的输出:
你應該收到以下的輸出:
namespace/serverless-workload configured
输出结果确认命名空间已成功创建。
Knative Serving 使用一个名为 Service 的自定义资源来部署和管理无服务器工作负载。该 Service 资源由 Knative Serving API 定义。
一旦创建或修改服务资源,Knative Serving将自动创建一个新的修订版本。修订版本是您工作负载的时间点快照。
每当创建新的版本时,流量将通过路由传递到新的版本。Knative Serving会自动为每个服务创建一个路由。您可以使用路由的域名访问您的工作负载。
要在Knative上部署无服务器工作负载,您必须创建一个服务资源。您可以通过两种不同的方式实现这一点:
- Using the kn, the official Knative CLI tool.
- Using the kubectl command line tool for applying YAML files to your Kubernetes cluster.
在接下来的子章节中,您将使用每一种方法。
选项1——使用Knative CLI进行操作。
Knative 的命令行界面(CLI) kn 允许您与 Knative 进行交互。
首先,通过下载最新版本的Knative CLI二进制文件来安装kn。
- wget https://github.com/knative/client/releases/download/knative-v1.8.1/kn-linux-amd64
wget命令将会获取该工具。
然后,将二进制文件移动到名为kn的文件夹中。
- mv kn-linux-amd64 kn
接下来,使用以下命令将其设为可执行。
- chmod +x kn
最后,将可执行二进制文件移动到您的路径上的目录中。
- cp kn /usr/local/bin/
确认kn是否已安装。
- kn version
你应该得到一个类似的输出。
Version: v1.8.1 Build Date: 2022-10-20 16:09:37 Git Revision: 1db36698 Supported APIs: * Serving – serving.knative.dev/v1 (knative-serving v1.8.0) * Eventing – sources.knative.dev/v1 (knative-eventing v1.8.0) – eventing.knative.dev/v1 (knative-eventing v1.8.0)
运行该命令后的输出显示kn已被安装。
要使用 kn 部署 Node.js 应用程序,你需要使用 kn 服务创建命令。你需要将一些标志与该命令一起使用。
- –image specifies the image of the container you want to deploy.
- –port specifies the port that your application listens on.
- –name specifies the name of the Service you want to create.
- –namespace specifies the namespace to which you want to deploy the workload.
要部署您的 Node.js 应用程序,请运行以下命令,并将其中“DockerHub 用户名”的部分更改为您自己的用户名:
- kn service create node-service \
- –image your_dockerhub_username/nodejs-image-demo \
- –port 8080 \
- –namespace serverless-workload
使用kn服务命令创建了名为node-service的Knative服务,端口设置为8080,并将命名空间标志设置为serverless-workload。image标志指示了上传到Dockerhub中的应用容器的位置。
你应该收到以下输出:
Creating service ‘node-service’ in namespace ‘serverless-workload’: 0.236s Configuration “node-service” is waiting for a Revision to become ready. 2.230s … 2.311s Ingress has not yet been reconciled. 2.456s Waiting for load balancer to be ready 2.575s Ready to serve. Service ‘node-service’ created to latest revision ‘node-service-00001’ is available at URL: http://node-service.serverless-workload.138.197.231.61.sslip.io
这个输出提供了Knative服务创建的状态。一旦服务创建完成,您将找到与服务链接的路由的URL。
运行下面的命令来验证 Service 资源是否已经创建:
- kn service list –namespace serverless-workload
kn服务列表命令会列出Knative Serving在特定命名空间中当前部署的所有服务。此命令可以让您访问每个服务的详细信息,包括名称、状态和URL。
你應該會得到一個相似的輸出。
NAME URL LATEST AGE CONDITIONS READY REASON node-service http://node-service.serverless-workload.138.197.231.61.sslip.io node-service-00001 88s 3 OK / 3 True
从这个输出中,你可以通过之前执行的 kn service 命令验证已经创建了一个新的 Knative 服务。你还可以找到路由的 URL ,以及服务的年龄和状态。
在这一部分中,你安装了Knative CLI并使用它部署了一个无服务器工作负载,用于运行你的Node.js应用程序。
选项2 – 使用YAML文件创建服务资源
您还可以通过创建一个YAML文件来部署Service资源。这种方法对于确保您的工作负载的版本控制非常有用。
首先,在包含Dockerfile的目录中创建一个子目录。本教程中将使用名称为knative的子目录。创建该文件夹。
- mkdir knative
接下来,您将在 knative 目录下创建一个名为 service.yaml 的 YAML 文件。
- nano knative/service.yaml
在新创建的service.yaml文件中,添加以下行来定义一个Service,以部署你的Node.js应用程序:
~/node_project/knative/service.yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: node-yaml-service
namespace: serverless-workload
spec:
template:
metadata:
name: node-yaml-service-v1
spec:
containers:
- image: docker.io/your_dockerhub_username/nodejs-image-demo
ports:
- containerPort: 8080
Knative 服务的 YAML 文件指定了以下信息:
- name in the first metadata section specifies the name of the Service resource.
- namespace specifies the namespace to which you want to deploy the workload.
- name in the first spec section specifies the name of the Revision.
- image in the second spec section specifies the image of the container you want to deploy.
- containerPort specifies the port your application listens on.
请确保使用您选择的应用和系统的信息以及您的DockerHub用户名更新已标记文本。
保存并关闭文件。
您可以通过运行以下命令现在部署服务资源:
- kubectl apply -f knative/service.yaml
你应该收到以下的输出:
service.serving.knative.dev/node-yaml-service created
输出显示Service资源已成功创建。
运行以下命令以验证已创建了Service资源:
- kn service list –namespace serverless-workload
kn service list命令将列出特定命名空间中当前部署的所有Knative Serving服务。该命令允许您访问每个服务的详细信息。
你应该收到以下的输出:
NAME URL LATEST AGE CONDITIONS READY REASON node-service http://node-service.serverless-workload.174.138.127.211.sslip.io node-service-00001 9d 3 OK / 3 True node-yaml-service http://node-yaml-service.serverless-workload.174.138.127.211.sslip.io node-yaml-service-v1 9d 3 OK / 3 True
您可以验证之前执行的Knative Service YAML文件已创建了一个新的Knative Service。在本节中,您创建了一个名为node-yaml-service的服务。您还可以找到Knative Route的URL。
在此部分中,您使用了一个YAML文件来为您的Node.js应用程序创建一个无服务器工作负载。
在这一步骤中,您使用kn CLI工具和一个YAML文件创建了Knative Service资源。接下来,您将访问使用Knative部署的应用程序负载。
步骤四——访问应用工作负载
现在您已经部署了无服务器工作负载,可以通过Knative Route创建的URL访问它。Knative Route定义了如何将传入的HTTP流量路由到特定的服务或应用程序。
要获取所有Knative路由的列表,请运行以下命令:
- kn route list –namespace serverless-workload
Kn路由列表命令会将Kubernetes集群中特定命名空间下的所有Kn路由列出来。
您应该会得到一个类似的输出。
NAME URL READY node-service http://node-service.serverless-workload.138.197.231.61.sslip.io True node-yaml-service http://node-yaml-service.serverless-workload.138.197.231.61.sslip.io True
您将使用由Knative路由生成的URL来确认一切是否按预期工作。
在浏览器中打开其中一个URL。当您在浏览器中访问该网站时,将加载您的Node应用程序的登陆页面。

你已经成功地在你的Kubernetes集群上使用Knative部署了一个无服务器的工作负载。
结论 (jié
在本教程中,您使用Knative部署了一个无服务器工作负载。您使用kn CLI工具和YAML文件创建了一个Knative Service资源。该资源在您的Kubernetes集群上部署了一个Node.js应用程序,您可以通过路由URL访问该应用程序。
想要了解更多Knative提供的功能,比如Pod的自动缩放、逐步将流量发布到修订版本以及事件组件等,请访问官方Knative文档。
要继续使用Silicon Cloud Kubernetes (DOKS)进行构建,请参考我们的Kubernetes使用说明文档。您还可以了解有关DOKS的更多信息,例如功能和可用性。如果遇到DOKS故障排除的情况,您可以参考我们的Kubernetes支持指南。