This article is based on Knative version V0.23
Personal blog
Start using Knative Eventing
Create a Knative Eventing Namespace
kubectl create namespace event-example
Copy the code
Adding a Broker
The broker routes events to different event receivers or consumers.
-
Add a broker called default with the following command:
kubectl create -f - <<EOF apiVersion: eventing.knative.dev/v1 kind: broker metadata: name: default namespace: event-example EOF Copy the code
-
Confirm that the broker is running correctly:
kubectl -n event-example get broker default Copy the code
Ensure that the Ready state is True:
NAME URL AGE READY REASON default http://broker-ingress.knative-eventing.svc.cluster.local/event-example/default 45s True Copy the code
Create event consumers
Here we create two event consumers, Hello -display and goodbye-display, to demonstrate how the event producer can be configured to target a specific consumer.
-
Deploy the Hello-display consumer to the cluster with the following command:
kubectl -n event-example apply -f - << EOF apiVersion: apps/v1 kind: Deployment metadata: name: hello-display spec: replicas: 1 selector: matchLabels: &labels app: hello-display template: metadata: labels: *labels spec: containers: - name: event-display image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display --- kind: Service apiVersion: v1 metadata: name: hello-display spec: selector: app: hello-display ports: - protocol: TCP port: 80 targetPort: 8080 EOF Copy the code
-
Deploy the inward-display consumer to the cluster with the following command:
kubectl -n event-example apply -f - << EOF apiVersion: apps/v1 kind: Deployment metadata: name: goodbye-display spec: replicas: 1 selector: matchLabels: &labels app: goodbye-display template: metadata: labels: *labels spec: containers: - name: event-display # Source code: https://github.com/knative/eventing/tree/main/cmd/event_display image: gcr.io/knative-releases/knative.dev/eventing/cmd/event_display --- kind: Service apiVersion: v1 metadata: name: goodbye-display spec: selector: app: goodbye-display ports: - protocol: TCP port: 80 targetPort: 8080 EOF Copy the code
-
Verify that the consumer is working properly with the following command:
kubectl -n event-example get deployments hello-display goodbye-display Copy the code
The above command lists our deployed hello-display and indone-display consumers:
NAME READY UP-TO-DATE AVAILABLE AGE hello-display 1/1 1 1 2m1s goodbye-display 1/1 1 1 47s Copy the code
The number of copies in the READY column should match the number in the AVAILABLE column.
Create triggers
Triggers define the events that each event consumer receives. Brokers uses Triggers to send the event to the correct consumer. Each trigger can specify a filter that can select related events based on the Cloud Event Context Attribute.
-
Create a trigger:
kubectl -n event-example apply -f - << EOF apiVersion: eventing.knative.dev/v1 kind: Trigger metadata: name: hello-display spec: broker: default filter: attributes: type: greeting subscriber: ref: apiVersion: v1 kind: Service name: hello-display EOF Copy the code
This command creates a trigger to send all events of type greeting to an event consumer named hello-display.
-
Create a second consumer:
kubectl -n event-example apply -f - << EOF apiVersion: eventing.knative.dev/v1 kind: Trigger metadata: name: goodbye-display spec: broker: default filter: attributes: source: sendoff subscriber: ref: apiVersion: v1 kind: Service name: goodbye-display EOF Copy the code
This command creates a trigger that sends all events with source Sendoff to an event consumer named indone-display.
-
Confirm that the trigger works properly:
kubectl -n event-example get triggers Copy the code
This command returns two triggers just created:
NAME BROKER SUBSCRIBER_URI AGE READY REASON goodbye-display default http://goodbye-display.event-example.svc.cluster.local/ 44s True hello-display default http://hello-display.event-example.svc.cluster.local/ 4m47s True Copy the code
If triggers are configured correctly, it is Ready and points to the correct broker and SUBSCRIBER_URI.
Create a Pod as the event producer
We use the curl command to manually send individual events to the broker as HTTP requests and demonstrate how these events are correctly received by event consumers.
You can only access the broker from within the cluster where you have Knative Eventing installed, so you must create a POD within the cluster to execute the curl command as an event producer.
Create a POD with the following command:
kubectl -n event-example apply -f - << EOF apiVersion: v1 kind: Pod metadata: labels: run: curl name: curl spec: containers: # This could be any image that we can SSH into and has curl. - image: radial/busyboxplus:curl imagePullPolicy: IfNotPresent name: curl resources: {} stdin: true terminationMessagePath: /dev/termination-log terminationMessagePolicy: File tty: true EOF
Copy the code
Send events to the broker
-
Go to the container you just created:
kubectl -n event-example attach curl -it Copy the code
-
Here we build three requests to show the various types of events that can be sent:
-
Build an event of type greeting:
curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/event-example/default" \ -X POST \ -H "Ce-Id: say-hello" \ -H "Ce - Specversion: 1.0" \ -H "Ce-Type: greeting" \ -H "Ce-Source: not-sendoff" \ -H "Content-Type: application/json" \ -d '{"msg":"Hello Knative!" } ' Copy the code
When the broker receives the event, hell-display is activated and sent to the event consumer. If the event is received, you will receive a response like 202 Accepted:
< HTTP/1.1 202 Accepted < Date: Wed, 02 Jun 2021 08:35:12 GMT < Content-Length: 0 Copy the code
-
Build an event with source as Sendoff:
curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/event-example/default" \ -X POST \ -H "Ce-Id: say-goodbye" \ -H "Ce - Specversion: 1.0" \ -H "Ce-Type: not-greeting" \ -H "Ce-Source: sendoff" \ -H "Content-Type: application/json" \ -d '{"msg":"Goodbye Knative!" } ' Copy the code
When the broker receives the event, inward-display is activated and the event is sent to the event consumer. If the event is received, you will receive a response like 202 Accepted:
< HTTP/1.1 202 Accepted < Date: Wed, 02 Jun 2021 08:42:02 GMT < Content-Length: 0 Copy the code
-
Build an event containing type greeting and source sendoff:
curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/event-example/default" \ -X POST \ -H "Ce-Id: say-hello-goodbye" \ -H "Ce - Specversion: 1.0" \ -H "Ce-Type: greeting" \ -H "Ce-Source: sendoff" \ -H "Content-Type: application/json" \ -d '{"msg":"Hello Knative! Goodbye Knative!" } ' Copy the code
When the broker receives the event, both helium-display and inward-display are activated and send the event to the corresponding event consumer. If the event is received, you will receive a response like 202 Accepted:
< HTTP/1.1 202 Accepted < Date: Wed, 02 Jun 2021 08:45:16 GMT < Content-Length: 0 Copy the code
-
-
Run the exit command to exit the container.
At this point we send two events to the Hello-display event consumer and two to the indone-display event consumer. Let’s make sure these events are received correctly.
Acknowledge event reception
Verify that the event was received by the correct subscriber.
-
To view logs of the Hello-display event consumer:
kubectl -n event-example logs -l app=hello-display --tail=100 Copy the code
You can see the Attributes and Data of the event sent to hello-display:
☁️ cloudevents.Event Context Attributes, specVersion: 1.0 type: greeting source: not-sendoff ID: say-hello datacontenttype: application/json Extensions, knativearrivaltime: 2021-06-02t08:35:12.137155445z Data, {" MSG ": "Hello Knative!" } ☁️ cloudevents.Event Context Attributes, specversion: 1.0 type: greeting source: Sendoff ID: say-hello-goodbye datacontenttype: application/json Extensions, knativearrivaltime: 2021-06-02t08:45:16.087732995z Data, {" MSG ": "Hello Knative! Goodbye Knative!" }Copy the code
-
To view the log for the indone-display event consumer:
kubectl -n event-example logs -l app=goodbye-display --tail=100 Copy the code
You can see the Attributes and Data for the event sent to indone-display:
☁️ cloudevents.Event Context Attributes, specVersion: 1.0 type: not-greeting source: Sendoff ID: say-goodbye datacontenttype: application/json Extensions, knativearrivaltime: 2021-06-02t08:42:02.227918434z Data, {" MSG ": "Goodbye Knative!" } ☁️ cloudevents.Event Context Attributes, specversion: 1.0 type: greeting source: Sendoff ID: say-hello-goodbye datacontenttype: application/json Extensions, knativearrivaltime: 2021-06-02t08:45:16.087732995z Data, {" MSG ": "Hello Knative! Goodbye Knative!" }Copy the code
The resources
- Knative. Dev/docs/eventi…