Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 45 ยท Compute

Containers, Artifact Registry and GKE

Build container images, store them in Artifact Registry and run them on GKE Autopilot or Standard with deployments and services, plus a reconciler demo.

A container packs your program with everything it needs to run, so it behaves the same on a laptop and in the cloud. Google Cloud gives you a private place to keep images (Artifact Registry) and a managed Kubernetes service to run many of them (Google Kubernetes Engine, or GKE).

If you are new to containers, the Docker tutorial explains images and Dockerfiles in depth. Here we focus on the Google Cloud parts: pushing an image, choosing between Autopilot and Standard clusters, and the idea at the heart of Kubernetes, which is the reconciliation loop.

From code to image

A Dockerfile describes how to build an image. A small Node example:

DOCKERFILE
FROM node:22-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
ENV PORT=8080
CMD ["node", "server.js"]

You can build it on your machine with Docker or in the cloud with Cloud Build (see Cloud Build and CI/CD). Either way the result is an image with a name and a tag.

Artifact Registry

Artifact Registry stores container images and also language packages (npm, Python, Maven and others). Images live in repositories, and the full name encodes where:

TEXT
REGION-docker.pkg.dev / PROJECT_ID / REPOSITORY / IMAGE : TAG
us-central1-docker.pkg.dev/my-project-id/apps/orders:1

Create a repository, let Docker authenticate with your gcloud login, then tag and push (read-only; needs a project and Docker):

Terminal
gcloud artifacts repositories create apps \
  --repository-format=docker --location=us-central1

gcloud auth configure-docker us-central1-docker.pkg.dev

docker build -t us-central1-docker.pkg.dev/my-project-id/apps/orders:1 .
docker push us-central1-docker.pkg.dev/my-project-id/apps/orders:1

Access is controlled by IAM: readers need roles/artifactregistry.reader, writers need roles/artifactregistry.writer. Keep the repository in the same region as the services that pull from it, to keep pulls fast and avoid transfer charges. Prefer immutable digests (orders@sha256:...) in production over tags such as latest, since a tag can move.

Kubernetes in one slide

The Kubernetes vocabulary

Pod One or more containers that run together.
Deployment Declares how many pods you want and which image.
Service A stable address in front of changing pods.
Node A machine that runs pods.

You declare the goal; Kubernetes works toward it.

1 / 2

You describe the desired state in YAML and Kubernetes keeps making reality match it. A Deployment and a Service for our image:

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: orders
spec:
  replicas: 3
  selector:
    matchLabels: { app: orders }
  template:
    metadata:
      labels: { app: orders }
    spec:
      containers:
        - name: orders
          image: us-central1-docker.pkg.dev/my-project-id/apps/orders:1
          ports:
            - containerPort: 8080
          resources:
            requests: { cpu: 250m, memory: 256Mi }
---
apiVersion: v1
kind: Service
metadata:
  name: orders
spec:
  type: LoadBalancer
  selector: { app: orders }
  ports:
    - port: 80
      targetPort: 8080

Resource requests matter: the scheduler uses them to place pods, and on Autopilot they are also what you pay for.

Autopilot or Standard

GKE has two modes of operation:

  • Autopilot: Google manages the nodes. You deploy pods, and the platform provisions capacity, applies security defaults and bills mostly per pod resource request. Less to operate, with some restrictions on what pods may do.
  • Standard: you manage node pools (machine types, sizes, autoscaling, upgrade timing). It gives the most control and is billed per node.

For most teams starting out, Autopilot is the recommended choice. Standard fits workloads that need special hardware or node-level settings.

Terminal
gcloud container clusters create-auto demo-cluster --region=us-central1
gcloud container clusters get-credentials demo-cluster --region=us-central1

kubectl apply -f orders.yaml
kubectl get pods
kubectl get service orders

Pods should run as a dedicated service account too. Workload Identity Federation for GKE maps a Kubernetes service account to a Google one, so pods get tokens without key files (see Service Accounts and Credentials).

A cluster is a larger commitment than a Cloud Run service. If one container behind HTTP is all you need, start with Cloud Run, and choose GKE when you need Kubernetes features such as custom controllers, complex networking or many cooperating services.

Advantages, drawbacks and where GKE fits

Advantages. Google created Kubernetes, so GKE gets new versions early and has the most mature integration: automatic node repair and upgrades, release channels, built-in logging and monitoring, node autoscaling, and Google's networking and load balancers. Add-ons such as Config Sync, Cloud Service Mesh and Backup for GKE are one setting away. It suits microservices, cloud-native applications and high-traffic applications that need rolling updates and automatic scaling.

Drawbacks. There is still a lot to learn, and a cluster has a baseline cost even when nearly idle. Autopilot restricts some node-level settings, and the more you use GKE-specific add-ons, the more lock-in you accept (though your manifests stay portable Kubernetes). Customization is more limited than on a cluster you build yourself.

Moving VMs into containers: Migrate to Containers

Migrate to Containers (formerly Migrate for Anthos) converts workloads that run on VMs, on-premises or in other clouds, into container images and Kubernetes manifests for GKE. The steps: assess the VM for suitability, migrate by generating a Dockerfile, a filesystem and deployment YAML from it, then deploy and test the result, keeping the original running until you are happy. Advantages: no application rewrite, faster path to Kubernetes, and consistent operations. The result is "containerized VM", not a redesigned microservice system, so treat it as a first step.

Config Sync: GitOps for clusters

Config Sync keeps cluster configuration in a Git repository and makes every cluster match it. Instead of running kubectl apply by hand, you commit YAML (namespaces, policies, deployments) to the repo, and an agent in each cluster pulls it, applies it and reverts manual drift. With many clusters it gives one source of truth and a Git history of who changed what. You enable it per cluster (as part of the fleet's configuration management) and point it at a repo, branch and directory with a RootSync object (or the older config-management.yaml).

YAML
# read-only sketch of a RootSync: sync the "clusters/prod" directory of a repo
apiVersion: configsync.gke.io/v1beta1
kind: RootSync
metadata:
  name: root-sync
  namespace: config-management-system
spec:
  sourceFormat: unstructured
  git:
    repo: https://github.com/example/platform-config
    branch: main
    dir: clusters/prod
    auth: none

The reconciliation loop

The magic of Kubernetes is a loop that repeats forever: observe the actual state, compare it with the desired state, and take a small step to close the gap. If a pod crashes, the actual count drops below desired and a new pod starts. If you lower replicas, extra pods are removed.

The stand-in below is not Kubernetes code. It is a small, deterministic model of the loop, including a "crash" at one step.

JS Stand-in: reconciling desired and actual replicas

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The loop never needs to be told "restart that pod". It only knows the goal, which makes the system self-healing. The same pattern powers managed instance groups, where the goal is a VM count.

A rolling update, in numbers

When you change the image, a Deployment replaces pods gradually, keeping some old ones until new ones are ready. Two settings control the pace: how many extra pods may exist (maxSurge) and how many may be missing (maxUnavailable).

JS Stand-in: rolling update steps

Press Run (or Ctrl + Enter) to see the console output here.

No output.

With maxUnavailable at 0 the capacity never drops below four pods while the new version rolls out. Real Kubernetes also waits for readiness probes, which this simplified model skips.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where do you store private container images on Google Cloud?

    Artifact Registry hosts container images and language packages in regional or multi-regional repositories.
  2. 2. In GKE Autopilot, who manages the nodes?

    Autopilot provisions and manages nodes for you. In Standard mode you manage node pools yourself.
  3. 3. What does a Kubernetes Deployment declare?

    A Deployment states the desired replicas and pod template. Controllers then keep reality in line with it.
  4. 4. Why prefer an image digest to the latest tag in production?

    A digest identifies exact image content, so deployments are repeatable.

Summary

  • Build an image, tag it with a full Artifact Registry name and push it after gcloud auth configure-docker.
  • Kubernetes keeps actual state equal to desired state with a reconciliation loop.
  • Pods run containers, Deployments keep their count and version, Services give them a stable address.
  • Autopilot manages nodes for you; Standard gives node control.
  • Choose Cloud Run for simple HTTP containers and GKE when you need Kubernetes.

Next: Cloud Storage Buckets and Objects