チュートリアル | Kubernetes Engine | Google Cloud
コンテナ化されたウェブ アプリケーションのデプロイを実行した際の作業ログ
gcloud コマンドライン ツールのデフォルトを設定する
$ gcloud config set project sunny-studio-241400
Updated property [core/project].
To take a quick anonymous survey, run:
$ gcloud alpha survey
$ gcloud config set compute/zone us-central1-b
Updated property [compute/zone].
ステップ 1: コンテナ イメージを作成する
hello-app ソースコードをダウンロード
$ git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
Cloning into 'kubernetes-engine-samples'...
remote: Enumerating objects: 510, done.
remote: Total 510 (delta 0), reused 0 (delta 0), pack-reused 510
Receiving objects: 100% (510/510), 396.14 KiB | 328.00 KiB/s, done.
Resolving deltas: 100% (231/231), done.
gcloud 上の事前に設定されたプロジェクト ID を取得し、シェルの PROJECT_ID 環境変数を設定
$ export PROJECT_ID="$(gcloud con
fig get-value project -q)"
Your active configuration is: [cloudshell-31490]
アプリケーションのコンテナ イメージを作成
$ docker build -t gcr.io/${PROJECT_ID}/hello-app:v1 .
Sending build context to Docker daemon 9.728kB
Step 1/7 : FROM golang:1.8-alpine
1.8-alpine: Pulling from library/golang
550fe1bea624: Pull complete
cbc8da23026a: Pull complete
9b35aaa06d7a: Pull complete
46ca6ce0ffd1: Pull complete
7a270aebe80a: Pull complete
8695117c367e: Pull complete
Digest: sha256:693568f2ab0dae1e19f44b41628d2aea148fac65974cfd18f83cb9863ab1a177
Status: Downloaded newer image for golang:1.8-alpine
---> 4cb86d3661bf
Step 2/7 : ADD . /go/src/hello-app
---> f25fb1b86836
Step 3/7 : RUN go install hello-app
---> Running in 539da6e6d4e9
Removing intermediate container 539da6e6d4e9
---> 8d11a4e9e759
Step 4/7 : FROM alpine:latest
latest: Pulling from library/alpine
e7c96db7181b: Pull complete
Digest: sha256:769fddc7cc2f0a1c35abb2f91432e8beecf83916c421420e6a6da9f8975464b6
Status: Downloaded newer image for alpine:latest
---> 055936d39205
Step 5/7 : COPY --from=0 /go/bin/hello-app .
---> 36058ccd665b
Step 6/7 : ENV PORT 8080
---> Running in 060bdc2c7fb6
Removing intermediate container 060bdc2c7fb6
---> edb140b715f2
Step 7/7 : CMD ["./hello-app"]
---> Running in 112d03ed9f94
Removing intermediate container 112d03ed9f94
---> ffc560d5fbad
Successfully built ffc560d5fbad
Successfully tagged gcr.io/sunny-studio-241400/hello-app:v1
ビルドが成功したことを確認
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
gcr.io/sunny-studio-241400/hello-app v1 ffc560d5fbad 3 minutes ago 11.4MB
<none> <none> 8d11a4e9e759 3 minutes ago 263MB
alpine latest 055936d39205 11 days ago 5.53MB
golang 1.8-alpine 4cb86d3661bf 15 months ago 257MB
ステップ 2: コンテナ イメージをアップロードする
コンテナ イメージを Container Registry にアップロード
$ gcloud docker -- push gcr.io/${PROJECT_ID}/hello-app:v1
WARNING: `gcloud docker` will not be supported for Docker client versions above 18.03.
As an alternative, use `gcloud auth configure-docker` to configure `docker` to
use `gcloud` as a credential helper, then use `docker` as you would for non-GCR
registries, e.g. `docker pull gcr.io/project-id/my-image`. Add
`--verbosity=error` to silence this warning: `gcloud docker
--verbosity=error -- pull gcr.io/project-id/my-image`.
See: https://cloud.google.com/container-registry/docs/support/deprecation-notices#gcloud-docker
The push refers to repository [gcr.io/sunny-studio-241400/hello-app]
3870cf33db85: Preparing
f1b5933fe4b5: Preparing
denied: Token exchange failed for project 'sunny-studio-241400'. Please enable Google Container Registry API in Cloud C
onsole at https://console.cloud.google.com/apis/api/containerregistry.googleapis.com/overview?project=sunny-studio-2414
00 before performing this operation.
ステップ 3: コンテナをローカルに実行する(オプション)
$ docker run --rm -p 8080:8080 gcr.io/${PROJECT_ID}/hello-app:v1
2019/05/22 00:20:10 Server listening on port 8080
2019/05/22 00:21:09 Serving request: /
2019/05/22 00:21:10 Serving request: /favicon.ico
ステップ 4: コンテナ クラスタを作成する
hello-cluster という名前の 3 ノードクラスタを作成
$ gcloud container clusters create hello-cluster --num-nodes=3
WARNING: In June 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=Kubernetes Engine API is not enabled for this project. Please ensure it is enabled in Google Cloud Console and try again: visit https://console.cloud.google.com/apis/api/container.googleapis.com/overview?project=sunny-studio-241400 to do so.
APIを有効化する
https://console.cloud.google.com/apis/api/container.googleapis.com/overview?project=sunny-studio-241400
にアクセスし有効化
再度実施して成功
$ gcloud container clusters create hello-cluster --num-nodes=3
WARNING: In June 2019, node auto-upgrade will be enabled by default for newly created clusters and node pools. To disab
le 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 defa
ult mode and can be disabled using `--no-enable-ip-alias` flag. Use `--[no-]enable-ip-alias` flag to suppress this warn
ing.
WARNING: Starting in 1.12, default node pools in new clusters will have their legacy Compute Engine instance metadata e
ndpoints 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 hello-cluster in us-central1-b... Cluster is being health-checked (master is healthy)...done.
Created [https://container.googleapis.com/v1/projects/sunny-studio-241400/zones/us-central1-b/clusters/hello-cluster].
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central
1-b/hello-cluster?project=sunny-studio-241400
kubeconfig entry generated for hello-cluster.
NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS
hello-cluster us-central1-b 1.12.7-gke.10 34.66.115.238 n1-standard-1 1.12.7-gke.10 3 RUNNING
クラスタの 3 つのワーカーVM インスタンスを確認
$ gcloud compute instances list
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
gke-hello-cluster-default-pool-be318044-4770 us-central1-b n1-standard-1 10.128.0.2 35.188.191.212 RUNNING
gke-hello-cluster-default-pool-be318044-f4ds us-central1-b n1-standard-1 10.128.0.3 35.184.203.168 RUNNING
gke-hello-cluster-default-pool-be318044-swlx us-central1-b n1-standard-1 10.128.0.4 35.188.154.208 RUNNING
クラスタ認証情報を取得
$ gcloud container clusters get-credentials hello-cluster
Fetching cluster endpoint and auth data.
kubeconfig entry generated for hello-cluster.
ステップ 5: アプリケーションをデプロイする
コマンドを実行して、アプリケーションをデプロイし、ポート 8080 でリッスン
$ kubectl run hello-web --image=gcr.io/${PROJECT_ID}/hello-app:v1 --port 8080
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl crea
te instead.
deployment.apps/hello-web created
Deployment によって作成されたポッドを表示するには、次のコマンドを実行
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-web-758bd5b7c4-dtzl4 0/1 ImagePullBackOff 0 80s
ステップ 6: アプリケーションをインターネットに公開する
$ kubectl expose deployment hello-web --type=LoadBalancer --port 80 --target-port 8080
service/hello-web exposed
IP確認
$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-web LoadBalancer 10.11.248.250 35.224.113.83 80:30871/TCP 98s
kubernetes ClusterIP 10.11.240.1 <none> 443/TCP 13m
ステップ 7: アプリケーションをスケールアップする
デプロイ リソースにレプリカを追加
デプロイに 2 つのレプリカを追加するには(合計 3 つ)、次のコマンドを実行します。
$ kubectl scale deployment hello-web --replicas=3
deployment.extensions/hello-web scaled
次のコマンドを実行して、クラスタで実行されている新しいレプリカを確認できます。
$ kubectl get deployment hello-web
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-web 3 3 3 0 27h
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-web-758bd5b7c4-5mg55 0/1 ErrImagePull 0 27h
hello-web-758bd5b7c4-dtzl4 0/1 ImagePullBackOff 0 27h
hello-web-758bd5b7c4-f7w9q 0/1 ImagePullBackOff 0 27h
ステップ 8: アプリの新しいバージョンをデプロイする
同じソースコードをビルドして、v2 としてタグ付けすることで、アプリケーションの v2 バージョン用のイメージを作成できます
$ docker build -t gcr.io/${PROJECT_ID}/hello-app:v2 .
Sending build context to Docker daemon 13.82kB
Step 1/7 : FROM golang:1.8-alpine
1.8-alpine: Pulling from library/golang
550fe1bea624: Pull complete
cbc8da23026a: Pull complete
9b35aaa06d7a: Pull complete
46ca6ce0ffd1: Pull complete
7a270aebe80a: Pull complete
8695117c367e: Pull complete
Digest: sha256:693568f2ab0dae1e19f44b41628d2aea148fac65974cfd18f83cb9863ab1a177
Status: Downloaded newer image for golang:1.8-alpine
---> 4cb86d3661bf
Step 2/7 : ADD . /go/src/hello-app
---> 453a92e02431
Step 3/7 : RUN go install hello-app
---> Running in 52531a87e4db
Removing intermediate container 52531a87e4db
---> 8b60d7afac56
Step 4/7 : FROM alpine:latest
latest: Pulling from library/alpine
e7c96db7181b: Pull complete
Digest: sha256:bf1684a6e3676389ec861c602e97f27b03f14178e5bc3f70dce198f9f160cce9
Status: Downloaded newer image for alpine:latest
---> 055936d39205
Step 5/7 : COPY --from=0 /go/bin/hello-app .
---> d67f2047a04d
Step 6/7 : ENV PORT 8080
---> Running in 78bccff4e1e1
Removing intermediate container 78bccff4e1e1
---> 799e66c45073
Step 7/7 : CMD ["./hello-app"]
---> Running in c63fbb095b86
Removing intermediate container c63fbb095b86
---> b0620237036e
Successfully built b0620237036e
Successfully tagged gcr.io/sunny-studio-241400/hello-app:v2
イメージを Google Container Registry に push します。
$ gcloud docker -- push
gcr.io/${PROJECT_ID}/hello-app:v2
WARNING: `gcloud docker` will not be supported for Docker client versions above 18.03.
As an alternative, use `gcloud auth configure-docker` to configure `docker` to
use `gcloud` as a credential helper, then use `docker` as you would for non-GCR
registries, e.g. `docker pull gcr.io/project-id/my-image`. Add
`--verbosity=error` to silence this warning: `gcloud docker
--verbosity=error -- pull gcr.io/project-id/my-image`.
See: https://cloud.google.com/container-registry/docs/support/deprecation-notices#gcloud-docker
The push refers to repository [gcr.io/sunny-studio-241400/hello-app]
657257387357: Pushed
f1b5933fe4b5: Layer already exists
v2: digest: sha256:978d664865501f17b19aece05c21e91a41919dc80d7f58f2465e2b9e6f4ae487 size: 739
To take a quick anonymous survey, run:
$ gcloud alpha survey
次に、イメージ更新で既存のデプロイにローリング更新を適用します。
$ kubectl set image dep
loyment/hello-web hello-web=gcr.io/${PROJECT_ID}/hello-app:v2
deployment.extensions/hello-web image updated
http://[EXTERNAL_IP] でアプリケーションに再度アクセスし、変更が有効になっていることを確認
クリーンアップ
サービスを削除する
$ kubectl delete servic
e hello-web
service "hello-web" deleted
hello-web サービス用にプロビジョニングされたロードバランサが削除されるのを待つ
$ gcloud compute forwar
ding-rules list
Listed 0 items.
コンテナ クラスタを削除する
$ gcloud container clus
ters delete hello-cluster
The following clusters will be deleted.
- [hello-cluster] in [us-central1-b]
Do you want to continue (Y/n)? y
Deleting cluster hello-cluster...done.
Deleted [https://container.googleapis.com/v1/projects/sunny-studio-241400/zones/us-central1-b/clusters/hello-cluster].