Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 35 ยท Kubernetes

Deployments and Services on DigitalOcean Kubernetes

Deploy an app to DigitalOcean Kubernetes with Deployments, Services and a DigitalOcean Load Balancer, from the manifest to the public IP, with labels and probes.

A cluster with no workloads is just empty machines. In Kubernetes you describe what you want in YAML manifests, and the cluster works to make reality match. On DOKS two objects do most of the work: a Deployment, which keeps N identical pods running, and a Service, which gives those pods one stable address. When the Service has type LoadBalancer, DOKS creates a real DigitalOcean Load Balancer for you.

This lesson shows the manifests and the kubectl flow. As in the other lessons of this chapter, cloud commands are read-only text, while the Python blocks are labelled stand-ins that model the matching logic.

A Deployment

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: shop-web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: shop-web
  template:
    metadata:
      labels:
        app: shop-web
    spec:
      containers:
        - name: web
          image: registry.digitalocean.com/my-registry/shop-web:1.4.0
          ports:
            - containerPort: 8080
          resources:
            requests: { cpu: 250m, memory: 256Mi }
            limits: { memory: 512Mi }
          readinessProbe:
            httpGet: { path: /healthz, port: 8080 }
            periodSeconds: 5

Points that matter in practice:

  • selector.matchLabels must match the pod template labels, or the Deployment is rejected.
  • The image comes from the DigitalOcean Container Registry (registry.digitalocean.com/...). Connect the registry to the cluster in the control panel, or with doctl kubernetes cluster registry add <cluster>, so nodes can pull private images.
  • Requests feed the scheduler (see clusters and node pools). A memory limit protects neighbours, but a CPU limit can throttle you, so many teams omit it.
  • A readiness probe keeps a pod out of the Service until it answers, which makes rollouts safe.

A Service of type LoadBalancer

YAML
apiVersion: v1
kind: Service
metadata:
  name: shop-web
  annotations:
    service.beta.kubernetes.io/do-loadbalancer-name: "shop-web-lb"
    service.beta.kubernetes.io/do-loadbalancer-protocol: "http"
    service.beta.kubernetes.io/do-loadbalancer-healthcheck-path: "/healthz"
spec:
  type: LoadBalancer
  selector:
    app: shop-web
  ports:
    - name: http
      port: 80
      targetPort: 8080

Apply both files and watch the Service until an external IP appears:

Terminal
kubectl apply -f deployment.yaml -f service.yaml
kubectl get deployment shop-web
kubectl get service shop-web --watch
kubectl rollout status deployment/shop-web

The EXTERNAL-IP column shows <pending> for a minute or two while DigitalOcean provisions the Load Balancer, then a real address. Annotations starting with service.beta.kubernetes.io/do-loadbalancer- configure it (name, protocol, health check, TLS, sticky sessions and more); check the current annotation list in the DigitalOcean documentation before relying on one. Note that the Load Balancer is a separate, billed resource. Delete the Service through kubectl, not from the Load Balancer page, so Kubernetes and DigitalOcean stay in sync.

flowchart LR
  U["Visitor"] --> LB["DigitalOcean Load Balancer"]
  LB --> N1["Node 1"]
  LB --> N2["Node 2"]
  N1 --> P1["Pod shop-web-1"]
  N2 --> P2["Pod shop-web-2"]
  N2 --> P3["Pod shop-web-3"]

The Service selects pods by label; the Load Balancer sends traffic to the nodes, which forward it to ready pods.

How a Service finds its pods

A Service has no list of pods. It holds a selector, and any ready pod whose labels contain all those key/value pairs receives traffic. This stand-in reproduces the matching rule.

Python Stand-in: label selector matching

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the not-ready pod is skipped, and that a typo in a selector yields an empty list: the classic "my Service has no endpoints" bug. In a real cluster, kubectl get endpoints shop-web shows the answer.

Rolling updates

Change the image tag and apply the file again. A Deployment replaces pods gradually: it starts new pods, waits for readiness, then stops old ones. The maxSurge and maxUnavailable settings control the pace. This stand-in counts the steps for a rollout with maxSurge=1, maxUnavailable=0.

Python Stand-in: rolling update steps

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With maxUnavailable=0 the total never drops below the desired count, so users do not see downtime. If the new pods never become ready (a bad image or failing probe), the rollout stalls and the old pods keep serving. Undo with kubectl rollout undo deployment/shop-web.

Everyday kubectl commands

Terminal
kubectl get pods -o wide
kubectl describe pod shop-web-7d9c-abcde
kubectl logs deployment/shop-web --tail=50
kubectl scale deployment/shop-web --replicas=5
kubectl port-forward service/shop-web 8080:80
kubectl delete -f service.yaml

Use describe first when a pod is stuck: its Events section explains image pull errors, failed probes and scheduling problems.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does a Service decide which pods receive traffic?

    A Service holds a selector; ready pods whose labels match it become its endpoints.
  2. 2. What happens when you create a Service of type LoadBalancer on DOKS?

    The cloud controller creates a real, billed DigitalOcean Load Balancer and fills in the external IP.
  3. 3. Why add a readiness probe?

    Pods that fail readiness are removed from Service endpoints, so rollouts do not send traffic to unready pods.
  4. 4. How should you remove a LoadBalancer Service?

    Kubernetes owns the Load Balancer's lifecycle, so deleting the Service removes it cleanly.

Summary

  • A Deployment keeps N pods running and performs rolling updates; a Service gives them a stable address.
  • type: LoadBalancer makes DOKS create a billed DigitalOcean Load Balancer, configured with do-loadbalancer-* annotations.
  • Services match pods by labels and only send traffic to ready pods.
  • Use kubectl describe, logs, rollout status and rollout undo for day-to-day work.

Next: Ingress and Persistent Storage on DOKS