Subscriptions¶
After you have created a Channel and a Sink, you can create a Subscription to enable event delivery.
The Subscription consists of a Subscription object, which specifies the Channel and the Sink (also known as the Subscriber) to deliver events to. You can also specify some Sink-specific options, such as how to handle failures.
For more information about Subscription objects, see Subscription.
Creating a Subscription¶
Create a Subscription between a Channel and a Sink by running:
kn subscription create <subscription-name> \
--channel <channel-name> \
--sink <sink-prefix>:<sink-name>
Optional flags: --sink-reply <sink-prefix>:<sink-name> (where reply events are sent) and --sink-dead-letter <sink-prefix>:<sink-name> (where failed events are sent).
--channelspecifies the source Channel. If you are not using the default Channel backed by theChannelresource, prefix the name with<Group:Version:Kind>:(for example,messaging.knative.dev:v1beta1:KafkaChannel:mychannel).--sinkspecifies the target destination (defaults to a Knative Service in the same namespace if no prefix is given). Supported prefixes:ksvc(Knative Service),svc(Kubernetes Service),channel(default Channel), orbroker(Eventing Broker).
-
Create a YAML file for the Subscription object using the following example:
apiVersion: messaging.knative.dev/v1 kind: Subscription metadata: name: <subscription-name> # Name of the Subscription. namespace: default spec: channel: apiVersion: messaging.knative.dev/v1 kind: Channel name: <channel-name> # Name of the Channel that the Subscription connects to. delivery: # Optional delivery configuration settings for events. deadLetterSink: # When this is configured, events that failed to be consumed are sent to the deadLetterSink. # The event is dropped, no re-delivery of the event is attempted, and an error is logged in the system. # The deadLetterSink value must be a Destination. ref: apiVersion: serving.knative.dev/v1 kind: Service name: <service-name> reply: # Optional configuration settings for the reply event. # This is the event Sink that events replied from the subscriber are delivered to. ref: apiVersion: messaging.knative.dev/v1 kind: InMemoryChannel name: <service-name> subscriber: # Required configuration settings for the Subscriber. This is the event Sink that events are delivered to from the Channel. ref: apiVersion: serving.knative.dev/v1 kind: Service name: <service-name> -
Apply the YAML file by running the command:
Wherekubectl apply -f <filename>.yaml<filename>is the name of the file you created in the previous step.
Listing Subscriptions¶
You can list all existing Subscriptions by using the kn CLI tool.
-
List all Subscriptions:
kn subscription list -
List Subscriptions in YAML format:
kn subscription list -o yaml
Describing a Subscription¶
You can print details about a Subscription by using the kn CLI tool:
kn subscription describe <subscription-name>
Example:
kn subscription describe mysubscription
Expected output
Name: mysubscription
Namespace: default
Annotations: messaging.knative.dev/creator=user@example.com
messaging.knative.dev/lastModifier=user@example.com
Age: 1h
Channel: Channel:mychannel (messaging.knative.dev/v1)
Subscriber:
URI: http://myservice.default.svc.cluster.local
Reply:
Name: myreply
Kind: InMemoryChannel
API Version: messaging.knative.dev/v1
DeadLetterSink:
Name: mydlq
Kind: Service
API Version: serving.knative.dev/v1
Conditions:
OK TYPE AGE REASON
++ Ready 1h
++ AddedToChannel 1h
++ ChannelReady 1h
Updating a Subscription¶
You can update an existing Subscription by using the kn CLI tool or by applying an updated YAML file.
Use the kn subscription update command to modify an existing Subscription.
Example:
kn subscription update mysubscription \
--sink ksvc:myservice \
--sink-dead-letter ksvc:error-handler \
--sink-reply channel:reply-channel
You can update individual parameters as needed:
- --sink: Change the subscriber (destination for events)
- --sink-dead-letter: Add or update the dead letter sink for failed deliveries
- --sink-reply: Update where reply events are sent
Note
When updating a Subscription, you must provide all the configuration parameters you want to keep. Any parameters not specified in the update command will use default values.
-
Get the current Subscription configuration:
kubectl get subscription <subscription-name> -o yaml > subscription.yaml -
Edit the YAML file to make your desired changes to the
specsection. -
Apply the updated YAML file:
kubectl apply -f subscription.yaml
Deleting Subscriptions¶
You can delete a Subscription by using the kn or kubectl CLI tools.
kn subscription delete <subscription-name>
kubectl delete subscription <subscription-name>