aws上にkubernetesの環境を構築する。クラスタ作成(masterとnodeの構築)まで書いていく
kopsとは
kubernetes公式デプロイツールの一つ Kops は Kubernetes Operationsの略。 cloud上でkubernetes(k8s)のインストール(クラスタ構築)、操作、削除ができる
環境
macOS: 10.13.x kops: 1.7.0 kubelet: 1.7.2 aws-cli: 1.11
クラスタ作成手順
各cliインストール
https://github.com/kubernetes/kops/blob/master/docs/install.md に従って、kops・kubectl・awscliをインストールする
brew update && brew install kops
brew install kubernetes-cli
pip install awscli
aws認証情報設定
とりあえずadminを設定しておけば問題ない。configureに登録&環境変数に設定する
aws configure
export AWS_ACCESS_KEY_ID=xxxx
export AWS_SECRET_ACCESS_KEY=xxxx
もし必要最低限の権限を持ったユーザ(グループ)を作成したい場合は、下リンク先のシェルを使うと簡単に作成できる
https://github.com/kubernetes/kops/blob/master/hack/new-iam-user.sh
chmod +x new-iam-user.sh
./new-iam-user.sh kops kops
必要な権限を持ったkops groupとkops userが作成される
DNS設定
普通に作成するとroute53周りの設定、ドメインを購入などをしなければならないみたいだが、kops v1.6.2~であれば、localで(?)作成することでそれらの設定なしにクラスタが作成できるらしい
https://github.com/kubernetes/kops/blob/master/docs/aws.md#configure-dns
a gossip-based cluster can be easily created. The only requirement to trigger this is to have the cluster name end with .k8s.local. If a gossip-based cluster is created then you can skip this section.
とある通り、クラスタ名の末尾を.k8s.localにすることで、gossip-based cluster(?)で作られるため、DNS設定しなくてもクラスタが作成できる
s3 bucket作成
configファイルなどを保存するbucketをあらかじめ作成する必要がある。作成したら環境変数に設定する
aws s3api create-bucket --bucket kops.test --create-bucket-configuration LocationConstraint=ap-northeast-1
export KOPS_STATE_STORE=s3://kops.test
クラスタ作成
# コマンド例
kops create cluster --name=kops.test.k8s.local --zones=ap-northeast-1a --image="ami-0417e362" --master-size="t2.micro" --node-size="t2.micro" --node-count="1" --network-cidr="172.30.0.0/16" --yes
# 各オプション
--name="cluster-name" # 任意の名前。例ではk8s.local
--zones="region" # クラスタを作成するregion
--vpc="vpc-id" # 指定したvpc-id内にクラスタ作成
--image="image-id" # デフォルトはdebian。例はubuntu16.04のimage-id
--master-size="instance-type" # デフォルトはt2.medium
--node-size="instance-type" # デフォルトはt2.medium
--node-count="1" # nodeインスタンス数
--master-count="1" # masterインスタンス数
--network-cidr="172.34.0.0/16" # 指定したアドレスのvpcを作成して、その中にクラスタ作成
--yes # クラスタ作成フラグ これがないとクラスタは作られない
実行すると、EC2上にk8sのクラスタ(例ではmaster1台、node1台)が作成される。さらにapi server接続用のELB、(必要なら)vpcなどの各種ネットワーク設定も作成される
クラスタ編集
クラスタ更新
# クラスタの設定を編集 editorが立ち上がる
kops edit cluster kops.test.k8s.local
# インスタンスタイプや数、imageを編集
# masterの編集(masterの名前は master-"region"になる模様)
kops edit ig --name=kops.test.k8s.local master-ap-northeast-1a
# nodesの編集
kops edit ig --name=kops.test.k8s.local nodes
# 設定の反映
kops update cluster kops.test.k8s.local --yes
クラスタ削除
kops delete cluster kops.test.k8s.local —yes