Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 35 ยท Kubernetes

Ingress and Persistent Storage on DOKS

Route many hostnames through one DigitalOcean Load Balancer with an Ingress, and keep data safe with PersistentVolumeClaims backed by DigitalOcean Block Storage.

One Service of type LoadBalancer per app gets expensive: every Load Balancer is billed separately. An Ingress solves that. A single ingress controller sits behind one Load Balancer and routes requests to many Services by hostname and path. The second half of this lesson covers state: pods are disposable, so databases and uploads need PersistentVolumeClaims, which on DOKS are backed by DigitalOcean Block Storage volumes.

Commands here are read-only text; the Python blocks are stand-ins that model the routing and sizing rules.

Ingress in one picture

flowchart LR
  U["Visitor"] --> LB["One DigitalOcean Load Balancer"]
  LB --> IC["Ingress controller (Nginx pods)"]
  IC -->|"shop.example.com"| S1["Service shop-web"]
  IC -->|"shop.example.com/api"| S2["Service shop-api"]
  IC -->|"blog.example.com"| S3["Service blog"]

The Load Balancer is created once, for the ingress controller's own Service. Ingress rules then fan traffic out.

You install a controller yourself; Kubernetes does not ship one. DigitalOcean offers an Nginx ingress controller as a one-click app in the control panel, and you can also install the community chart with Helm:

Terminal
helm upgrade --install ingress-nginx ingress-nginx \
  --repo https://kubernetes.github.io/ingress-nginx \
  --namespace ingress-nginx --create-namespace

kubectl get service -n ingress-nginx

The controller's Service has type LoadBalancer, so DOKS creates the Load Balancer. Point your DNS A records for shop.example.com and blog.example.com at that one IP.

Writing Ingress rules

YAML
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: shop
spec:
  ingressClassName: nginx
  rules:
    - host: shop.example.com
      http:
        paths:
          - path: /api
            pathType: Prefix
            backend:
              service: { name: shop-api, port: { number: 80 } }
          - path: /
            pathType: Prefix
            backend:
              service: { name: shop-web, port: { number: 80 } }
    - host: blog.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service: { name: blog, port: { number: 80 } }

When several paths match, the longest prefix wins, so /api/orders goes to shop-api while /about falls through to shop-web. A request for an unknown host reaches the controller's default backend, which returns 404. This stand-in reproduces the decision.

Python Stand-in: host and longest-prefix routing

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 /apiary: with pathType: Prefix, matching is by whole path segments, so /api does not capture /apiary.

For HTTPS, install cert-manager and add a tls: block to the Ingress, or terminate TLS at the DigitalOcean Load Balancer. The tradeoffs are covered in TLS certificates.

Persistent storage with Block Storage

DOKS installs the DigitalOcean CSI driver. It provides StorageClasses such as do-block-storage (the default; the volume is deleted when the claim is deleted) and do-block-storage-retain (the volume survives). Run kubectl get storageclass to see what your cluster has.

YAML
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pgdata
spec:
  accessModes: [ReadWriteOnce]
  storageClassName: do-block-storage
  resources:
    requests:
      storage: 20Gi

Mount it in a pod with volumes: and volumeMounts:. The driver creates a real Block Storage volume, attaches it to the node running the pod and formats it. Important rules:

  • ReadWriteOnce only. A block volume attaches to one node at a time. For shared files across nodes use Spaces (object storage) or an NFS-style service instead.
  • Same region. The volume lives in the cluster's region and can only attach to nodes there.
  • It is billed per GiB even when idle, and snapshots are billed separately.
  • Use StatefulSets for databases so each replica gets its own claim.
  • Prefer a managed database for production data; running Postgres in Kubernetes is possible but you own backups and upgrades (see managed databases).
Python Stand-in: check and price a claim

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.

The price constant is an example, not a quote: always read the current pricing page.

Reclaim policy: delete or retain

With the default class, deleting the PVC deletes the volume and its data. With the retain class, Kubernetes keeps the volume and you reattach or delete it yourself. For anything you would be sad to lose, use retain plus regular snapshots (see backups and snapshots).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why use an Ingress instead of one LoadBalancer Service per app?

    Every LoadBalancer Service creates a billed Load Balancer; an ingress controller shares one.
  2. 2. With rules for /api and /, which backend gets a request for /api/orders?

    Longer prefixes are more specific and take priority.
  3. 3. Which access mode do DigitalOcean block volumes support?

    A volume attaches to one node at a time, so only ReadWriteOnce works.
  4. 4. Which StorageClass keeps the volume after the claim is deleted?

    The retain class sets the reclaim policy to Retain so data survives deleting the PVC.

Summary

  • An ingress controller behind one Load Balancer routes many hosts and paths to Services; the longest matching prefix wins.
  • You must install a controller (Nginx one-click or Helm) and point DNS at its Load Balancer.
  • PVCs create DigitalOcean Block Storage volumes: single node, single region, billed per GiB.
  • Use the retain class and snapshots for valuable data, or use a managed database.

Next: Scaling and Upgrading DOKS