Getting Started with Eventing

Use this guide to learn how to create, send, and verify events in Knative. The steps in this guide demonstrate a basic developer flow for managing events in Knative, including:

  1. Installing the Knative Eventing component

  2. Creating and configuring Knative Eventing Resources

  3. Sending events with HTTP requests

  4. Verifying events were sent correctly

Before you begin

To complete this guide, you will need the following installed and running:

Important Note: Some Knative Eventing features do not work when using Minikube due to this bug. For local testing you can use kind.

Installing Knative Eventing

If you previously created a Knative cluster, you might already have Knative Eventing installed and running. You can check to see if the Eventing component exists on your cluster by running:

  1. kubectl get pods --namespace knative-eventing

If the knative-eventing namespace or the imc-controller-* does not exist, use the following steps to install Knative Eventing with the in-memory channel:

  1. Make sure that you have a functioning Kubernetes cluster. See the Comprehensive Install guide for more information.

    • Old versions of Knative Serving doesn’t necessarily work well with latest Knative Eventing, so try to install the latest version of Knative Serving.

    • If your Kubernetes cluster comes with pre-installed Istio, make sure it has cluster-local-gateway deployed. Depending on which Istio version you have, you’d need to apply the istio-knative-extras.yaml in the corresponding version folder at here.

  2. Install the Eventing CRDs by running the following command:

    1. kubectl apply --selector knative.dev/crd-install=true \
    2. --filename https://github.com/knative/eventing/releases/download/v0.15.0/eventing.yaml
  3. Finish installing Eventing resources by running the following command:

    1. kubectl apply --filename https://github.com/knative/eventing/releases/download/v0.15.0/eventing.yaml

    Installing the CRDs first can prevent race conditions, which might cause failures during the install. Race conditions exist during install because a CRD must be defined before an instance of that resource can be created. Defining all CRDs before continuing with the rest of the installation prevents this problem.

  4. Confirm that Knative Eventing is correctly installed by running the following command:

    1. kubectl get pods --namespace knative-eventing

    This will return the following result:

    1. NAME READY STATUS RESTARTS AGE
    2. broker-controller-56b4d58667-fm979 1/1 Running 0 4m36s
    3. broker-filter-5bdbc8d8dd-kpswx 1/1 Running 0 4m35s
    4. broker-ingress-d896b6b46-4nbtr 1/1 Running 0 4m35s
    5. eventing-controller-5fc5645584-qvspl 1/1 Running 0 4m36s
    6. eventing-webhook-7674b867dc-zw9qh 1/1 Running 0 4m36s
    7. imc-controller-6b548d6468-gnbkn 1/1 Running 0 4m35s
    8. imc-dispatcher-655cdf6ff6-mkc29 1/1 Running 0 4m35s
    9. mt-broker-controller-6d66c4c6f6-xdn7b 1/1 Running 0 4m35s

Setting up Knative Eventing Resources

Before you start to manage events, you need to create the objects needed to transport the events.

Creating and configuring an Eventing namespace

In this section you create the event-example namespace and then add the knative-eventing-injection label to that namespace. You use namespaces to group together and organize your Knative resources, including the Eventing subcomponents.

  1. Run the following command to create a namespace called event-example:

    1. kubectl create namespace event-example

    This creates an empty namespace called event-example.

  2. Add a label to your namespace with the following command:

    1. kubectl label namespace event-example knative-eventing-injection=enabled

    This gives the event-example namespace the knative-eventing-injection label, which adds resources that will allow you to manage your events.

In the next section, you will need to verify that the resources you added in this section are running correctly. Then, you can create the rest of the eventing resources you need to manage events.

Validating that the Broker is running

The Broker ensures that every event sent by event producers arrives at the correct event consumers. The Broker was created when you labeled your namespace as ready for eventing, but it is important to verify that your Broker is working correctly. In this guide, you will use the default broker.

  1. Run the following command to verify that the Broker is in a healthy state:

    1. kubectl --namespace event-example get Broker default

    This shows the Broker that you created:

    1. NAME READY REASON URL AGE
    2. default True http://broker-ingress.knative-eventing.svc.cluster.local/event-example/default 1m

    When the Broker has the READY=True state, it can begin to manage any events it receives.

  2. If READY=False, wait 2 minutes and re-run the command. If you continue to receive the READY=False, see the Debugging Guide to help troubleshoot the issue.

Now that your Broker is ready to manage events, you can create and configure your event producers and consumers.

Creating event consumers

Your event consumers receive the events sent by event producers. In this step, you will create two event consumers, hello-display and goodbye-display, to demonstrate how you can configure your event producers to selectively target a specific consumer.

  1. To deploy the hello-display consumer to your cluster, run the following command:

    1. kubectl --namespace event-example apply --filename - << END
    2. apiVersion: apps/v1
    3. kind: Deployment
    4. metadata:
    5. name: hello-display
    6. spec:
    7. replicas: 1
    8. selector:
    9. matchLabels: &labels
    10. app: hello-display
    11. template:
    12. metadata:
    13. labels: *labels
    14. spec:
    15. containers:
    16. - name: event-display
    17. # Source code: https://github.com/knative/eventing-contrib/tree/master/cmd/event_display
    18. image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
    19. ---
    20. # Service pointing at the previous Deployment. This will be the target for event
    21. # consumption.
    22. kind: Service
    23. apiVersion: v1
    24. metadata:
    25. name: hello-display
    26. spec:
    27. selector:
    28. app: hello-display
    29. ports:
    30. - protocol: TCP
    31. port: 80
    32. targetPort: 8080
    33. END
  2. To deploy the goodbye-display consumer to your cluster, run the following command:

    1. kubectl --namespace event-example apply --filename - << END
    2. apiVersion: apps/v1
    3. kind: Deployment
    4. metadata:
    5. name: goodbye-display
    6. spec:
    7. replicas: 1
    8. selector:
    9. matchLabels: &labels
    10. app: goodbye-display
    11. template:
    12. metadata:
    13. labels: *labels
    14. spec:
    15. containers:
    16. - name: event-display
    17. # Source code: https://github.com/knative/eventing-contrib/tree/master/cmd/event_display
    18. image: gcr.io/knative-releases/knative.dev/eventing-contrib/cmd/event_display
    19. ---
    20. # Service pointing at the previous Deployment. This will be the target for event
    21. # consumption.
    22. kind: Service
    23. apiVersion: v1
    24. metadata:
    25. name: goodbye-display
    26. spec:
    27. selector:
    28. app: goodbye-display
    29. ports:
    30. - protocol: TCP
    31. port: 80
    32. targetPort: 8080
    33. END
  3. Just like you did with the Broker, verify that your event consumers are working by running the following command:

    1. kubectl --namespace event-example get deployments hello-display goodbye-display

    This lists the hello-display and goodbye-display consumers that you deployed:

    1. NAME READY UP-TO-DATE AVAILABLE AGE
    2. hello-display 1/1 1 1 26s
    3. goodbye-display 1/1 1 1 16s

    The number of replicas in your READY column should match the number of replicas in your AVAILABLE column, which might take a few minutes. If after two minutes the numbers do not match, then see the Debugging Guide to help troubleshoot the issue.

Creating Triggers

A Trigger defines the events that you want each of your event consumers to receive. Your Broker uses triggers to forward events to the right consumers. Each trigger can specify a filter to select relevant events based on the Cloud Event context attributes.

  1. To create the first Trigger, run the following command:

    1. kubectl --namespace event-example apply --filename - << END
    2. apiVersion: eventing.knative.dev/v1alpha1
    3. kind: Trigger
    4. metadata:
    5. name: hello-display
    6. spec:
    7. filter:
    8. attributes:
    9. type: greeting
    10. subscriber:
    11. ref:
    12. apiVersion: v1
    13. kind: Service
    14. name: hello-display
    15. END

    The command creates a trigger that sends all events of type greeting to your event consumer named hello-display.

  2. To add the second Trigger, run the following command:

    1. kubectl --namespace event-example apply --filename - << END
    2. apiVersion: eventing.knative.dev/v1alpha1
    3. kind: Trigger
    4. metadata:
    5. name: goodbye-display
    6. spec:
    7. filter:
    8. attributes:
    9. source: sendoff
    10. subscriber:
    11. ref:
    12. apiVersion: v1
    13. kind: Service
    14. name: goodbye-display
    15. END

    The command creates a trigger that sends all events of source sendoff to your event consumer named goodbye-display.

  3. Verify that the triggers are working correctly by running the following command:

    1. kubectl --namespace event-example get triggers

    This returns the hello-display and goodbye-display triggers that you created:

    1. NAME READY REASON BROKER SUBSCRIBER_URI AGE
    2. goodbye-display True default http://goodbye-display.event-example.svc.cluster.local/ 9s
    3. hello-display True default http://hello-display.event-example.svc.cluster.local/ 16s

    If the triggers are correctly configured, they will be ready and pointing to the correct Broker (the default broker) and SUBSCRIBER_URI (triggerName.namespaceName.svc.cluster.local). If this is not the case, see the Debugging Guide to help troubleshoot the issue.

You have now created all of the resources needed to receive and manage events. You created the Broker, which manages the events sent to event consumers with the help of triggers. In the next section, you will make the event producer that will be used to create your events.

Creating event producers

In this section you will create an event producer that you can use to interact with the Knative Eventing subcomponents you created earlier. Most events are created systematically, but this guide uses curl to manually send individual events and demonstrate how these events are received by the correct event consumer. Because you can only access the Broker from within your Eventing cluster, you must create a Pod within that cluster to act as your event producer.

In the following step, you will create a Pod that executes your curl commands to send events to the Broker in your Eventing cluster.

To create the Pod, run the following command:

  1. kubectl --namespace event-example apply --filename - << END
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5. labels:
  6. run: curl
  7. name: curl
  8. spec:
  9. containers:
  10. # This could be any image that we can SSH into and has curl.
  11. - image: radial/busyboxplus:curl
  12. imagePullPolicy: IfNotPresent
  13. name: curl
  14. resources: {}
  15. stdin: true
  16. terminationMessagePath: /dev/termination-log
  17. terminationMessagePolicy: File
  18. tty: true
  19. END

Now that you’ve set up your Eventing cluster to send and consume events, you will use HTTP requests to manually send separate events and demonstrate how each of those events can target your individual event consumers in the next section.

Sending Events to the Broker

Now that you’ve created the Pod, you can create an event by sending an HTTP request to the Broker. SSH into the Pod by running the following command:

  1. kubectl --namespace event-example attach curl -it

You have sshed into the Pod, and can now make an HTTP request. A prompt similar to the one below will appear:

  1. Defaulting container name to curl.
  2. Use 'kubectl describe pod/ -n event-example' to see all of the containers in this pod.
  3. If you don't see a command prompt, try pressing enter.
  4. [ root@curl:/ ]$

To show the various types of events you can send, you will make three requests:

  1. To make the first request, which creates an event that has the type greeting, run the following in the SSH terminal:

    1. curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/event-example/default" \
    2. -X POST \
    3. -H "Ce-Id: say-hello" \
    4. -H "Ce-Specversion: 0.3" \
    5. -H "Ce-Type: greeting" \
    6. -H "Ce-Source: not-sendoff" \
    7. -H "Content-Type: application/json" \
    8. -d '{"msg":"Hello Knative!"}'

    When the Broker receives your event, hello-display will activate and send it to the event consumer of the same name.

    If the event has been received, you will receive a 202 Accepted response similar to the one below:

    1. < HTTP/1.1 202 Accepted
    2. < Content-Length: 0
    3. < Date: Mon, 12 Aug 2019 19:48:18 GMT
  2. To make the second request, which creates an event that has the source sendoff, run the following in the SSH terminal:

    1. curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/event-example/default" \
    2. -X POST \
    3. -H "Ce-Id: say-goodbye" \
    4. -H "Ce-Specversion: 0.3" \
    5. -H "Ce-Type: not-greeting" \
    6. -H "Ce-Source: sendoff" \
    7. -H "Content-Type: application/json" \
    8. -d '{"msg":"Goodbye Knative!"}'

    When the Broker receives your event, goodbye-display will activate and send the event to the event consumer of the same name.

    If the event has been received, you will receive a 202 Accepted response similar to the one below:

    1. < HTTP/1.1 202 Accepted
    2. < Content-Length: 0
    3. < Date: Mon, 12 Aug 2019 19:48:18 GMT
  3. To make the third request, which creates an event that has the type greeting and thesource sendoff, run the following in the SSH terminal:

    1. curl -v "http://broker-ingress.knative-eventing.svc.cluster.local/event-example/default" \
    2. -X POST \
    3. -H "Ce-Id: say-hello-goodbye" \
    4. -H "Ce-Specversion: 0.3" \
    5. -H "Ce-Type: greeting" \
    6. -H "Ce-Source: sendoff" \
    7. -H "Content-Type: application/json" \
    8. -d '{"msg":"Hello Knative! Goodbye Knative!"}'

    When the Broker receives your event, hello-display and goodbye-display will activate and send the event to the event consumer of the same name.

    If the event has been received, you will receive a 202 Accepted response similar to the one below:

    1. < HTTP/1.1 202 Accepted
    2. < Content-Length: 0
    3. < Date: Mon, 12 Aug 2019 19:48:18 GMT
  4. Exit SSH by typing exit into the command prompt.

You have sent two events to the hello-display event consumer and two events to the goodbye-display event consumer (note that say-hello-goodbye activates the trigger conditions for both hello-display and goodbye-display). You will verify that these events were received correctly in the next section.

Verifying events were received

After sending events, verify that your events were received by the appropriate Subscribers.

  1. Look at the logs for the hello-display event consumer by running the following command:

    1. kubectl --namespace event-example logs -l app=hello-display --tail=100

    This returns the Attributes and Data of the events you sent to hello-display:

    1. ☁️ cloudevents.Event
    2. Validation: valid
    3. Context Attributes,
    4. specversion: 0.3
    5. type: greeting
    6. source: not-sendoff
    7. id: say-hello
    8. time: 2019-05-20T17:59:43.81718488Z
    9. contenttype: application/json
    10. Extensions,
    11. knativehistory: default-broker-srk54-channel-24gls.event-example.svc.cluster.local
    12. Data,
    13. {
    14. "msg": "Hello Knative!"
    15. }
    16. ☁️ cloudevents.Event
    17. Validation: valid
    18. Context Attributes,
    19. specversion: 0.3
    20. type: greeting
    21. source: sendoff
    22. id: say-hello-goodbye
    23. time: 2019-05-20T17:59:54.211866425Z
    24. contenttype: application/json
    25. Extensions,
    26. knativehistory: default-broker-srk54-channel-24gls.event-example.svc.cluster.local
    27. Data,
    28. {
    29. "msg": "Hello Knative! Goodbye Knative!"
    30. }
  2. Look at the logs for the goodbye-display event consumer by running the following command:

    1. kubectl --namespace event-example logs -l app=goodbye-display --tail=100

    This returns the Attributes and Data of the events you sent to goodbye-display:

    1. ☁️ cloudevents.Event
    2. Validation: valid
    3. Context Attributes,
    4. specversion: 0.3
    5. type: not-greeting
    6. source: sendoff
    7. id: say-goodbye
    8. time: 2019-05-20T17:59:49.044926148Z
    9. contenttype: application/json
    10. Extensions,
    11. knativehistory: default-broker-srk54-channel-24gls.event-example.svc.cluster.local
    12. Data,
    13. {
    14. "msg": "Goodbye Knative!"
    15. }
    16. ☁️ cloudevents.Event
    17. Validation: valid
    18. Context Attributes,
    19. specversion: 0.3
    20. type: greeting
    21. source: sendoff
    22. id: say-hello-goodbye
    23. time: 2019-05-20T17:59:54.211866425Z
    24. contenttype: application/json
    25. Extensions,
    26. knativehistory: default-broker-srk54-channel-24gls.event-example.svc.cluster.local
    27. Data,
    28. {
    29. "msg": "Hello Knative! Goodbye Knative!"
    30. }

Cleaning up

After you finish this guide, delete your namespace to conserve resources if you do not plan to use them.

Note: If you plan to continue learning about Knative Eventing with one of our code samples, check the requirements of the sample and make sure you do not need a namespace before you delete event-example. You can always reuse your namespaces.

Run the following command to delete event-example:

  1. kubectl delete namespace event-example

This removes the namespace and all of its resources from your cluster.

What’s next

You’ve learned the basics of the Knative Eventing workflow. Here are some additional resources to help you continue to build with the Knative Eventing component.