前回、k8s構築ツールの1つとしてHelmを紹介しました。

今回はKubespray/kopsを取り上げます。

表:Kubernetesの3つのユースケース(第3回の再掲) 

Kubespray

KubesprayはAnsibleベースのKubernetes構築ツールです。AWSやGCPなどのクラウド環境や、ベアメタル(RHELなどのOS)に対応しており、マルチノードクラスタを構築できます。

早速インストールしていきましょう。まずは、Kubesprayの公式リポジトリからgit cloneし、pipを使ってKubesprayを利用する際に必要なモジュールをインストールします。

$git clone https://github.com/kubernetes-incubator/kubespray.git
Cloning into 'kubespray'...
remote: Counting objects: 27935, done.
remote: Compressing objects: 100% (26/26), done.
remote: Total 27935 (delta 10), reused 10 (delta 1), pack-reused 27908
Receiving objects: 100% (27935/27935), 8.38 MiB | 226.00 KiB/s, done.
Resolving deltas: 100% (15357/15357), done.

$cd kubespray/
$python -V && pip -V
Python 3.6.5
pip 18.0 from /Users/yujishono/.pyenv/versions/3.6.5/lib/python3.6/site-packages/pip (python 3.6)

$sudo pip install -r requirements.txt

~中略~

  Running setup.py install for ansible-modules-hashivault ... done
Successfully installed ansible-2.6.4 ansible-modules-hashivault-3.9.7 asn1crypto-0.24.0 bcrypt-3.1.4 cffi-1.11.5 cryptography-2.3.1 hvac-0.6.4 netaddr-0.7.19 paramiko-2.4.1 pbr-4.2.0 pyasn1-0.4.4 pycparser-2.18 pynacl-1.2.1

Kubesprayを利用するための事前準備が終わったら、Ansible Playbookを使ってKubesprayをデプロイし、クラスタ環境を構築します。なお、Vagrantによる構築も可能です。

$cp -R inventory/sample inventory/mycluster

$declare -a IPS=(10.10.1.3 10.10.1.4 10.10.1.5)
$CONFIG_FILE=inventory/mycluster/hosts.ini python3 contrib/inventory_builder/inventory.py ${IPS[@]}
DEBUG: Adding group all
DEBUG: Adding group kube-master

~中略~

DEBUG: adding host node5 to group kube-node
DEBUG: adding host node6 to group kube-node

$ansible-playbook -i inventory/mycluster/inventory.ini --become --become-user=root cluster.yml

デプロイが完了したら、クラスタ構築は完了です。masterノードにsshログインすることで、「kubectl get nodes」などkubectlによるクラスタ管理が可能になります。

kops

kopsはコマンドラインベースのKubernetesクラスタ構築ツールです。 AWSのみ公式サポートされており、GCEやOpenStackはベータ版として提供されています。 インフラ構成管理ツールであるTerraformの構成ファイルを生成することもできます。

早速、brewを使ってkopsをインストールします。

$brew install kops

~中略~

==> Installing kops
==> Downloading https://homebrew.bintray.com/bottles/kops-1.15.0.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/04/04fa19e30a1dee4d2c76b59d5c6b3742d80a91aefbd7bf5e3a3500ed4aef93bc?__gda__=exp=1578840225~hmac=8bf7258c667a19844cab1f85ff2bfd153eb37
################################################ 100.0%
==> Pouring kops-1.15.0.mojave.bottle.tar.gz
==> Caveats
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
  /usr/local/Cellar/kops/1.15.0: 5 files, 134.7MB

~後略~

$

クラスタ作成時に必要な公開鍵を作成します。

$ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/yujishono/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/yujishono/.ssh/id_rsa.
Your public key has been saved in /Users/yujishono/.ssh/id_rsa.pub.
The key fingerprint is:
~後略~

AWSのCLIクライアントをインストールします。

$brew install awscli

~中略~

==> Installing awscli
==> Downloading https://homebrew.bintray.com/bottles/awscli-1.17.0.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/3a/3afb7070bd5b9f000d69a2e5864c3a8d71a3a3a956f1c3b60d24a8ff7454be32?__gda__=exp=1578842481~hmac=f326dfd476becb1769ec66fcca22911ea49ac
################################################ 100.0%
==> Pouring awscli-1.17.0.mojave.bottle.tar.gz
==> Caveats
The "examples" directory has been installed to:
  /usr/local/share/awscli/examples

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions and functions have been installed to:
  /usr/local/share/zsh/site-functions
==> Summary
  /usr/local/Cellar/awscli/1.17.0: 8,955 files, 60.2MB

~中略~

==> awscli
The "examples" directory has been installed to:
  /usr/local/share/awscli/examples

~後略~

MFAを利用している場合にはAWS_SESSION_TOKENなどのセキュリティの設定を実施します。

※環境変数に設定する値の取得
$aws sts get-session-token --serial-number arn:aws:iam::[アカウント名]:mfa/[ユーザ名] --token-code [MFA値]
CREDENTIALS [AWS_ACCESS_KEY_IDに設定する値]   2020-01-25T12:27:52Z     [AWS_SECRET_ACCESS_KEYに設定する値]  [AWS_SESSION_TOKENに設定する値]

※環境変数の設定
$export AWS_ACCESS_KEY_ID=[上記で入手した値]
$export AWS_SECRET_ACCESS_KEY=[上記で入手した値]
$export AWS_SESSION_TOKEN=[上記で入手した値]

kopsクラスタで利用するS3を作成します。

$export S3_BUCKET=example-state-store-$(cat /dev/random | LC_ALL=C tr -dc "[:alpha:]" | tr '[:upper:]' '[:lower:]' | head -c 32)
$export KOPS_STATE_STORE=s3://${S3_BUCKET}
$aws s3 mb $KOPS_STATE_STORE
make_bucket: example-state-store-qupvccmguarvgzfydpzbczpnefizcynq
$aws s3api put-bucket-versioning \
>   --bucket $S3_BUCKET \
>   --versioning-configuration \
>   Status=Enabled

各種事前構築が完了し、いよいよkops create clusterコマンドでクラスタを作成します。

$kops create cluster \
>   --name example.cluster.k8s.local \
>   --zones ap-northeast-1a,ap-northeast-1c \
>   --yes
I0125 09:40:32.285453   47888 create_cluster.go:517] Inferred --cloud=aws from zone "ap-northeast-1a"
~中略~
I0125 09:40:59.668065   47888 update_cluster.go:294] Exporting kubecfg for cluster
kops has set your kubectl context to example.cluster.k8s.local
Cluster is starting.  It should be ready in a few minutes.
~後略~

作成したクラスタを検証し、ノードを確認します。

$kops validate cluster
Using cluster from kubectl context: example.cluster.k8s.local


State Store: Required value: Please set the --state flag or export KOPS_STATE_STORE.
For example, a valid value follows the format s3://<bucket>.
You can find the supported stores in https://github.com/kubernetes/kops/blob/master/docs/state.md.

$kubectl get nodes
NAME                                               STATUS   ROLES    AGE     VERSION
ip-172-20-45-180.ap-northeast-1.compute.internal   Ready    node     7d18h   v1.15.7
ip-172-20-49-131.ap-northeast-1.compute.internal   Ready    master   7d18h   v1.15.7
ip-172-20-79-6.ap-northeast-1.compute.internal     Ready    node     7d18h   v1.15.7

以上でクラスタ構築は完了です。

まとめ

今回は、k8sを簡単に使いこなす手段としてKubesprayとkopsを紹介しました。前回までに紹介したRancherやHelmと合わせて選択肢の1つとして押さえておくとよいでしょう。

次回は、軽量k8sツールであるMicroK8sとk3sを紹介します。併せて学習サイトとしてKatacodaを紹介します。

著者紹介


正野 勇嗣 (SHONO Yuji ) - NTTデータ 課長

2011年まで開発自動化技術のR&Dに従事。その後、開発プロジェクト支援やトラブルシューティング等に主戦場を移す。「ソースコード自動生成」に加えて、JenkinsやMaven等の「ビルド自動化」、JsTestDriverやSelenium等の「テスト自動化」を扱うようになり、多様化する開発自動化技術動向に興味。

最近は第四の自動化であるInfrastructure as Code等の「基盤自動化」の魅力に惹かれている。開発自動化技術に関する雑誌・記事執筆も行う。3児のパパ。