基於kerberos的hadoop安全集羣搭建

[TOC]html

上一份工做主要負責大數據平臺的建設,在這個過程當中積累了一些Hadoop生態組件的搭建和使用筆記,因爲時間關係,不打算去修改其中的錯別字和排版問題,直接釋出原始筆記。

前置條件

我所在的集羣有三臺服務其,對應的host分別爲master,slave1,slave2。hadoop服務的安裝分部爲node

| 機器host| 組件狀況|
| :-------- | --------:|
| master| namenode、datanode、journalnode、resourcemanager、nodemanager、jobhistoryserver|
| slave1| namenode、datanode、journalnode、resourcemanager、nodemanager|
|slave2 | datanode、journalnode、nodemanager|linux

kerberos相關

首先咱們要安裝好kerberos,kerberos的安裝搭建參考連接
https://www.cnblogs.com/nices...git

給hadoop各組件建立kerberos帳號

進入kerberos的admin.local後,依次執行下述命令github

//組件web服務的princial
addprinc -randkey HTTP/master@TEST.COM
addprinc -randkey HTTP/slave1@TEST.COM
addprinc -randkey HTTP/slave2@TEST.COM

//namenode的princial
addprinc -randkey nn/master@TEST.COM
addprinc -randkey nn/slave1@TEST.COM

//datanode的princial
addprinc -randkey dn/master@TEST.COM    
addprinc -randkey dn/slave1@TEST.COM
addprinc -randkey dn/slave2@TEST.COM

//journalnode的princial
addprinc -randkey jn/master@TEST.COM    
addprinc -randkey jn/slave1@TEST.COM
addprinc -randkey jn/slave2@TEST.COM

//resourcemanager 的princial
addprinc -randkey rm/master@TEST.COM
addprinc -randkey rm/slave1@TEST.COM

//nodemanager的principal
addprinc -randkey nm/master@TEST.COM    
addprinc -randkey nm/slave1@TEST.COM
addprinc -randkey nm/slave2@TEST.COM

//job hisotry server的princial
addprinc -randkey jhs/master@TEST.COM

將這些帳號作成keytab

一樣是在admin.local中,將上述帳號認證信息作成keytabweb

ktadd -k /opt/keytab_store/http.service.keytab HTTP/master@TEST.COM
ktadd -k /opt/keytab_store/http.service.keytab HTTP/slave1@TEST.COM
ktadd -k /opt/keytab_store/http.service.keytab HTTP/slave2@TEST.COM

ktadd -k /opt/keytab_store/nn.service.keytab nn/master@TEST.COM
ktadd -k /opt/keytab_store/nn.service.keytab nn/slave1@TEST.COM

ktadd -k /opt/keytab_store/dn.service.keytab dn/master@TEST.COM    
ktadd -k /opt/keytab_store/dn.service.keytab dn/slave1@TEST.COM
ktadd -k /opt/keytab_store/dn.service.keytab dn/slave2@TEST.COM

ktadd -k /opt/keytab_store/jn.service.keytab jn/master@TEST.COM    
ktadd -k /opt/keytab_store/jn.service.keytab jn/slave1@TEST.COM
ktadd -k /opt/keytab_store/jn.service.keytab jn/slave2@TEST.COM

ktadd -k /opt/keytab_store/rm.service.keytab rm/master@TEST.COM
ktadd -k /opt/keytab_store/rm.service.keytab rm/slave1@TEST.COM

ktadd -k /opt/keytab_store/nm.service.keytab nm/master@TEST.COM    
ktadd -k /opt/keytab_store/nm.service.keytab nm/slave1@TEST.COM
ktadd -k /opt/keytab_store/nm.service.keytab nm/slave2@TEST.COM

ktadd -k /opt/keytab_store/jhs.service.keytab jhs/master@TEST.COM

多個帳號能夠作到一個keytab中去,上述的命令作了多個文件,不一樣組件角色的單獨放到了一個keytab文件中。其實內部網絡,能夠把全部的hadoop相關組件作成一個大的keytab文件,下降配置複雜性。apache

將上述的keytab文件,分發到集羣全部機器安全

core-site.xml

關鍵配置網絡

<property>
                <name>hadoop.security.authentication</name>
                <value>kerberos</value>
        </property>
        <property>
                <name>hadoop.security.authorization</name>
                <value>true</value>
        </property>
        <property>
                <name>hadoop.security.auth_to_local</name>
                <value>
                        RULE:[2:$1/$2@$0]([ndj]n/.*@TEST.COM)s/.*/hdfs/
                        RULE:[2:$1/$2@$0]([rn]m/.*@TEST.COM)s/.*/yarn/
                        RULE:[2:$1/$2@$0](jhs/.*@TEST.COM)s/.*/mapred/
                        DEFAULT
                </value>
        </property>

上述配置的意思是 在整個集羣中費用kerberos做爲安全認證和受權,
hadoop.security.auth_to_local 配置組件之間互訪時被訪問的服務,如何從訪問的Principal中抽取出實際的用戶。大體規則以第一行爲例,表示將namenode, 和datanode ,journalnode的principal 映射成爲hdfs的user
而最終的default是上述規則都不匹配時的默認規則,默認規則會直接從principal中提取第一個斜槓前面的信息做爲user。好比test/xxhost@DOMIAN.COM 會被識別成明爲test的usertcp

HDFS

<property>
        <name>dfs.block.access.token.enable</name>
        <value>true</value>
    </property>
<property>
        <name>dfs.namenode.kerberos.principal</name>
        <value>nn/_HOST@TEST.COM</value>
    </property>
<property>
        <name>dfs.namenode.keytab.file</name>
        <value>/opt/keytab_store/nn.service.keytab</value>
    </property>
<property>
        <name>dfs.namenode.kerberos.internal.spnego.principal</name>
        <value>${dfs.web.authentication.kerberos.principal}</value>
    </property>
<property>
        <name>dfs.journalnode.kerberos.principal</name>
        <value>jn/_HOST@TEST.COM</value>
    </property>
<property>
        <name>dfs.journalnode.keytab.file</name>
        <value>/opt/keytab_store/jn.service.keytab</value>
    </property>
<property>
        <name>dfs.journalnode.kerberos.internal.spnego.principal</name>
        <value>${dfs.web.authentication.kerberos.principal}</value>
    </property>
<property>
        <name>dfs.datanode.kerberos.principal</name>
        <value>dn/_HOST@TEST.COM</value>
    </property>
<property>
        <name>dfs.datanode.keytab.file</name>
        <value>/opt/keytab_store/dn.service.keytab</value>
    </property>
<property>
        <name>dfs.web.authentication.kerberos.principal</name>
        <value>HTTP/_HOST@TEST.COM</value>
    </property>
<property>
        <name>dfs.web.authentication.kerberos.keytab</name>
        <value>/opt/keytab_store/http.service.keytab</value>
    </property>
 <property>
        <name>dfs.http.policy</name>
        <value>HTTPS_ONLY</value>
    </property>
<property>
        <name>dfs.data.transfer.protection</name>
        <value>authentication</value>
    </property>

其中大致配置是配置各組件使用的principal是什麼。其中的_HOST至關於語法糖,hadoop會根據本機hostname,替換該配置,從而實現不一樣機器相同配置文件的目的

datanode的安全配置

因爲datanode數據傳輸走的不是rpc,而是http。因此datanode沒法使用kerberos的方式進行認證。爲了解決這個問題,有兩種方式的配置,來實現datanode數據傳輸的安全性

  • JSVC
  • TLS/SSL

JSVC方式的大致原理是使用JSVC工具,讓datanode可以使用特權端口啓動,所謂特權端口是指1024如下的端口,這種安全配置假定攻擊者沒法獲取root權限,因此也就沒法操做datanode來實現。hadoop 2.6.0之前,只能使用這種方式,配置較爲複雜,不在這裏贅述。hadoop 2.6.0之後引入了SASL方式,經過TLS/SSL來實現數據的安全傳輸,下面介紹這種方式

證書生成和安裝

TLS/SSL相關原理見文檔 ,這裏粘貼地址

首先保證機器上已經安裝好了openssl。下面是詳細的配置。核心思想是,作一個私有的CA,而後經過這個私有的CA證書給全部的其它證書籤名,經過將私有CA的證書安裝到各機器的信任區裏,實現一個各機器間的TLS/SSL通訊

而後在集羣中隨便找一臺機器,先生成CA證書,這裏在Master這臺機器上操做

openssl req -new -x509 -keyout ca_private.key -out ca_cert -days 9999 -subj '/C=CN/ST=chengdu/L=chengdu/O=bigdata/OU=bigdata/CN=master'

將上述的CA私鑰跟更要證書拷貝到各個機器。而後再各機器上作以下操做,固然若是咱們在生成證書時,用的密碼徹底同樣也能夠在一個機器上作,最後把相關的keystore和truststore分發到全部的機器。

//生成本身的公私祕鑰對
keytool -keystore keystore -alias localhost -validity 9999 -genkey -keyalg RSA -keysize 2048 -dname "CN=slave2, OU=bigdata, O=bigdata, L=chengdu, ST=chengdu, C=CN"

//將上述的CA公鑰證書導入本機的信任區truststore
keytool -keystore truststore -alias CARoot -import -file ca_cert 

//將上述的CA公鑰導入本機的keystore中
keytool -keystore keystore -alias CARoot -import -file ca_cert 

//將本機的公鑰證書導出
keytool -certreq -alias localhost -keystore keystore -file local_cert

//對CA私鑰,對本機的公鑰證書進行簽名
openssl x509 -req -CA hd_ca_cert -CAkey ca_private.key -in local_cert -out local_cert_signed -days 9999 -CAcreateserial 

//將簽名後的證書導入的本身的Keystore
keytool -keystore keystore -alias localhost -import -file local_cert_signed

hdfs-site.xml的重點配置

配置dfs.http.policy的value爲HTTPS_ONLY
配置dfs.data.transfer.protection的value爲authenticationintegrity privacy任意一種。通常內部集羣用authentication便可

  • authentication ,只認證簽名
  • integrity 除了認證簽名外,還驗證數據是否被篡改
  • privacy,數據除了上述的認證和完整性驗證以外還要加密傳輸

ssl-client.xml 和 ssl-server.xml配置

hadoop在在跟core-site.xml同級目錄下通常有ssl-client.xml.example和ssl-server.xml.example兩個模板文件,咱們能夠直接去掉template來後做爲配置文件來配置。他們是用來配置當前組件做爲服務端時,本身的證書kestore位置,和做爲客戶端時,本身的信任證書truststore位置

ssl-client.xml配置以下

<configuration>                                                                                                                 
                                                                                                                                
<property>                                                                                                                      
  <name>ssl.client.truststore.location</name>                                                                                   
  <value>/opt/ssl_store/truststore</value>                                                                                      
  <description>Truststore to be used by clients like distcp. Must be                                                            
  specified.                                                                                                                    
  </description>                                                                                                                
</property>                                                                                                                     
                                                                                                                                
<property>                                                                                                                      
  <name>ssl.client.truststore.password</name>                                                                                   
  <value>123456</value>                                                                                                         
  <description>Optional. Default value is "".                                                                                   
  </description>                                                                                                                
</property>                                                                                                                     
                                                                                                                                
<property>                                                                                                                      
  <name>ssl.client.truststore.type</name>                                                                                       
  <value>jks</value>                                                                                                            
  <description>Optional. The keystore file format, default value is "jks".                                                      
  </description>                                                                                                                
</property>                                                                                                                     
                                                                                                                                
<property>                                                                                                                      
  <name>ssl.client.truststore.reload.interval</name>                                                                            
  <value>10000</value>                                                                                                          
  <description>Truststore reload check interval, in milliseconds.                                                               
  Default value is 10000 (10 seconds).                                                                                          
  </description>                                                                                                                
</property>                                                                                                                     
                                                                                                                                
<property>                                                                                                                      
  <name>ssl.client.keystore.location</name>                                                                                     
  <value>/opt/ssl_store/keystore</value>                                                                                        
  <description>Keystore to be used by clients like distcp. Must be                                                              
  specified.                                                                                                                    
  </description>                                                                                                                
</property>      
<property>
  <name>ssl.client.keystore.password</name>
  <value>123456</value>
  <description>Optional. Default value is "".
  </description>
</property>

<property>
  <name>ssl.client.keystore.keypassword</name>
  <value>123456</value>
  <description>Optional. Default value is "".
  </description>
</property>

<property>
  <name>ssl.client.keystore.type</name>
  <value>jks</value>
  <description>Optional. The keystore file format, default value is "jks".
  </description>
</property>

</configuration>

ssl-server.xml

<property>
  <name>ssl.server.keystore.password</name>
  <value>123456</value>
  <description>Must be specified.
  </description>
</property>

<property>
  <name>ssl.server.keystore.keypassword</name>
  <value>123456</value>
  <description>Must be specified.
  </description>
</property>

<property>
  <name>ssl.server.keystore.type</name>
  <value>jks</value>
  <description>Optional. The keystore file format, default value is "jks".
  </description>
</property>

<property>
  <name>ssl.server.exclude.cipher.list</name>
  <value>TLS_ECDHE_RSA_WITH_RC4_128_SHA,SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA,
  SSL_RSA_WITH_DES_CBC_SHA,SSL_DHE_RSA_WITH_DES_CBC_SHA,
  SSL_RSA_EXPORT_WITH_RC4_40_MD5,SSL_RSA_EXPORT_WITH_DES40_CBC_SHA,
  SSL_RSA_WITH_RC4_128_MD5</value>
  <description>Optional. The weak security cipher suites that you want excluded
  from SSL communication.</description>
</property>

</configuration>

上述配置的123456是咱們在作證書時使用的密碼

yarn

總體配置

<property>
        <name>yarn.resourcemanager.principal</name>
        <value>rm/_HOST@TEST.COM</value>
    </property>
<property>
        <name>yarn.resourcemanager.keytab</name>
        <value>/opt/keytab_store/rm.service.keytab</value>
    </property>
<property>
        <name>yarn.nodemanager.principal</name>
        <value>nm/_HOST@TEST.COM</value>
    </property>
<property>
        <name>yarn.nodemanager.keytab</name>
        <value>/opt/keytab_store/nm.service.keytab</value>
    </property>
<property>
        <!--安全集羣必須使用下面的LinuxContainerExecutor-->
        <name>yarn.nodemanager.container-executor.class</name>
        <value>org.apache.hadoop.yarn.server.nodemanager.LinuxContainerExecutor</value>
    </property>
<property>
        <name>yarn.nodemanager.linux-container-executor.group</name>
        <value>hadoop</value>
    </property>
<property>
        <name>yarn.nodemanager.linux-container-executor.path</name>
        <value>/opt/hadoop-3.1.3/bin/container-executor</value>
    </property>

container-executor

build LinuxContainerExecutor

上述yarn.nodemanager.linux-container-executor.path指定了LinuxContainerExecutor對應的可執行文件container-executor的路徑。
hadoop發行包在bin路徑下,通常就已經有這個文件了。
這個文件執行須要一個配置,container-executor.cfg 。其默認加載的是$HADOOP_HOME/etc/hadoop/container-executor.cfg這個路徑的配置文件。

但因爲這個路徑自己又有hadoop的其它配置文件,而container-executor又要求container-executor.cfg所在路徑全部層級權限都只能root訪問。這會致使咱們其其它組件啓動出現各類奇奇古怪的問題。

因此咱們須要另外指定container-executor.cfg文件的位置。但問題是container-executor這個二進制文件在構建時,已經寫死了文件路徑。若是咱們須要重指定配置文件路徑,須要從新打包container-executor。構建步驟爲

  • 首先下載同版本的hadoop源碼
  • 進入到源碼包的路徑hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-nodemanager
  • 使用命令mvn package -DskipTests=true -Dcontainer-executor.conf.dir=/etc/hadoop/ 構建,container-executor.conf.dir參數即指定新的container-executor.cfg文件路徑
  • 構建完成後,在構建路徑下的target/native/target/usr/local/bin路徑便可找到新構建的container-executor,將其拷貝到$HADOOP_HOME/bin下,替換原來的程序便可

    配置container-executor.cfg

    在/etc/hadoop/中,建立container-executor.cfg,其配置內容以下

yarn.nodemanager.linux-container-executor.group=hadoop
banned.users=hdfs,yarn,mapred,bin
min.user.id=1000
allowed.system.users=
feature.tc.enabled=false

注意配置每行不要有空格,yarn.nodemanager.linux-container-executor.group這個配置值同yarn-site.xml中的一致

總結權限配置須要配置的項
file

文件權限修改

chown root:hadoop /opt/hadoop-3.1.3/bin/container-executor
chmod 6050 /opt/hadoop-3.1.3/bin/container-executor
chown root:hadoop /etc/hadoop/container-executor.cfg
chmod 400 /etc/hadoop/container-executor.cfg

假設在yarn-site.xml的中yarn.nodemanager.local-dirs 配置 路徑爲/home/var/data/hadoop/nodemanager/data
yarn.nodemanager.log-dirs配置路徑爲 /home/var/data/hadoop/nodemanager/log,還須要作如下權限配置

chown yarn:hadoop /home/var/data/hadoop/nodemanager/data 
chown yarn:hadoop /home/var/data/hadoop/nodemanager/log  
chmod 755 /home/var/data/hadoop/nodemanager/data
chmod 755 /home/var/data/hadoop/nodemanager/log

mapreduce

<property>
        <name>mapreduce.jobhistory.keytab</name>
        <value>/opt/keytab_store/jhs.service.keytab</value>
    </property>
 <property>
        <name>mapreduce.jobhistory.principal</name>
        <value>jhs/_HOST@TEST.COM</value>
    </property>

啓動

配置完後,按原來的方式啓動便可。只是因爲hdfs開起了SSL/TLS ,其原來的9870端口,變成了9871, 且須要經過https訪問。好比咱們這地址爲:https://master:9871

參考資料

https://hadoop.apache.org/doc...

https://secfree.github.io/blo...

https://blog.csdn.net/picway/...

https://developer.aliyun.com/...

https://makeling.github.io/bi...

https://makeling.github.io/bi...

http://secfree.github.io/blog...

歡迎關注個人我的公衆號"西北偏北UP",記錄代碼人生,行業思考,科技評論
相關文章
相關標籤/搜索