Hadoop Kerberos 集成

Posted hyunbar777

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hadoop Kerberos 集成相关的知识,希望对你有一定的参考价值。

Hadoop

官网参考:https://hadoop.apache.org/docs/r2.7.3/hadoop-project-dist/hadoop-common/SecureMode.html

2、创建Hadoop系统用户

为Hadoop开启Kerberos,需为不同服务准备不同的用户,启动服务时需要使用相应的用户。须在所有节点创建以下用户和用户组。

User:Group

Daemons

hdfs:hadoop

NameNode, Secondary NameNode, JournalNode, DataNode

yarn:hadoop

ResourceManager, NodeManager

mapred:hadoop

MapReduce JobHistory Server


Hadoop

创建hadoop组

[root@hadoop01 ~]# groupadd hadoop
[root@hadoop02 ~]# groupadd hadoop
[root@hadoop03 ~]# groupadd hadoop

创建各用户并设置

[root@hadoop01 ~]# useradd hdfs -g hadoop
[root@hadoop01 ~]# echo hdfs | passwd --stdin  hdfs
[root@hadoop01 ~]# useradd yarn -g hadoop
[root@hadoop01 ~]# echo yarn | passwd --stdin yarn
[root@hadoop01 ~]# useradd mapred -g hadoop
[root@hadoop01 ~]# echo mapred | passwd --stdin mapred
[root@hadoop02 ~]# useradd hdfs -g hadoop
[root@hadoop02 ~]# echo hdfs | passwd --stdin  hdfs
[root@hadoop02 ~]# useradd yarn -g hadoop
[root@hadoop02 ~]# echo yarn | passwd --stdin yarn
[root@hadoop02 ~]# useradd mapred -g hadoop
[root@hadoop02 ~]# echo mapred | passwd --stdin mapred
[root@hadoop03 ~]# useradd hdfs -g hadoop
[root@hadoop03 ~]# echo hdfs | passwd --stdin  hdfs
[root@hadoop03 ~]# useradd yarn -g hadoop
[root@hadoop03 ~]# echo yarn | passwd --stdin yarn
[root@hadoop03 ~]# useradd mapred -g hadoop
[root@hadoop03 ~]# echo mapred | passwd --stdin mapred

3、Hadoop Kerberos配置

3.1 为Hadoop各服务创建Kerberos主体(Principal)

主体格式如下:​​ServiceName/HostName@REALM​​如:​​dn/hadoop01@EXAMPLE.COM​

  • 各服务所需主体如下

环境:3台节点,主机名分别为hadoop01,hadoop02,hadoop03

服务

所在主机

主体(Principal)

NameNode

hadoop01

nn/hadoop01

DataNode

hadoop01

dn/hadoop01

DataNode

hadoop02

dn/hadoop02

DataNode

hadoop03

dn/hadoop03

Secondary NameNode

hadoop03

sn/hadoop03

ResourceManager

hadoop02

rm/hadoop02

NodeManager

hadoop01

nm/hadoop01

NodeManager

hadoop02

nm/hadoop02

NodeManager

hadoop03

nm/hadoop03

JobHistory Server

hadoop01

jhs/hadoop01

Web UI

hadoop01

HTTP/hadoop01

Web UI

hadoop02

HTTP/hadoop02

Web UI

hadoop03

HTTP/hadoop03

  • 创建主体说明

1)路径准备

为服务创建的主体,需要通过密钥文件keytab文件进行认证,故需为各服务准备一个安全的路径用来存储keytab文件。

[root@hadoop01 ~]# mkdir /etc/security/keytab/
[root@hadoop01 ~]# chown -R root:hadoop /etc/security/keytab/
[root@hadoop01 ~]# chmod 770 /etc/security/keytab/

2)管理员主体认证

为执行创建主体的语句,需登录Kerberos 数据库客户端,登录之前需先使用Kerberos的管理员用户进行认证,执行以下命令并根据提示输入。

[root@hadoop01 ~]# kinit admin/admin

3)登录数据库客户端

[root@hadoop01 ~]# kadmin

4)执行创建主体的语句

kadmin:  addprinc -randkey test/test
kadmin:  xst -k /etc/security/keytab/test.keytab test/test

说明:

(1)addprinc test/test:作用是新建主体

  • addprinc:增加主体
  • -randkey:随机,因hadoop各服务均通过keytab文件认证,故可随机生成
  • test/test:新增的主体

(2)xst -k /etc/security/keytab/test.keytab test/test:作用是将主体的密钥写入keytab文件

  • xst:将主体的密钥写入keytab文件
  • -k /etc/security/keytab/test.keytab:指明keytab文件路径和文件名
  • test/test:主体

(3)为方便创建主体,可使用如下命令

[root@hadoop01 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey test/test"
[root@hadoop01 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/test.keytab test/test"

说明:

-p:主体

-w:pw

-q:执行语句

(4)操作主体的其他命令,可参考官方文档,地址如下:

​http://web.mit.edu/kerberos/krb5-current/doc/admin/admin_commands/kadmin_local.html#commands​

  • 创建主体

1)在所有节点创建keytab文件目录

[root@hadoop01 ~]# mkdir /etc/security/keytab/
[root@hadoop01 ~]# chown -R root:hadoop /etc/security/keytab/
[root@hadoop01 ~]# chmod 770 /etc/security/keytab/
[root@hadoop02 ~]# mkdir /etc/security/keytab/
[root@hadoop02 ~]# chown -R root:hadoop /etc/security/keytab/
[root@hadoop02 ~]# chmod 770 /etc/security/keytab/
[root@hadoop03 ~]# mkdir /etc/security/keytab/
[root@hadoop03 ~]# chown -R root:hadoop /etc/security/keytab/
[root@hadoop03 ~]# chmod 770 /etc/security/keytab/

2)以下命令在hadoop01节点执行

NameNode(hadoop01)

[root@hadoop01 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey nn/hadoop01"
[root@hadoop01 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/nn.service.keytab nn/hadoop01"

DataNode(hadoop01)

[root@hadoop01 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey dn/hadoop01"
[root@hadoop01 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/dn.service.keytab dn/hadoop01"

NodeManager(hadoop01)

[root@hadoop01 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey nm/hadoop01"
[root@hadoop01 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/nm.service.keytab nm/hadoop01"

JobHistory Server(hadoop01)

[root@hadoop01 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey jhs/hadoop01"
[root@hadoop01 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/jhs.service.keytab jhs/hadoop01"

Web UI(hadoop01)

[root@hadoop01 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey HTTP/hadoop01"
[root@hadoop01 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/spnego.service.keytab HTTP/hadoop01"

3)以下命令在hadoop02执行

ResourceManager(hadoop02)

[root@hadoop02 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey rm/hadoop02"
[root@hadoop02 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/rm.service.keytab rm/hadoop02"

DataNode(hadoop02)

[root@hadoop02 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey dn/hadoop02"
[root@hadoop02 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/dn.service.keytab dn/hadoop02"

NodeManager(hadoop02)

[root@hadoop02 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey nm/hadoop02"
[root@hadoop02 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/nm.service.keytab nm/hadoop02"

Web UI(hadoop02)

[root@hadoop02 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey HTTP/hadoop02"
[root@hadoop02 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/spnego.service.keytab HTTP/hadoop02"

4)以下命令在hadoop03执行

DataNode(hadoop03)

[root@hadoop03 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey dn/hadoop03"
[root@hadoop03 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/dn.service.keytab dn/hadoop03"

Secondary NameNode(hadoop03)

[root@hadoop03 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey sn/hadoop03"
[root@hadoop03 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/sn.service.keytab sn/hadoop03"

NodeManager(hadoop03)

[root@hadoop03 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey nm/hadoop03"
[root@hadoop03 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/nm.service.keytab nm/hadoop03"

Web UI(hadoop03)

[root@hadoop03 ~]# kadmin -padmin/admin -wadmin -q"addprinc -randkey HTTP/hadoop03"
[root@hadoop03 ~]# kadmin -padmin/admin -wadmin -q"xst -k /etc/security/keytab/spnego.service.keytab HTTP/hadoop03"
  • 修改所有节点keytab文件的所有者和访问权限
[root@hadoop01 ~]# chown -R root:hadoop /etc/security/keytab/
[root@hadoop01 ~]# chmod 660 /etc/security/keytab/*
[root@hadoop02 ~]# chown -R root:hadoop /etc/security/keytab/
[root@hadoop02 ~]# chmod 660 /etc/security/keytab/*
[root@hadoop03 ~]# chown -R root:hadoop /etc/security/keytab/
[root@hadoop03 ~]# chmod 660 /etc/security/keytab/*


Hadoop

Hadoop

Hadoop

3.2 修改Hadoop配置文件

  • 官网案例

Hadoop SecureModel

Hadoop

Hadoop

Hadoop

Hadoop

Hadoop

Hadoop

Hadoop

需要修改的内容如下,修改完毕需要分发所改文件。

  • core-site.xml
[root@hadoop01 ~]# vim /data/hadoop-3.1.3/etc/hadoop/core-site.xml

增加以下内容

[root@hadoop01 ~]# vim /data/hadoop-3.1.3/etc/hadoop/core-site.xml
增加以下内容
<!-- Kerberos主体到系统用户的映射机制 -->
<property>
<name>hadoop.security.auth_to_local.mechanism</name>
<value>MIT</value>
</property>
<!-- Kerberos主体到系统用户的具体映射规则 -->
<property>
<name>hadoop.security.auth_to_local</name>
<value>
RULE:[2:$1/$2@$0]([ndj]n\\/.*@EXAMPLE\\.COM)s/.*/hdfs/
RULE:[2:$1/$2@$0]([rn]m\\/.*@EXAMPLE\\.COM)s/.*/yarn/
RULE:[2:$1/$2@$0](jhs\\/.*@EXAMPLE\\.COM)s/.*/mapred/
DEFAULT
</value>
</property>
<!-- 启用Hadoop集群Kerberos安全认证 -->
<property>
<name>hadoop.security.authentication</name>
<value>kerberos</value>
</property>
<!-- 启用Hadoop集群授权管理 -->
<property>
<name>hadoop.security.authorization</name>
<value>true</value>
</property>
<!-- Hadoop集群间RPC通讯设为仅认证模式 -->
<property>
<name>hadoop.rpc.protection</name>
<value>authentication</value>
</property>
  • hdfs-site.xml
[root@hadoop01 ~]# vim /data/hadoop-3.1.3/etc/hadoop/hdfs-site.xml

增加以下内容

[root@hadoop01 ~]# vim /data/hadoop-3.1.3/etc/hadoop/hdfs-site.xml
增加以下内容
<!-- 访问DataNode数据块时需通过Kerberos认证 -->
<property>
<name>dfs.block.access.token.enable</name>
<value>true</value>
</property>
<!-- NameNode服务的Kerberos主体,_HOST会自动解析为服务所在的主机名 -->
<property>
<name>dfs.namenode.kerberos.principal</name>
<value>nn/_HOST@EXAMPLE.COM</value>
</property>
<!-- NameNode服务的Kerberos密钥文件路径 -->
<property>
<name>dfs.namenode.keytab.file</name>
<value>/etc/security/keytab/nn.service.keytab</value>
</property>
<!-- Secondary NameNode服务的Kerberos主体 -->
<property>
<name>dfs.secondary.namenode.keytab.file</name>
<value>/etc/security/keytab/sn.service.keytab</value>
</property>
<!-- Secondary NameNode服务的Kerberos密钥文件路径 -->
<property>
<name>dfs.secondary.namenode.kerberos.principal</name>
<value>sn/_HOST@EXAMPLE.COM</value>
</property>
<!-- NameNode Web服务的Kerberos主体 -->
<property>
<name>dfs.namenode.kerberos.internal.spnego.principal</name>
<value>HTTP/_HOST@EXAMPLE.COM</value>
</property>
<!-- WebHDFS REST服务的Kerberos主体 -->
<property>
<name>dfs.web.authentication.kerberos.principal</name>
<value>HTTP/_HOST@EXAMPLE.COM</value>
</property>
<!-- Secondary NameNode Web UI服务的Kerberos主体 -->
<property>
<name>dfs.secondary.namenode.kerberos.internal.spnego.principal</name>
<value>HTTP/_HOST@EXAMPLE.COM</value>
</property>
<!-- Hadoop Web UI的Kerberos密钥文件路径 -->
<property>
<name>dfs.web.authentication.kerberos.keytab</name>
<value>/etc/security/keytab/spnego.service.keytab</value>
</property>
<!-- DataNode服务的Kerberos主体 -->
<property>
<name>dfs.datanode.kerberos.principal</name>
<value>dn/_HOST@EXAMPLE.COM</value>
</property>
Hadoop 集群上的 Kerberos 身份验证

Kerberos学习

hadoophbasezookeeper集成kerberos认证

Airflow 与 Kerberos 的集成如何工作?

ApacheHadoop 3.2.1集群集成Kerberos

Kerberos常见问题汇总