其他分享
首页 > 其他分享> > Knative的事件驱动:Knative在OpenShift上的实践3

Knative的事件驱动:Knative在OpenShift上的实践3

作者:互联网

一、Knative Eventing:点对点模式

我们知道Knative的两大核心组件:Serving和 Eventing。Serving部分在前面两篇文章已经介绍完了。本篇开始我们介绍Eventing。


Knative Eventing Sources是发出事件的软件。Source的工作是连接、消耗、捕获和潜在地缓冲事件。通常从外部系统,然后将这些事件中继到sink(接收器)。



Knative Eventing Sources are software components that emit events. The job of a Source is to connect to, drain, capture and potentially buffer events; often from an external system and then relay those events to the Sink.


Knative Eventing Sources默认自带以下四种类型:

# kubectl api-resources --api-group='sources.eventing.knative.dev'

图片

我们首先看一个定义source的配置文件:

# cat eventinghello-source.yaml

图片

上图标志位的含义:

1.定义了事件源的类型,事件系统即用型部署了一系列源,它还提供了部署自定义资源的方法

 2.定义某一个的事件源类型的具体内容

Knative Eventing Sink可以指定event receiver(event consumer)。通过Event Source的配置中引用sink,可以设置点对点的通信,如下图所标的位置:

3.sink 可以是K8S或knative的service

4.此处我们将sind定义为Knative Service 

5.sink被部署后的名字是eventinghello 


检查配置文件:

图片

# oc apply -f eventinghello-source.yaml

cronjobsource.sources.eventing.knative.dev/event-greeter-cronjob-source created


确认source部署成功:

#oc -n knativetutorial5 get cronjobsources.sources.eventing.knative.dev event-greeter-cronjob-source

图片

source通过计划任务发出event,crontab在pod中运行:

图片


运行以下命令以创建将用作cron事件的订阅服务器的Knative服务。下面配置将会创建knative service.

# cat  eventing-hello-sink.yaml

图片

查看knative service:


图片

我们查看sink 对应pod的日志:

图片


我们观察sink的pod,每两分钟接受一次消息,接受完毕后,无其他消息,pod会被杀掉。

图片


二、Knative Eventing:Channel订阅模式

图片


channel或sink是event source和subscriber之间的接口。内置channel可存储传入事件并将事件数据分发给订户。在将事件转发给订户时,通道会根据CloudEvent规范转换事件数据。


创建event channel:

[root@lb.weixinyucluster ~/knative-tutorial/eventing]# cat channel.yaml

apiVersion: messaging.knative.dev/v1alpha1

kind: Channel

metadata:

  name: eventinghello-ch


oc apply -n knativetutorial -f channel.yaml

# oc get inmemorychannel.messaging.knative.dev/eventinghello-ch

NAME               READY   REASON   URL                                                                    AGE

eventinghello-ch   True             http://eventinghello-ch-kn-channel.knativetutorial.svc.cluster.local   2m49s

event source监听外部事件,例如kafka主题或FTP服务器上的文件。它负责将接收到的事件及其数据排放到已配置的sink。


cat event-source.yaml

图片

 1.Channel API属于 api-group messaging.eventing.knative.dev 

2. Kind是Channel而不是像之前那种指定为Knative的service


[root@lb.weixinyucluster ~/knative-tutorial/eventing]# oc apply -n knativetutorial -f event-sourcechch.yaml

cronjobsource.sources.eventing.knative.dev/my-cjs created


[root@lb.weixinyucluster ~/knative-tutorial/eventing]# oc -n knativetutorial get cronjobsources.sources.eventing.knative.dev

NAME     READY   AGE

my-cjs   True    32s

创建Subscriber Services

图片

[root@lb.weixinyucluster ~/knative-tutorial/eventing]# oc apply -n knativetutorial -f eventing-helloa-sink.yaml

service.serving.knative.dev/eventinghelloa created

[root@lb.weixinyucluster ~/knative-tutorial/eventing]# oc apply -n knativetutorial -f eventing-hellob-sink.yaml

service.serving.knative.dev/eventinghellob created

[root@lb.weixinyucluster ~/knative-tutorial/eventing]#

现在,为eventinghelloa创建适当的Subscription到Channel eventinghello-ch:

图片

[root@lb.weixinyucluster ~/knative-tutorial/eventing]# oc apply -n knativetutorial -f eventing-helloa-sub.yaml

subscription.messaging.knative.dev/eventinghelloa-sub created

[root@lb.weixinyucluster ~/knative-tutorial/eventing]# oc apply -n knativetutorial -f eventing-hellob-sub.yaml

subscription.messaging.knative.dev/eventinghellob-sub created

[root@lb.weixinyucluster ~/knative-tutorial/eventing]#


查看pod:

图片

查看pod的日志:# oc logs -f eventinghelloa-v1-deployment-5b6454fcd-cw9lh -c user-containe

图片

# oc logs -f eventinghellob-v1-deployment-85bb64586f-n9tl7 -c user-container

图片

我们看到两个knative service订阅者会受到的消息是相同的。


三、Knative Eventing:Triggers与Brokers模式

图片


通过用knative-eventing-injection = enabled标记knativetutorial10命名空间,将使Knative Eventing部署默认的Knative Eventing Broker及其相关的Ingress:

# oc label namespace knativetutorial10  knative-eventing-injection=enabled

namespace/knativetutorial10 labeled


查看broker,已经部署成功:

图片


现在,已经配置了broker,您需要创建sinks eventingaloha和eventingbonjour,它们将接收已过滤的事件。

图片

# oc apply - -f eventing-aloha-sink.yaml

service.serving.knative.dev/eventingaloha created


# oc apply -f eventing-bonjour-sink.yaml

service.serving.knative.dev/eventingbonjour created

现在,为eventingaloha创建trigger,该trigger将过滤后的事件与service相关联:

# cat trigger-helloaloha.yaml

apiVersion: eventing.knative.dev/v1alpha1

kind: Trigger

metadata:

  name: helloaloha

spec:

  filter:

    attributes:

      type: greeting

  subscriber:

    ref:

     apiVersion: serving.knative.dev/v1

     kind: Service

     name: eventingaloha


# oc apply -f trigger-helloaloha.yaml

trigger.eventing.knative.dev/helloaloha created



# cat  trigger-hellobonjour.yaml

apiVersion: eventing.knative.dev/v1alpha1

kind: Trigger

metadata:

  name: hellobonjour

spec:

  filter:

    attributes:

      type: greeting

  subscriber:

    ref:

     apiVersion: serving.knative.dev/v1

     kind: Service

     name: eventingbonjour


# oc apply -f trigger-hellobonjour.yaml

trigger.eventing.knative.dev/hellobonjour created


#oc get triggers.eventing.knative.dev helloaloha hellobonjour

图片

找出eventhingaloha的subscriberURI:

#  oc get trigger helloaloha -o yaml |grep -i subscriberUri

  subscriberUri: http://eventingaloha.knativetutorial10.svc.cluster.local


# oc get trigger hellobonjour -o  yaml |grep -i subscriberUri

  subscriberUri: http://eventingbonjour.knativetutorial10.svc.cluster.local


找到broker’s subscriberURI:

# oc get broker default -o jsonpath='{.status.address.url}'

http://default-broker.knativetutorial10.svc.cluster.local



创建一个执行curl命令的pod:

[root@lb.weixinyucluster ~/knative-tutorial]# cat curler.yaml

apiVersion: v1

kind: Pod

metadata:

  labels:

    run: curler

  name: curler

spec:

  containers:

  - name: curler

    image: fedora:29

    tty: true


curl eventingaloha的 subcriberURI

[root@lb.weixinyucluster ~/knative-tutorial]# oc apply -f curler.yaml

pod/curler created

[root@lb.weixinyucluster ~/knative-tutorial]# oc exec -it curler -- /bin/bash


[root@curler /]# curl -v "http://eventingaloha.knativetutorial10.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"}'


图片

curl eventingbonjour的subcriberURI


curl -v "http://eventingbonjour.knativetutorial10.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"}'


图片

接下来,通过curl broker的subcriberURI,同时触发eventingaloha 和 eventingbonjour:

curl -v "http://default-broker.knativetutorial10.svc.cluster.local" \

-X POST \

-H "Ce-Id: say-hello" \

-H "Ce-Specversion: 1.0" \

-H "Ce-Type: greeting" \

-H "Ce-Source: mycurl" \

-H "Content-Type: application/json" \

-d '{"key":"from a curl"}'


图片

"Ce-Type: greeting"”是确保aloha和bonjour都对此事件做出响应的关键。


标签:eventing,oc,dev,事件驱动,knative,Ce,OpenShift,Knative
来源: https://blog.51cto.com/u_15127570/2711250