・Jenkinsの管理ノードに外部サーバを追加したい。
・Jenkinsから外部サーバのジョブを実行できるようにしたい。
・具体的な手順を教えてほしい。
こういった疑問に答えます。
本記事の内容
- Jenkinsに外部サーバを管理ノードとして追加する手順
- Jenkinsから外部サーバに対してジョブを実行する手順
この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。
こういった私が、解説していきます。
私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。
- 本記事の内容
- Jenkinsに外部サーバを管理ノードとして追加する手順
- Jenkinsから外部サーバに対してジョブを実行する手順
Jenkinsに外部サーバを管理ノードとして追加する手順
- JenkinsサーバでSSH秘密鍵ファイルと公開鍵ファイル作成
- 管理対象となる外部サーバ側で公開鍵ファイルを準備
- Jenkinsサーバから管理対象サーバへSSH接続できるかテスト
- Jenkins管理画面でSSH認証情報の設定
- Jenkins管理画面で管理ノードに外部サーバを追加
- SSH接続「known_hosts」ファイルのエラーの対処手順
前提
今回の構成イメージ図は以下です。
手順①:JenkinsサーバでSSH秘密鍵ファイルと公開鍵ファイル作成
cd /var/lib/jenkins
mkdir .ssh
cd /var/lib/jenkins/.ssh
[root@SV-SATURN ~]# cd /var/lib/jenkins [root@SV-SATURN jenkins]# mkdir .ssh [root@SV-SATURN jenkins]# cd /var/lib/jenkins/.ssh
ssh-keygen
[root@SV-SATURN .ssh]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): /var/lib/jenkins/.ssh/id_rsa Enter passphrase (empty for no passphrase): ★★★秘密鍵のパスフレーズ Enter same passphrase again: ★★★秘密鍵のパスフレーズ Your identification has been saved in /var/lib/jenkins/.ssh/id_rsa. Your public key has been saved in /var/lib/jenkins/.ssh/id_rsa.pub. The key fingerprint is: SHA256:uFh62iMHkG8BTBvSiCxm1utvVQMpxKSPuzbkV7+kJLA root@SV-SATURN The key's randomart image is: +---[RSA 2048]----+ |+=+ +o . | (途中省略) | ..=o.o .. | +----[SHA256]-----+
手順②:管理対象となる外部サーバ側で公開鍵ファイルを準備
※事前に「jenkins」というユーザーを作成済みです。
mkdir ~/.ssh
cd ~/.ssh
touch authorized_keys
[jenkins@sv-neptune ~]$ mkdir ~/.ssh [jenkins@sv-neptune .ssh]$ cd ~/.ssh [jenkins@sv-neptune .ssh]$ touch authorized_keys [jenkins@sv-neptune .ssh]$ ls -l 合計 0 -rw-rw-r-- 1 jenkins jenkins 0 11月 7 19:55 authorized_keys
Jenkinsサーバで作成した公開鍵ファイルをFTPでアップロード
公開鍵ファイルの中身を「authorized_keys」に追記
cat id_rsa.pub >> authorized_keys
[jenkins@sv-neptune .ssh]$ cat id_rsa.pub >> authorized_keys
アップロードした公開鍵ファイルはもう不要なので削除
rm id_rsa.pub
[jenkins@sv-neptune .ssh]$ rm id_rsa.pub
権限変更
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
[jenkins@sv-neptune .ssh]$ chmod 700 ~/.ssh [jenkins@sv-neptune .ssh]$ chmod 600 ~/.ssh/authorized_keys [jenkins@sv-neptune .ssh]$ ls -l 合計 4 -rw------- 1 jenkins jenkins 396 11月 7 20:08 authorized_keys
手順③:Jenkinsサーバから管理対象サーバへSSH接続できるかテスト
ssh -i /var/lib/jenkins/.ssh/id_rsa jenkins@sv-neptune
[root@SV-SATURN .ssh]# ssh -i /var/lib/jenkins/.ssh/id_rsa jenkins@sv-neptune The authenticity of host 'sv-neptune (192.168.1.105)' can't be established. ECDSA key fingerprint is SHA256:●●●●●●●●●●●●●●●●●●●● ECDSA key fingerprint is MD5:●●●●●●●●●●●●●●●●●●●● Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'sv-neptune,192.168.1.105' (ECDSA) to the list of known hosts. Enter passphrase for key '/var/lib/jenkins/.ssh/id_rsa': ############################### # SV-NEPTUNE # 192.168.1.105 # LAMP Server # CentOS8.2 ############################### Activate the web console with: systemctl enable --now cockpit.socket Last login: Sat Nov 7 19:53:53 2020 from 192.168.1.101 [jenkins@sv-neptune ~]$
手順④:Jenkins管理画面でSSH認証情報の設定
Jenkinsの管理>Manage Credentials
「global」のドメインをクリック
「認証情報の追加」をクリック
認証情報の内容を入力する
- 種類:SSHユーザー名と秘密鍵
- スコープ:グローバル
- ID:jenkins
- 説明:てきとうでOK
- ユーザー名:jenkins
- 秘密鍵:直接入力で「Add」ボタンをクリック
- パスフレーズ:秘密鍵ファイル作成時に設定したパスフレーズ
秘密鍵は「直接入力」で、Jenkinsサーバで作成した「id_rsa」ファイルを開いて丸ごとコピーして貼り付けます。
認証情報の追加が完了。
手順⑤:Jenkins管理画面で管理ノードに外部サーバを追加
「ノードの管理」をクリック
新規ノード作成 をクリック
ノード名にホスト名を入力して、「Permanent Agent」を選択して「OK」をクリック
管理対象サーバの情報を入力します。
- ノード名:ホスト名
- 説明:てきとうでOK
- リモートFSルート:管理対象サーバ側のjenkinsユーザーのホームディレクトリ
- ラベル:ホスト名と同じにしました
- 用途:このマシーンを特定ジョブ専用にする
- 起動方法:SSH経由でUnixマシンのスレーブエージェントを起動
- ホスト:ホスト名
- 認証情報:前の手順で作成しておいたSSH秘密鍵認証が選択できるようになっています
- Host Key Verification Strategy:Known hosts file Verification Strategy
- 可用性:Keep this agent online as much as possible
一通り入力したら一番下の「保存」ボタンをクリック。
ノードの一覧に追加される。しかし赤い×マーク表示
管理対象ノードが追加できましたが、赤い×マークが表示されてしまいました。
「詳細はログを参照」とあるので、ログを確認します。
どうやら、SSHの接続が上手くできずにエラーになっているようです。
SSHLauncher{host='SV-NEPTUNE', port=22, credentialsId='jenkins', jvmOptions='', javaPath='', prefixStartSlaveCmd='', suffixStartSlaveCmd='', launchTimeoutSeconds=60, maxNumRetries=10, retryWaitTime=15, sshHostKeyVerificationStrategy=hudson.plugins.sshslaves.verifiers.KnownHostsFileKeyVerificationStrategy, tcpNoDelay=true, trackCredentials=true} [11/07/20 20:22:50] [SSH] SV-NEPTUNE:22とのSSHコネクションをオープン /var/lib/jenkins/.ssh/known_hosts [SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it. Key exchange was not finished, connection is closed. SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.", retrying in 15 seconds. There are 10 more retries left. /var/lib/jenkins/.ssh/known_hosts [SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it. Key exchange was not finished, connection is closed. SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.", retrying in 15 seconds. There are 9 more retries left. /var/lib/jenkins/.ssh/known_hosts [SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it. Key exchange was not finished, connection is closed. SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.", retrying in 15 seconds. There are 8 more retries left. /var/lib/jenkins/.ssh/known_hosts [SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it. Key exchange was not finished, connection is closed. SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.", retrying in 15 seconds. There are 7 more retries left. /var/lib/jenkins/.ssh/known_hosts [SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it. Key exchange was not finished, connection is closed. SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.", retrying in 15 seconds. There are 6 more retries left. /var/lib/jenkins/.ssh/known_hosts [SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it. Key exchange was not finished, connection is closed. SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.", retrying in 15 seconds. There are 5 more retries left. /var/lib/jenkins/.ssh/known_hosts [SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it. Key exchange was not finished, connection is closed. SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.", retrying in 15 seconds. There are 4 more retries left. /var/lib/jenkins/.ssh/known_hosts [SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it. Key exchange was not finished, connection is closed. SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.", retrying in 15 seconds. There are 3 more retries left. /var/lib/jenkins/.ssh/known_hosts [SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it. Key exchange was not finished, connection is closed. SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.", retrying in 15 seconds. There are 2 more retries left. /var/lib/jenkins/.ssh/known_hosts [SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it. Key exchange was not finished, connection is closed. SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.", retrying in 15 seconds. There are 1 more retries left. /var/lib/jenkins/.ssh/known_hosts [SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it. Key exchange was not finished, connection is closed. ERROR: Connection is not established! java.lang.IllegalStateException: Connection is not established! at com.trilead.ssh2.Connection.getRemainingAuthMethods(Connection.java:988) at com.cloudbees.jenkins.plugins.sshcredentials.impl.TrileadSSHPublicKeyAuthenticator.getRemainingAuthMethods(TrileadSSHPublicKeyAuthenticator.java:88) at com.cloudbees.jenkins.plugins.sshcredentials.impl.TrileadSSHPublicKeyAuthenticator.canAuthenticate(TrileadSSHPublicKeyAuthenticator.java:80) at com.cloudbees.jenkins.plugins.sshcredentials.SSHAuthenticator.newInstance(SSHAuthenticator.java:218) at com.cloudbees.jenkins.plugins.sshcredentials.SSHAuthenticator.newInstance(SSHAuthenticator.java:171) at hudson.plugins.sshslaves.SSHLauncher.openConnection(SSHLauncher.java:863) at hudson.plugins.sshslaves.SSHLauncher$1.call(SSHLauncher.java:435) at hudson.plugins.sshslaves.SSHLauncher$1.call(SSHLauncher.java:422) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) [11/07/20 20:25:35] Launch failed - cleaning up connection [11/07/20 20:25:35] [SSH] コネクション終了
SSH接続で使う「known_hosts」ファイルのエラーが出ていました。
/var/lib/jenkins/.ssh/known_hosts [SSH] No Known Hosts file was found at /var/lib/jenkins/.ssh/known_hosts. Please ensure one is created at this path and that Jenkins can read it.
Key exchange was not finished, connection is closed.
SSH Connection failed with IOException: "Key exchange was not finished, connection is closed.", retrying in 15 seconds. There are 10 more retries left.
手順⑥:SSH接続「known_hosts」ファイルのエラーの対処手順
rootユーザーのホームディレクトリ配下にある「known_hosts」ファイルを一度クリアします。
[root@SV-SATURN .ssh]# cd ~/.ssh
[root@SV-SATURN .ssh]# ls
known_hosts
[root@SV-SATURN .ssh]# cat known_hosts
sv-neptune,192.168.1.105 ecdsa-sha2-nistp256 ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
ssh-keygen -R sv-neptune
[root@SV-SATURN .ssh]# ssh-keygen -R sv-neptune
# Host sv-neptune found: line 1
/root/.ssh/known_hosts updated.
Original contents retained as /root/.ssh/known_hosts.old
[root@SV-SATURN .ssh]# cat known_hosts
[root@SV-SATURN .ssh]# ls
known_hosts known_hosts.old
[root@SV-SATURN .ssh]# ls -l
合計 4
-rw------- 1 root root 0 11月 7 20:35 known_hosts
-rw-r--r-- 1 root root 186 11月 7 20:11 known_hosts.old
「known_hosts」をクリア後、SSH接続ができました。
JenkinsサーバのJenkinsユーザーのログインシェルを変更
Jenkinsインストール時に自動作成されるjenkinsユーザーのログインシェルは/bin/false
になっているので、su
コマンドではjenkinsユーザーに切り替えができません。
cat /etc/passwd | grep jenkins
[root@SV-SATURN ~]# cat /etc/passwd | grep jenkins jenkins:x:996:993:Jenkins Automation Server:/var/lib/jenkins:/bin/false
なので、ログインシェルを/bin/bash
に変更します。
chsh jenkins
[root@SV-SATURN ~]# chsh jenkins jenkins のシェルを変更します。 新しいシェル [/bin/false]: /bin/bash シェルを変更しました。
jenkinsユーザーに切り替え
su - jenkins
[root@SV-SATURN .ssh]# su - jenkins 最終ログイン: 2020/11/08 (日) 09:57:59 JST pc-ganymedeから開始日時 pts/0
jenkinsユーザーで外部サーバにSSH接続してknownホストに追加
ssh -i ~/.ssh/id_rsa jenkins@sv-neptune
-bash-4.2$ ssh -i ~/.ssh/id_rsa jenkins@sv-neptune The authenticity of host 'sv-neptune (192.168.1.105)' can't be established. ECDSA key fingerprint is SHA256:I0Lyk7G2ctNnd31f5qelI8cMAeIEs9agwMxuRRE2XgI. ECDSA key fingerprint is MD5:e7:54:fe:56:3b:74:5e:01:86:38:b4:63:b4:24:49:48. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'sv-neptune,192.168.1.105' (ECDSA) to the list of known hosts. Enter passphrase for key '/var/lib/jenkins/.ssh/id_rsa':★★★パスフレーズ ############################### # SV-NEPTUNE # 192.168.1.105 # LAMP Server # CentOS8.1 ############################### Activate the web console with: systemctl enable --now cockpit.socket Last login: Sat Nov 7 20:55:36 2020 from 192.168.1.101 [jenkins@sv-neptune ~]$
known_hostsファイルが作成されたことを確認
cat /var/lib/jenkins/.ssh/known_hosts
-bash-4.2$ cat known_hosts sv-neptune,192.168.1.105 ecdsa-sha2-nistp256 ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●
赤い×マークが消えました。
「Trust SSH Host Key」をクリック
「Yes」をクリック
管理対象サーバの追加が完了しました。
Jenkinsから外部サーバに対してジョブを実行する手順
最後に、追加したサーバに対してJenkinsからジョブを実行するテストをしてみます。
テスト用のジョブを作成(フリースタイル・プロジェクトのビルド)
「実行するノードを制限」でラベルを設定
テスト用のジョブにシェルを記述
今回はテスト用に「Hello World」をテキストファイルに出力するシェルを書きました。
コンソール出力で結果を確認
「Finished: SUCCESS」と出力されました!
外部サーバ側で実行結果の成果物ファイルを確認
cd ~/workspace/test-sv-neptune-job01/
[jenkins@sv-neptune workspace]$ cd ~/workspace/test-sv-neptune-job01/ [jenkins@sv-neptune test-sv-neptune-job01]$ ls -l 合計 4 -rw-rw-r-- 1 jenkins jenkins 12 11月 7 20:53 hello.txt [jenkins@sv-neptune test-sv-neptune-job01]$ cat hello.txt Hello World
ファイルに「Hello World」と書かれていました。成功です!
[改訂第3版]Jenkins実践入門 ビルド・テスト・デプロイを自動化する技術 WEB+DB PRESS plus |
参考にしたサイト
https://iij.github.io/bootcamp/development/jenkins/iij.github.io
インフラエンジニアの勉強ができるオンライン学習サイト
Udemy (オンライン動画学習サイト)
Udemy(ユーデミー)は、オンラインで動画で学習できるサービスです。
ITエンジニア系の講座もたくさん公開されています。
Udemyの特徴
- ユーザー登録すれば、第1回の講座は無料で視聴できる
- 無料のコンテンツもある
- 過去に受講した人の人数や、受講した人の評価も表示されるので選びやすい