示例环境说明
具体步骤
创建一个channel
kn channel create imc01 --type messaging.knative.dev:v1:InMemoryChannel
apiVersion: messaging.knative.dev/v1
kind: Channel
metadata:
name: imc01
spec:
channelTemplate:
apiVersion: messaging.knative.dev/v1
kind: InMemoryChannel
查看channel
kn channel list
创建2个Sink: kservice/event-display01/kservice/event-display02
kn service create event-display01 --image ikubernetes/event_display --port 8080 --scale-min 1
kn service create event-display02 --image ikubernetes/event_display --port 8080 --scale-min 1
---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: event-display01
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/min-scale: "1"
spec:
containers:
- image: ikubernetes/event_display
ports:
- containerPort: 8080
---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: event-display02
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/min-scale: "1"
spec:
containers:
- image: ikubernetes/event_display
ports:
- containerPort: 8080
创建subscription: 负责连接channel和sink。这里模拟使用同一channel,创建多个subscription,验证分发效果
# /sub01负责连接kservice/event-display01至channel/imc01
kn subscription create sub01 --channel imc01 --sink ksvc:event-display01
#/sub02负责连接kservice/event-display02至channel/imc01
kn subscription create sub02 --channel imc01 --sink ksvc:event-display02
apiVersion: messaging.knative.dev/v1
kind: Subscription
metadata:
name: subscription1
spec:
channel:
apiVersion: messaging.knative.dev/v1
kind: Channel
name: imc01
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: event-display01
---
apiVersion: messaging.knative.dev/v1
kind: Subscription
metadata:
name: subscription2
spec:
channel:
apiVersion: messaging.knative.dev/v1
kind: Channel
name: imc01
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: event-display02
查看subscription:
验证
创建一个客户端Pod,使用curl命令基于HTTP协议推送event
kubectl run client-$RANDOM --image=ikubernetes/admin-box:v1.2 --restart=Never --rm -it --command -- /bin/bash
进入测试的pod里面,向channel/imc01的URL发起事件推送请求
curl -v "http://imc01-kn-channel.default.svc.cluster.local" -X POST -H "Content-Type: application/cloudevents+json" \
-d '{"id": "say-hello", "specversion": "1.0", "type": "com.icloud2native.sayhi", "source": "sendoff", "data": {"msg":"Hello Knative imc01 Channel"}}'
在sink1: event-display01查看log,能收到相关的event
kubectl logs -f event-display01-00001-deployment-64cd5c8866-48t4m
在sink2: event-display02查看log,能收到相关的event
kubectl logs -f event-display02-00001-deployment-664ccfc45d-k96zz
Broker
消息代理模式
Point-to-point messaging
Publish/subscribe messaging
Broker
Knative Eventing支持以下几种类型的Broker
基于Channel的多租户Broker (Multi-tenant channel-based broker,简称为MT-Channel-based Broker)
其它的可用的Broker类型
Knative Serving在名称空间级别提供了一个名为default的默认Broker,但使用前需要通过某种方式先行完成创建
创建默认Broker的方法
命令式命令,或使用配置文件
kn broker create default --namespace NS_NAME
在Trigger资源上使用特定的Annotation自动创建
在名称空间上添加特定的Label自动创建
删除默认的Broker
示例环境说明
具体步骤
创建两个ksvc作为sink: event-display-hi/event-display-bye
$ kn service create event-display-hi --image ikubernetes/event_display --port 8080 --scale-min 1
$ kn service create event-display-bye --image ikubernetes/event_display --port 8080 --scale-min 1
---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: event-display-hi
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/min-scale: "1"
spec:
containers:
- image: ikubernetes/event_display
ports:
- containerPort: 8080
---
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: event-display-bye
spec:
template:
metadata:
annotations:
autoscaling.knative.dev/min-scale: "1"
spec:
containers:
- image: ikubernetes/event_display
ports:
- containerPort: 8080
创建broker
$ kn broker create default --class MTChannelBasedBroker
# 其中的“--class”选项在使用默认的Broker类时,可以省略
apiVersion: eventing.knative.dev/v1
kind: Broker
metadata:
name: default
创建trigger:两个trigger分别过滤了不同类型的事件
kn trigger create trigger1 --broker default --sink ksvc:event-display-hi --filter type=com.icloud2native.sayhi
kn trigger create trigger2 --broker default --sink ksvc:event-display-bye --filter type=com.icloud2native.saybye
---
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: trigger1
spec:
broker: default
filter:
attributes:
type: com.icloud2native.sayhi
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: event-display-hi
---
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
name: trigger2
spec:
broker: default
filter:
attributes:
type: com.icloud2native.saybye
subscriber:
ref:
apiVersion: serving.knative.dev/v1
kind: Service
name: event-display-bye
列出两个trigger
kn trigger list
测试验证:event-display-hi的sink只收到hi的event,event-display-bye的sink只收到bye的event
创建一个客户端Pod,使用curl命令基于HTTP协议推送event
kubectl run client-$RANDOM --image=ikubernetes/admin-box:v1.2 --restart=Never --rm -it --command -- /bin/bash
在启动的客户端Pod中,向Broker/default的URL发起事件推送事件,sayhi和saybye类型的事件都要推送
curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/default/default" -X POST -H "Content-Type: application/cloudevents+json" \
-d '{"id": "say-hi", "specversion": "1.0", "type": "com.icloud2native.sayhi", "source": "sendoff", "data": {"msg":"Hello Knative default Broker Say HI"}}'
curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/default/default" -X POST -H "Content-Type: application/cloudevents+json" \
-d '{"id": "say-bye", "specversion": "1.0", "type": "com.icloud2native.saybye", "source": "sendoff", "data": {"msg":"Hello Knative default Broker Say BYE"}}'
获取event-display-hi中的日志信息,验证是否仅存在sayhi类型的事件
获取event-display-bye中的日志信息,验证是否仅存在saybye类型的事件