com4dc’s blog

Javaプログラマーのはずだけど運用してます

GKEでKubernetes実行環境をセットアップ

GCP環境作成

こちら を参考にセットアップしてみた記録。

目標はこの記事と同じく最小限のKubernetes学習環境を整えること!

GCP利用開始までの手順

  1. Googleアカウント作成
  2. GCPコンソール有効化
  3. Cloud SDKのセットアップ

Googleアカウント作成

GCP利用のためのアカウントを新規で作成する。

  • XXXXX@gmail.com
  • Chromeのユーザー切り替えで利用できるようにしておく(アカウント分離のため)

MFAのTitan Securityの設定を忘れないこと

GCPコンソール有効化

f:id:com4dc:20191010182408j:plain

ポチッと押すとしばらく時間がかかる。気長にまとう。

Cloud SDKセットアップ

待っている間にGCPを操作するために必要なツールをセットアップしておく。こちらの手順 を参考にインストールしていく。

$ curl https://sdk.cloud.google.com | bash
(snip)

x google-cloud-sdk/VERSION
x google-cloud-sdk/.install/
x google-cloud-sdk/.install/gcloud-deps.snapshot.json
x google-cloud-sdk/.install/gcloud-deps.manifest
x google-cloud-sdk/.install/core.snapshot.json
x google-cloud-sdk/.install/core.manifest
x google-cloud-sdk/.install/.download/

/your/path/google-cloud-sdk/install.sh
Welcome to the Google Cloud SDK!

To help improve the quality of this product, we collect anonymized usage data
and anonymized stacktraces when crashes are encountered; additional information
is available at <https://cloud.google.com/sdk/usage-statistics>. You may choose
to opt out of this collection now (by choosing 'N' at the below prompt), or at
any time in the future by running the following command:

    gcloud config set disable_usage_reporting true

Do you want to help improve the Google Cloud SDK (Y/n)?  Y
(snip)

╠════════════════════════════════════════════════════════════╣
╠═ Installing: Default set of gcloud commands               ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gcloud cli dependencies                      ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Creating backup and activating new installation          ═╣
╚════════════════════════════════════════════════════════════╝

Performing post processing steps...done.

Update done!


Modify profile to update your $PATH and enable shell command
completion?

Do you want to continue (Y/n)?  Y

The Google Cloud SDK installer will now prompt you to update an rc
file to bring the Google Cloud CLIs into your environment.

Enter a path to an rc file to update, or leave blank to use
[/your/path/.bash_profile]:
Backing up [/your/path/.bash_profile] to [/your/path/.bash_profile.backup].
[/your/path/.bash_profile] has been updated.

==> Start a new shell for the changes to take effect.


For more information on how to get started, please visit:
  https://cloud.google.com/sdk/docs/quickstarts

インストール完了。ツールの展開先は、ひとまず特に考えずに自身のHomeディレクトリ以下に展開した。

Initialize Update

インストール直後なのでUpdateを実行する。今回は特に何も更新なし。

$ gcloud components update

All components are up to date.

GKEクラスタ立ち上げ

kubernetesを利用するためにまずはNode3つのクラスタを一つ立ち上げる。

$ gcloud container clusters create gke-ignite-cluster --preemptible --machine-type=f1-micro --num-nodes=3 --disk-size=10

ERROR: (gcloud.container.clusters.create) You do not currently have an active account selected.
Please run:

  $ gcloud auth login

to obtain new credentials, or if you have already logged in with a
different account:

  $ gcloud config set account ACCOUNT

to select an already authenticated account to use.

まずはじめにユーザーの認証情報が要求される。(そりゃそうだ)

今回は初めて認証情報を作成するので、 $ gcloud auth login を実行する。

$ gcloud auth login
Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?code_challenge=XXXXXXXXXXXX-t3EgkGPFWHXoVw-s95gEm7U&prompt=select_account&code_challenge_method=S256&access_type=offline&redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&response_type=code&client_id=XXXXXXXXX.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth


WARNING: `gcloud auth login` no longer writes application default credentials.
If you need to use ADC, see:
  gcloud auth application-default --help

You are now logged in as [XXXXXXXXX].
Your current project is [None].  You can change this setting by running:
  $ gcloud config set project PROJECT_ID

コマンドを実行すると、生成したURLをブラウザが勝手に実行します。自分の場合、Chromeでユーザーの切替を行っていたため、ユーザーを切り替えたタブを使って自分自身でURLをコピペして実行しました。通常のGoogleアカウントへのログイン画面へ遷移し、GCPへの権限移譲の画面が現れます。問題がなければ承認。

認証情報の作成も問題なくできたので、再度同じコマンドを実行します。

$ gcloud container clusters create gke-ignite-cluster --preemptible --machine-type=f1-micro --num-nodes=3 --disk-size=10
ERROR: (gcloud.container.clusters.create) One of [--zone, --region] must be supplied: Please specify location.

失敗しました。エラーメッセージを確認します。

Region指定がない

--region を指定しろと言われているようです。北海道だと意外と西海岸が近いので米国の中でもLAを選択しおました。ここは日本でも良いかもしれません(AWSの癖で安い方は米国という考えから選択。厳密に調べていません。)

$ gcloud container clusters create gke-ignite-cluster --preemptible --machine-type=f1-micro --num-nodes=3 --disk-size=10 --region us-west2
ERROR: (gcloud.container.clusters.create) The required property [project] is not currently set.
You may set it for your current workspace by running:

  $ gcloud config set project VALUE

or it can be set temporarily by the environment variable [CLOUDSDK_CORE_PROJECT]

次は別のエラーメッセージ。

Project指定がない

project が指定されていないためNGのようです。GCPコンソールでプロジェクトを新規作成して設定します。

$ gcloud config set project YOUR_PROJECT_ID
Updated property [core/project].

再度実行してみます。

$ gcloud container clusters create gke-ignite-cluster --preemptible --machine-type=f1-micro --num-nodes=3 --disk-size=10 --region us-west2
WARNING: In October 2019, node auto-upgrade will be enabled by default for newly created clusters and node pools. To disable it, use the `--no-enable-autoupgrade` flag.
WARNING: Starting in 1.12, new clusters will have basic authentication disabled by default. Basic authentication can be enabled (or disabled) manually using the `--[no-]enable-basic-auth` flag.
WARNING: Starting in 1.12, new clusters will not have a client certificate issued. You can manually enable (or disable) the issuance of the client certificate using the `--[no-]issue-client-certificate` flag.
WARNING: Currently VPC-native is not the default mode during cluster creation. In the future, this will become the default mode and can be disabled using `--no-enable-ip-alias` flag. Use `--[no-]enable-ip-alias` flag to suppress this warning.
WARNING: Starting in 1.12, default node pools in new clusters will have their legacy Compute Engine instance metadata endpoints disabled by default. To create a cluster with legacy instance metadata endpoints disabled in the default node pool, run `clusters create` with the flag `--metadata disable-legacy-endpoints=true`.
WARNING: Your Pod address range (`--cluster-ipv4-cidr`) can accommodate at most 1008 node(s).
This will enable the autorepair feature for nodes. Please see https://cloud.google.com/kubernetes-engine/docs/node-auto-repair for more information on node autorepairs.
ERROR: (gcloud.container.clusters.create) ResponseError: code=403, message=
    (1) insufficient regional quota to satisfy request: resource "CPUS": request requires '9.0' and is short '1.0'. project has a quota of '8.0' with '8.0' available. View and manage quotas at https://console.cloud.google.com/iam-admin/quotas?usage=USED&project=YOUR_PROJECT_ID
    (2) insufficient regional quota to satisfy request: resource "IN_USE_ADDRESSES": request requires '9.0' and is short '1.0'. project has a quota of '8.0' with '8.0' available. View and manage quotas at https://console.cloud.google.com/iam-admin/quotas?usage=USED&project=YOUR_PROJECT_ID.

処理は走り始めましたが、最後にERRORで停止しました。

ERROR を見るとCPUのQuotaに引っかかってるようです。無料枠のアカウントだからかも。Upgradeして300$使い切れる枠に変更します。

f:id:com4dc:20191010182455j:plain

同じくだめ

再実行しましたが、NG。もう一度よく見てみます。

    (2) insufficient regional quota to satisfy request: resource "IN_USE_ADDRESSES": request requires '9.0' and is short '1.0'. project has a quota of '8.0' with '8.0' available. View and manage quotas at https://console.cloud.google.com/iam-admin/quotas?usage=USED&project=YOUR_PROJECT_ID.

IP Addressの上限値に引っかかってるようで上限緩和を申請しないといけないらしい。とりあえず9個にしとく

f:id:com4dc:20191010182511j:plain

めっちゃ早かった。

再実行

再実行してみます。

$ gcloud container clusters create gke-ignite-cluster --preemptible --machine-type=f1-micro --num-nodes=3 --disk-size=10 --region us-west2
WARNING: In October 2019, node auto-upgrade will be enabled by default for newly created clusters and node pools. To disable it, use the `--no-enable-autoupgrade` flag.
WARNING: Starting in 1.12, new clusters will have basic authentication disabled by default. Basic authentication can be enabled (or disabled) manually using the `--[no-]enable-basic-auth` flag.
WARNING: Starting in 1.12, new clusters will not have a client certificate issued. You can manually enable (or disable) the issuance of the client certificate using the `--[no-]issue-client-certificate` flag.
WARNING: Currently VPC-native is not the default mode during cluster creation. In the future, this will become the default mode and can be disabled using `--no-enable-ip-alias` flag. Use `--[no-]enable-ip-alias` flag to suppress this warning.
WARNING: Starting in 1.12, default node pools in new clusters will have their legacy Compute Engine instance metadata endpoints disabled by default. To create a cluster with legacy instance metadata endpoints disabled in the default node pool, run `clusters create` with the flag `--metadata disable-legacy-endpoints=true`.
WARNING: Your Pod address range (`--cluster-ipv4-cidr`) can accommodate at most 1008 node(s).
This will enable the autorepair feature for nodes. Please see https://cloud.google.com/kubernetes-engine/docs/node-auto-repair for more information on node autorepairs.
Creating cluster gke-ignite-cluster in us-west2...⠶

作成され始めました。以下クラスタのステータスを確認。

kubeconfig entry generated for gke-ignite-cluster.
NAME                LOCATION  MASTER_VERSION  MASTER_IP     MACHINE_TYPE  NODE_VERSION   NUM_NODES  STATUS
gke-ignite-cluster  us-west2  1.13.7-gke.24   35.235.73.88  f1-micro      1.13.7-gke.24  9          RUNNING

完了です。ここまでやったことは

  • credentialsの作成
  • projectの作成
  • regionの指定
  • アカウントのUpgrade
  • IP Addressの上限緩和

なかなか手順が多い。

Node Poolの作成

ノードプールとは。

ノードプールとは、同じクラスタに含まれる、すべてが同じように構成されたノード インスタンスのサブセットです。

うーん?わかるようなわからないような・・・。

$ gcloud container node-pools create ignite-node-pool --cluster gke-ignite-cluster --machine-type=f1-micro --num-nodes=1 --disk-size=10 --region us-west2
WARNING: In October 2019, node auto-upgrade will be enabled by default for newly created clusters and node pools. To disable it, use the `--no-enable-autoupgrade` flag.
WARNING: Starting in 1.12, new node pools will be created with their legacy Compute Engine instance metadata APIs disabled by default. To create a node pool with legacy instance metadata endpoints disabled, run `node-pools create` with the flag `--metadata disable-legacy-endpoints=true`.
This will enable the autorepair feature for nodes. Please see https://cloud.google.com/kubernetes-engine/docs/node-auto-repair for more information on node autorepairs.
Creating node pool ignite-node-pool...done.
Created [https://container.googleapis.com/v1/projects/YOUR_PROJECT_ID/zones/us-west2/clusters/gke-ignite-cluster/nodePools/ignite-node-pool].
NAME              MACHINE_TYPE  DISK_SIZE_GB  NODE_VERSION
ignite-node-pool  f1-micro      10            1.13.7-gke.24

こちらもIP制限でひっかかったので再度Quota上限緩和請求。ちょっとずつ上限緩和するのもどうかと思ったので20に増加させました。 申請通過後、上記コマンド再実行させた結果、無事立ち上がりました。

$ gcloud container node-pools list --cluster gke-ignite-cluster --region us-west2
NAME              MACHINE_TYPE  DISK_SIZE_GB  NODE_VERSION
default-pool      f1-micro      10            1.13.7-gke.24
ignite-node-pool  f1-micro      10            1.13.7-gke.24

Nodes確認

$ kubectl get nodes
NAME                                                  STATUS     ROLES    AGE   VERSION
gke-gke-ignite-clust-ignite-node-pool-1f5479a0-r8wz   NotReady   <none>   15m   v1.13.7-gke.24
gke-gke-ignite-clust-ignite-node-pool-8d9d5994-80mt   Ready      <none>   15m   v1.13.7-gke.24
gke-gke-ignite-clust-ignite-node-pool-f57eee91-p05p   Ready      <none>   15m   v1.13.7-gke.24
gke-gke-ignite-cluster-default-pool-70937820-jp42     Ready      <none>   26m   v1.13.7-gke.24
gke-gke-ignite-cluster-default-pool-70937820-qw8x     NotReady   <none>   26m   v1.13.7-gke.24
gke-gke-ignite-cluster-default-pool-70937820-wchg     NotReady   <none>   26m   v1.13.7-gke.24
gke-gke-ignite-cluster-default-pool-771bc537-0r4b     NotReady   <none>   26m   v1.13.7-gke.24
gke-gke-ignite-cluster-default-pool-771bc537-1gjl     Ready      <none>   26m   v1.13.7-gke.24
gke-gke-ignite-cluster-default-pool-771bc537-mkj8     Ready      <none>   26m   v1.13.7-gke.24
gke-gke-ignite-cluster-default-pool-a4ded3e8-9gkn     Ready      <none>   14m   v1.13.7-gke.24
gke-gke-ignite-cluster-default-pool-a4ded3e8-n9nv     NotReady   <none>   26m   v1.13.7-gke.24
gke-gke-ignite-cluster-default-pool-a4ded3e8-wrpq     Ready      <none>   26m   v1.13.7-gke.24

めっちゃたくさんNodeが立ち上がっています。とりあえずひとまずこれでKubernetesの環境は一通り揃った(と思う)

余談

過去いくつかのkubernetes環境をkubectlで利用したことがある場合、GKEな環境に切り替わってない場合がありそう(自分はそうだった)なので、kubectx 等で自分がどの環境にいるかを確認してから kubectl は実行したほうが良さそう。

まとめ

手順としては公式のドキュメントが充実していたため、困ったら公式を見たりエラーメッセージを見るだけで解決するには時間がかからなかった。 ただ、最小コストでKubernetes環境を構築したいという目標からちょうど良い記事を見つけたので自分も試してみた次第。今の所Podも何も動いてないのでちょっとずつ手を動かして覚えていきたい。

blog.a-know.me