kafka开启kerberos

devtools/2024/12/22 22:19:27/

一、基本环境准备

  1. 创建票据创建Kerberos主体(Principal):

使用kadmin.local或kadmin命令为Zookeeper和Kafka服务创建Kerberos主体。例如:

注意有几台机器创建几个

kadmin.local -q "addprinc -randkey zookeeper/dshieldcdh01@HADOOP139.COM"

kadmin.local -q "addprinc -randkey zookeeper/dshieldcdh02@HADOOP139.COM" 

kadmin.local -q "addprinc -randkey zookeeper/dshieldcdh03@HADOOP139.COM"  

kadmin.local -q "addprinc -randkey kafka/dshieldcdh01@HADOOP139.COM"

kadmin.local -q "addprinc -randkey kafka/dshieldcdh02@HADOOP139.COM"

kadmin.local -q "addprinc -randkey kafka/dshieldcdh03@HADOOP139.COM"

  1. 验证主体是否创建成功

kadmin.local -q "listprincs"

[root@dshieldcdh02 ~]#  kadmin.local -q "listprincs"

Authenticating as principal root/admin@HADOOP139.COM with password.

K/M@HADOOP139.COM

host/dshieldcdh01@HADOOP139.COM

host/dshieldcdh02@HADOOP139.COM

kadmin/admin@HADOOP139.COM

kadmin/changepw@HADOOP139.COM

kadmin/dshieldcdh02@HADOOP139.COM

kafka/dshieldcdh01@HADOOP139.COM

kafka/dshieldcdh02@HADOOP139.COM

kafka/dshieldcdh03@HADOOP139.COM

kiprop/dshieldcdh02@HADOOP139.COM

krbtgt/HADOOP139.COM@HADOOP139.COM

root/admin@HADOOP139.COM

zookeeper/dshieldcdh01@HADOOP139.COM

zookeeper/dshieldcdh02@HADOOP139.COM

zookeeper/dshieldcdh03@HADOOP139.COM

  1. 创建keytab

mkdir /etc/security/keytabs/

kadmin.local -q "xst -k /etc/security/keytabs/kafka.keytab kafka/dshieldcdh01@HADOOP139.COM"

kadmin.local -q "xst -k /etc/security/keytabs/kafka.keytab kafka/dshieldcdh02@HADOOP139.COM"

kadmin.local -q "xst -k /etc/security/keytabs/kafka.keytab kafka/dshieldcdh03@HADOOP139.COM"

  1. 验证KeyTab文件内容:

klist -kt /etc/security/keytabs/zookeeper.keytab 

klist -kt /etc/security/keytabs/kafka.keytab

kinit -kt /etc/security/keytabs/zookeeper.keytab zookeeper/dshieldcdh02@HADOOP139.COM

  1. 将keytab文件拷贝到其他两天zookeeper上,需要将keytab文件拷贝过去才可以使用

scp  /etc/security/keytabs/*keytab root@dshieldcdh01:/etc/security/keytabs/                                                                                                                                                                

scp  /etc/security/keytabs/*keytab root@dshieldcdh03:/etc/security/keytabs/

  1. 在其他机器上验证keytab文件可用

kinit -kt /etc/security/keytabs/zookeeper.keytab zookeeper/dshieldcdh01@HADOOP139.COM

二、Zookeeper配置Kerberos

  1. 配置Zookeeper的JAAS文件:

在Zookeeper的配置目录下创建JAAS配置文件(如zookeeper_jaas.conf),内容如下:

java

Server { 

  com.sun.security.auth.module.Krb5LoginModule required 

  useKeyTab=true 

  storeKey=true 

  keyTab="/etc/security/keytabs/zookeeper.keytab" 

  principal="zookeeper/dshieldcdh01@HADOOP139.COM" 

  useTicketCache=false; 

}; 

Client { 

  com.sun.security.auth.module.Krb5LoginModule required 

  useKeyTab=true 

  storeKey=true 

  keyTab="/etc/security/keytabs/zookeeper.keytab" 

  principal="zookeeper/dshieldcdh01@HADOOP139.COM" 

  useTicketCache=false; 

};

注意修改principal和keyTab路径以匹配实际环境。

在Zookeeper的启动脚本中添加JVM参数,指定JAAS配置文件的路径。

配置zookeeper的kerberos验证,切换到配置文件目录下cd conf,添加zoo.cfg配置文件,cp zoo_sample.cfg zoo.cfg打开zoo.cfg配置文件,添加配置,修改Zookeeper的配置文件cat zoo.cfg 启用SASL认证,并指定认证提供者。

authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider

jaasLoginRenew=3600000

kerberos.removeHostFromPrincipal=true

kerberos.removeRealmFromPrincipal=true

export JVMFLAGS="-Djava.security.auth.login.config= /usr/local/apache-zookeeper-3.6.4/conf/zookeeper_jaas.conf"

scp /usr/local/apache-zookeeper-3.6.4/conf/zookeeper_jaas.conf root@dshieldcdh02:/usr/local/apache-zookeeper-3.6.4/conf     

scp /usr/local/apache-zookeeper-3.6.4/conf/zookeeper_jaas.conf root@dshieldcdh03:/usr/local/apache-zookeeper-3.6.4/conf

[root@dshieldcdh03 ~]# cat /usr/local/apache-zookeeper-3.6.4/conf/zookeeper_jaas.conf

Server {

com.sun.security.auth.module.Krb5LoginModule required

useKeyTab=true

storeKey=true

useTicketCache=false

keyTab="/etc/security/keytabs/zookeeper.keytab"

principal="zookeeper/dshieldcdh03@HADOOP139.COM";

};

cat /usr/local/apache-zookeeper-3.6.4/conf/zookeeper_client_jaas.conf

Client {

com.sun.security.auth.module.Krb5LoginModule required

useKeyTab=true

storeKey=true

useTicketCache=false

keyTab="/usr/local/apacje-zookeeper-3.6.4/conf/zk.service.keytab"

principal="zookeeper/dshieldcdh03@HADOOP139.COM";

};

三、Kafka配置Kerberos

将kafka用户的keytab文件拷贝到其他服务器上

scp /etc/security/keytabs/kafka.keytab root@ dshieldcdh02:/etc/security/keytabs/kafka.keytab

配置Kafka的JAAS文件:

在Kafka的配置目录下创建JAAS配置文件(如kafka_client_jaas.conf),内容如下:

kafka_client_jaas.conf

KafkaServer {

        com.sun.security.auth.module.Krb5LoginModule required

        useKeyTab=true

        keyTab="/etc/security/keytabs/kafka.keytab"

        storeKey=true

        useTicketCache=false

        serviceName="kafka"

        principal="kafka/dshieldcdh01@HADOOP139.COM";

        };

        KafkaClient {

        com.sun.security.auth.module.Krb5LoginModule required

        useKeyTab=true

        keyTab="/etc/security/keytabs/kafka.keytab"

        storeKey=true

        useTicketCache=false

        serviceName="kafka"

        principal="kafka/dshieldcdh01@HADOOP139.COM";

        };

        Client {

        com.sun.security.auth.module.Krb5LoginModule required

        useKeyTab=true

        keyTab="/etc/security/keytabs/zookeeper.keytab"

        storeKey=true

        useTicketCache=false

        serviceName="zookeeper"

        principal=" zookeeper/dshieldcdh01@HADOOP139.COM";

        };

        com.sun.security.jgss.krb5.initiate {

        com.sun.security.auth.module.Krb5LoginModule required

        renewTGT=false

        doNotPrompt=true

        useKeyTab=true

        keyTab="/etc/security/keytabs/kafka.keytab"

        storeKey=true

        useTicketCache=false

        serviceName="kafka"

        principal="kafka/dshieldcdh01@HADOOP139.COM";

        };

kafka_server_jaas.conf

KafkaServer {

        com.sun.security.auth.module.Krb5LoginModule required

        useKeyTab=true

        keyTab="/etc/security/keytabs/kafka.keytab"

        storeKey=true

        useTicketCache=false

        serviceName="kafka"

        principal="kafka/dshieldcdh01@HADOOP139.COM";

        };

        KafkaClient {

        com.sun.security.auth.module.Krb5LoginModule required

        useKeyTab=true

        keyTab="/etc/security/keytabs/kafka.keytab"

        storeKey=true

        useTicketCache=false

        serviceName="kafka"

        principal="kafka/dshieldcdh01@HADOOP139.COM";

        };

        Client {

        com.sun.security.auth.module.Krb5LoginModule required

        useKeyTab=true

        keyTab="/etc/security/keytabs/zookeeper.keytab"

        storeKey=true

        useTicketCache=false

        serviceName="zookeeper"

        principal=" zookeeper/dshieldcdh01@HADOOP139.COM";

        };

        com.sun.security.jgss.krb5.initiate {

        com.sun.security.auth.module.Krb5LoginModule required

        renewTGT=false

        doNotPrompt=true

        useKeyTab=true

        keyTab="/etc/security/keytabs/kafka.keytab"

        storeKey=true

        useTicketCache=false

        serviceName="kafka"

        principal="kafka/dshieldcdh01@HADOOP139.COM";

        };

注意修改principal、keyTab路径和serviceName以匹配实际环境。

修改Kafka的启动脚本:

在Kafka的启动脚本中添加JVM参数,指定JAAS配置文件的路径。

cat kafka_client_jaas.conf

kafkaClient {

        com.sun.security.auth.module.Krb5LoginModule required

        useKeyTab=true

storeKey=true

serviceName=kafka

        keyTab="/etc/security/keytabs/kafka.keytab"

        principal="kafka/dshieldcdh01@HADOOP139.COM";

};

cat server.properties

broker.id=1

hostname=dshieldcdh01

listerners=SASL_PLAINTEXT://dshieldcdh01:9092

security.inter.broker.protocol=SASL_PLAINTEXT

sasl.mechanism.inter.broker.protocol=GSSAPI

sasl.enabled.mechanisms= GSSAPI

sasl.kerberos.service.name=kaka

zookeeper.connect=dshieldcdh01:2181, dshieldcdh02:2181, dshieldcdh03:2181

zookeeper.set.acl=true

zookeeper.connection.timeout.ms=18000

[kafka@dshieldcdh01 config]$ pwd

/usr/local/kafka/config

[kafka@dshieldcdh01 config]$ scp kafka_jaas.conf dshieldcdh02:/usr/local/kafka/config

scp kafka_jaas.conf dshieldcdh03:/usr/local/kafka/config

#kerberos

listeners=SASL_PLAINTEXT://ambarim2:9092

advertised.listeners=SASL_PLAINTEXT://ambarim2:9092

security.inter.broker.protocol=SASL_PLAINTEXT

sasl.mechanism.inter.broker.protocol=GSSAPI

principal.to.local.class=kafka.security.auth.KerberosPrincipalToLocal

isasl.enabled.mechanisms=GSSAPI

zookeeper.connect=dshieldcdh01:2181,dshieldcdh02:2181,dshieldcdh03:2181


http://www.ppmy.cn/devtools/97369.html

相关文章

使用C#禁止Windows系统插入U盘(除鼠标键盘以外的USB设备)

试用网上成品的禁用U盘的相关软件,发现使用固态硬盘改装的U盘以及手机等设备,无法被禁止,无奈下,自己使用C#手搓了一个。 基本逻辑: 开机自启;启动时,修改注册表,禁止系统插入USB存…

微信小程序的四种弹窗使用

​ 在做小程序的过程中,弹窗也算是非常实用的功能了,这几天写的几个功能就用到了弹窗,也可能是初学者的问题,比较菜,想找一个可以带图片的自定义的弹窗,,这里简单介绍一下官方封装好的四个弹窗…

leetcode108.把升序数组转换成二叉搜索树

题目描述 [-10,-3,0,5,9] 转换成如下二叉搜索树: 解题的核心原理是:二叉搜索树的中序遍历结果是一个升序数组,所以根节点的数值,也位于数组的中部。 class Solution {public TreeNode sortedArrayToBST(int[] nums) {return h…

设计模式实战:数据分析系统的设计与实现

在数据驱动的业务环境中,数据分析系统是决策支持的核心工具。为了构建一个高效、灵活的数据分析系统,我们可以结合多种设计模式,如策略模式、装饰模式和模板方法模式。本文将详细介绍这些模式在数据分析系统中的应用,帮助开发者设计出可扩展且易于维护的系统。 系统设计流…

【TCP】核心机制:延时应答、捎带应答和面向字节流

文章目录 延时应答捎带应答面向字节流粘包问题方案一:指定分隔符方案二:指定数据的长度 TCP 报头首部长度保留(6 位)选项序号确认序号 延时应答 尽可能降低可靠传输带来的性能影响 提升性能>让滑动窗口变大 如果我们立即返回 …

05--kubernetes组件与安装

前言:终于写到kubernetes(k8s),容器编排工具不止k8s一个,它的优势在于搭建集群,也是传统运维和云计算运维的第一道门槛,这里会列出两种安装方式,详细步骤会在下文列出,文…

docker部署LNMP

docker部署LNMP nginx 1.22 172.111.0.10 docker-nginx mysql 8.0.30 172.111.0.20 docker-mysql php 8.1.27 172.111.0.30 docker-php docker:单节点部署,只能在一台机器上部署,如果跨机器容器无法操作,无法通信。 做高可用…

Python实现GAN(生成对抗网络)图像修复算法

目录 1. GAN简介与图像修复2. PyTorch和CUDA简介3. 数据加载与预处理3.1 安装依赖3.2 数据加载3.3 数据遮挡4. 构建GAN图像修复模型4.1 生成器4.2 判别器5. 训练GAN模型5.1 损失函数与优化器5.2 训练循环6. 测7. 实现GUI进行图像修复8. 总结与扩展扩展方向:1. GAN简介与图像修…