Knative實戰:Broker和Trigger的使用

本文基於Knative v0.23版本git

我的博客github

開始使用Knative Eventing

建立一個Knative Eventing namespace

kubectl create namespace event-example
複製代碼

添加一個broker

broker路由事件到不一樣的事件接收器或消費者。json

  • 用如下命令添加一個叫作default的broker:api

    kubectl create -f - <<EOF apiVersion: eventing.knative.dev/v1 kind: broker metadata: name: default namespace: event-example EOF
    複製代碼
  • 確認broker正確運行:bash

    kubectl -n event-example get broker default
    複製代碼

    確保Ready狀態爲Truemarkdown

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

建立事件消費者

在這裏咱們建立兩個事件消費者,hello-displaygoodbye-display,來演示怎麼配置事件生產者來針對特定的消費者。app

  • 用如下命令部署hello-display消費者到集羣中:curl

    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消費者到集羣中:oop

    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
    複製代碼
  • 用如下命令確認消費者正常工做:url

    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!"
      }
    複製代碼

參考資料

相關文章
相關標籤/搜索