Jack Moore

Email: jack(at)jmoore53.com
Project Updates

Getting Started with Knative Eventing!

22 Feb 2022 » kubernetes, golang, programming, software

Setting up Knative Eventing

In a nutshell Knative Eventing looks like the following:

Event Source -> Brokers -> Triggers -> Consumers

Example Application:

# Namespace for sample application
apiVersion: v1
kind: Namespace
metadata:
  name: knative-eventing-sample
---
# A default broker
apiVersion: eventing.knative.dev/v1
kind: Broker
metadata:
  name: default
  namespace: knative-eventing-sample
  annotations:
    # Note: you can set the eventing.knative.dev/broker.class annotation to change the class of the broker.
    # The default broker class is MTChannelBasedBroker, but Knative also supports use of the other class.
    eventing.knative.dev/broker.class: MTChannelBasedBroker
spec: {}
---
# Event Display app deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-display
  namespace: event-example
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
---
# Service that exposes helloworld-go app.
# This will be the subscriber for the Trigger
kind: Service
apiVersion: v1
metadata:
  name: helloworld-go
  namespace: knative-eventing-sample
spec:
  selector:
    app: helloworld-go
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  #type: LoadBalancer
---
# Knative Eventing Trigger to trigger the helloworld-go service
apiVersion: eventing.knative.dev/v1
kind: Trigger
metadata:
  name: helloworld-go
  namespace: knative-eventing-sample
spec:
  broker: default
  filter:
    attributes:
      type: dev.knative.samples.helloworld
      source: dev.knative.samples/helloworldsource
  subscriber:
    ref:
      apiVersion: v1
      kind: Service
      name: helloworld-go

After deploying the application, it can be tested with the following:

# Start Curl
kubectl -n knative-eventing-sample attach curl -it


# Within the Container:
# Run the curl command
If you dont see a command prompt, try pressing enter.
[ root@curl:/ ]$ 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!"}'
> POST /event-example/default HTTP/1.1
> User-Agent: curl/7.35.0
> Host: broker-ingress.knative-eventing.svc.cluster.local
> Accept: */*
> Ce-Id: say-hello
> Ce-Specversion: 1.0
> Ce-Type: greeting
> Ce-Source: not-sendoff
> Content-Type: application/json
> Content-Length: 24
>
< HTTP/1.1 202 Accepted
< Allow: POST, OPTIONS
< Date: Fri, 25 Feb 2022 04:26:25 GMT
< Content-Length: 0

# Outside the container run the following:
kubectl -n knative-eventing-sample logs -l app=hello-display --tail=100

# Should show the following:
2022/02/21 15:52:32 Hello World Message from received event "Hello World from the curl pod."
2022/02/21 15:52:32 Responding with event
Validation: valid
Context Attributes,
  specversion: 1.0
  type: dev.knative.samples.hifromknative
  source: knative/eventing/samples/hello-world
  id: d0e650e5-5eef-495c-ba76-281303755946
  datacontenttype: application/json
Data,
  {
    "msg": "Hi from helloworld-go app!"
  }
© Jack Moore