With the OpenShift Application Services rhoas
command-line interface (CLI), you can manage OpenShift Streams for Apache Kafka from a terminal.
As a developer of applications and services,
you can use rhoas
to create Kafka instances and connect your applications and services to these instances.
You can also create Kafka topics to store data for producers and consumers.
This guide describes how to get started quickly by doing the following:
-
You’ve a command-line terminal application.
-
You’ve a registered OpenShift cluster.
-
You’ve installed the
rhoas
CLI. For more information, see Installing the rhoas CLI.
A Kafka instance includes a Kafka cluster, bootstrap server, and other required configurations for connecting to Kafka producer and consumer services.
-
You’re logged in to
rhoas
.
-
On the command line, create a Kafka instance.
Creating a Kafka instance$ rhoas kafka create
-
Type
my-kafka
for your instance name. -
When prompted to select a cluster, use the arrows on your keyboard to select your OpenShift cluster and press Enter to provision your Kafka instance.
-
When prompted to select an instance size, use the arrows on your keyboard to select 1 or 2 streaming units, and press Enter.
A streaming unit determines the default maximum capacity of a Kafka instance. An instance with a larger size can handle higher loads and process more events, has more storage, and can handle more clients and connections.
The CLI displays information about the created instance including billing model, cloud provider, cluster ID of the cluster the instance is provisioned on, and instance type.
CLI uses service contexts to group service instances against which commands are executed. The created Kafka instance is set automatically in the current context.
NoteAlternatively, to provision a Kafka instance by specifying the Kafka name and cluster ID, enter the following command:
rhoas kafka create --name my-kafka --cluster-id <CLUSTER_ID>
. Replace <CLUSTER_ID> with your cluster ID. Using this command creates an instance with the default value size of 1 streaming unit. -
Verify the details and
ready
status of a Kafka instance.Verifying the Kafka instance$ rhoas context status kafka
NoteIf you have multiple Kafka instances, you can make the current context switch to a different instance by using the
rhoas context set-kafka
command.
To connect your applications or services to a Kafka instance, you must first create a service account with credentials. The credentials are exported to a file on your computer, which you can use to authenticate your application with your Kafka instance.
-
You’ve created a Kafka instance, and it has a
ready
status.
-
Create a service account.
This example creates a service account and saves the credentials in a JSON file.
Creating a service account$ rhoas service-account create --file-format json --short-description="rhoas-service-account"
NoteWhen creating a service account, you can choose the file format and location to save the credentials. For more information, see the
rhoas service-account create
command help. -
To verify your service account credentials, view the
credentials.json
file that you created.Examplecredentials.json
file{ "clientID":"<client-id>", "clientSecret":"<client-secret>", "oauthTokenUrl": "https://sso.redhat.com/auth/realms/redhat-external/protocol/openid-connect/token" }
You use these credentials and the bootstrap server URL to connect your applications and services to your Kafka instance.
-
Set the permissions for your service account to access the Kafka instance resources.
This example allows applications to produce and consume messages from topics starting with a specified prefix that belong to any consumer group. The service account information you input here is the
clientID
value from the previous step.Assigning access permissions to a service account$ rhoas kafka acl grant-access --consumer --producer \ --service-account <client-id> --topic-prefix myapp --group all
The permissions you assign depend on whether your application produces or consumes messages.
After creating a Kafka instance, you can create Kafka topics to start producing and consuming messages in your services.
-
You’ve created a Kafka instance, and it has a
ready
status.
Note
|
You can use List Kafka instances
$ rhoas kafka list Selecting a Kafka instance to use
$ rhoas context set-kafka --name my-kafka |
-
Create a Kafka topic with default values.
This example creates the
my-topic
Kafka topic.Creating a Kafka topic with default values$ rhoas kafka topic create --name my-topic
NoteIf you do not want to use the default values, you can specify the number of partitions (
--partitions
) and message retention time (--retention-ms
). For more information, use the command helprhoas kafka topic create -h
. -
If necessary, you can edit or delete the topic by using the
rhoas kafka topic update
andrhoas kafka topic delete
commands.
After creating a Kafka instance, you can generate a configuration file that your applications can use to connect to your Kafka instance.
-
You’ve created a Kafka instance, and it has a
ready
status. -
The Kafka instance is set in the current context.
-
Your user account and org have quota for creating service accounts.
-
Generate a configuration file for the current service context.
This example generates a JSON file with configurations for the Kafka instance you created.
Generating a configuration file$ rhoas generate-config --type json
For more information about the rhoas
commands you can use to manage your Kafka instance,
use the following command help:
-
rhoas kafka -h
for Kafka instances -
rhoas service-account -h
for service accounts -
rhoas kafka acl -h
for access management -
rhoas kafka topic -h
for Kafka topics