阅读 389

Knative实战:Broker和Trigger的使用

本文基于Knative v0.23版本

个人博客

开始使用Knative Eventing

创建一个Knative Eventing namespace

kubectl create namespace event-example
复制代码

添加一个broker

broker路由事件到不同的事件接收器或消费者。

  • 用以下命令添加一个叫做default的broker:

    kubectl create -f - <<EOF
    apiVersion: eventing.knative.dev/v1
    kind: broker
    metadata:
     name: default
     namespace: event-example
    EOF
    复制代码
  • 确认broker正确运行:

    kubectl -n event-example get broker default
    复制代码

    确保Ready状态为True

    NAME      URL                                                                              AGE   READY   REASON
    default   http://broker-ingress.knative-eventing.svc.cluster.local/event-example/default   45s   True
    复制代码

创建事件消费者

在这里我们创建两个事件消费者,hello-displaygoodbye-display,来演示怎么配置事件生产者来针对特定的消费者。

  • 用以下命令部署hello-display消费者到集群中:

    kubectl -n event-example apply -f - << EOF
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: hello-display
    spec:
      replicas: 1
      selector:
        matchLabels: &labels
          app: hello-display
      template:
        metadata:
          labels: *labels
        spec:
          containers:
            - name: event-display
              image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display
    
    ---
    
    kind: Service
    apiVersion: v1
    metadata:
      name: hello-display
    spec:
      selector:
        app: hello-display
      ports:
      - protocol: TCP
        port: 80
        targetPort: 8080
    EOF
    复制代码
  • 用以下命令部署goodbye-display消费者到集群中:

    kubectl -n event-example apply -f - << EOF
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: goodbye-display
    spec:
      replicas: 1
      selector:
        matchLabels: &labels
          app: goodbye-display
      template:
        metadata:
          labels: *labels
        spec:
          containers:
            - name: event-display
              # Source code: https://github.com/knative/eventing/tree/main/cmd/event_display
              image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display
    
    ---
    
    kind: Service
    apiVersion: v1
    metadata:
      name: goodbye-display
    spec:
      selector:
        app: goodbye-display
      ports:
      - protocol: TCP
        port: 80
        targetPort: 8080
    EOF
    复制代码
  • 用以下命令确认消费者正常工作:

    kubectl -n event-example get deployments hello-display goodbye-display
    复制代码

    上述命令能列出我们部署的hello-displaygoodbye-display消费者:

    NAME              READY   UP-TO-DATE   AVAILABLE   AGE
    hello-display     1/1     1            1           2m1s
    goodbye-display   1/1     1            1           47s
    复制代码

    READY列副本数量要与AVAILABLE列匹配。

创建triggers

Triggers定义了每个事件消费者接受到的事件。Brokers使用triggers将事件发送给正确的消费者。每个trigger能够指定一个过滤器,该过滤器能够基于Cloud Event context attribute选择相关的事件。

  • 创建一个trigger:

    kubectl -n event-example apply -f - << EOF
    apiVersion: eventing.knative.dev/v1
    kind: Trigger
    metadata:
      name: hello-display
    spec:
      broker: default
      filter:
        attributes:
          type: greeting
      subscriber:
        ref:
         apiVersion: v1
         kind: Service
         name: hello-display
    EOF
    复制代码

    该命令创建了一个的trigger来发送所有typegreeting的事件到一个名为hello-display的事件消费者。

  • 创建第二个消费者:

    kubectl -n event-example apply -f - << EOF
    apiVersion: eventing.knative.dev/v1
    kind: Trigger
    metadata:
      name: goodbye-display
    spec:
      broker: default
      filter:
        attributes:
          source: sendoff
      subscriber:
        ref:
         apiVersion: v1
         kind: Service
         name: goodbye-display
    EOF
    复制代码

    该命令创建了一个trigger用来发送所有sourcesendoff的事件到名为goodbye-display的事件消费者。

  • 确认trigger正常工作:

    kubectl -n event-example get triggers
    复制代码

    该命令返回刚刚创建的两个triggers:

    NAME              BROKER    SUBSCRIBER_URI                                            AGE     READY   REASON
    goodbye-display   default   http://goodbye-display.event-example.svc.cluster.local/   44s     True
    hello-display     default   http://hello-display.event-example.svc.cluster.local/     4m47s   True
    复制代码

    如果triggers配置正确,则会Ready并且指向正确的broker和SUBSCRIBER_URI。

创建一个Pod作为事件生产者

我们使用curl命令手动将单个事件作为HTTP请求发送给broker,并演示这些事件怎么被事件消费者正确的接收。

只能从安装了Knative Eventing集群内访问broker,所以必须在集群内创建一个pod来执行curl命令作为事件生产者。

用以下命令创建pod:

kubectl -n event-example apply -f - << EOF
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: curl
  name: curl
spec:
  containers:
    # This could be any image that we can SSH into and has curl.
  - image: radial/busyboxplus:curl
    imagePullPolicy: IfNotPresent
    name: curl
    resources: {}
    stdin: true
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    tty: true
EOF
复制代码

给broker发送事件

  • 进入刚刚创建的容器内:

    kubectl -n event-example attach curl -it
    复制代码
  • 这里构建三种请求来展示可以发送的各种类型的事件:

    • 构建typegreeting的事件:

      curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/event-example/default" \
        -X POST \
        -H "Ce-Id: say-hello" \
        -H "Ce-Specversion: 1.0" \
        -H "Ce-Type: greeting" \
        -H "Ce-Source: not-sendoff" \
        -H "Content-Type: application/json" \
        -d '{"msg":"Hello Knative!"}'
      复制代码

      当broker接收到该事件时,hello-display会被激活并将事件发送给对应事件消费者。如果接收到了事件,则会收到如下类似202 Accepted的回应:

      < HTTP/1.1 202 Accepted
      < Date: Wed, 02 Jun 2021 08:35:12 GMT
      < Content-Length: 0
      复制代码
    • 构建sourcesendoff的事件:

      curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/event-example/default" \
        -X POST \
        -H "Ce-Id: say-goodbye" \
        -H "Ce-Specversion: 1.0" \
        -H "Ce-Type: not-greeting" \
        -H "Ce-Source: sendoff" \
        -H "Content-Type: application/json" \
        -d '{"msg":"Goodbye Knative!"}'
      复制代码

      当broker接收到该事件时,goodbye-display会被激活并将事件发送给对应事件消费者。如果接收到了事件,则会收到如下类似202 Accepted的回应:

      < HTTP/1.1 202 Accepted
      < Date: Wed, 02 Jun 2021 08:42:02 GMT
      < Content-Length: 0
      复制代码
    • 构建包含typegreetingsourcesendoff的事件:

      curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/event-example/default" \
        -X POST \
        -H "Ce-Id: say-hello-goodbye" \
        -H "Ce-Specversion: 1.0" \
        -H "Ce-Type: greeting" \
        -H "Ce-Source: sendoff" \
        -H "Content-Type: application/json" \
        -d '{"msg":"Hello Knative! Goodbye Knative!"}'
      复制代码

      当broker接收到该事件时,hello-displaygoodbye-display都会被激活并将事件发送给对应事件消费者。如果接收到了事件,则会收到如下类似202 Accepted的回应:

      < HTTP/1.1 202 Accepted
      < Date: Wed, 02 Jun 2021 08:45:16 GMT
      < Content-Length: 0
      复制代码
  • 使用exit命令退出容器。

到这里我们发送了两个事件给hello-display事件消费者,两个事件给goodbye-display。后面让我们确认正确接收了这些事件。

确认事件接收

确认事件被正确的订阅者接收了。

  • 查看hello-display事件消费者的日志:

    kubectl -n event-example logs -l app=hello-display --tail=100
    复制代码

    可以看到发送给hello-display的事件的AttributesData

    ☁️  cloudevents.Event
    Context Attributes,
      specversion: 1.0
      type: greeting
      source: not-sendoff
      id: say-hello
      datacontenttype: application/json
    Extensions,
      knativearrivaltime: 2021-06-02T08:35:12.137155445Z
    Data,
      {
        "msg": "Hello Knative!"
      }
    ☁️  cloudevents.Event
    Context Attributes,
      specversion: 1.0
      type: greeting
      source: sendoff
      id: say-hello-goodbye
      datacontenttype: application/json
    Extensions,
      knativearrivaltime: 2021-06-02T08:45:16.087732995Z
    Data,
      {
        "msg": "Hello Knative! Goodbye Knative!"
      }
    复制代码
  • 查看goodbye-display事件消费者的日志:

    kubectl -n event-example logs -l app=goodbye-display --tail=100
    复制代码

    可以看到发送给goodbye-display的事件的AttributesData

    ☁️  cloudevents.Event
    Context Attributes,
      specversion: 1.0
      type: not-greeting
      source: sendoff
      id: say-goodbye
      datacontenttype: application/json
    Extensions,
      knativearrivaltime: 2021-06-02T08:42:02.227918434Z
    Data,
      {
        "msg": "Goodbye Knative!"
      }
    ☁️  cloudevents.Event
    Context Attributes,
      specversion: 1.0
      type: greeting
      source: sendoff
      id: say-hello-goodbye
      datacontenttype: application/json
    Extensions,
      knativearrivaltime: 2021-06-02T08:45:16.087732995Z
    Data,
      {
        "msg": "Hello Knative! Goodbye Knative!"
      }
    复制代码

参考资料

文章分类
后端
文章标签