設定 Broker 預設值¶
如果您具有 Knative 安裝的叢集管理員權限,您可以修改 ConfigMap 以變更叢集上 Broker 的全域預設設定選項。
Knative Eventing 提供一個 config-br-defaults
ConfigMap,其中包含控管預設 Broker 建立的設定值。
預設的 config-br-defaults
ConfigMap 如下所示
apiVersion: v1
kind: ConfigMap
metadata:
name: config-br-defaults
namespace: knative-eventing
data:
# Configures the default for any Broker that does not specify a spec.config or Broker class.
default-br-config: |
clusterDefault:
brokerClass: MTChannelBasedBroker
apiVersion: v1
kind: ConfigMap
name: config-br-default-channel
namespace: knative-eventing
在此情況下,叢集中建立的每個新 Broker 都會預設使用 MTChannelBasedBroker
Broker 類別,以及 knative-eventing
命名空間中的 config-br-default-channel
ConfigMap 來進行設定,除非 Broker 的 eventing.knative.dev/broker.class
註釋和/或 .spec.config
中另有指定(請參閱 開發人員設定選項)。
但是,如果您希望使用 Kafka Channel 作為建立的任何 Broker 的預設 Channel 實作,您可以變更 config-br-defaults
ConfigMap,使其如下所示
apiVersion: v1
kind: ConfigMap
metadata:
name: config-br-defaults
namespace: knative-eventing
data:
# Configures the default for any Broker that does not specify a spec.config or Broker class.
default-br-config: |
clusterDefault:
brokerClass: MTChannelBasedBroker
apiVersion: v1
kind: ConfigMap
name: kafka-channel
namespace: knative-eventing
現在,叢集中建立的每個沒有 spec.config
的 Broker 都會設定為使用 kafka-channel
ConfigMap。如需更多關於建立 kafka-channel
ConfigMap 以與 Broker 搭配使用的資訊,請參閱 Kafka Channel ConfigMap 文件。
您也可以透過在 namespaceDefaults
區段中定義,來修改一個或多個專用命名空間的預設 Broker 設定。例如,如果您想要預設將 config-br-default-channel
ConfigMap 用於所有 Broker,但想要將 kafka-channel
ConfigMap 用於 namespace-1
和 namespace-2
,您可以使用下列 ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: config-br-defaults
namespace: knative-eventing
data:
default-br-config: |
clusterDefault:
brokerClass: MTChannelBasedBroker
apiVersion: v1
kind: ConfigMap
name: config-br-default-channel
namespace: knative-eventing
namespaceDefaults:
namespace-1:
apiVersion: v1
kind: ConfigMap
name: kafka-channel
namespace: knative-eventing
namespace-2:
apiVersion: v1
kind: ConfigMap
name: kafka-channel
namespace: knative-eventing
設定 Broker 類別¶
除了為每個 Broker 個別設定 Broker 類別之外,也可以在叢集範圍內或以每個命名空間為基礎定義預設的 Broker 類別
設定叢集的預設 Broker 類別¶
您可以設定 clusterDefault
Broker 類別,以便叢集中建立的任何沒有 eventing.knative.dev/broker.class
註釋的 Broker 都使用此預設 Broker 類別
apiVersion: v1
kind: ConfigMap
metadata:
name: config-br-defaults
namespace: knative-eventing
data:
# Configures the default for any Broker that does not specify a spec.config or Broker class.
default-br-config: |
clusterDefault:
brokerClass: MTChannelBasedBroker
設定命名空間的預設 Broker 類別¶
您可以修改一個或多個命名空間的預設 Broker 類別。
例如,如果您想要將 KafkaBroker
Broker 類別用於叢集上建立的所有其他 Broker,但想要將 MTChannelBasedBroker
Broker 類別用於在 namespace-1
和 namespace-2
中建立的 Broker,您可以使用下列 ConfigMap 設定
apiVersion: v1
kind: ConfigMap
metadata:
name: config-br-defaults
namespace: knative-eventing
data:
# Configures the default for any Broker that does not specify a spec.config or Broker class.
default-br-config: |
clusterDefault:
brokerClass: KafkaBroker
namespaceDefaults:
namespace1:
brokerClass: MTChannelBasedBroker
namespace2:
brokerClass: MTChannelBasedBroker
注意
請注意,不同的 Broker 類別通常需要不同的設定 ConfigMap。請參閱不同 Broker 實作的設定選項,了解其參考的 ConfigMap 看起來應該如何(例如,針對 MTChannelBasedBroker 或 適用於 Apache Kafka 的 Knative Broker)。
設定傳遞規格預設值¶
您可以為 Broker 設定預設事件傳遞參數,這些參數會在事件無法傳遞時套用
apiVersion: v1
kind: ConfigMap
metadata:
name: config-br-defaults
namespace: knative-eventing
data:
# Configures the default for any Broker that does not specify a spec.config or Broker class.
default-br-config: |
clusterDefault:
brokerClass: MTChannelBasedBroker
apiVersion: v1
kind: ConfigMap
name: kafka-channel
namespace: knative-eventing
delivery:
retry: 10
backoffDelay: PT0.2S
backoffPolicy: exponential
namespaceDefaults:
namespace-1:
apiVersion: v1
kind: ConfigMap
name: config-br-default-channel
namespace: knative-eventing
delivery:
deadLetterSink:
ref:
kind: Service
namespace: example-namespace
name: example-service
apiVersion: v1
uri: example-uri
retry: 10
backoffPolicy: exponential
backoffDelay: "PT0.2S"
失效信件佇列¶
您可以設定 deadLetterSink
傳遞參數,以便在事件無法傳遞時,將其傳送到指定的事件接收器。
重試¶
您可以設定事件傳送到失效信件佇列之前必須重試的最少次數,方法是使用整數值設定 retry
傳遞參數。
退避延遲¶
您可以設定 backoffDelay
傳遞參數,以指定在失敗後嘗試重試事件傳遞之前的時間延遲。backoffDelay
參數的持續時間是使用 ISO 8601 格式指定的。
退避策略¶
backoffPolicy
傳遞參數可用於指定重試退避策略。該策略可以指定為線性或指數。當使用線性退避策略時,退避延遲是在重試之間指定的時間間隔。當使用指數退避策略時,退避延遲等於 backoffDelay*2^<numberOfRetries>
。
將 Istio 與 Knative Brokers 整合¶
使用 JSON Web Token (JWT) 和 Istio 保護 Knative Broker¶
先決條件¶
- 您已安裝 Knative Eventing。
- 您已安裝 Istio。
步驟¶
-
為
knative-eventing
命名空間加上標籤,以便 Istio 可以處理以 JWT 為基礎的使用者驗證,方法是執行命令kubectl label namespace knative-eventing istio-injection=enabled
-
重新啟動 broker ingress pod,以便可以將
istio-proxy
容器注入為 sidecar,方法是執行命令kubectl delete pod <broker-ingress-pod-name> -n knative-eventing
其中
<broker-ingress-pod-name>
是您的 Broker ingress pod 的名稱。現在 pod 有兩個容器
knative-eventing <broker-ingress-pod-name> 2/2 Running 1 175m
-
建立 Broker,然後執行命令取得 Broker 的 URL
kubectl get broker <broker-name>
其中
<broker-name>
是您的 Broker 的名稱。輸出範例
NAMESPACE NAME URL AGE READY REASON default my-broker http://broker-ingress.knative-eventing.svc.cluster.local/default/my-broker 6s True
-
執行下列命令以啟動
curl
podkubectl -n default run curl --image=radial/busyboxplus:curl -i --tty
-
使用 HTTP POST 針對 Broker URL 傳送 CloudEvent,方法是執行下列命令
curl -X POST -v \ -H "content-type: application/json" \ -H "ce-specversion: 1.0" \ -H "ce-source: my/curl/command" \ -H "ce-type: my.demo.event" \ -H "ce-id: 0815" \ -d '{"value":"Hello Knative"}' \ <broker-URL>
其中
<broker-URL>
是您的 Broker 的 URL。例如curl -X POST -v \ -H "content-type: application/json" \ -H "ce-specversion: 1.0" \ -H "ce-source: my/curl/command" \ -H "ce-type: my.demo.event" \ -H "ce-id: 0815" \ -d '{"value":"Hello Knative"}' \ http://broker-ingress.knative-eventing.svc.cluster.local/default/my-broker
-
您將會收到
202
HTTP 回應碼,表示代理 (broker) 已接受該請求。... * Mark bundle as not supporting multiuse < HTTP/1.1 202 Accepted < allow: POST, OPTIONS < date: Tue, 15 Mar 2022 13:37:57 GMT < content-length: 0 < x-envoy-upstream-service-time: 79 < server: istio-envoy < x-envoy-decorator-operation: broker-ingress.knative-eventing.svc.cluster.local:80/*
-
在
knative-eventing
命名空間中套用一個AuthorizationPolicy
物件,以描述 Broker 的路徑僅限於特定使用者存取。apiVersion: security.istio.io/v1beta1 kind: AuthorizationPolicy metadata: name: require-jwt namespace: knative-eventing spec: action: ALLOW rules: - from: - source: requestPrincipals: ["testing@secure.istio.io/testing@secure.istio.io"] to: - operation: methods: ["POST"] paths: ["/default/my-broker"]
-
在
istio-system
命名空間中,為使用者requestPrincipal
建立一個RequestAuthentication
物件。apiVersion: security.istio.io/v1beta1 kind: RequestAuthentication metadata: name: "jwt-example" namespace: istio-system spec: jwtRules: - issuer: "testing@secure.istio.io" jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.13/security/tools/jwt/samples/jwks.json"
-
現在重新嘗試執行
curl
命令會收到來自伺服器的403 - Forbidden
回應碼。... * Mark bundle as not supporting multiuse < HTTP/1.1 403 Forbidden < content-length: 19 < content-type: text/plain < date: Tue, 15 Mar 2022 13:47:53 GMT < server: istio-envoy < connection: close < x-envoy-decorator-operation: broker-ingress.knative-eventing.svc.cluster.local:80/*
-
若要存取 Broker,請在請求中加入 Bearer JSON Web Token。
TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.13/security/tools/jwt/samples/demo.jwt -s) curl -X POST -v \ -H "content-type: application/json" \ -H "Authorization: Bearer ${TOKEN}" \ -H "ce-specversion: 1.0" \ -H "ce-source: my/curl/command" \ -H "ce-type: my.demo.event" \ -H "ce-id: 0815" \ -d '{"value":"Hello Knative"}' \ <broker-URL>
伺服器現在會回應
202
回應碼,表示已接受該 HTTP 請求。* Mark bundle as not supporting multiuse < HTTP/1.1 202 Accepted < allow: POST, OPTIONS < date: Tue, 15 Mar 2022 14:05:09 GMT < content-length: 0 < x-envoy-upstream-service-time: 40 < server: istio-envoy < x-envoy-decorator-operation: broker-ingress.knative-eventing.svc.cluster.local:80/*