LoginSignup
7
13

More than 5 years have passed since last update.

【メモ】 AWSにkopsを使ってkubernetesのクラスタを構築する

Last updated at Posted at 2017-01-10

kopsでクラスターをインストールしてみたのでメモしておく

眠い。

元ネタ

普通に上の記事を見ながらやった方が良い

kubectlのインストール

# 最新が1.5.1だったので1.5.1をインストール
# Linux
wget https://storage.googleapis.com/kubernetes-release/release/v1.5.1/bin/linux/amd64/kubectl
chmod +x kubectl
mv kubectl /usr/local/bin/kubectl

# Macで作業する場合は以下らしい(やってない)
wget https://storage.googleapis.com/kubernetes-release/release/v1.5.1/bin/darwin/amd64/kubectl
chmod +x kubectl
mv kubectl /usr/local/bin/kubectl

kopsのインストール

# Linuxから作業した場合
wget https://github.com/kubernetes/kops/releases/download/v1.4.1/kops-linux-amd64
chmod +x kops-linux-amd64
mv kops-linux-amd64 /usr/local/bin/kops

# Macで作業する場合は以下らしい(やってない)
wget https://github.com/kubernetes/kops/releases/download/v1.4.1/kops-darwin-amd64
chmod +x kops-darwin-amd64
mv kops-darwin-amd64 /usr/local/bin/kops

AWS Cliをインストール/設定しておく

cliで使うユーザの権限でS3とかIAMの作成権限とか必要っぽいのでIAMロールに追加しておく

今回つけておいたポリシー (不要なのもあるかも)

  • AmazonEC2FullAccess
  • AmazonSQSFullAccess (どっかでSQS権限が必要とか見た記憶があるんだけど幻だったかもしれない)
  • IAMFullAccess
  • AmazonS3FullAccess
  • AmazonRoute53FullAccess
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
sudo python get-pip.py
sudo pip install awscli
aws configure

Route53にkubernetesクラスター用のゾーンを作成

# aws cliが使えるなら以下の感じで作る
aws route53 create-hosted-zone --name dev.example.com --caller-reference 1

S3にクラスターの設定ファイルを置くバケットを作る

# aws cliが使えるなら以下の感じで作る
aws s3 mb s3://clusters.dev.example.com

# 以下の設定をbash_profileあたりに書いて置く
# export KOPS_STATE_STORE=s3://clusters.dev.example.com
vi ~/.bash_profile
source ~/.bash_profile

クラスターの作成

この時点では設定ファイルが出来上がるだけでEC2インスタンスとかは立ち上がらないです

# VPCを指定しないと自動でVPCが作られてそこにインスタンスが配置される
kops create cluster --zones=ap-northeast-1a apnortheast1.dev.example.com
# 既存のVPCを指定する場合はvpcオプションとnetwork-cidrオプションを指定すればOK
# https://github.com/kubernetes/kops/blob/master/docs/run_in_existing_vpc.md
kops create cluster --zones=ap-northeast-1a apnortheast1.dev.example.com --vpc=vpc-xxxxxxx --network-cidr=172.31.0.0/16

クラスターの定義を修正

デフォルトで立ち上げるとそこそこ良いインスタンス作られるので貧乏仕様に変更

# クラスターの設定
# デフォルトのままで問題ないかと。名前が気に入らなかったり、kubernetesのバージョンとか変えたかったら編集
kops edit cluster apnortheast1.dev.example.com

# master用インスタンスの設定
# なんか自動でmaster-zone名で名前ついてる
# machineTypeを好きなインスタンスタイプに変える
# おそらくmaxSize/minSizeでmasterのインスタンス数を制御できる
# イメージはAMI IDを指定しても大丈夫。デフォルトだとDebian(jessie)だったけど、ami-eec1c380とか指定すればCentOS7で作成される
# https://github.com/kubernetes/kops/blob/master/docs/images.md
kops edit ig --name=apnortheast1.dev.example.com master-ap-northeast-1a

# node用インスタンスの設定
# マスター用と同じノリ
kops edit ig --name=apnortheast1.dev.example.com nodes

クラスター情報の更新

# 設定ファイルだけ反映
kops update cluster apnortheast1.dev.example.com

# 問題なければ --yesをつけて実行するとインスタンスとか作られる
# ちなみにVPCは自動的に"apnortheast1.dev.example.com"で作成されている
# networkCIDRが一致してるVPCがあったらそこに作ってくれるのかな?
# -> 20170111追記 create時に指定すればVPCを指定できた。createのとこにコマンド追記。後からは変えられないっぽい
kops update cluster apnortheast1.dev.example.com --yes

API用のDNSレコードができてないバグ

https://github.com/kubernetes/kops/issues/859

v1.5で修正されるかな

sshログインしてみる

コンソールで確認するとパブリックIPが付与されているので~/.ssh/id_rsaを使ってssh接続できる

# デフォルトで~/.ssh/id_rsa.pubを使って鍵交換してくれるっぽい
# なので-iで指定しなくても一緒
ssh -i ~/.ssh/id_rsa $publicIP
# kubectlのバージョン確認
kubectl version
# ノードの確認
kubectl get nodes
NAME                                               STATUS    AGE
ip-172-20-46-247.ap-northeast-1.compute.internal   Ready     1h
ip-172-20-51-144.ap-northeast-1.compute.internal   Ready     1h
ip-172-20-61-100.ap-northeast-1.compute.internal   Ready     1h
7
13
0

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
7
13