- How to package a web application in a Docker container image.
- Run that container image on a Google Kubernetes Engine (GKE) cluster.
- Deploy the web application as a load-balanced set of replicas that can scale to the needs of your users.
ketan_patel@cloudshell:~ (svo-mvp)$ export PROJECT_ID=svo-mvp
ketan_patel@cloudshell:~ (svo-mvp)$ echo $PROJECT_ID
svo-mvp
ketan_patel@cloudshell:~ (svo-mvp)$ gcloud config set project $PROJECT_ID
Updated property [core/project].
ketan_patel@cloudshell:~ (svo-mvp)$ gcloud artifacts repositories create hello-repo --repository-format=docker --location=us-west1 --description="Docker Repository"
Create request issued for: [hello-repo]
Waiting for operation [projects/svo-mvp/locations/us-west1/operations/1ce34909-c895-4698-9dcf-bc9080511533] to complete.
..done.
Created repository [hello-repo].
Waiting for operation [projects/svo-mvp/locations/us-west1/operations/1ce34909-c895-4698-9dcf-bc9080511533] to complete.
..done.
Created repository [hello-repo].
ketan_patel@cloudshell:~ (svo-mvp)$
ketan_patel@cloudshell:~ (svo-mvp)$ gcloud artifacts locations list | grep us-west
LOCATIONS: us-west1
LOCATIONS: us-west2
LOCATIONS: us-west3
LOCATIONS: us-west4
ketan_patel@cloudshell:~ (svo-mvp)$
Building the container image
Download the hello-app source code and Dockerfile by running the following commands:
ketan_patel@cloudshell:~ (svo-mvp)$ git clone https://github.com/GoogleCloudPlatform/kubernetes-engine-samples
Cloning into 'kubernetes-engine-samples'...
remote: Enumerating objects: 4472, done.
remote: Counting objects: 100% (1143/1143), done.
remote: Compressing objects: 100% (285/285), done.
remote: Total 4472 (delta 950), reused 994 (delta 844), pack-reused 3329
Receiving objects: 100% (4472/4472), 2.99 MiB | 13.56 MiB/s, done.
Resolving deltas: 100% (2620/2620), done.
ketan_patel@cloudshell:~ (svo-mvp)$ cd kubernetes-engine-samples/
ketan_patel@cloudshell:~/kubernetes-engine-samples (svo-mvp)$ ls -l
total 140
drwxr-xr-x 4 ketan_patel ketan_patel 4096 Jul 20 21:35 ai-ml
drwxr-xr-x 3 ketan_patel ketan_patel 4096 Jul 20 21:35 autopilot
drwxr-xr-x 5 ketan_patel ketan_patel 4096 Jul 20 21:35 batch
drwxr-xr-x 3 ketan_patel ketan_patel 4096 Jul 20 21:35 cloud-pubsub
drwxr-xr-x 2 ketan_patel ketan_patel 4096 Jul 20 21:35 cloudsql
drwxr-xr-x 3 ketan_patel ketan_patel 4096 Jul 20 21:35 cost-optimization
drwxr-xr-x 4 ketan_patel ketan_patel 4096 Jul 20 21:35 custom-metrics-autoscaling
drwxr-xr-x 3 ketan_patel ketan_patel 4096 Jul 20 21:35 databases
drwxr-xr-x 4 ketan_patel ketan_patel 4096 Jul 20 21:35 gke-scheduled-autoscaler
drwxr-xr-x 6 ketan_patel ketan_patel 4096 Jul 20 21:35 gke-stateful-kafka
drwxr-xr-x 4 ketan_patel ketan_patel 4096 Jul 20 21:35 gke-stateful-mysql
drwxr-xr-x 6 ketan_patel ketan_patel 4096 Jul 20 21:35 gke-stateful-postgres
drwxr-xr-x 5 ketan_patel ketan_patel 4096 Jul 20 21:35 gke-vpa-recommendations
drwxr-xr-x 5 ketan_patel ketan_patel 4096 Jul 20 21:35 guestbook
drwxr-xr-x 4 ketan_patel ketan_patel 4096 Jul 20 21:35 hello-app
drwxr-xr-x 2 ketan_patel ketan_patel 4096 Jul 20 21:35 hello-app-cdn
drwxr-xr-x 4 ketan_patel ketan_patel 4096 Jul 20 21:35 hello-app-redis
drwxr-xr-x 3 ketan_patel ketan_patel 4096 Jul 20 21:35 hello-app-tls
-rw-r--r-- 1 ketan_patel ketan_patel 11357 Jul 20 21:35 LICENSE
drwxr-xr-x 2 ketan_patel ketan_patel 4096 Jul 20 21:35 load-balancing
drwxr-xr-x 2 ketan_patel ketan_patel 4096 Jul 20 21:35 migrating-node-pool
drwxr-xr-x 3 ketan_patel ketan_patel 4096 Jul 20 21:35 monitoring
drwxr-xr-x 2 ketan_patel ketan_patel 4096 Jul 20 21:35 network-policies
drwxr-xr-x 9 ketan_patel ketan_patel 4096 Jul 20 21:35 quickstart
-rw-r--r-- 1 ketan_patel ketan_patel 828 Jul 20 21:35 README.md
drwxr-xr-x 3 ketan_patel ketan_patel 4096 Jul 20 21:35 security
drwxr-xr-x 2 ketan_patel ketan_patel 4096 Jul 20 21:35 stateful-workload-filestore
drwxr-xr-x 2 ketan_patel ketan_patel 4096 Jul 20 21:35 terraform
drwxr-xr-x 2 ketan_patel ketan_patel 4096 Jul 20 21:35 try-gke
drwxr-xr-x 12 ketan_patel ketan_patel 4096 Jul 20 21:35 whereami
drwxr-xr-x 2 ketan_patel ketan_patel 4096 Jul 20 21:35 windows-multi-arch
drwxr-xr-x 2 ketan_patel ketan_patel 4096 Jul 20 21:35 wordpress-persistent-disks
drwxr-xr-x 3 ketan_patel ketan_patel 4096 Jul 20 21:35 workload-metrics
ketan_patel@cloudshell:~/kubernetes-engine-samples (svo-mvp)$
ketan_patel@cloudshell:~/kubernetes-engine-samples (svo-mvp)$
ketan_patel@cloudshell:~/kubernetes-engine-samples (svo-mvp)$ echo $PROJECT_ID
svo-mvp
ketan_patel@cloudshell:~/kubernetes-engine-samples (svo-mvp)$ cd hello-app
ketan_patel@cloudshell:~/kubernetes-engine-samples/hello-app (svo-mvp)$ ls -l
total 28
-rw-r--r-- 1 ketan_patel ketan_patel 1778 Jul 20 21:35 cloudbuild.yaml
drwxr-xr-x 2 ketan_patel ketan_patel 4096 Jul 20 21:35 cloudshell
-rw-r--r-- 1 ketan_patel ketan_patel 852 Jul 20 21:35 Dockerfile
-rw-r--r-- 1 ketan_patel ketan_patel 1467 Jul 20 21:35 main.go
drwxr-xr-x 2 ketan_patel ketan_patel 4096 Jul 20 21:35 manifests
-rw-r--r-- 1 ketan_patel ketan_patel 1561 Jul 20 21:35 README.md
-rw-r--r-- 1 ketan_patel ketan_patel 1109 Jul 20 21:35 skaffold.yaml
ketan_patel@cloudshell:~/kubernetes-engine-samples/hello-app (svo-mvp)$ cat Dockerfile
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
FROM golang:1.19.2 as builder
WORKDIR /app
RUN go mod init hello-app
COPY *.go ./
RUN CGO_ENABLED=0 GOOS=linux go build -o /hello-app
FROM gcr.io/distroless/base-debian11
WORKDIR /
COPY --from=builder /hello-app /hello-app
ENV PORT 8080
USER nonroot:nonroot
Build and tag the Docker image for hello-app:
ketan_patel@cloudshell:~/kubernetes-engine-samples/hello-app (svo-mvp)$ docker build -t us-west1-docker.pkg.dev/${PROJECT_ID}/hello-repo/hello-app:v1 .
[+] Building 43.5s (13/13) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 891B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for gcr.io/distroless/base-debian11:latest 1.1s
=> [internal] load metadata for docker.io/library/golang:1.19.2 1.0s
=> [builder 1/5] FROM docker.io/library/golang:1.19.2@sha256:992d5fea982526ce265a0631a391e3c94694f4d15190fd170f35d91b2e6cb0 30.0s
=> => resolve docker.io/library/golang:1.19.2@sha256:992d5fea982526ce265a0631a391e3c94694f4d15190fd170f35d91b2e6cb0ba 0.0s
=> => sha256:17c9e6141fdb3387e5a1c07d4f9b6a05ac1498e96029fa3ea55470d4504f7770 55.05MB / 55.05MB 1.0s
=> => sha256:4edced8587e6c18412817019074f5e04a8ede4e2fc89d06af13df3f80d78a70d 10.88MB / 10.88MB 0.4s
=> => sha256:992d5fea982526ce265a0631a391e3c94694f4d15190fd170f35d91b2e6cb0ba 2.36kB / 2.36kB 0.0s
=> => sha256:dce494d5814b5644d6ab2321aa466ef7a3c54ba46141b1efce6184fb7c63d360 7.11kB / 7.11kB 0.0s
=> => sha256:de4a4c6caea8801bb0b7377e10220a914da403bc93fa79663cbf2dcf1800b6f1 5.16MB / 5.16MB 0.3s
=> => sha256:2fddf0539591f8e364c9adb3d495d1ba2ca8a8df420ad23b58e7bcee7986ea6c 1.80kB / 1.80kB 0.0s
=> => sha256:a7969cffbf46e6a91291fd76b19ecbe93c03ea4ded0d14042aecb4c0c4211a43 54.59MB / 54.59MB 1.3s
=> => sha256:efa7548a174d51c2cadd24f9e629f436fbf1f501daba3e757d78c32f31c7fe83 85.97MB / 85.97MB 2.1s
=> => extracting sha256:17c9e6141fdb3387e5a1c07d4f9b6a05ac1498e96029fa3ea55470d4504f7770 7.1s
=> => sha256:a86067690e454a7f294329b6cc83502da1e3748726c0f1819d37f6dd3a69884c 148.88MB / 148.88MB 4.1s
=> => sha256:d0a75b47d9366434d29afda0a459a52f8c199b1150e2343fcbe727d87648afb5 156B / 156B 1.5s
=> => extracting sha256:de4a4c6caea8801bb0b7377e10220a914da403bc93fa79663cbf2dcf1800b6f1 0.4s
=> => extracting sha256:4edced8587e6c18412817019074f5e04a8ede4e2fc89d06af13df3f80d78a70d 0.4s
=> => extracting sha256:a7969cffbf46e6a91291fd76b19ecbe93c03ea4ded0d14042aecb4c0c4211a43 4.2s
=> => extracting sha256:efa7548a174d51c2cadd24f9e629f436fbf1f501daba3e757d78c32f31c7fe83 4.4s
=> => extracting sha256:a86067690e454a7f294329b6cc83502da1e3748726c0f1819d37f6dd3a69884c 11.2s
=> => extracting sha256:d0a75b47d9366434d29afda0a459a52f8c199b1150e2343fcbe727d87648afb5 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 1.50kB 0.0s
=> [stage-1 1/3] FROM gcr.io/distroless/base-debian11@sha256:73deaaf6a207c1a33850257ba74e0f196bc418636cada9943a03d7abea980d6 5.6s
=> => resolve gcr.io/distroless/base-debian11@sha256:73deaaf6a207c1a33850257ba74e0f196bc418636cada9943a03d7abea980d6d 0.0s
=> => sha256:73deaaf6a207c1a33850257ba74e0f196bc418636cada9943a03d7abea980d6d 1.46kB / 1.46kB 0.0s
=> => sha256:e03afa0858f2679999f6f9403e47509b63c2905a42a638fb21089f639af4ab28 1.60kB / 1.60kB 0.0s
=> => sha256:559bc54043fc1429f1b9c4e16f52670c7861b7c7fd4125129c29c924b293c2b2 2.12kB / 2.12kB 0.0s
=> => sha256:a7ca0d9ba68fdce7e15bc0952d3e898e970548ca24d57698725836c039086639 103.73kB / 103.73kB 1.7s
=> => extracting sha256:a7ca0d9ba68fdce7e15bc0952d3e898e970548ca24d57698725836c039086639 0.0s
=> => sha256:fe5ca62666f04366c8e7f605aa82997d71320183e99962fa76b3209fdfbb8b58 21.20kB / 21.20kB 2.1s
=> => extracting sha256:fe5ca62666f04366c8e7f605aa82997d71320183e99962fa76b3209fdfbb8b58 0.0s
=> => sha256:b02a7525f878e61fc1ef8a7405a2cc17f866e8de222c1c98fd6681aff6e509db 716.49kB / 716.49kB 2.3s
=> => sha256:fcb6f6d2c9986d9cd6a2ea3cc2936e5fc613e09f1af9042329011e43057f3265 317B / 317B 2.3s
=> => extracting sha256:b02a7525f878e61fc1ef8a7405a2cc17f866e8de222c1c98fd6681aff6e509db 0.8s
=> => sha256:e8c73c638ae9ec5ad70c49df7e484040d889cca6b4a9af056579c3d058ea93f0 198B / 198B 2.5s
=> => sha256:1e3d9b7d145208fa8fa3ee1c9612d0adaac7255f1bbc9ddea7e461e0b317805c 113B / 113B 2.6s
=> => sha256:4aa0ea1413d37a58615488592a0b827ea4b2e48fa5a77cf707d0e35f025e613f 385B / 385B 2.7s
=> => sha256:7c881f9ab25e0d86562a123b5fb56aebf8aa0ddd7d48ef602faf8d1e7cf43d8c 355B / 355B 2.8s
=> => sha256:5627a970d25e752d971a501ec7e35d0d6fdcd4a3ce9e958715a686853024794a 130.56kB / 130.56kB 2.9s
=> => sha256:96266735468f361ae6828901a80fc15a7f75e26640351df9e0f0f9824f36cf92 5.85MB / 5.85MB 3.3s
=> => sha256:2758d0c31c8ca76c3379e7b1be20adc4144e9230873bb2c5bdb41f3691fa75bc 2.06MB / 2.06MB 3.3s
=> => extracting sha256:fcb6f6d2c9986d9cd6a2ea3cc2936e5fc613e09f1af9042329011e43057f3265 0.0s
=> => sha256:08553ba93cfea7ad45b59911d8ed0a025489e7c3623920dfda331b9a49f1e8aa 961.77kB / 961.77kB 3.5s
=> => extracting sha256:e8c73c638ae9ec5ad70c49df7e484040d889cca6b4a9af056579c3d058ea93f0 0.0s
=> => extracting sha256:1e3d9b7d145208fa8fa3ee1c9612d0adaac7255f1bbc9ddea7e461e0b317805c 0.0s
=> => extracting sha256:4aa0ea1413d37a58615488592a0b827ea4b2e48fa5a77cf707d0e35f025e613f 0.0s
=> => extracting sha256:7c881f9ab25e0d86562a123b5fb56aebf8aa0ddd7d48ef602faf8d1e7cf43d8c 0.0s
=> => extracting sha256:5627a970d25e752d971a501ec7e35d0d6fdcd4a3ce9e958715a686853024794a 0.0s
=> => extracting sha256:96266735468f361ae6828901a80fc15a7f75e26640351df9e0f0f9824f36cf92 0.5s
=> => extracting sha256:2758d0c31c8ca76c3379e7b1be20adc4144e9230873bb2c5bdb41f3691fa75bc 0.2s
=> => extracting sha256:08553ba93cfea7ad45b59911d8ed0a025489e7c3623920dfda331b9a49f1e8aa 0.1s
=> [builder 2/5] WORKDIR /app 1.0s
=> [builder 3/5] RUN go mod init hello-app 0.3s
=> [builder 4/5] COPY *.go ./ 0.1s
=> [builder 5/5] RUN CGO_ENABLED=0 GOOS=linux go build -o /hello-app 9.9s
=> [stage-1 2/3] COPY --from=builder /hello-app /hello-app 0.0s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:a0eb4924f93813db0ae4094a271b60f65cd25d69fddb81746eda98290eaf8f79 0.0s
=> => naming to us-west1-docker.pkg.dev/svo-mvp/hello-repo/hello-app:v1 0.0s
ketan_patel@cloudshell:~/kubernetes-engine-samples/hello-app (svo-mvp)$
ketan_patel@cloudshell:~/kubernetes-engine-samples/hello-app (svo-mvp)$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
us-west1-docker.pkg.dev/svo-mvp/hello-repo/hello-app v1 a0eb4924f938 6 minutes ago 26.9MB
ketan_patel@cloudshell:~/kubernetes-engine-samples/hello-app (svo-mvp)$
Add IAM policy bindings to your service account:
ketan_patel@cloudshell:~ (svo-mvp)$ gcloud artifacts repositories add-iam-policy-binding ketan-repo --location=us-west1 --member=serviceAccount:180636258465-compute@
developer.gserviceaccount.com --role="roles/artifactregistry.reader"
Updated IAM policy for repository [ketan-repo]. bindings:
- members:
- serviceAccount:180636258465-compute@developer.gserviceaccount.com
role: roles/artifactregistry.reader
etag: BwYA8vuDhvo=
version: 1
ketan_patel@cloudshell:~ (svo-mvp)$
Pushing the Docker image to Artifact Registry
ketan_patel@cloudshell:~/kubernetes-engine-samples/hello-app (svo-mvp)$ gcloud auth configure-docker us-west1-docker.pkg.dev
WARNING: Your config file at [/home/ketan_patel/.docker/config.json] contains these credential helper entries:
{
"credHelpers": {
"gcr.io": "gcloud",
"us.gcr.io": "gcloud",
"eu.gcr.io": "gcloud",
"asia.gcr.io": "gcloud",
"staging-k8s.gcr.io": "gcloud",
"marketplace.gcr.io": "gcloud",
"us-west1-docker.pkg.dev": "gcloud"
}
}
Adding credentials for: us-west1-docker.pkg.dev
gcloud credential helpers already registered correctly.
ketan_patel@cloudshell:~/kubernetes-engine-samples/hello-app (svo-mvp)$ docker push us-west1-docker.pkg.dev/svo-mvp/hello-repo/hello-app:v1
The push refers to repository [us-west1-docker.pkg.dev/svo-mvp/hello-repo/hello-app]
39aceb11be9f: Pushed
6a1069d9378c: Pushed
1c47a89b8f41: Pushed
c60b28d3f33c: Pushed
4cb10dd2545b: Pushed
d2d7ec0f6756: Pushed
1a73b54f556b: Pushed
e624a5370eca: Pushed
d52f02c6501c: Pushed
ff5700ec5418: Pushed
7bea6b893187: Pushed
6fbdf253bbc2: Pushed
e023e0e48e6e: Pushed
v1: digest: sha256:eb8fbab1fd1230fa57d10ea0a5ab4aaa0b66a4d3885d22f6bf5ac8c23f49a97d size: 3033
ketan_patel@cloudshell:~/kubernetes-engine-samples/hello-app (svo-mvp)$
ketan_patel@cloudshell:~/kubernetes-engine-samples/hello-app (svo-mvp)$ gcloud config set compute/region us-west1
Updated property [compute/region].
ketan_patel@cloudshell:~/kubernetes-engine-samples/hello-app (svo-mvp)$ gcloud container clusters create-auto hello-cluster
Note: The Pod address range limits the maximum size of the cluster. Please refer to https://cloud.google.com/kubernetes-engine/docs/how-to/flexible-pod-cidr to learn how to optimize IP address allocation.
Creating cluster hello-cluster in us-west1... Cluster is being health-checked (master is healthy)...done.
Created [https://container.googleapis.com/v1/projects/svo-mvp/zones/us-west1/clusters/hello-cluster].
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-west1/hello-cluster?project=svo-mvp
kubeconfig entry generated for hello-cluster.
NAME: hello-cluster
LOCATION: us-west1
MASTER_VERSION: 1.26.5-gke.1200
MASTER_IP: 35.227.144.240
MACHINE_TYPE: e2-medium
NODE_VERSION: 1.26.5-gke.1200
NUM_NODES: 3
STATUS: RUNNING
ketan_patel@cloudshell:~/kubernetes-engine-samples/hello-app (svo-mvp)$
No comments:
Post a Comment