channel和subscription实践
创建channel
[root@master sources]# kn channel create imc01
Channel 'imc01' created in namespace 'default'.
[root@master sources]# kn channel create imc02 --type messaging.knative.dev:v1:InMemoryChannel
Channel 'imc02' created in namespace 'default'.
[root@master sources]# kn channel list
NAME TYPE URL AGE READY REASON
imc01 InMemoryChannel http://imc01-kn-channel.default.svc.cluster.local 9m14s True
imc02 InMemoryChannel http://imc02-kn-channel.default.svc.cluster.local 5m43s True
查看channel详细信息
[root@master sources]# kn channel list imc01 -o yaml
创建两个订阅者
[root@master sources]# kn service create event-display-01 --image ikubernetes/event_display --port 8080 --scale-min 1
Creating service 'event-display-01' in namespace 'default':
0.063s The Route is still working to reflect the latest desired specification.
0.071s ...
0.085s Configuration "event-display-01" is waiting for a Revision to become ready.
6.359s ...
6.424s Ingress has not yet been reconciled.
6.486s Waiting for load balancer to be ready
6.684s Ready to serve.
Service 'event-display-01' created to latest revision 'event-display-01-00001' is available at URL:
http://event-display-01.default.yang.com
[root@master sources]# kn service create event-display-02 --image ikubernetes/event_display --port 8080 --scale-min 1
Creating service 'event-display-02' in namespace 'default':
0.061s The Route is still working to reflect the latest desired specification.
0.099s ...
0.119s Configuration "event-display-02" is waiting for a Revision to become ready.
8.392s ...
8.439s Ingress has not yet been reconciled.
8.508s Waiting for load balancer to be ready
8.723s Ready to serve.
Service 'event-display-02' created to latest revision 'event-display-02-00001' is available at URL:
http://event-display-02.default.yang.com
[root@master sources]# kubectl get pods
NAME READY STATUS RESTARTS AGE
event-display-01-00001-deployment-57f8d8697b-glgdg 3/3 Running 0 42s
event-display-02-00001-deployment-66d7fb7bd9-xf8cx 3/3 Running 0 3m37s
[root@master sources]# kn channel delete imc02
Channel 'imc02' deleted in namespace 'default'.
[root@master sources]# kn channel list
NAME TYPE URL AGE READY REASON
imc01 InMemoryChannel http://imc01-kn-channel.default.svc.cluster.local 18m True
创建两个订阅
[root@master sources]# kn subscription create sub01 --channel messaging.knative.dev:v1:InmemoryChannel:imc01 --sink ksvc:event-display-01
Subscription 'sub01' created in namespace 'default'.
[root@master sources]# kn subscription create sub02 --channel messaging.knative.dev:v1:InmemoryChannel:imc01 --sink ksvc:event-display-02
Subscription 'sub02' created in namespace 'default'.
[root@master sources]# kn subscription list
NAME CHANNEL SUBSCRIBER REPLY DEAD LETTER SINK READY REASON
sub01 InmemoryChannel:imc01 ksvc:event-display-01 True
sub02 InmemoryChannel:imc01 ksvc:event-display-02 True
curl请求命令
curl -v "http://imc01-kn-channel.default.svc.cluster.local" -X POST \
-H "Ce-Id: say-hello" \
-H "Ce-Specversion: 1.0" \
-H "Ce-Type: com.magedu.sayhievent" \
-H "Ce-Time: 2022-10-02T11:36:56.7181741Z" \
-H "Ce-Source: sendoff" \
-H "Content-Type: application/json" \
-d '{"msg":"Hello MageEdu Knative from imc01!"}'
创建一个pods集群内访问imc01
[root@master k8s-gitlab]# kubectl run client-$RANDOM --image=ikubernetes/admin-box:v1.2 --restart=Never -it --command -- /bin/sh
If you don't see a command prompt, try pressing enter.
root@client-31297 # curl -v "http://imc01-kn-channel.default.svc.cluster.local/" -X POST \
> -H "Ce-Id: say-hello" \
> -H "Ce-Specversion: 1.0" \
> -H "Ce-Type: com.magedu.sayhievent" \
> -H "Ce-Time: 2022-10-02T11:36:56.7181741Z" \
> -H "Ce-Source: sendoff" \
> -H "Content-Type: application/json" \
> -d '{"msg":"Hello MageEdu Knative from imc01!"}'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 10.100.135.196:80...
* TCP_NODELAY set
* Connected to imc01-kn-channel.default.svc.cluster.local (10.100.135.196) port 80 (#0)
> POST / HTTP/1.1
> Host: imc01-kn-channel.default.svc.cluster.local
> User-Agent: curl/7.67.0
> Accept: */*
> Ce-Id: say-hello
> Ce-Specversion: 1.0
> Ce-Type: com.magedu.sayhievent
> Ce-Time: 2022-10-02T11:36:56.7181741Z
> Ce-Source: sendoff
> Content-Type: application/json
> Content-Length: 43
>
* upload completely sent off: 43 out of 43 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 202 Accepted
< allow: POST, OPTIONS
< date: Fri, 21 Oct 2022 04:59:04 GMT
< content-length: 0
< x-envoy-upstream-service-time: 1
< server: envoy
<
* Connection #0 to host imc01-kn-channel.default.svc.cluster.local left intact
请求完后查看01和02都可以看到事件