Spring Bootで作ったWebアプリをGoogle CloudのGKEを用いてKubernetesにデプロイする方法をご紹介します。Google Cloudを使ったことがない方でも順を追ってできるように記載していますので、ぜひ参考にしてみて下さい。まずはStep1としてサンプルアプリをデプロイする方法を通じてGKEの使い方を理解してもらえればと思います。すでにGKEを触ったことがある方はご存じの内容かと思いますので、Step2から始めてもらえればと思います。
username@cloudshell:~/kubernetes-engine-samples/quickstarts/hello-app (quizappproject-xxxxxx)$ docker build -t us-central1-docker.pkg.dev/${PROJECT_ID}/hello-repo/hello-app:v1 .
[+] Building 48.2s (13/13) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 983B 0.0s
=> WARN: FromAsCasing: 'as' and 'FROM' keywords' casing do not match (line 16) 0.0s
=> [internal] load metadata for gcr.io/distroless/base-debian11:latest 1.5s
=> [internal] load metadata for docker.io/library/golang:1.23.2 2.7s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [builder 1/5] FROM docker.io/library/golang:1.23.2@sha256:ad5c126b5cf501a8caef751a243bb717ec204ab1aa56dc41dc11be089fafcb4f 22.4s
=> => resolve docker.io/library/golang:1.23.2@sha256:ad5c126b5cf501a8caef751a243bb717ec204ab1aa56dc41dc11be089fafcb4f 0.0s
=> => sha256:ad5c126b5cf501a8caef751a243bb717ec204ab1aa56dc41dc11be089fafcb4f 9.74kB / 9.74kB 0.0s
=> => sha256:db933bde327ab6b27f54c29b092ab3bf9276738432ed1bc730b5c9bd98ff33ef 2.32kB / 2.32kB 0.0s
~~中略~~
=> => writing image sha256:5d2f9fac3e81795c41f3cd6dd9b82bb2d764512a7092a8945f046521e4bd08a0 0.0s
=> => naming to us-central1-docker.pkg.dev/quizappproject-xxxxxx/hello-repo/hello-app:v1 0.0s
2 warnings found (use docker --debug to expand):
- FromAsCasing: 'as' and 'FROM' keywords' casing do not match (line 16)
- LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "ENV key value" format (line 25)
下記コマンドを実行して、ビルドがうまくいっていることを確認します。
username@cloudshell:~/kubernetes-engine-samples/quickstarts/hello-app (quizappproject-xxxxxx)$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
us-central1-docker.pkg.dev/quizappproject-xxxxxx/hello-repo/hello-app v1 5d2f9fac3e81 32 seconds ago 28MB
続いてコンテナイメージの動作確認をしてみましょう。下記コマンドを実行してください。
username@cloudshell:~/kubernetes-engine-samples/quickstarts/hello-app (quizappproject-xxxxxx)$ docker run --rm -p 8080:8080 us-central1-docker.pkg.dev/${PROJECT_ID}/hello-repo/hello-app:v1
2024/12/15 12:14:35 Server listening on port 8080
username@cloudshell:~/kubernetes-engine-samples/quickstarts/hello-app (quizappproject-xxxxxx)$ gcloud config set compute/region us-central1
WARNING: Property validation for compute/region was skipped.
Updated property [compute/region].
続いて下記URLにアクセスしてKubernetes Engin APIを有効にします。{プロジェクトID}をご自身の値に変換してアクセスしてください。 https://console.developers.google.com/apis/api/container.googleapis.com/overview?project={プロジェクトID}
username@cloudshell:~/kubernetes-engine-samples/quickstarts/hello-app (quizappproject-xxxxxx)$ gcloud container clusters create-auto hello-cluster
Note: The Kubelet readonly port (10255) is now deprecated. Please update your workloads to use the recommended alternatives. See https://cloud.google.com/kubernetes-engine/docs/how-to/disable-kubelet-readonly-port for ways to check usage and for migration instructions.
Creating cluster hello-cluster in us-central1... Cluster is being health-checked (Kubernetes Control Plane is healthy)...done.
Created
.
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central1/hello-cluster?project=quizappproject-xxxxxx
kubeconfig entry generated for hello-cluster.
NAME: hello-cluster
LOCATION: us-central1
MASTER_VERSION: 1.30.6-gke.1125000
MASTER_IP: 35.222.110.93
MACHINE_TYPE: e2-small
NODE_VERSION: 1.30.6-gke.1125000
NUM_NODES: 3
STATUS: RUNNING
username@cloudshell:~/kubernetes-engine-samples/quickstarts/hello-app (quizappproject-xxxxxx)$
username@cloudshell:~/kubernetes-engine-samples/quickstarts/hello-app (quizappproject-xxxxxx)$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-app-676bf8b664-46llx 1/1 Running 0 2m2s
hello-app-676bf8b664-nmvqx 1/1 Running 0 2m14s
username@cloudshell:~/kubernetes-engine-samples/quickstarts/hello-app (quizappproject-xxxxxx)$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-app-service LoadBalancer 34.118.233.194 <pending> 80:31114/TCP 10s
kubernetes ClusterIP 34.118.224.1 <none> 443/TCP 11m
下記の通りEXTERNAL-IPが割り当てられたら完了です。
username@cloudshell:~/kubernetes-engine-samples/quickstarts/hello-app (quizappproject-xxxxxx)$ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-app-service LoadBalancer 34.118.233.194 34.59.239.14 80:31114/TCP 2m29s
kubernetes ClusterIP 34.118.224.1 <none> 443/TCP 13m
username@cloudshell:~/kubernetes-engine-samples/quickstarts/hello-app (quizappproject-xxxxxx)$ kubectl delete service hello-app-service
service "hello-app-service" deleted
続いてGKEクラスタを削除します。
username@cloudshell:~/kubernetes-engine-samples/quickstarts/hello-app (quizappproject-xxxxxx)$ gcloud container clusters delete hello-cluster --region us-central1
The following clusters will be deleted.
- [hello-cluster] in [us-central1]
Do you want to continue (Y/n)? y
Deleting cluster hello-cluster...done.
Deleted
コメント