Channel and Subscribers

Channels

Channels are an event forwarding and persistence layer where each channel is a separate Kubernetes Custom Resource. A Channel may be backed by Apache Kafka or InMemoryChannel. This recipe focuses on InMemoryChannel.

Subscriptions

Subscriptions are how you register your service to listen to a particular channel.

Channel(Sink)

The channel or sink is an interface between the event source and the subscriber. The channels are built in to store the incoming events and distribute the event data to the subscribers. When forwarding event to subscribers the channel transforms the event data as per CloudEvent specification.

Create Event Channel

apiVersion: messaging.knative.dev/v1
kind: Channel
metadata:
  name: eventinghello-ch (1)
1 The name of the channel. Knative makes it addressable, i.e. resolveable to a target (a consumer service)

Run the following commands to create the channel:

  • kn

  • kubectl

kn channel create eventinghello-ch

Verification

  • kn

  • kubectl

kn channel list
kubectl get channels.messaging.knative.dev

Running the above command should return the following result:

Event Source

The event source listens to external events e.g. a kafka topic or for a file on a FTP server. It is responsible to drain the received event(s) along with its data to a configured sink.

Create Event Source

apiVersion: sources.knative.dev/v1beta1
kind: PingSource
metadata:
  name: event-greeter-ping-source
spec:
  schedule: "*/2 * * * *"
  jsonData: '{"message": "Thanks for doing Knative Tutorial"}'
  sink:
   ref:
    apiVersion: messaging.knative.dev/v1 (1)
    kind: Channel (2)
    name: eventinghello-ch
1 The Channel API is in api-group messaging.eventing.knative.dev
2 Kind is Channel instead of direct to a specific service; default is InMemoryChannel implementation

Run the following commands to create the event source resources:

  • kn

  • kubectl

kn source ping create event-greeter-ping-source \
  --schedule "*/2 * * * *" \
  --sink channel:eventinghello-ch
  --data '{"message": "Thanks for doing Knative Tutorial"}'

Verification

  • kn

  • kubectl

kn source ping list

Running the above command should return the following result:

NAME                        SCHEDULE      SINK                       AGE   CONDITIONS   READY   REASON
event-greeter-ping-source   */2 * * * *   Channel:eventinghello-ch   24s   4 OK / 4     True
kubectl get pingsource.sources.knative.dev

Running the above command should return the following result:

NAME                        READY   REASON   SINK                                                                 AGE
event-greeter-ping-source   True             http://eventinghello-ch-kn-channel.kn-cheatsheet.svc.cluster.local   5s

Event Subscriber

The event subscription is responsible of connecting the channel(sink) with the service. Once a service is connected to a channel it starts receiving the events (cloud events).

Create Subscriber Services

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: eventinghelloa
spec:
  template:
    metadata:
      name: eventinghelloa-v1 (1)
      annotations:
        autoscaling.knative.dev/target: "1"
    spec:
      containers:
      - image: quay.io/rhdevelopers/eventinghello:0.0.2
1 The string of eventinghelloa will help you identify this particular service.
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: eventinghellob
spec:
  template:
    metadata:
      name: eventinghellob-v1 (1)
      annotations:
        autoscaling.knative.dev/target: "1"
    spec:
      containers:
      - image: quay.io/rhdevelopers/eventinghello:0.0.2
1 The string of eventinghellob will help you identify this particular service.

Deploy eventing-helloa-sink

  • kn

  • kubectl

kn service create eventinghelloa \
  --concurrency-target=1 \
  --revision-name=eventinghelloa-v1 \
  --image=quay.io/rhdevelopers/eventinghello:0.0.2

Deploy eventing-hellob-sink

  • kn

  • kubectl

kn service create eventinghellob \
  --concurrency-target=1 \
  --revision-name=eventinghellob-v1 \
  --image=quay.io/rhdevelopers/eventinghello:0.0.2

Create Channel Subscribers

Now create the appropriate Subscription for eventinghelloa to the Channel eventinghello-ch:

apiVersion: messaging.knative.dev/v1
kind: Subscription
metadata:
  name: eventinghelloa-sub
spec:
  channel:
    apiVersion: messaging.knative.dev/v1
    kind: Channel
    name: eventinghello-ch
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1
      kind: Service
      name: eventinghelloa

And create the appropriate Subscription for eventinghellob to the Channel eventinghello-ch:

apiVersion: messaging.knative.dev/v1
kind: Subscription
metadata:
  name: eventinghellob-sub
spec:
  channel:
    apiVersion: messaging.knative.dev/v1
    kind: Channel
    name: eventinghello-ch
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1
      kind: Service
      name: eventinghellob

Subscribe to eventinghelloa

  • kn

  • kubectl

kn subscription create eventinghelloa-sub \
  --channel eventinghello-ch \
  --sink eventinghelloa

Subscribe to eventinghelloab

  • kn

  • kubectl

kn subscription create eventinghellob-sub \
  --channel eventinghello-ch \
  --sink eventinghellob

Verification

  • kn

  • kubectl

kn subscription list

Running the above command should return the following result:

NAME                 CHANNEL                    SUBSCRIBER            REPLY   DEAD LETTER SINK   READY   REASON
eventinghelloa-sub   Channel:eventinghello-ch   ksvc:eventinghelloa                              True
eventinghellob-sub   Channel:eventinghello-ch   ksvc:eventinghellob                              True
kubectl get subscription.messaging.knative.dev

Running the above command should return the following result:

NAME                 AGE   READY   REASON
eventinghelloa-sub   26s   True
eventinghellob-sub   6s    True

If you wait approximately 2 minutes for the PingSource then you will see both eventinghelloa and eventinghellob begin to run in the knativetutorial.

watch 'kubectl get pods -n knativetutorial'
Watch pods in the knativetutorial
NAME                                                      READY STATUS  AGE
eventinghelloa-v1-deployment-d86bf4847-hvbk6               2/2   Running 5s
eventinghellob-v1-deployment-5c986c7586-4clpb              2/2   Running 5s

See what you have deployed

channel

  • kn

  • kubectl

kn channel list
NAME               TYPE              URL                                                                    AGE   READY   REASON
eventinghello-ch   InMemoryChannel   http://eventinghello-ch-kn-channel.knativetutorial.svc.cluster.local   13m   True
kubectl get channels.messaging.knative.dev eventinghello-ch
NAME               URL                                                                    AGE   READY   REASON
eventinghello-ch   http://eventinghello-ch-kn-channel.knativetutorial.svc.cluster.local   35m   True

source

  • kn

  • kubectl

kn source ping list
NAME                        SCHEDULE      SINK                       AGE   CONDITIONS   READY   REASON
event-greeter-ping-source   */2 * * * *   Channel:eventinghello-ch   10m   4 OK / 4     True
kubectl get pingsource.sources.knative.dev  event-greeter-ping-source
NAME                        SINK                                                                   AGE   READY   REASON
event-greeter-ping-source   http://eventinghello-ch-kn-channel.knativetutorial.svc.cluster.local   12m   True

subscriptions

  • kn

  • kubectl

kn subscription list
NAME                 CHANNEL                    SUBSCRIBER            REPLY   DEAD LETTER SINK   READY   REASON
eventinghelloa-sub   Channel:eventinghello-ch   ksvc:eventinghelloa                              True
eventinghellob-sub   Channel:eventinghello-ch   ksvc:eventinghellob                              True
kubectl get subscriptions.messaging.knative.dev
NAME                 AGE   READY   REASON
eventinghelloa-sub   10m   True
eventinghellob-sub   10m   True
Add -oyaml to the above commands to get more details about each object that were queried for.

Cleanup

  • kn

  • kubectl

kn service delete eventinghelloa
kn subscription delete eventinghelloa-sub
kn service delete eventinghellob
kn subscription delete eventinghellob-sub
kn source ping delete event-greeter-ping-source
kn channel delete eventinghello-ch
kubectl delete -f eventing-helloa-sink.yaml
kubectl delete -f eventing-helloa-sub.yaml
kubectl delete -f eventing-hellob-sink.yaml
kubectl delete -f eventing-hellob-sub.yaml
kubectl delete -f event-source.yaml
kubectl delete -f channel.yaml