跳至內容

建立 ContainerSource

API version v1

ContainerSource 物件會啟動一個容器映像,該映像會產生事件並將訊息傳送到接收器 URI。您也可以使用 ContainerSource 來支援 Knative 中的自有事件來源。

若要使用 ContainerSource 建立自訂事件來源,您必須建立容器映像,以及使用映像 URI 的 ContainerSource。

開始之前

您必須先在叢集上安裝 Knative Eventing,才能建立 ContainerSource 物件。

開發、建置和發布容器映像

您可以使用任何語言來開發容器映像,並可以使用您喜歡的任何工具來建置和發布映像。以下是一些基本準則

  • ContainerSource 控制器會注入兩個環境變數;K_SINKK_CE_OVERRIDES,分別從 spec.sinkspec.ceOverrides 解析。
  • 事件訊息會傳送到 K_SINK 中指定的接收器 URI。訊息必須以 CloudEvents HTTP 格式 以 POST 方式傳送。

建立 ContainerSource 物件

  1. 建置事件來源的映像,並將其發布到您的映像儲存庫。您的映像必須讀取環境變數 K_SINK,並將訊息發佈到 K_SINK 中指定的 URL。

    您可以使用下列 YAML 來部署示範 heartbeats 事件來源

    apiVersion: sources.knative.dev/v1
    kind: ContainerSource
    metadata:
      name: heartbeat-source
    spec:
      template:
        spec:
          containers:
            - image: gcr.io/knative-nightly/knative.dev/eventing/cmd/heartbeats:latest
              name: heartbeats
      sink:
        ref:
          apiVersion: serving.knative.dev/v1
          kind: Service
          name: event-display
    
  2. 執行命令以建立 ContainerSource 的命名空間

    kubectl create namespace <namespace>
    

    其中 <namespace> 是您想要 ContainerSource 使用的命名空間。例如,heartbeat-source

  3. 建立接收器。如果您還沒有接收器,您可以使用下列 Knative 服務,該服務會將傳入的訊息傾印到其記錄中

    注意

    若要建立 Knative 服務,您必須在叢集上安裝 Knative Serving。

    • 若要建立接收器,請執行命令

      kn service create event-display --port 8080 --image gcr.io/knative-releases/knative.dev/eventing/cmd/event_display
      
    1. 使用下列範例建立 YAML 檔案

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: event-display
      spec:
        replicas: 1
        selector:
          matchLabels: &labels
            app: event-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: event-display
      spec:
        selector:
          app: event-display
        ports:
        - protocol: TCP
          port: 80
          targetPort: 8080
      
    2. 執行命令以套用 YAML 檔案

      kubectl apply -f <filename>.yaml
      
      其中 <filename> 是您在上一個步驟中建立的檔案名稱。

  4. 使用特定引數和環境設定建立具體的 ContainerSource

    • 若要建立 ContainerSource,請執行命令

      kn source container create <name> --image <image-uri> --sink <sink> -e POD_NAME=<pod-name> -e POD_NAMESPACE=<pod-namespace>
      
      其中

      • <name> 是您想要 ContainerSource 物件使用的名稱,例如,test-heartbeats
      • <image-uri> 對應於您在步驟 1 中建置和發布的映像 URI,例如,gcr.io/knative-nightly/knative.dev/eventing/cmd/heartbeats
      • <pod-name> 是容器在其內部執行的 Pod 名稱,例如,mypod
      • <pod-namespace> 是 Pod 在其內部執行的命名空間,例如,event-test
      • <sink> 是您的接收器名稱,例如,event-display。如需可用選項的清單,請參閱 Knative 用戶端文件
    1. 使用下列範本建立 YAML 檔案

      apiVersion: sources.knative.dev/v1
      kind: ContainerSource
      metadata:
        name: <containersource-name>
      spec:
        template:
          spec:
            containers:
              - image: <event-source-image-uri>
                name: <container-name>
                env:
                  - name: POD_NAME
                    value: "<pod-name>"
                  - name: POD_NAMESPACE
                    value: "<pod-namespace>"
        sink:
          ref:
            apiVersion: v1
            kind: Service
            name: <sink>
      
      其中

      • <namespace> 是您為 ContainerSource 建立的命名空間,例如,containersource-example
      • <containersource-name> 是您想要 ContainerSource 使用的名稱,例如,test-heartbeats
      • <event-source-image-uri> 對應於您在步驟 1 中建置和發布的映像 URI,例如,gcr.io/knative-nightly/knative.dev/eventing/cmd/heartbeats
      • <container-name> 是您的事件來源名稱,例如,heartbeats
      • <pod-name> 是容器在其內部執行的 Pod 名稱,例如,mypod
      • <pod-namespace> 是 Pod 在其內部執行的命名空間,例如,event-test
      • <sink> 是您的接收器名稱,例如,event-display

      如需關於您可以為 ContainerSource 物件設定的欄位的詳細資訊,請參閱 ContainerSource 參考

    2. 執行命令以套用 YAML 檔案

      kubectl apply -f <filename>.yaml
      
      其中 <filename> 是您在上一個步驟中建立的檔案名稱。

    注意

    會設定引數和環境變數,並傳遞至容器。

驗證 ContainerSource 物件

  1. 執行命令以檢視事件取用者的記錄

    kubectl -n <namespace> logs -l <pod-name> --tail=200
    
    其中

    • <namespace> 是包含 ContainerSource 物件的命名空間。
    • <pod-name> 是容器在其內部執行的 Pod 名稱。

    例如

    $ kubectl -n containersource-example logs -l app=event-display --tail=200
    
  2. 驗證輸出是否傳回 ContainerSource 傳送到您接收器的事件屬性。在下列範例中,命令已傳回 ContainerSource 傳送到 event-display 服務的事件的 AttributesData 屬性

    ☁️  cloudevents.Event
    Validation: valid
    Context Attributes,
      specversion: 1.0
      type: dev.knative.eventing.samples.heartbeat
      source: https://knative.dev.org.tw/eventing/cmd/heartbeats/#event-test/mypod
      id: 2b72d7bf-c38f-4a98-a433-608fbcdd2596
      time: 2019-10-18T15:23:20.809775386Z
      contenttype: application/json
    Extensions,
      beats: true
      heart: yes
      the: 42
    Data,
      {
        "id": 2,
        "label": ""
      }
    

刪除 ContainerSource 物件

若要刪除 ContainerSource 物件和命名空間中的所有相關資源

  • 執行命令以刪除命名空間

    kubectl delete namespace <namespace>
    

    其中 <namespace> 是包含 ContainerSource 物件的命名空間。

參考文件

請參閱 ContainerSource 參考

我們使用分析工具和 Cookie 來了解網站流量。您使用我們網站的相關資訊會與 Google 分享以達到此目的。了解更多。