《OpenShift 4.x HOL教程汇总》
说明:本文已经在OpenShift 4.13
典型创建Knative服务的方法
一般我们可以使用Knative的客户端(kn)将一个Container Image发布成一个Serverless的服务,例如:
$ kn service create hello-world --image=openshift/hello-openshift
OpenShift将Souce-2-Image功能和Kn功能打通了,这样在部署应用的时候可以直接选择以Knative的Serverless方式部署应用。下面我们说明如何直接将Web应用当成Serverless运行。
将Web应用部署为Knative Service
这种部署Web应用的方式需要在OpenShift环境中提前安装好OpenShift Serverless Operator,并部署Knative Serving环境。
- 登录OpenShift控制台,然后进入Developer视图。
- 先创建一个名为knative-webapp的项目。
- 然后点击左侧菜单的“+Add”,然后进入“From Git”。
- 按下图在Git Repo URL中填写“https://github.com/sclorg/cakephp-ex.git”,确认在Builder区域选中“PHP”。
- 为资源类型选择“无服务器部署”,然后点击最下面的Create。
- 界面跳转到Topology,会看到Web应用构建和部署进度。最后部署完可以看到当前部署了1个Pod,稍等一段时间,部署会减到0个Pod。
- 访问应用图标中的Open URL ,可以看到部署的Pod会先增加到1个,然后Web应用界面才出来。
- 点击步骤 6 图的“设置流量分配”,会看到下面的对话框。由于我们只部署了一个Revision,所以无法在“流量分配”中增加Revision,因此关闭对话框即可。
- 执行以下命令,可以基于现有的Image更新 Knative的cakephp-ex-git服务,会生成名为 ‘cakephp-ex-git-00002’ 的revision。
$ kn service update cakephp-ex-git --image image-registry.openshift-image-registry.svc:5000/knative-webapp/cakephp-ex-git
Updating Service 'cakephp-ex-git' in namespace 'serverless':
0.059s The Configuration is still working to reflect the latest desired specification.
5.134s Traffic is not yet migrated to the latest revision.
5.179s Ingress has not yet been reconciled.
5.235s Waiting for load balancer to be ready
5.391s Ready to serve.
Service 'cakephp-ex-git' updated to latest revision 'cakephp-ex-git-00002' is available at URL:
https://cakephp-ex-git-knative-webapp.apps.cluster-229bt.229bt.sandbox886.opentlc.com
- 可以再次通过 “设置流量分配”,将 ‘cakephp-ex-git-00001’ 和 ‘cakephp-ex-git-00002’ 都设为 “50”。
- 调整后可在拓扑中看到结果。
- 回到Administration视图,在Serverless中可以分别看到Service、Revisions、Routes。