上一篇博客教会大家如何搭建kafka消息队列的集群,这一章增加kafka集群的密码验证,增加集群的安全性。
注:上一篇博客https://www.cnblogs.com/yclh/p/14769664.html
--------zoopkeeper改造---------
1、 修改zoopkeeper的配置文件
cd /opt/zookeeper/zookeeper-3.4.14/conf
vi zoo.cfg
增加如下内容
authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
requireClientAuthScheme=sasl
jaasLoginRenew=3600000
增加新的配置文件
vi zk_server_jaas.conf
增加如下内容
Server {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="admin"
user_kafka="kafka-user"
user_producer="prod-pwd";
user_consumer="cons-user";
};
2、jar包引入
Zookeeper部署根目录下创建一个路径for_sasl
cd /opt/zookeeper/
mkdir for_sasl
将如下这些jar包放入for_sasl 中
kafka-clients-0.10.0.1.jar
lz4-1.3.0.jar
slf4j-api-1.7.21.jar
slf4j-log4j12-1.7.21.jar
snappy-java-1.1.2.6.jar
修改配置文件
cd /opt/zookeeper/zookeeper-3.4.14/bin
vi zkEnv.sh
添加如下内容
for i in /opt/zookeeper/for_sasl/*.jar;
do CLASSPATH="$i:$CLASSPATH"
done
SERVER_JVMFLAGS=" -Djava.security.auth.login.config=/opt/zookeeper/zookeeper-3.4.14/conf/zk_server_jaas.conf "
以上操作所有节点都做
3、启动zookeeper
cd /opt/zookeeper/zookeeper-3.4.14/bin
#启动服务(3台都需要操作)
./zkServer.sh start
#检查服务器状态
./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Mode: follower #他是否为领导
------------------kafka改造------------------------------
新增配置文件:kafka_server_jaas.conf
1、修改kafka的配置文件
cd /opt/kafka/kafka_2.11-2.1.1/config
vi kafka_server_jaas.conf
添加如下内容:
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="admin"
user_admin="admin"
user_testProducer="handleP-gzwl" #预留用户begin 注:这里用户名是testProducer 密码是handleP-gzwl
user_testConsumer="handleC-gzwl"
user_testAProducer="oilStatP-gzwl"
user_testAConsumer="oilStatC-gzwl" #预留用户end
user_producer="prod-pwd"
user_consumer="cons-user";
};
Client {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="kafka"
password="kafka-user";
};
2、配置server.properties
vi server.properties
添加如下部分 注意不同主机不同的地址 192.168.0.104、192.168.0.105、192.168.0.106
listeners=SASL_PLAINTEXT://192.168.0.104:9092
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.enabled.mechanisms=PLAIN
sasl.mechanism.inter.broker.protocol=PLAIN
authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
allow.everyone.if.no.acl.found=true
原有的:advertised.listeners=PLAINTEXT://192.168.0.104:9092
也要改成SASL_PLAINTEXT
这两行注销掉
#inter.broker.protocol.version=0.9.0
#log.message.format.version=0.9.0
3、创建启动脚本
cd /opt/kafka/kafka_2.11-2.1.1/bin
cp kafka-server-start.sh kafka-server-start-sasl.sh
vi kafka-server-start-sasl.sh
将最后一行 exec $base_dir/kafka-run-class.sh $EXTRA_ARGS kafka.Kafka "$@"
改为
exec $base_dir/kafka-run-class.sh $EXTRA_ARGS -Djava.security.auth.login.config=/opt/kafka/kafka_2.11-2.1.1/config/kafka_server_jaas.conf kafka.Kafka "$@"
4、 启动kafka:
kafka-server-start-sasl.sh -daemon /opt/kafka/kafka_2.11-2.1.1/config/server.properties
5、关闭脚本
kafka-server-stop.sh
------------------kafka常用工具类改造------------------------------
1、创建kafka_client_jaas.conf文件
cd /opt/kafka/kafka_2.11-2.1.1/config
vi kafka_client_jaas.conf
添加如下内容:
KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="producer"
password="prod-pwd";
};
2、加入环境变量
cd /opt/kafka/kafka_2.11-2.1.1/config
vi admin_sasl.config
添加如下内容:
security.protocol = SASL_PLAINTEXT
sasl.mechanism = PLAIN
测试:
kafka-console-producer.sh --broker-list 192.168.0.104:9092 --topic handle
3、 使kafka-console-producer.sh好用
cd /opt/kafka/kafka_2.11-2.1.1/config
vi kafka_handle_p_jaas.conf
KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="handleProducer"
password="handleP-gzwl";
};
cd /opt/kafka/kafka_2.11-2.1.1/bin
cp kafka-console-producer.sh kafka-console-producer-sasl.sh
vi kafka-console-producer-sasl.sh
将 exec $(dirname $0)/kafka-run-class.sh kafka.tools.ConsoleProducer "$@"
改成 exec $(dirname $0)/kafka-run-class.sh -Djava.security.auth.login.config=/opt/kafka/kafka_2.11-2.1.1/config/kafka_handle_p_jaas.conf kafka.tools.ConsoleProducer "$@"
vi producer.config
添加如下内容:
security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
测试:
kafka-console-producer-sasl.sh --broker-list 192.168.0.104:9092 --topic handle --producer.config /opt/kafka/kafka_2.11-2.1.1/config/producer.config
3、使 kafka-consumer-groups.sh 变得好用
cd /opt/kafka/kafka_2.11-2.1.1/bin
cp kafka-consumer-groups.sh kafka-consumer-groups-sasl.sh
vi kafka-consumer-groups-sasl.sh
将最后一行 exec $(dirname $0)/kafka-run-class.sh kafka.admin.ConsumerGroupCommand "$@"
改为 exec $(dirname $0)/kafka-run-class.sh -Djava.security.auth.login.config=/opt/kafka/kafka_2.11-2.1.1/config/kafka_client_jaas.conf kafka.admin.ConsumerGroupCommand "$@"
注:这里的配置文件kafka_client_jaas.conf不通topic要用不同的用户名和密码
------------------------group监控----------------------------
超级用户可以查看所有group 注:stat是消费者的group
kafka-consumer-groups-sasl.sh --bootstrap-server SASL_PLAINTEXT://192.168.0.104:9092 --describe --group stat --command-config /opt/kafka/kafka_2.11-2.1.1/config/admin_sasl.config
4、 kafka-topics.sh不用改直接用
创建topic
kafka-topics.sh --create --zookeeper 192.168.0.104:12181 --replication-factor 2 --partitions 2 --topic test2
查看topic
kafka-topics.sh --list --zookeeper localhost:12181
或者
kafka-topics.sh --list --zookeeper 192.168.0.104:12181
查看topic的详细情况
kafka-topics.sh --describe --zookeeper 192.168.0.104:12181
指定topic查看
kafka-topics.sh --describe --zookeeper 192.168.0.104:12181 --topic handle
删除topic
kafka-topics.sh --delete --zookeeper 192.168.0.104:12181 --topic test
添加topic的partion
kafka-topics.sh --alter --topic jyzImageAllNew --zookeeper localhost:12181 --partitions 4