一:生成配置cert
生成SSL证书
使用jre自带的keytool工具生成,linux和windows下生成的证书可以通用
生成含有一个私钥的keystore文件,有效期10年(本文证书密码统一使用test123)
keytool -genkeypair -alias certificatekey -dname “CN=127.0.0.1, OU=127.0.0.1, O=127.0.0.1, L=SH, ST=SH, C=CN” -keyalg RSA -validity 3650 -keystore keystore.jks
查看生成的keystore文件
keytool -list -v -keystore keystore.jks
导出证书
keytool -export -alias certificatekey -keystore keystore.jks -rfc -file selfsignedcert.cer
导入证书到truststore文件中
keytool -import -alias certificatekey -file selfsignedcert.cer -keystore truststore.jks
查看生成的truststore文件
keytool -list -v -keystore truststore.jks
二、分别修改kafka配置文件server.properties
vim server.properties
listeners=PLAINTEXT://hadoop100:9092,SSL://hadoop100:9093
#server.keystore.jks的地址
ssl.keystore.location=/opt/kafka_2.13-2.5.0/config/cret/keystore.jks
ssl.keystore.password=test123
ssl.truststore.location=/opt/kafka_2.13-2.5.0/config/cret/truststore.jks
ssl.truststore.password=test123
ssl.client.auth=required
ssl.enabled.protocols=TLSv1.2
ssl.keystore.type=JKS
ssl.truststore.type=JKS
#关闭主机名
ssl.endpoint.identification.algorithm=
#设置内部访问也用SSL,默认值为security.inter.broker.protocol=PLAINTEXT
security.inter.broker.protocol=SSL
启动zk,kafka
bin/zookeeper-server-start.sh -daemon config/zookeeper.properties
bin/kafka-server-start.sh -daemon config/server.properties
三、创建clientssl.properties
security.inter.broker.protocol=SSL
ssl.keystore.location=/opt/kafka_2.13-2.5.0/config/cret/keystore.jks
ssl.keystore.password=test123
ssl.key.password=test123
ssl.truststore.location=/opt/kafka_2.13-2.5.0/config/cret/truststore.jks
ssl.truststore.password=test123
四:测试
发送消息
bin/kafka-console-producer.sh --broker-list 192.168.50.189:9093 --topic my-topic-kraft --producer.config client-ssl.properties
查看消息
bin/kafka-console-consumer.sh --bootstrap-server 192.168.50.189:9093 --topic my-topic-kraft --consumer.config client-ssl.properties