Setting up knative eventing
lets send some messages
- tags
- knative
- kubernetes
- eventing
Contents
Here's a walk through of how to setup knative eventing. I couldn't find this information in one place and it took longer than I expected to sort it out.
There are a couple of ways you can use eventing. One is to sent messages directly to specific services. This I outline walk through as a direct event.
Another is to use a broker and a trigger. A broker is basically the place you send stuff to, and then you define specific listeners by creating a trigger.
A third way it to use a channel and then create a subscription to that channel.
What's the differene?
Brokers/Trigger and Channels/Subscriptions are very very similar. The code that you use to send an event, in knative parlance, an event source is exactly the same, and the code you use to receive an event, or an event sink is exactly the same, the difference is in the deployment configuration and what the underlying service is that provides the event routing.
Note that the sources that are described in the knative documentation
are the default ones, but writing your own is as simple as creating a
binding
(which gives you a K_SINK
environment variable which will
target your event), so it doesn't need to be all that complicated.
Also a bit confusing, is that brokers are themselves sinks, as well as sources.
With brokers and triggers, you can setup event type filtering on the trigger itself so that your service will only see messages of a specific type.
For channels I think you need to to the filtering on the receiving side.
Install knative-eventing
Since we've setup the knative operator previously, lets tell it to turn on eventing.
eventing.yaml
|
|
And then apply:
|
|
namespace/knative-eventing configured knativeeventing.operator.knative.dev/knative-eventing unchanged
Check the status of the deployment
|
|
Deploy the test services
Create the echo service
(Code is below)
|
|
Create the send service
(Code is below)
|
|
Receiving events
Direct event test
Create the source
This created an event source that sends an event every minute. We
tell it to send the message directly to ruby-echo
, the service that we
just created.
|
|
Ping source 'heartbeat-source' created in namespace 'default'.
View results
This is deployed on my domain, yours should be different.
|
|
Hello from the ruby eco service {"specversion"=>"1.0", "id"=>"0dd72dfa-eac9-40a1-b48b-00df271cd62e", "source"=>"/apis/v1/namespaces/default/pingsources/heartbeat-source", "type"=>"dev.knative.sources.ping", "data_encoded"=>"", "data"=>"", "time"=>"2021-12-14T18:50:00.060257784Z"}
Cleanup
|
|
Ping source 'heartbeat-source' deleted in namespace 'default'.
Broker and Trigger test
Create the Broker
|
|
Broker 'default' successfully created in namespace 'default'.
Create the source
Here we are using the ping source again, but pointing it to
broker:default
instead of the service.
|
|
Ping source 'heartbeat-source' created in namespace 'default'.
Create the trigger
Now that we are pinging the broker, we need to tell the broker to send
that message type to our service, ruby-echo
.
|
|
Trigger 'heartbeat-trigger' successfully created in namespace 'default'.
View results
|
|
Hello from the ruby echo service {"specversion"=>"1.0", "id"=>"1aef1313-e8ea-4b14-9d91-691f6ecbcc03", "source"=>"/apis/v1/namespaces/default/pingsources/heartbeat-source", "type"=>"dev.knative.sources.ping", "data_encoded"=>"", "data"=>"", "time"=>"2021-12-14T18:57:00.319157126Z", "knativearrivaltime"=>"2021-12-14T18:57:00.319736536Z"}
Cleanup
|
|
Broker 'default' successfully deleted in namespace 'default'. Ping source 'heartbeat-source' deleted in namespace 'default'. Trigger 'heartbeat-trigger' deleted in namespace 'default'.
Channel test
Channel
Simple.
|
|
Channel 'heartbeat' created in namespace 'default'.
Ping the heartbeat channel
This is sending to channel:heartbeat
|
|
Ping source 'heartbeat-source' created in namespace 'default'.
Subscribe to the event
Here we create the subscription, which goes to our service.
|
|
Subscription 'heartbeat-sub' created in namespace 'default'.
See the results
|
|
Hello from the ruby echo service {"specversion"=>"1.0", "id"=>"88171ffc-3008-4df4-98cd-c157ef5a1aea", "source"=>"/apis/v1/namespaces/default/pingsources/heartbeat-source", "type"=>"dev.knative.sources.ping", "data_encoded"=>"", "data"=>"", "time"=>"2021-12-14T18:59:00.082372715Z"}
Cleanup
|
|
Channel 'heartbeat' deleted in namespace 'default'. Ping source 'heartbeat-source' deleted in namespace 'default'. Subscription 'heartbeat-sub' deleted in namespace 'default'.
Sending events
Now we can look at creating and sending events. The receiving side is
the same, but we need to create a service that will send a message.
In each scenario this will be the same code, but depending upon how we
deploy it, it will have a different K_SINK
value in the environment.
Direct binding
Create direct binding
|
|
Sink binding 'direct-binding' created in namespace 'default'.
Test
curl http://ruby-echo.default.gitgratitude.com
Hello from the ruby echo service
|
|
sent
|
|
Hello from the ruby echo service {"specversion"=>"1.0", "id"=>"1234-1234-1234", "source"=>"/mycontext", "type"=>"com.example.someevent", "data_encoded"=>"{\"message\":\"Hello\"}", "data"=>{"message"=>"Hello"}, "datacontenttype"=>"application/json"}
Clean up
|
|
Through a broker
Create a broker
|
|
Create the trigger
|
|
Trigger 'send-trigger' successfully created in namespace 'default'.
Create the binding
|
|
Sink binding 'broker-binding' created in namespace 'default'.
Test
curl http://ruby-echo.default.gitgratitude.com
Hello from the ruby echo service
|
|
sent
|
|
Hello from the ruby echo service {"specversion"=>"1.0", "id"=>"1234-1234-1234", "source"=>"/mycontext", "type"=>"com.example.someevent", "data_encoded"=>"{\"message\":\"Hello\"}", "data"=>{"message"=>"Hello"}, "datacontenttype"=>"application/json", "knativearrivaltime"=>"2021-12-14T23:44:09.238452314Z"}
Cleanup
|
|
Trigger 'send-trigger' deleted in namespace 'default'. Sink binding 'broker-binding' deleted in namespace 'default'.
On a channel
Create channel
Simple.
|
|
Create source binding to the channel
|
|
Subscribe to the event
Here we create the subscription, which goes to our service.
|
|
Subscription 'message-sub' created in namespace 'default'.
Test
curl http://ruby-echo.default.gitgratitude.com
Hello from the ruby echo service
|
|
sent
|
|
Hello from the ruby echo service {"specversion"=>"1.0", "id"=>"1234-1234-1234", "source"=>"/mycontext", "type"=>"com.example.someevent", "data_encoded"=>"{\"message\":\"Hello\"}", "data"=>{"message"=>"Hello"}, "datacontenttype"=>"application/json"}
Cleanup
|
|
Code
ruby-echo
|
|
ruby-send
|
|
Previously
Next