|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * serverless/event_sources/serverless-kafka-source.adoc |
| 4 | +[id="serverless-kafka-source-kn_{context}"] |
| 5 | += Creating a Kafka event source by using the kn CLI |
| 6 | + |
| 7 | +This section describes how to create a Kafka event source by using the `kn` command. |
| 8 | + |
| 9 | +:FeatureName: Creating a Kafka event source by using the `kn` CLI |
| 10 | +include::../modules/technology-preview.adoc[leveloffset=+0] |
| 11 | + |
| 12 | +.Prerequisites |
| 13 | + |
| 14 | +* The {ServerlessOperatorName}, Knative Eventing, Knative Serving, and the `KnativeKafka` custom resource are installed on your cluster. |
| 15 | +* You have created a project or have access to a project with the appropriate roles and permissions to create applications and other workloads in {product-title}. |
| 16 | +* You have access to a Red Hat AMQ Streams (Kafka) cluster that produces the Kafka messages you want to import. |
| 17 | + |
| 18 | +.Procedure |
| 19 | + |
| 20 | +. To verify that the Kafka event source is working, create a Knative service that dumps incoming events into the service logs: |
| 21 | ++ |
| 22 | +[source, terminal] |
| 23 | +---- |
| 24 | +$ kn service create event-display \ |
| 25 | + --image quay.io/openshift-knative/knative-eventing-sources-event-display |
| 26 | +---- |
| 27 | + |
| 28 | +. Create a `KafkaSource` resource: |
| 29 | ++ |
| 30 | +[source,terminal] |
| 31 | +---- |
| 32 | +$ kn source kafka create mykafkasrc \ |
| 33 | + --servers my-cluster-kafka-bootstrap.kafka.svc:9092 \ |
| 34 | + --topics my-topic --consumergroup my-consumer-group \ |
| 35 | + --sink event-display |
| 36 | +---- |
| 37 | ++ |
| 38 | +The `--servers`, `--topics`, and `--consumergroup` options specify the connection parameters to the Kafka cluster. The `--consumergroup` option is optional. |
| 39 | + |
| 40 | +. Optional: View details about the `KafkaSource` resource you created: |
| 41 | ++ |
| 42 | +[source, terminal] |
| 43 | +---- |
| 44 | +$ kn source kafka describe mykafkasrc |
| 45 | +---- |
| 46 | ++ |
| 47 | +.Example output |
| 48 | +[source, terminal] |
| 49 | +---- |
| 50 | +Name: mykafkasrc |
| 51 | +Namespace: kafka |
| 52 | +Age: 1h |
| 53 | +BootstrapServers: my-cluster-kafka-bootstrap.kafka.svc:9092 |
| 54 | +Topics: my-topic |
| 55 | +ConsumerGroup: my-consumer-group |
| 56 | +
|
| 57 | +Sink: |
| 58 | + Name: event-display |
| 59 | + Namespace: default |
| 60 | + Resource: Service (serving.knative.dev/v1) |
| 61 | +
|
| 62 | +Conditions: |
| 63 | + OK TYPE AGE REASON |
| 64 | + ++ Ready 1h |
| 65 | + ++ Deployed 1h |
| 66 | + ++ SinkProvided 1h |
| 67 | +---- |
| 68 | + |
| 69 | +.Verification steps |
| 70 | + |
| 71 | +. Trigger the Kafka instance to send a message to the topic: |
| 72 | ++ |
| 73 | +[source,terminal] |
| 74 | +---- |
| 75 | +$ oc -n kafka run kafka-producer \ |
| 76 | + -ti --image=quay.io/strimzi/kafka:latest-kafka-2.7.0 --rm=true \ |
| 77 | + --restart=Never -- bin/kafka-console-producer.sh \ |
| 78 | + --broker-list my-cluster-kafka-bootstrap:9092 --topic my-topic |
| 79 | +---- |
| 80 | ++ |
| 81 | +Enter the message in the prompt. This command assumes that: |
| 82 | ++ |
| 83 | +* The Kafka cluster is installed in the `kafka` namespace. |
| 84 | +* The `KafkaSource` object has been configured to use the `my-topic` topic. |
| 85 | + |
| 86 | +. Verify that the message arrived by viewing the logs: |
| 87 | ++ |
| 88 | +[source,terminal] |
| 89 | +---- |
| 90 | +$ oc logs $(oc get pod -o name | grep event-display) -c user-container |
| 91 | +---- |
| 92 | ++ |
| 93 | +.Example output |
| 94 | +[source,terminal] |
| 95 | +---- |
| 96 | +☁️ cloudevents.Event |
| 97 | +Validation: valid |
| 98 | +Context Attributes, |
| 99 | + specversion: 1.0 |
| 100 | + type: dev.knative.kafka.event |
| 101 | + source: /apis/v1/namespaces/default/kafkasources/mykafkasrc#my-topic |
| 102 | + subject: partition:46#0 |
| 103 | + id: partition:46/offset:0 |
| 104 | + time: 2021-03-10T11:21:49.4Z |
| 105 | +Extensions, |
| 106 | + traceparent: 00-161ff3815727d8755848ec01c866d1cd-7ff3916c44334678-00 |
| 107 | +Data, |
| 108 | + Hello! |
| 109 | +---- |
0 commit comments