Connecting to Kafka Using the Client (Ciphertext Access)
This section describes how to access a Kafka instance with SASL enabled on an open-source Kafka client. With SASL enabled, there is SASL authentication required in such a connection and data is transmitted securely in ciphertext.
Notes and Constraints
- For security purposes, TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, and TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 are supported.
- Each Kafka broker allows a maximum of 1000 connections from each IP address by default. Excess connections will be rejected. You can change the limit by referring to Modifying Kafka Instance Configuration Parameters, that is, to modify parameter max.connections.per.ip.
Prerequisites
- The network between the client and the Kafka instance is available. For details about the network requirements, see Kafka Network Connection Conditions.
- Security group rules have been properly configured.
Before accessing a Kafka instance with ciphertext access enabled on a client, configure proper security group rules for the instance. For details, see Table 2.
- The Kafka instance address has been obtained.
Obtain the instance connection address in the Connection area on the Overview page on the Kafka console.
For private access within a VPC, the Kafka connection addresses are shown as follows.
Figure 1 Kafka instance addresses for private access within a VPC - The SASL mechanism in use is known.
In the Connection area on the Kafka instance details page, view SASL Mechanism. For instances that were created much earlier, if SASL Mechanism is not displayed on the instance details page, PLAIN is used by default.
Figure 2 SASL mechanism in use - The security protocol in use is known.
In the Connection area on the Kafka instance details page, view Security Protocol. For instances that were created much earlier, if Security Protocol is not displayed on the instance details page, SASL_SSL is used by default.
- If automatic topic creation is not enabled for the Kafka instance, create a topic before connecting to the instance.
- The client.truststore.jks certificate has been downloaded. Click the Kafka instance to go to the Basic Information tab page. Click Download next to SSL Certificate in the Connection area. Download and decompress the package to obtain the client certificate file client.truststore.jks.
- Compatible Kafka CLI is available. Ensure that the Kafka instance and the CLI use the same version.
- JDK v1.8.111 or later has been installed on the server, and the JAVA_HOME and PATH environment variables have been configured as follows:
Add the following lines to the .bash_profile file in the home directory as an authorized user. In this command, /opt/java/jdk1.8.0_151 is the JDK installation path. Change it to the path where you install JDK.
export JAVA_HOME=/opt/java/jdk1.8.0_151 export PATH=$JAVA_HOME/bin:$PATH
Run the source .bash_profile command for the modification to take effect.
Accessing the Instance Using CLI
The following uses Linux as an example.
- Map hosts to IP addresses in the /etc/hosts file on the host where the client is located, so that the client can quickly parse the instance brokers.
- Run the following command to edit the /etc/hosts file:
vim /etc/hosts
- Press i and add the mapping between the host and IP address to the /etc/hosts file.
ip 1 host name 1 ip 2 host name 2 ip 3 host name 3
Table 1 Parameters in the /etc/hosts file Parameter
Description
ip
Connection address of the Kafka instance, which is obtained from Prerequisites.
host name
Name of each instance host.
Host names are user-defined and must be unique.
For example:
10.154.48.120 server01 10.154.48.121 server02 10.154.48.122 server03
- Press Esc. Enter the following line and press Enter. Save the /etc/hosts file and exit.
:wq
- Run the following command to edit the /etc/hosts file:
- Decompress the Kafka CLI package.
Access the directory where the CLI package is stored and run the following command to decompress the package:
tar -zxf {kafka_tar}
In the preceding command, {kafka_tar} indicates the name of the CLI package.
For example:
tar -zxf kafka_2.12-2.7.2.tgz
- Modify the configuration files of the Kafka CLI.
Configuration files: producer.properties and consumer.properties
Modify the producer.properties file.
- Go to the /config directory of the Kafka CLI.
cd {kafka_tar}/config
- Edit the producer.properties file.
vim producer.properties
- Press i and add the following content to the producer.properties file:
- Kafka instances support two authentication mechanisms. Add the one obtained in SASL authentication mechanism to the configuration file. If both SCRAM-SHA-512 and PLAIN are enabled, use either of them in connection configurations.
- PLAIN:
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \ username="**********" \ password="**********"; sasl.mechanism=PLAIN
Table 2 SASL authentication mechanism parameters Parameter
Description
username
Set in instance creation or user creation.
password
Set in instance creation or user creation.
- SCRAM-SHA-512:
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required \ username="**********" \ password="**********"; sasl.mechanism=SCRAM-SHA-512
Table 3 SASL authentication mechanism parameters Parameter
Description
username
Set in instance creation or user creation.
password
Set in instance creation or user creation.
- PLAIN:
- Kafka instances support two security protocols. Add the one obtained in the security protocol to the configuration file.
- SASL_SSL:
security.protocol=SASL_SSL ssl.truststore.location={ssl_truststore_path} ssl.truststore.password=dms@kafka ssl.endpoint.identification.algorithm=
Table 4 SASL_SSL parameters Parameter
Description
ssl.truststore.location
Path for storing the client.truststore.jks certificate
Even in Windows, you need to use slashes (/) for the certificate path. Do not use backslashes (\), which are used by default for paths in Windows. Otherwise, the client will fail to obtain the certificate.
ssl.truststore.password
Password of the Kafka client certificate.
dms@kafka by default and cannot be changed.
ssl.endpoint.identification.algorithm
Whether to verify the certificate domain name This parameter must be left blank, which indicates disabling domain name verification.
- SASL_PLAINTEXT:
security.protocol=SASL_PLAINTEXT
- SASL_SSL:
- Kafka instances support two authentication mechanisms. Add the one obtained in SASL authentication mechanism to the configuration file. If both SCRAM-SHA-512 and PLAIN are enabled, use either of them in connection configurations.
- Press Esc. Enter the following line and press Enter. Save the producer.properties file and exit.
:wq
Modify the consumer.properties file.
- Edit the consumer.properties file.
vim consumer.properties
- Press i and add the following content to the consumer.properties file:
- Kafka instances support two authentication mechanisms. Add the one obtained in SASL authentication mechanism to the configuration file. If both SCRAM-SHA-512 and PLAIN are enabled, use either of them in connection configurations.
- PLAIN:
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \ username="**********" \ password="**********"; sasl.mechanism=PLAIN
Table 5 SASL authentication mechanism parameters Parameter
Description
username
Set in instance creation or user creation.
password
Set in instance creation or user creation.
- SCRAM-SHA-512:
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required \ username="**********" \ password="**********"; sasl.mechanism=SCRAM-SHA-512
Table 6 SASL authentication mechanism parameters Parameter
Description
username
Set in instance creation or user creation.
password
Set in instance creation or user creation.
- PLAIN:
- Kafka instances support two security protocols. Add the one obtained in the security protocol to the configuration file.
- SASL_SSL:
security.protocol=SASL_SSL ssl.truststore.location={ssl_truststore_path} ssl.truststore.password=dms@kafka ssl.endpoint.identification.algorithm=
Table 7 SASL_SSL parameters Parameter
Description
ssl.truststore.location
Path for storing the client.truststore.jks certificate
Even in Windows, you need to use slashes (/) for the certificate path. Do not use backslashes (\), which are used by default for paths in Windows. Otherwise, the client will fail to obtain the certificate.
ssl.truststore.password
Password of the Kafka client certificate.
dms@kafka by default and cannot be changed.
ssl.endpoint.identification.algorithm
Whether to verify the certificate domain name This parameter must be left blank, which indicates disabling domain name verification.
- SASL_PLAINTEXT:
security.protocol=SASL_PLAINTEXT
- SASL_SSL:
- Kafka instances support two authentication mechanisms. Add the one obtained in SASL authentication mechanism to the configuration file. If both SCRAM-SHA-512 and PLAIN are enabled, use either of them in connection configurations.
- Press Esc. Enter the following line and press Enter. Save the consumer.properties file and exit.
:wq
- Go to the /config directory of the Kafka CLI.
- Access the /bin directory of the Kafka CLI.
In Windows, you need to access the /bin/windows directory.
cd ../bin
- Run the following command to create messages:
./kafka-console-producer.sh --broker-list {connection addr} --topic {topic name} --producer.config ../config/producer.properties
Table 8 Message production parameters Parameter
Description
Connection Address
Connection address of the Kafka instance.
Obtained in Prerequisites.
Topic Name
The name of the topic created for the Kafka instance.
If automatic topic creation is enabled for the Kafka instance, set this parameter to the name of a created topic or a topic that has not been created.
The following example uses connection addresses 10.xx.xx.45:9093,10.xx.xx.127:9093,10.xx.xx.103:9093.
After running the preceding command, you can send a message to the Kafka instance by writing it and pressing Enter. Each line of content is sent as a message.
[root@ecs-kafka bin]#./kafka-console-producer.sh --broker-list 10.xx.xx.45:9093,10.xx.xx.127:9093,10.xx.xx.103:9093 --topic topic-demo --producer.config ../config/producer.properties >Hello >DMS >Kafka! >^C[root@ecs-kafka bin]#
To stop producing messages, press Ctrl+C to exit.
- Run the following command to retrieve messages:
./kafka-console-consumer.sh --bootstrap-server {connection-address} --topic {topic-name} --group {consumer-group-name} --from-beginning --consumer.config ../config/consumer.properties
Table 9 Message consumption parameters Parameter
Description
Connection Address
Connection address of the Kafka instance.
Obtained in Prerequisites.
Topic Name
The name of the topic created for the Kafka instance.
Consumer Group Name
The consumer group name set based on your service requirements. If a consumer group name starts with a special character, for example, a number sign (#), monitoring data cannot be displayed.
WARNING:Ensure that the consumer group name in the command line is the same as that specified in the configuration file (consumer.properties), if any. Otherwise, message consumption may fail.
To view and change the consumer group name in the configuration file, see 6.a.
To view and change the consumer group name in the configuration file:
- Access the /config directory of the Kafka CLI.
cd ../config
- Edit the consumer.properties file.
vim consumer.properties
- Press i to view and change the consumer group name.
group.id indicates the consumer group name. You can change the name as required.
# consumer group id group.id=test-consumer-group
- Press Esc. Enter the following line and press Enter. Save the consumer.properties file and exit.
:wq
Sample message consumption:
[root@ecs-kafka bin]# ./kafka-console-consumer.sh --bootstrap-server 10.xx.xx.45:9093,10.xx.xx.127:9093,10.xx.xx.103:9093 --topic topic-demo --group order-test --from-beginning --consumer.config ../config/consumer.properties Hello DMS Kafka! ^CProcessed a total of 3 messages [root@ecs-kafka bin]#
To stop consuming messages, press Ctrl+C to exit.
- Access the /config directory of the Kafka CLI.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot