0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Hadoopクラスタにknox/ldapを導入してみた。

0
Last updated at Posted at 2025-12-27

はじめに

現在構築しているHadoopクラスタでは、
以下の問題があるため、この解決策として、
LDAP,knpxを追加する。

  • Hadoop管理画面に対し、SSL,LDAP認証が有効化されていない。

仕様

対象 認証 備考
YARN UI2 LDAP 認証あり Knox
JobHistory UI LDAP 認証あり Knox

SSL 自己署名 Knox
LDAP 別筐体 Web UI あり(phpLDAPadmin)

構成ホスト

ホスト名 役割
master1 NN(nn1) / JobHistory
master2 NN(nn2) / RM(rm1)
master3 RM(rm2)
worker1 DN / NM
worker2 DN / NM
gateway1 Knox
ldap1 LDAP + Web UI

0. 事前対応

以下のページを基にHadoopクラスタを構築していること。

追加して2台のまっさらなホスト(gateway1,ldap1)を導入すること。

以下を「全ノード(master/worker/gateway1/ldap1)」に必ず入れる
IPは環境に応じ変更すること。

sudo tee /etc/hosts <<'EOF'
192.168.11.45 master1
192.168.11.46 master2
192.168.11.47 master3
192.168.11.48 worker1
192.168.11.49 worker2
192.168.11.43 gateway1
192.168.11.44 ldap1
EOF
sudo tee /etc/cloud/templates/hosts.debian.tmpl <<'EOF'
192.168.11.45 master1
192.168.11.46 master2
192.168.11.47 master3
192.168.11.48 worker1
192.168.11.49 worker2
192.168.11.43 gateway1
192.168.11.44 ldap1
EOF

1. LDAP サーバ構築(ldap1)

1-1. インストール

sudo apt update
sudo DEBIAN_FRONTEND=noninteractive apt install -y \
  slapd ldap-utils phpldapadmin apache2
sudo dpkg-reconfigure slapd

上記設定時のプロンプト対応:

  • Omit OpenLDAP server configuration? → No
  • DNS domain name → example.com
  • Organization name → Example
  • Administrator password → 任意
  • Do you want the database to be removed when slapd is purged? → yes
  • Move old database? → Yes

1-2. phpLDAPadmin 設定

sudo vi /etc/phpldapadmin/config.php

確認・修正:

$servers->setValue('server','host','127.0.0.1');
$servers->setValue('server','base',array('dc=example,dc=com'));

Web UI アクセス

ログイン
DN : cn=admin,dc=example,dc=com
Password : slapd 設定時のパスワード

1-3. ユーザー用 OU 作成(UIで作成してもよい。)

コマンドで作成する場合

sudo tee ou-users.ldif <<'EOF'
dn: ou=users,dc=example,dc=com
objectClass: organizationalUnit
ou: users
EOF
ldapadd -x -D cn=admin,dc=example,dc=com -W -f ou-users.ldif

1-4. LDAP ユーザー作成(UIで作成してもよい。)

コマンドで作成する場合

sudo tee user-hdfsuser.ldif <<'EOF'
dn: uid=hdfsuser,ou=users,dc=example,dc=com
objectClass: inetOrgPerson
cn: HDFS User
sn: User
uid: hdfsuser
userPassword: password123
EOF
ldapadd -x -D cn=admin,dc=example,dc=com -W -f user-hdfsuser.ldif

ldap Web UIにて、上記OU/ユーザーが登録されていればよい。

2. Knox 構築(gateway1)

2-1. Java

sudo apt update
sudo apt install -y openjdk-11-jdk

2-2. Knox ユーザー

sudo useradd -r -m -d /opt/knox -s /bin/bash knox

2-3. Knox インストール

sudo su - knox
wget https://downloads.apache.org/knox/2.1.0/knox-2.1.0.tar.gz
tar xzf knox-2.1.0.tar.gz
mv knox-2.1.0/* ./
rm -rf knox-2.1.0*
exit

3. SSL(自己署名証明書)

sudo -u knox mkdir -p /opt/knox/data/security/keystores

3-1. CA 作業ディレクトリ作成

sudo mkdir -p /opt/ca
cd /opt/ca
sudo chown $USER .

3-2. CA 秘密鍵作成

openssl genrsa -out ca.key 4096

3-3. CA 証明書作成(10年)

openssl req -x509 -new -nodes \
  -key ca.key \
  -sha256 -days 3650 \
  -out ca.crt \
  -subj "/C=JP/O=Local-CA/OU=Hadoop/CN=My-Internal-CA"

3-4. SAN 定義ファイル作成

cat > knox-san.cnf <<'EOF'
[ req ]
default_bits       = 2048
prompt             = no
default_md         = sha256
distinguished_name = dn
req_extensions     = req_ext

[ dn ]
C  = JP
O  = Local
OU = Hadoop
CN = gateway1

[ req_ext ]
subjectAltName = @alt_names

[ alt_names ]
DNS.1 = gateway1
DNS.2 = gateway1.local
IP.1  = 192.168.11.43
EOF

※ IP.1 は gateway1 の IP に置き換え

3-5. Knox サーバ秘密鍵作成

openssl genrsa -out knox.key 2048

3-6. 証明書署名要求(CSR)作成

openssl req -new \
  -key knox.key \
  -out knox.csr \
  -config knox-san.cnf

3-7. CA で署名(サーバ証明書作成)

openssl x509 -req \
  -in knox.csr \
  -CA ca.crt -CAkey ca.key -CAcreateserial \
  -out knox.crt \
  -days 825 -sha256 \
  -extensions req_ext \
  -extfile knox-san.cnf

3-8. PKCS12 形式にまとめる

openssl pkcs12 -export \
  -in knox.crt \
  -inkey knox.key \
  -name gateway-identity \
  -out knox.p12 \
  -passout pass:changeit \
  -certfile ca.crt

3-9. JKS に変換

keytool -importkeystore \
  -destkeystore gateway.jks \
  -deststorepass changeit \
  -destkeypass changeit \
  -srckeystore knox.p12 \
  -srcstoretype PKCS12 \
  -srcstorepass changeit \
  -alias gateway-identity

3-10. Knox 用パスへ配置

sudo mv gateway.jks /opt/knox/data/security/keystores/gateway.jks
sudo chown knox:knox /opt/knox/data/security/keystores/gateway.jks

3-11. クライアントへ CA 証明書配布

クライアントPCからもgateway1への名前解決できること。

配布するのは これだけ:

/opt/ca/ca.crt

Windows

ca.crt をダブルクリック
ローカルコンピューター
信頼されたルート証明機関

macOS

sudo security add-trusted-cert \
  -d -r trustRoot \
  -k /Library/Keychains/System.keychain ca.crt

Linux(Ubuntu)

sudo cp ca.crt /usr/local/share/ca-certificates/knox-ca.crt
sudo update-ca-certificates

4.Master Secret作成

sudo -u knox /opt/knox/bin/knoxcli.sh create-master

→Secretとして"changeit"を2回入力する。

5. Knox 設定

5-1. gateway-site.xml

sudo -u knox tee /opt/knox/conf/gateway-site.xml <<'EOF'
<configuration>
  <property>
    <name>gateway.port</name>
    <value>8443</value>
  </property>
  <property>
    <name>gateway.ssl.enabled</name>
    <value>true</value>
  </property>
  <property>
    <name>gateway.keystore.path</name>
    <value>data/security/keystores/gateway.jks</value>
  </property>
  <property>
    <name>gateway.keystore.password</name>
    <value>changeit</value>
  </property>
</configuration>
EOF

5-2. Knox topology

sudo -u knox tee /opt/knox/conf/topologies/secure.xml <<'EOF'
<topology>
  <gateway>
    <provider>
      <role>authentication</role>
      <name>ShiroProvider</name>
      <enabled>true</enabled>

      <param>
        <name>main.ldapRealm</name>
        <value>org.apache.hadoop.gateway.shirorealm.KnoxLdapRealm</value>
      </param>

      <param>
        <name>main.ldapRealm.contextFactory.url</name>
        <value>ldap://ldap1:389</value>
      </param>
      <param>
        <name>main.ldapRealm.contextFactory.authenticationMechanism</name>
        <value>simple</value>
      </param>

      <param>
        <name>main.ldapRealm.userDnTemplate</name>
        <value>uid={0},ou=users,dc=example,dc=com</value>
      </param>

      <param>
        <name>urls./**</name>
        <value>authcBasic</value>
      </param>
    </provider>

    <provider>
      <role>identity-assertion</role>
      <name>Default</name>
      <enabled>true</enabled>
    </provider>
  </gateway>

  <!-- JobHistory UI -->
  <service>
    <role>JOBHISTORYUI</role>
    <url>http://master1:19888</url>
  </service>

  <!-- YARN UI2 -->
  <service>
    <role>YARNUIV2</role>
    <url>http://master2:8088</url>
  </service>
</topology>
EOF

6. Knox 起動

sudo tee /etc/systemd/system/knox.service >/dev/null <<'EOF'
[Unit]
Description=Apache Knox Gateway
After=network.target

[Service]
Type=oneshot
User=knox
Group=knox
WorkingDirectory=/opt/knox
Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
RemainAfterExit=yes

# 起動
ExecStart=/opt/knox/bin/gateway.sh start

# 起動完了判定(8443がLISTENになるまで最大30秒待つ)
ExecStartPost=/bin/bash -lc 'for i in {1..30}; do ss -lnt | grep -q ":8443" && exit 0; sleep 1; done; echo "Knox did not listen on 8443" >&2; exit 1'

# 停止
ExecStop=/opt/knox/bin/gateway.sh stop

Restart=on-failure
RestartSec=3
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now knox
ss -lntp | grep 8443

→*.8443と出ること。

7. 動作確認

BASIC認証が出たらhdfsuser/password123を入力する。

  • YARN UI
https://gateway1:8443/gateway/secure/yarnuiv2/
  • JobHistory UI
https://gateway1:8443/gateway/secure/jobhistory/
0
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?