配置 Broker 默认值¶
如果您对 Knative 安装具有集群管理员权限,则可以修改 ConfigMaps 以更改集群上 Brokers 的全局默认配置选项。
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
在这种情况下,如果在 Brokers eventing.knative.dev/broker.class
注释和/或 .spec.config
中未指定其他内容(请参阅 开发人员配置选项),则集群中创建的每个新 Broker 默认情况下将在集群中使用 MTChannelBasedBroker
Broker 类和 knative-eventing
命名空间中的 config-br-default-channel
ConfigMap 进行配置。
但是,如果您希望将 Kafka 通道用作创建的任何 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: kafka-channel
namespace: knative-eventing
现在,在集群中创建的每个没有 spec.config
的 Broker 都将被配置为使用 kafka-channel
ConfigMap。有关创建 kafka-channel
ConfigMap 以与您的 Broker 一起使用的更多信息,请参阅 Kafka 通道 ConfigMap 文档。
您还可以通过在 namespaceDefaults
部分中定义它来修改一个或多个专用命名空间的默认 Broker 配置。例如,如果您想对所有 Brokers 默认使用 config-br-default-channel
ConfigMap,但想对 namespace-1
和 namespace-2
使用 kafka-channel
ConfigMap,则可以使用以下 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 类¶
您可以配置 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 类。
例如,如果您想对集群中创建的其他所有 Brokers 使用 KafkaBroker
Broker 类,但想对 namespace-1
和 namespace-2
中创建的 Brokers 使用 MTChannelBasedBroker
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 类通常需要不同的配置 ConfigMaps。请参阅不同 Broker 实现 的配置选项,了解其引用的 ConfigMaps 的外观(例如,对于 MTChannelBasedBroker 或 用于 Apache Kafka 的 Knative Broker)。
配置交付规范默认值¶
您可以为 Brokers 配置默认事件交付参数,这些参数在事件无法交付的情况下应用
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
-
重新启动代理入口 Pod,以便
istio-proxy
容器可以作为 sidecar 注入,方法是运行以下命令kubectl delete pod <broker-ingress-pod-name> -n knative-eventing
其中
<broker-ingress-pod-name>
是您的代理入口 Pod 的名称。该 Pod 现在有两个容器
knative-eventing <broker-ingress-pod-name> 2/2 Running 1 175m
-
创建一个代理,然后使用以下命令获取代理的 URL
kubectl get broker <broker-name>
其中
<broker-name>
是代理的名称。示例输出
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
-
通过对代理 URL 运行以下命令来发送带有 HTTP POST 的 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>
是代理的 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 响应代码,表明代理已接受该请求... * 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
对象来描述对代理的路径仅限于给定用户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/*
-
要访问代理,请将 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/*