OpenShift 4.3 之Knative-Tutorial(7) Eventing之Broker和Trigger

文章目录

  • 概念
  • 操作说明
    • 部署Broker
    • 部署Service
    • 部署Event Source
    • 部署Trigger
    • 测试验证
      • 利用Event Source验证
      • 手动发请求验证
  • 参考

概念

Knative Eventing的Broker和Trigger对象可实现对事件进行过滤,事件的消费方可以利用这种机制只获取自己感兴趣的事件,而不是接收所有 Channel 中的事件信息。本文将向Broker发送两种不同的测试包文(报文头有区别),通过在两个Tigger中设置不同的Filter获取对应类型的Event,然后分别发给后台对应的Knative Serving的服务处理。
OpenShift 4.3 之Knative-Tutorial(7) Eventing之Broker和Trigger_第1张图片

操作说明

部署Broker

在Knative中当对一个项目打knative-eventing-injection=enabled标签,Knative Eventing就会为项目提供一个名为default的Broker。

  1. 执行命令为knativetutorial 项目打标签。
$ oc label namespace knativetutorial knative-eventing-injection=enabled
  1. 执行命令,查看生成的Broker、Pod和Service。可以看到名为default的Broker的访问地址是一个内部地址。
$ oc -nknativetutorial get broker
NAME      READY   REASON   URL                                                       AGE
default   True             http://default-broker.knativetutorial.svc.cluster.local   65m

$ oc -nknativetutorial get pod
NAME                                                              READY   STATUS    RESTARTS   AGE
default-broker-filter-b944cfc76-fn4tp                             1/1     Running   0          89m
default-broker-ingress-6f668f8b94-cfjm9                           1/1     Running   0          89m

$ oc -nknativetutorial get service
NAME                             TYPE           CLUSTER-IP       EXTERNAL-IP                                                  PORT(S)                             AGE
default-broker                   ClusterIP      172.30.106.246   <none>                                                       80/TCP,9090/TCP                     89m
default-broker-filter            ClusterIP      172.30.177.241   <none>                                                       80/TCP,9090/TCP                     89m
default-kne-trigger-kn-channel   ExternalName   <none>           imc-dispatcher.knative-eventing.svc.cluster.local            <none>                              89m

部署Service

  1. 创建eventing-aloha-sink.yaml和eventing-bonjour-sink.yaml文件,分别是以下内容:
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: eventingaloha
spec:
  template:
    metadata:
      name: eventingaloha-v1
      annotations:
        autoscaling.knative.dev/target: "1"
    spec:
      containers:
      - image: quay.io/burrsutter/eventinghello:0.0.1
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: eventingbonjour
spec:
  template:
    metadata:
      name: eventingbonjour-v1
      annotations:
        autoscaling.knative.dev/target: "1"
    spec:
      containers:
      - image: quay.io/burrsutter/eventinghello:0.0.1
  1. 执行命令,生成两个Service,然后查看多出来的和这两个Service相关的资源。
$ oc -n knativetutorial apply -f eventing-aloha-sink.yaml
$ oc -n knativetutorial apply -f eventing-bonjour-sink.yaml

$ oc --namespace knativetutorial get service
NAME                             TYPE           CLUSTER-IP       EXTERNAL-IP                                                  PORT(S)                             AGE
eventingaloha                    ExternalName   <none>           kourier-internal.knative-serving-ingress.svc.cluster.local   <none>                              55m
eventingaloha-v1                 ClusterIP      172.30.179.201   <none>                                                       80/TCP                              55m
eventingaloha-v1-private         ClusterIP      172.30.165.146   <none>                                                       80/TCP,9090/TCP,9091/TCP,8022/TCP   55m
eventingbonjour                  ExternalName   <none>           kourier-internal.knative-serving-ingress.svc.cluster.local   <none>                              54m
eventingbonjour-v1               ClusterIP      172.30.11.132    <none>                                                       80/TCP                              55m
eventingbonjour-v1-private       ClusterIP      172.30.64.59     <none>                                                       80/TCP,9090/TCP,9091/TCP,8022/TCP   55m

部署Event Source

  1. 创建event-source-broker.yaml文件,内容如下。它定义了名为my-cjs的CronJobSource,将消息发给名为default的Broker。
apiVersion: sources.eventing.knative.dev/v1alpha1
kind: CronJobSource
metadata:
  name: my-cjs
spec:
  schedule: "*/2 * * * *"
  data: '{"message": "From CronJob Source"}'
  sink:
   ref:
    apiVersion: eventing.knative.dev/v1alpha1
    kind: Broker
    name: default
  1. 执行命令,生成名为my-cjs的CronJobSource,然后查看生成的CronJobSource资源。
$ oc -n knativetutorial apply -f event-source-broker.yaml
$ oc -n knativetutorial get cronjobsources
NAME     READY   AGE
my-cjs   True    90s

部署Trigger

  1. 创建trigger-helloaloha.yaml和trigger-hellobonjour.yaml文件,文件内容分别如下。两个文件主要差别是Trigger的filter不同,分别可以接收"Ce-Type: aloha"和"Ce-Type: bonjour"的Header请求。
apiVersion: eventing.knative.dev/v1alpha1
kind: Trigger
metadata:
  name: helloaloha
spec:
  broker: default
  #filter:
  #  attributes:
  #    type: aloha
  subscriber:
    ref:
     apiVersion: serving.knative.dev/v1
     kind: Service
     name: eventingaloha
apiVersion: eventing.knative.dev/v1alpha1
kind: Trigger
metadata:
  name: hellobonjour
spec:
  broker: default
  #filter:
  #  attributes:
  #    type: bonjour
  subscriber:
    ref:
     apiVersion: serving.knative.dev/v1
     kind: Service
     name: eventingbonjour
  1. 执行命令,生成两个Trigger,然后查看生成的资源Trigger资源。
$ oc -n knativetutorial apply -f trigger-helloaloha.yaml
$ oc -n knativetutorial apply -f trigger-hellobonjour.yaml
$ oc -n knativetutorial get trigger
NAME           READY   REASON   BROKER    SUBSCRIBER_URI                                             AGE
helloaloha     True             default   http://eventingaloha.knativetutorial.svc.cluster.local     66m
hellobonjour   True             default   http://eventingbonjour.knativetutorial.svc.cluster.local   66m

测试验证

利用Event Source验证

由于名为my-cjs的CronJobSource会定时向Broker发请求,而且此时2个Trigger都没有Filter,因此2个Knative Serving的服务可同时接收到定时请求。
OpenShift 4.3 之Knative-Tutorial(7) Eventing之Broker和Trigger_第2张图片

手动发请求验证

  1. 编辑eventing-aloha-sink.yaml和eventing-bonjour-sink.yaml文件,去掉文件中的注释,以便给Trigger增加Filter。
  2. 然后执行以下命令,更新这2个Trigger对象。
$ oc -n knativetutorial apply -f trigger-helloaloha.yaml
$ oc -n knativetutorial apply -f trigger-hellobonjour.yaml
  1. 为了能通过内部地址访问broker,先创建curler.yaml文件,其内容如下。
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: curler
  name: curler
spec:
  containers:
  -- name: curler
    image: fedora:29 
    tty: true
  1. 根据curler.yaml文件创建curler容器,然后进入该容器。
$ oc -n knativetutorial apply -f curler.yaml
$ oc -n knativetutorial exec -it curler -- /bin/bash
  1. 等eventingaloha和eventingalohb的Knative Serving都变为0后,在curler 容器中执行命令,向broker发送Header带有"Ce-Type: aloha"的请求,然后确认和aloha相关的服务运行起来。
curl -v "http://default-broker.knativetutorial.svc.cluster.local" \
-X POST \
-H "Ce-Id: say-hello" \
-H "Ce-Specversion: 1.0" \
-H "Ce-Type: aloha" \
-H "Ce-Source: mycurl" \
-H "Content-Type: application/json" \
-d '{"key":"from a curl"}'

OpenShift 4.3 之Knative-Tutorial(7) Eventing之Broker和Trigger_第3张图片
4.等eventingaloha和eventingalohb的Knative Serving都变为0后,在curler 容器中执行命令,向broker发送Header带有"Ce-Type: bonjour"的请求,然后确认和bonjour相关的服务运行起来。

curl -v "http://default-broker.knativetutorial.svc.cluster.local" \
-X POST \
-H "Ce-Id: say-hello" \
-H "Ce-Specversion: 1.0" \
-H "Ce-Type: bonjour" \
-H "Ce-Source: mycurl" \
-H "Content-Type: application/json" \
-d '{"key":"from a curl"}'

OpenShift 4.3 之Knative-Tutorial(7) Eventing之Broker和Trigger_第4张图片

参考

  • https://github.com/RedHatWorkshops/knative-on-ocp4/blob/master/7.UsingBrokersAndTriggers.md

你可能感兴趣的:(Knative,OpenShift,4,Dev)