GCP DevOps Project

Sprint 06

Sprint 06

In this lesson, you’ll learn how to expose your containerized application on Google Kubernetes Engine (GKE) by extending your existing Deployment manifest with a Service resource. By the end of this sprint, you’ll be able to access your app through a stable, load-balanced endpoint.

Prerequisites

  • A running GKE cluster
  • kubectl configured to point at your GKE cluster
  • An existing gke-deployment.yaml that successfully deploys your container

Previously, we deployed our container to GKE, but the application wasn’t reachable from outside the cluster.

The image shows a YAML file icon with an arrow pointing to a diagram of a person, symbolizing the extension of a gke.yaml file to include code for exposing an application via an endpoint.

Why You Need a Service

A Kubernetes Service

  • Provides a stable IP or DNS name for your Pods
  • Handles load balancing across Pod replicas
  • Supports multiple types of exposure (ClusterIP, NodePort, LoadBalancer)

Note

A Service decouples the network policy from your Pods. Even if your Pods get rescheduled or replaced, the Service IP/DNS remains constant.

Comparing Service Types

Service TypeDescriptionUse Case
ClusterIPInternal access within the clusterMicroservices communication
NodePortOpens a port on each node’s IPQuick testing, on-prem clusters
LoadBalancerProvisions a cloud load balancer (GCP, AWS, Azure)Exposing applications to the public internet automatically

For GKE, LoadBalancer is the most straightforward way to get an external IP.

Updating gke-deployment.yaml

Add the following Service manifest below your existing Deployment spec in gke-deployment.yaml:

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
  labels:
    app: my-app
spec:
  type: LoadBalancer             # Exposes the Service via a cloud load balancer
  ports:
    - port: 80                   # External port
      targetPort: 8080           # Container port
      protocol: TCP
  selector:
    app: my-app                  # Matches Deployment pods

Then apply the changes:

kubectl apply -f gke-deployment.yaml

Run kubectl get svc my-app-service to retrieve the external IP:

NAME             TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)        AGE
my-app-service   LoadBalancer   10.96.5.123    34.68.12.34     80:31234/TCP   2m

Your application is now accessible at http://34.68.12.34.

Warning

Provisioning a LoadBalancer can incur additional GCP costs. Be sure to delete unused Services when you’re done.

Watch Video

Watch video content

Previous
Sprint 05 review