Knative Tutorial

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/v1beta1
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:

kubectl apply -n knativetutorial -f channel.yaml

Verification

kubectl -n knativetutorial 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/v1alpha2
kind: PingSource
metadata:
  name: event-greeter-ping-source
spec:
  schedule: "*/2 * * * *"
  jsonData: '{"message": "Thanks for doing Knative Tutorial"}'
  sink:
   ref:
    apiVersion: messaging.knative.dev/v1alpha1 (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:

kubectl apply -n knativetutorial -f event-source.yaml

Verification

kubectl -n knativetutorial 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.
kubectl apply -n knativetutorial -f eventing-helloa-sink.yaml
kubectl apply -n knativetutorial -f eventing-hellob-sink.yaml

Create Channel Subscribers

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

apiVersion: messaging.knative.dev/v1beta1
kind: Subscription
metadata:
  name: eventinghelloa-sub
spec:
  channel:
    apiVersion: messaging.knative.dev/v1beta1
    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/v1beta1
kind: Subscription
metadata:
  name: eventinghellob-sub
spec:
  channel:
    apiVersion: messaging.knative.dev/v1beta1
    kind: Channel
    name: eventinghello-ch
  subscriber:
    ref:
      apiVersion: serving.knative.dev/v1
      kind: Service
      name: eventinghellob
kubectl apply -n knativetutorial -f eventing-helloa-sub.yaml
kubectl apply -n knativetutorial -f eventing-hellob-sub.yaml

Verification

kubectl -n knativetutorial get subscription.messaging.knative.dev

Running the above command should return the following result:

NAME                       AGE
eventinghelloa-sub         39s
eventinghellob-sub         39s

If you wait approximately 2 minutes for the PingSource then you will see both eventinghelloa and eventinghellob begin to run in the 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

kubectl --namespace knativetutorial channels.messaging.knative.dev eventinghello-ch

source

kubectl --namespace knativetutorial get pingsource.sources.knative.dev  eventinghello-ping-source

subscriptions

kubectl --namespace knativetutorial get subscriptions.knative.dev
Add -oyaml to the above commands to get more details about each object that were queried for.

Cleanup

kubectl -n knativetutorial delete -f eventing-helloa-sink.yaml
kubectl -n knativetutorial delete -f eventing-helloa-sub.yaml
kubectl -n knativetutorial delete -f eventing-hellob-sink.yaml
kubectl -n knativetutorial delete -f eventing-hellob-sub.yaml
kubectl -n knativetutorial delete -f event-source.yaml