LoginSignup
2
1

More than 5 years have passed since last update.

既存のAWS環境にkopsでsubnetを指定してkubernetesを構築

Last updated at Posted at 2018-03-24

現在利用していて、複数のsubnetがすでに登録されているAWS環境に、kopsでkubernetesを構築する方法をまとめてみました。また東京リージョンでmaster冗長化も試してみました。

環境

Mac OS 10.12.6
kops 1.8.1

設定

今回の環境では以下の設定になっています。適宜読み替えてください。

項目 備考
vpc-id vpc-xxxxxxxx 既存アカウントで作成済のvpc-idを利用
region ap-northeast-1 東京リージョン
cidr 10.16.0.0/16
zone ap-northeast-1a,ap-northeast-1c,ap-northeast-1d 3つのzoneを利用

master-zonesの指定を2つにすると以下のようなエラーになります。
etcdが「The recommended etcd cluster size is 3, 5 or 7・・・」という制約に引っかかってるんでしょうか。
ap-northeast-1dがあってよかった。

There should be an odd number of master-zones, for etcd's quorum.  Hint: Use --zones and --master-zones to declare node zones and master zones separately.

バケット作成

terraformだとこんな感じかとおもいます。

resource "aws_s3_bucket" "k8s-dev" {
  bucket = "k8s-dev.example.com"
  acl    = "private"

  tags {
    Name        = "k8s-dev.example.com"
    Environment = "Dev"
  }
}

Route53設定

terraformだとこんな感じかとおもいます。

resource "aws_route53_zone" "k8s-dev" {
   name = "k8s-dev.example.com"
}

登録が終わったらちゃんと名前解決ができるように上位のDNSにNSレコードを登録しておきます。

環境変数の設定

# 権限のあるIDとKEYを設定
export AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXXXX
export AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXX

# リージョン設定
export AWS_DEFAULT_REGION=ap-northeast-1
export ZONES="ap-northeast-1a,ap-northeast-1c,ap-northeast-1d"

# 各ノードのサイズ
export NODE_SIZE=${NODE_SIZE:-m4.large}
export MASTER_SIZE=${MASTER_SIZE:-m4.large}

# kopsの設定が保存されるバケット(さきほど作ったもの)
export KOPS_STATE_STORE="s3://k8s-dev.example.com"

kops create

--yesをつけない

$ kops create cluster k8s-test.example.com \
--vpc=vpc-xxxxxxxx \
--network-cidr=10.16.0.0/16 \
--node-count 3 \
--zones $ZONES \
--node-size $NODE_SIZE \
--master-size $MASTER_SIZE \
--master-zones $ZONES

kops edit

ここで既存のサブネットとぶつからないように書き換える


$ kops edit cluster k8s-test.example.com

  subnets:
  - cidr: 10.16.100.0/24 #ここを書き換える
    name: ap-northeast-1a
    type: Public
    zone: ap-northeast-1a
  - cidr: 10.16.101.0/24 #ここを書き換える
    name: ap-northeast-1c
    type: Public
    zone: ap-northeast-1c
  - cidr: 10.16.102.0/24 #ここを書き換える
    name: ap-northeast-1d
    type: Public
    zone: ap-northeast-1d

念のため設定確認

--yesはつけない

$ kops update cluster k8s-test.example.com

kops update

ここで構築される

$ kops update cluster k8s-test.example.com --yes

しばらくまつと出来上がる

確認

$ kubectl get nodes
NAME                                                STATUS    ROLES     AGE       VERSION
ip-10-16-100-217.ap-northeast-1.compute.internal   Ready     node      1h        v1.8.7
ip-10-16-100-72.ap-northeast-1.compute.internal    Ready     master    1h        v1.8.7
ip-10-16-101-229.ap-northeast-1.compute.internal   Ready     master    1h        v1.8.7
ip-10-16-101-86.ap-northeast-1.compute.internal    Ready     node      1h        v1.8.7
ip-10-16-102-136.ap-northeast-1.compute.internal   Ready     node      1h        v1.8.7
ip-10-16-102-154.ap-northeast-1.compute.internal   Ready     master    1h        v1.8.7
2
1
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
2
1