《OpenShift 4.x HOL教程汇总》
说明:本文已经在OpenShift 4.6环境中验证
文章目录
- 说明
- 客户端环境
- 配置Knative环境
-
- 安装OpenShift Serverless Operator
- 用控制台创建Knative Serving应用运行环境
- 用脚本创建Knative Serving应用运行环境
- 部署并测试Hello的Serverless应用
说明
OpenShift Serverless Operator为无服务器应用提供提供了Knative Serving和Knative Eventing的运行环境。只需要以下步骤就可以在OpenShift上运行Knative应用:安装所需的Operator和配置Knative Serving运行环境。
客户端环境
从以下地址下载对应环境的OpenShift Client和Knative Client,然后将oc和kn配置到PATH中。
- https://mirror.openshift.com/pub/openshift-v4/clients/oc/4.6/
- https://mirror.openshift.com/pub/openshift-v4/clients/serverless/latest/
执行命令下载以下客户端程序,它们会在后面操作中使用到。
$ curl -Lo stern https://github.com/wercker/stern/releases/download/1.9.0/stern_linux_amd64
$ curl -Lo hey https://hey-release.s3.us-east-2.amazonaws.com/hey_linux_amd64
$ curl -Lo yq https://github.com/mikefarah/yq/releases/download/3.2.1/yq_linux_amd64
$ chmod +x stern hey yq
$ sudo mv stern hey yq /usr/local/bin
配置Knative环境
我们可以用OpenShift控制台或OpenShift命令行创建Knative及其所需的运行环境。
安装OpenShift Serverless Operator
注意:通过OpenShift Console安装OpenShift Serverless Operator时候,OpenShift会自动安装OpenShift Serverless Operator所依赖的Service Mesh Opeator,因此Operator的安装过程非常简单。
- 用集群管理员登录OpenShift Console。
- 在Administrator视图中进入Operators->OperatorHub,然后找到“Red Hat OpenShift Serverless”,点击进入。
- 在右滑窗口中接受所有缺省配置,然后在“Install Operator”页面中接受默认选项,再点击Install。。
- 完后上一步后,可以在Operators->Installed Operators中看到安装好的Red Hat OpenShift Serverless。此时就完成了OpenShift Serverless Operator安装。
OpenShift 4 - Knative-Tutorial(1) 创建Serverless运行环境说明客户端环境配置Knative环境部署并测试Hello的Serverless应用
用控制台创建Knative Serving应用运行环境
Knative Serving提供了Serverless应用的支撑运行环境。Knative Serving必须安装在knative-serving项目中。
- 创建knative-serving项目。
$ oc new-project knative-serving
- 切换到knative-serving项目,再次进入Operators->Installed Operators,然后进入OpenShift Serverless的配置。
- 在Overview页面中点击Knative Serving区域的Create Instance链接。
OpenShift 4 - Knative-Tutorial(1) 创建Serverless运行环境说明客户端环境配置Knative环境部署并测试Hello的Serverless应用 - 在Create Knative Serving界面中接受缺省配置,然后点击Create。
- 然后进入名为knative-serving的Knative Serving配置。需要等一段时间,直到下图中5个状态全部为True,此时就成功创建了Knative Serving环境。
OpenShift 4 - Knative-Tutorial(1) 创建Serverless运行环境说明客户端环境配置Knative环境部署并测试Hello的Serverless应用
用脚本创建Knative Serving应用运行环境
- 创建knative-serving.yaml文件,内容为以下部分:
apiVersion: v1
kind: Namespace
metadata:
name: knative-serving
---
apiVersion: operator.knative.dev/v1alpha1
kind: KnativeServing
metadata:
name: knative-serving
namespace: knative-serving
- 执行命令创建项目和KnativeServing资源
$ oc apply -f knative-serving.yaml
- 查看knative-serving的状态,成功完成后显示以下内容。
$ oc get knativeserving.operator.knative.dev/knative-serving -n knative-serving --template='{{range .status.conditions}}{{printf "%s =%s\n" .type .status}}{{end}}'
DependenciesInstalled =True
DeploymentsAvailable =True
InstallSucceeded =True
Ready =True
VersionMigrationEligible =True
部署并测试Hello的Serverless应用
- 创建knative-demo项目。
$ oc new-project knative-demo
- 部署Knative的Hello应用。其中“–env TARGET=Knative”是将字符串传给TARGET环境变量。
$ kn service create hello --image gcr.io/knative-samples/helloworld-go --env TARGET=Knative
Creating service 'hello' in namespace 'knative-demo':
0.296s The Route is still working to reflect the latest desired specification.
0.393s Configuration "hello" is waiting for a Revision to become ready.
33.138s ...
33.369s Ingress has not yet been reconciled.
33.466s Configuration "hello" is waiting for a Revision to become ready.
33.543s Ingress has not yet been reconciled.
74.629s Ready to serve.
Service 'hello' created with latest revision 'hello-nvhqs-1' and URL:
http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
- 查看Knative的Service列表。
$ kn service list
NAME URL LATEST AGE CONDITIONS READY REASON
hello http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com hello-nvhqs-1 2m4s 3 OK / 3 True
4. 查看knative-demo项目中的所有资源,其中包括Knative的configuration、service、revision、route对象,还有OpenShift的service、deployment、rs对象。**注意**:当前项目中还没有pod资源。
$ oc get all -n knative-demo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hello ExternalName <none> cluster-local-gateway.knative-serving-ingress.svc.cluster.local <none> 10m
service/hello-nvhqs-1 ClusterIP 172.30.69.130 <none> 80/TCP 10m
service/hello-nvhqs-1-private ClusterIP 172.30.125.146 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 10m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/hello-nvhqs-1-deployment 0/0 0 0 10m
NAME DESIRED CURRENT READY AGE
replicaset.apps/hello-nvhqs-1-deployment-66db57b95f 0 0 0 10m
NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON
revision.serving.knative.dev/hello-nvhqs-1 hello hello-nvhqs-1 1 True
NAME LATESTCREATED LATESTREADY READY REASON
configuration.serving.knative.dev/hello hello-nvhqs-1 hello-nvhqs-1 True
NAME URL READY REASON
route.serving.knative.dev/hello http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com True
NAME URL LATESTCREATED LATESTREADY READY REASON
service.serving.knative.dev/hello http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com hello-nvhqs-1 hello-nvhqs-1 True
- 在OpenShift Console中进入knative-demo的Developer视图,可以看到和hello应用相关的资源。
OpenShift 4 - Knative-Tutorial(1) 创建Serverless运行环境说明客户端环境配置Knative环境部署并测试Hello的Serverless应用 - 访问hello应用的地址,确认可以返回“Hello Knative!”,其中“Knative”为传进去的环境变量。
$ curl $(kn route list | grep hello | awk 'NR==1 {print $2}')
Hello Knative!
- 此时立即执行以下命令,确认此时可以查看项目在运行的Pod资源。不过在一分钟以后会又看不到Pod了。
$ oc get pod
NAME READY STATUS RESTARTS AGE
pod/hello-rfgt4-1-deployment-a0cd0125-cfvny 2/2 Running 0 22s
- 更新名为hello的service的环境变量内容。
$ kn service update hello --env TARGET=Kn
Updating Service 'hello' in namespace 'knative-demo':
10.760s Traffic is not yet migrated to the latest revision.
10.871s Ingress has not yet been reconciled.
12.231s Ready to serve.
Service 'hello' updated with latest revision 'hello-cfvny-2' and URL:
http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
- 访问hello应用的地址,确认可以返回结果已经变化。
$ curl $(kn route list | grep hello | awk 'NR==1 {print $2}')
Hello Kn!
- 查看项目所有资源,确认OpenShift的deployment、rs对象和Knative的revision对象都已经有了一个新版的配置。
$ oc get all
NAME READY STATUS RESTARTS AGE
pod/hello-lnvkj-2-deployment-58fcc9d677-7kr9s 2/2 Running 0 48s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/hello ExternalName <none> kourier-internal.knative-serving-ingress.svc.cluster.local 80/TCP 10m
service/hello-lnvkj-2 ClusterIP 172.30.20.162 <none> 80/TCP 48s
service/hello-lnvkj-2-private ClusterIP 172.30.234.104 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 48s
service/hello-rddql-1 ClusterIP 172.30.123.87 <none> 80/TCP 10m
service/hello-rddql-1-private ClusterIP 172.30.5.1 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 10m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/hello-lnvkj-2-deployment 1/1 1 1 48s
deployment.apps/hello-rddql-1-deployment 0/0 0 0 10m
NAME DESIRED CURRENT READY AGE
replicaset.apps/hello-lnvkj-2-deployment-58fcc9d677 1 1 1 48s
replicaset.apps/hello-rddql-1-deployment-6479596766 0 0 0 10m
NAME LATESTCREATED LATESTREADY READY REASON
configuration.serving.knative.dev/hello hello-lnvkj-2 hello-lnvkj-2 True
NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON
revision.serving.knative.dev/hello-lnvkj-2 hello hello-lnvkj-2 2 True
revision.serving.knative.dev/hello-rddql-1 hello hello-rddql-1 1 True
NAME URL LATESTCREATED LATESTREADY READY REASON
service.serving.knative.dev/hello http://hello-knative-demo.apps.cluster-pek-e4bf.pek-e4bf.sandbox1216.opentlc.com hello-lnvkj-2 hello-lnvkj-2 True
NAME URL READY REASON
route.serving.knative.dev/hello http://hello-knative-demo.apps.cluster-pek-e4bf.pek-e4bf.sandbox1216.opentlc.com True
- 查看名为hello的service详细信息。确认最新的revision(hello-cfvny-2)接收了100%的请求。
$ kn service describe hello
Name: hello
Namespace: knative-demo
Age: 5h
URL: http://hello-knative-demo.apps.cluster-pek-e4bf.pek-e4bf.sandbox1216.opentlc.com
Revisions:
100% @latest (hello-lnvkj-2) [2] (2m)
Image: gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
Conditions:
OK TYPE AGE REASON
++ Ready 2m
++ ConfigurationsReady 2m
++ RoutesReady 2m
- 查看Knative的revision对象。确认结果和(10)看到的项目包括的revision一样。
$ kn revision list
NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON
hello-lnvkj-2 hello 100% 2 2m16s 3 OK / 4 True
hello-rddql-1 hello 1 12m 3 OK / 4 True
- 执行命令,将请求平均发给以上2个revision。其中“hello-rddql-1”是GENERATION=1的revision名称。
$ kn service update hello --traffic @latest=50 --traffic hello-rddql-1=50
Updating Service 'hello' in namespace 'knative-demo':
0.066s The Route is still working to reflect the latest desired specification.
0.090s Ingress has not yet been reconciled.
1.450s Ready to serve.
Service 'hello' updated with latest revision 'hello-lnvkj-2' (unchanged) and URL:
http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
- 查看Knative的service对象,确认配置修改为2个revision各自接收50%的请求。
$ kn service describe hello
Name: hello
Namespace: knative-demo
Age: 5h
URL: http://hello.knative-demo.apps.cluster-shanghai-3d3e.shanghai-3d3e.example.opentlc.com
Revisions:
50% @latest (hello-lnvkj-2) [2] (12m)
Image: gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
50% hello-rddql-1 [1] (21m)
Image: gcr.io/knative-samples/helloworld-go (pinned to 5ea96b)
Conditions:
OK TYPE AGE REASON
++ Ready 6m
++ ConfigurationsReady 41m
++ RoutesReady 6m
- 在OpenShift控制台的Developer视图中可以上可以看到hello应用的Toplogy。
OpenShift 4 - Knative-Tutorial(1) 创建Serverless运行环境说明客户端环境配置Knative环境部署并测试Hello的Serverless应用 - 点击上图的Set Traffic Distribution按钮,可以在弹出窗口中可以为不同的Revision分配流量比例。
OpenShift 4 - Knative-Tutorial(1) 创建Serverless运行环境说明客户端环境配置Knative环境部署并测试Hello的Serverless应用 - 多次访问hello应用,确认返回结果部分是“Hello Knative!”、部分是“Hello Kn!”。
$ curl $(kn route list | grep hello | awk 'NR==1 {print $2}')
Hello Knative!
$ curl $(kn route list | grep hello | awk 'NR==1 {print $2}')
Hello Kn!