LoginSignup
7
5

More than 5 years have passed since last update.

GKE でサービスを1から構築する #1

Last updated at Posted at 2018-05-18

概要

CaaSの右も左もわからない人間が1からGKEを使おうとしたらどうなるのか残す。
hello world を deploy するところまで行きたい。

今回のまとめ

  • google-cloud-sdk のインストールは brew じゃないほうがいいかも
    • google-cloud-sdk で入れるコマンド( kubectl )の PATH がうまく通ってない
    • not brew でうまくいくかは未検証
  • gcloud コマンドで操作するに辺り、プロジェクト側で事前に開けないといけないAPIがある
    • Compute Engine API
    • Kubernetes Engine API

触って見るところから

GKE の無料トライアル申込みをする。

何はなくともここから。
https://cloud.google.com/kubernetes-engine/?hl=ja

申し込んだら、GoogleCloudPlatform に飛んだ。昔 AppEngine 使ってみようとして頓挫した残骸が見えるが無視・・・したいがそもそもプロジェクトの消し方が気になるので調べる。

・・・とりあえず残骸を消す

メモ:Google Cloud Platform のプロジェクトの消し方
https://cloud.google.com/resource-manager/docs/creating-managing-projects?hl=ja#shutting_down_deleting_projects

プロジェクトを作る

全部消したら、この画面になった。

image.png

作成する。

image.png

クイックスタートを見る

ここ。
https://cloud.google.com/kubernetes-engine/docs/quickstart?hl=ja

ローカルシェル(macos)に必要なクライアントを入れる

この辺の環境構築は早めにまとめる。

まずここ。
https://cloud.google.com/sdk/docs/quickstart-macos?hl=ja

で調べると Homebrew で入りそうなのでこっちで入れる。

# cask の tap が先に必要
$ brew cask install google-cloud-sdk

マニュアルに戻ると、初期化作業とかがあるのでやる。

$ gcloud init
...
Pick cloud project to use: 
 [1] sample-project-204514
 [2] Create a new project
Please enter numeric choice or text value (must exactly match list 
item):  1
...

なんかブラウザと連動してアカウント認証してた。なにこれすごい。
リージョン設定しろとかなんとか書いてある気がするけどとりあえず進む。

image.png

これが出なかったので、たぶん Google Compute Engine API が有効になってないのである。

コンソールをもう一回眺めてみると・・・

Not setting default zone/region (this feature makes it easier to use
[gcloud compute] by setting an appropriate default value for the
--zone and --region flag).
See https://cloud.google.com/compute/docs/gcloud-compute section on how to set
default compute region and zone manually. If you would like [gcloud init] to be
able to do this for you the next time you run it, make sure the
Compute Engine API is enabled for your project on the
https://console.developers.google.com/apis page.

似たようなこと書いてある気がする。
案内の通り以下にアクセス。
https://console.developers.google.com/apis

APIの有効化ボタン押して、 Compute Engine API って検索したらちゃんと出たので有効化。
最終的に Google APIs の dashboard に以下が出てればいいかな。
image.png

もう一回 init してみる。

...
Pick configuration to use:
 [1] Re-initialize this configuration [default] with new settings 
 [2] Create a new configuration
Please enter your numeric choice:  1
...

最初から設定させてくれるそうなので順番に答えていく。

Do you want to configure a default Compute Region and Zone? (Y/n)?  Y

Which Google Compute Engine zone would you like to use as project 
default?
If you do not specify a zone via a command line flag while working 
with Compute Engine resources, the default is assumed.
 [1] us-east1-b
 [2] us-east1-c
 [3] us-east1-d
 [4] us-east4-c
...
 [25] europe-west2-a
 [26] asia-east1-b
 [27] asia-east1-a
 [28] asia-east1-c
...
Please enter numeric choice or text value (must exactly match list 
item):  28 

リージョン選べそう!とりあえずasiaっぽい28辺りで。
たぶん最終的にこれが出ればOK.

Your Google Cloud SDK is configured and ready to use!

一応設定を見る。

 $ gcloud config list
[compute]
region = asia-east1
zone = asia-east1-c
[core]
account = ***********
disable_usage_reporting = True
project = sample-project-204514

Your active configuration is: [default]

おっけーっぽい。クイックスタートに戻ろう。

kubctl をインストールする

すっかり忘れてた。クイックスタートに書いてある。

gcloud components install kubectl

gcloud のデフォルト設定を構成する

さっきの init で設定された項目ばかりなのでスキップ。
プロジェクトごとに config 切るのがいいのかなって思ったけど欲張らずスルー。

Kubernetes Engine クラスタの作成

クラスタ作ろう。

$ gcloud container clusters create sample-cluster
WARNING: Currently node auto repairs are disabled by default. In the future this will change and they will be enabled by default. Use `--[no-]enable-autorepair` flag  to suppress this warning.
WARNING: Starting in Kubernetes v1.10, new clusters will no longer get compute-rw and storage-ro scopes added to what is specified in --scopes (though the latter will remain included in the default --scopes). To use these scopes, add them explicitly to --scopes. To use the new behavior, set container/new_scopes_behavior property (gcloud config set container/new_scopes_behavior true).
ERROR: (gcloud.container.clusters.create) ResponseError: code=403, message=The Kubernetes Engine API is not enabled for project sample-project-204514. Please ensure it is enabled in the Google Cloud Console at https://console.cloud.google.com/apis/api/container.googleapis.com/overview?project=sample-project-204514 and try again.

作れない。APIがまたあいてないっぽい。
さっきのここでもう一回探してみる。
https://console.developers.google.com/apis

Kubernetes Engine API があるので、有効化。これがdashboardに出ればOK。
image.png

クラスタ作ろう(2回目)

$ gcloud container clusters create sample-cluster
WARNING: Currently node auto repairs are disabled by default. In the future this will change and they will be enabled by default. Use `--[no-]enable-autorepair` flag  to suppress this warning.
WARNING: Starting in Kubernetes v1.10, new clusters will no longer get compute-rw and storage-ro scopes added to what is specified in --scopes (though the latter will remain included in the default --scopes). To use these scopes, add them explicitly to --scopes. To use the new behavior, set container/new_scopes_behavior property (gcloud config set container/new_scopes_behavior true).
Creating cluster sample-cluster...done.                                                                                                              
Created [https://container.googleapis.com/v1/projects/sample-project-204514/zones/asia-east1-c/clusters/sample-cluster].
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/asia-east1-c/sample-cluster?project=sample-project-204514
kubeconfig entry generated for sample-cluster.
NAME            LOCATION      MASTER_VERSION  MASTER_IP        MACHINE_TYPE   NODE_VERSION  NUM_NODES  STATUS
sample-cluster  asia-east1-c  1.8.8-gke.0     104.199.171.229  n1-standard-1  1.8.8-gke.0   3          RUNNING

WARNING が2つ出てるけど設定系っぽいので追々。とりあえず cluster はあがったっぽい。やったぜ!

うきうきして見逃してたけど認証情報を一回持ってくる必要があるらしいので持ってくる。

$ gcloud container clusters get-credentials sample-cluster
Fetching cluster endpoint and auth data.
kubeconfig entry generated for sample-cluster.

クラスタ作成者以外はこれをやるのかな?

アプリケーションをクラスタにデプロイする

ついに来たー!既にあるアプリを deploy するらしい。

Deployment の作成

$ kubectl run hello-server --image gcr.io/google-samples/hello-app:1.0 --port 8080
-bash: kubectl: command not found

?!
brew で入れたのが原因でpathが通らないのかな・・・

$ ls -l `which gcloud`
lrwxr-xr-x  1 sat8bit  admin  71  5 18 23:35 /usr/local/bin/gcloud -> /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin/gcloud
$ find /usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/ -name kubectl
/usr/local/Caskroom/google-cloud-sdk/latest/google-cloud-sdk//bin/kubectl

予想通りSDKの配下にあるので、ここにPATHを通す。brew で install はやめたほうがよさそう?

$ export PATH=$PATH:`brew --prefix`/Caskroom/google-cloud-sdk/latest/google-cloud-sdk/bin

でもう一回Deploymentを作成する。

$ kubectl run hello-server --image gcr.io/google-samples/hello-app:1.0 --port 8080
deployment "hello-server" created

作れたっぽい。が、この時点で WebUI に hello-server の文字を見つけられず、、ローカルに作っただけかな。

Service の作成

$ kubectl expose deployment hello-server --type "LoadBalancer"
service "hello-server" exposed

なんかうまくいったのか・・・?getしてみる。

$ kubectl get service hello-server
NAME           TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
hello-server   LoadBalancer   10.55.243.206   <pending>     8080:31351/TCP   49s

$ kubectl get service hello-server
NAME           TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)          AGE
hello-server   LoadBalancer   10.55.243.206   ***********   8080:31351/TCP   52s

EXTERNAL-IP が pending だったので何回か実行したら払い出された。
叩いてみる。

$ curl http://***********:8080
Hello, world!
Version: 1.0.0
Hostname: hello-server-66cb56b679-fwnd5

動いたー!

以上です。
次回は Docker で動くアプリケーションを作って、デプロイする。

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