You can use an ApsaraMQ for Kafka instance as an input for Logstash. This topic describes how to configure Logstash to consume messages from an ApsaraMQ for Kafka instance over the internet.
Prerequisites
You must meet the following prerequisites:
-
Create and deploy an ApsaraMQ for Kafka instance. This topic uses a non-serverless instance as an example. For more information, see Connect to an instance over the internet and a VPC.
-
Download and install Logstash. For more information, see Download Logstash.
-
Download and install Java Development Kit (JDK) 8. For more information, see Download JDK 8.
Step 1: Get connection information
Logstash connects to ApsaraMQ for Kafka by using the endpoint of ApsaraMQ for Kafka and authenticates with a username and password.
Log on to the ApsaraMQ for Kafka console.
In the Resource Distribution section of the Overview page, select the region where the ApsaraMQ for Kafka instance that you want to manage resides.
-
On the Instances page, click the name of your instance.
In the Endpoint Information section of the Instance Details page, view the endpoints of the instance. In the Configuration Information section, obtain the values of the Username and Password parameters.
NoteFor information about the differences among different types of endpoints, see Endpoints and protocols.
Step 2: Create a topic
Create a topic to store messages.
Log on to the ApsaraMQ for Kafka console.
In the Resource Distribution section of the Overview page, select the region where the ApsaraMQ for Kafka instance that you want to manage resides.
ImportantYou must create topics in the region where your Elastic Compute Service (ECS) instance is deployed. A topic cannot be used across regions. For example, if the producers and consumers of messages run on an ECS instance that is deployed in the China (Beijing) region, the topic must also be created in the China (Beijing) region.
On the Instances page, click the name of the instance that you want to manage.
In the left-side navigation pane, click Topics.
On the Topics page, click Create Topic.
In the Create Topic panel, specify the properties of the topic and click OK.
Parameter
Description
Example
Name
The topic name.
demo
Description
The topic description.
demo test
Partitions
The number of partitions in the topic.
12
Storage Engine
NoteYou can specify the storage engine type only if you use a non-serverless Professional Edition instance. For other types of instances, Cloud Storage is selected by default.
The type of the storage engine that is used to store messages in the topic.
ApsaraMQ for Kafka supports the following types of storage engines:
Cloud Storage: If you select this value, the system uses Alibaba Cloud disks for the topic and stores data in three replicas in distributed mode. This storage engine features low latency, high performance, long durability, and high reliability. If you set the Instance Edition parameter to Standard (High Write) when you created the instance, you can set this parameter only to Cloud Storage.
Local Storage: If you select this value, the system uses the in-sync replicas (ISR) algorithm of open source Apache Kafka and stores data in three replicas in distributed mode.
Cloud Storage
Message Type
The message type of the topic. Valid values:
Normal Message: By default, messages that have the same key are stored in the same partition in the order in which the messages are sent. If a broker in the cluster fails, the order of messages that are stored in the partitions may not be preserved. If you set the Storage Engine parameter to Cloud Storage, this parameter is automatically set to Normal Message.
Partitionally Ordered Message: By default, messages that have the same key are stored in the same partition in the order in which the messages are sent. If a broker in the cluster fails, messages are still stored in the partitions in the order in which the messages are sent. Messages in some partitions cannot be sent until the partitions are restored. If you set the Storage Engine parameter to Local Storage, this parameter is automatically set to Partitionally Ordered Message.
Normal Message
Log Cleanup Policy
The log cleanup policy that is used by the topic.
If you set the Storage Engine parameter to Local Storage, you must configure the Log Cleanup Policy parameter. You can set the Storage Engine parameter to Local Storage only if you use an ApsaraMQ for Kafka Professional Edition instance.
ApsaraMQ for Kafka provides the following log cleanup policies:
Delete: the default log cleanup policy. If sufficient storage space is available in the system, messages are retained based on the maximum retention period. After the storage usage exceeds 85%, the system deletes the earliest stored messages to ensure service availability.
Compact: the log compaction policy that is used in Apache Kafka. Log compaction ensures that the latest values are retained for messages that have the same key. This policy is suitable for scenarios such as restoring a failed system or reloading the cache after a system restarts. For example, when you use Kafka Connect or Confluent Schema Registry, you must store the information about the system status and configurations in a log-compacted topic.
ImportantYou can use log-compacted topics only in specific cloud-native components, such as Kafka Connect and Confluent Schema Registry. For more information, see aliware-kafka-demos.
Compact
Tag
The tags that you want to attach to the topic.
demo
After a topic is created, you can view the topic on the Topics page.
Step 3: Send a message
Send a message to the topic that you created.
-
Log on to the ApsaraMQ for Kafka console.
In the Resource Distribution section of the Overview page, select the region where the ApsaraMQ for Kafka instance that you want to manage resides.
On the Instances page, click the name of the instance that you want to manage.
In the left-side navigation pane, click Topics.
On the Topics page, click the name of the topic that you want to manage. In the upper-right corner of the Topic Details page, click Send Message.Name
In the Start to Send and Consume Message panel, configure the parameters to send a message for testing.
If you set the Sending Method parameter to Console, perform the following steps:
In the Message Key field, enter the message key. Example: demo.
In the Message Content field, enter the message content. Example: {"key": "test"}.
Configure the Send to Specified Partition parameter to specify whether to send the test message to a specific partition.
If you want to send the test message to a specific partition, click Yes and enter the partition ID in the Partition ID field. Example: 0. For information about how to query partition IDs, see View partition status.
If you do not want to send the test message to a specific partition, click No.
Use ApsaraMQ for Kafka SDKs or run the Docker commands that are displayed in the Start to Send and Consume Message panel to subscribe to the test message.
If you set the Sending Method parameter to Docker, perform the following steps to run a Docker container:
Run the Docker commands that are displayed in the Run the Docker container to produce a sample message section to send the test message.
Run the Docker commands that are displayed in the How do I consume a message after the message is sent? section to subscribe to the test message.
If you set the Sending Method parameter to SDK, select an SDK for the required programming language or framework and an access method to send and subscribe to the test message.
Step 4: Create a consumer group
Create a consumer group for Logstash.
Log on to the ApsaraMQ for Kafka console.
In the Resource Distribution section of the Overview page, select the region where the ApsaraMQ for Kafka instance that you want to manage resides.
On the Instances page, click the name of the instance that you want to manage.
In the left-side navigation pane, click Groups.
On the Groups page, click Create Group.
In the Create Group panel, enter a group name in the Group ID field and a group description in the Description field, attach tags to the group, and then click OK.
After you create a consumer group, you can view the consumer group on the Groups page.
Step 5: Consume messages with Logstash
On the machine where Logstash is installed, start Logstash to consume messages from the topic that you created.
-
Run the cd command to switch to the bin directory of Logstash.
-
Run the following command to download the kafka.client.truststore.jks certificate file.
wget -O kafka.client.truststore.jks https://raw.githubusercontent.com/AliwareMQ/aliware-kafka-demos/master/kafka-log-stash-demo/vpc-ssl/mix.4096.client.truststore.jks -
Create a configuration file named jaas.conf.
-
Run the command
vim jaas.confto create an empty configuration file. Press the i key to enter the insert mode.
-
Add the following content.
KafkaClient { org.apache.kafka.common.security.plain.PlainLoginModule required username="XXX" password="XXX"; };Parameter
Description
Example
username
The username for your internet- and VPC-accessible instance.
alikafka_pre-cn-v0h1***
password
The password for your internet- and VPC-accessible instance.
GQiSmqbQVe3b9hdKLDcIlkrBK6***
Press the Esc key to return to the CLI mode.
Press the : key to enter the bottom line mode. Enter wq and press the Enter key to save the file and exit.
-
-
Create a configuration file named input.conf.
-
Run the command
vim input.confto create an empty configuration file. Press the i key to enter the insert mode.
-
Add the following content.
input { kafka { bootstrap_servers => "alikafka-pre-cn-zv**********-1.alikafka.aliyuncs.com:9093,alikafka-pre-cn-zv**********-2.alikafka.aliyuncs.com:9093,alikafka-pre-cn-zv**********-3.alikafka.aliyuncs.com:9093" topics => ["logstash_test"] security_protocol => "SASL_SSL" sasl_mechanism => "PLAIN" jaas_path => "/home/logstash-7.6.2/bin/jaas.conf" ssl_truststore_password => "KafkaOnsClient" ssl_truststore_location => "/home/logstash-7.6.2/bin/kafka.client.truststore.jks" ssl_endpoint_identification_algorithm => "" group_id => "logstash_group" consumer_threads => 3 auto_offset_reset => "earliest" } } output { stdout { codec => rubydebug } }Parameter
Description
Example
bootstrap_servers
ApsaraMQ for Kafka provides an SSL endpoint for internet access.
alikafka-pre-cn-zv**********-1.alikafka.aliyuncs.com:9093,alikafka-pre-cn-zv**********-2.alikafka.aliyuncs.com:9093,alikafka-pre-cn-zv**********-3.alikafka.aliyuncs.com:9093
topics
The name of the topic.
logstash_test
security_protocol
The security protocol. Default value: SASL_SSL. You do not need to change this value.
SASL_SSL
sasl_mechanism
The security authentication mechanism. Default value: PLAIN. You do not need to change this value.
PLAIN
jaas_path
The path to the jaas.conf configuration file.
/home/logstash-7.6.2/bin/jaas.conf
ssl_truststore_password
The password for the kafka.client.truststore.jks certificate. Default value: KafkaOnsClient. You do not need to change this value.
KafkaOnsClient
ssl_truststore_location
The path to the kafka.client.truststore.jks certificate.
/home/logstash-7.6.2/bin/kafka.client.truststore.jks
ssl_endpoint_identification_algorithm
This parameter is required for Logstash 6.x and later.
Leave this parameter empty.
group_id
The name of the consumer group.
logstash_group
consumer_threads
The number of consumer threads. We recommend that you set this value to the topic's partition count.
3
auto_offset_reset
Specifies how the consumer offset is reset. Valid values:
-
earliest: Starts consuming from the earliest message.
-
latest: Starts consuming from the latest message.
earliest
-
Press the Esc key to return to the CLI mode.
Press the : key to enter the bottom line mode. Enter wq and press the Enter key to save the file and exit.
-
-
Run the following command to consume messages.
./logstash -f input.confExample output:
{ "message" => "{xxx}", "@version" => "1", "@timestamp" => 2020-05-18T08:13:02.403Z }
More information
For more information about the parameters, see Kafka input plugin.