Deploying, Scaling, and Monitoring a Stateful Application

Introduction to Kubernetes

Frank Heilmann

Platform Architect and Freelance Instructor

Recap Stateless Applications

  • Short recap: stateless applications map to "Deployments" in Kubernetes
  • Used when each Pod of the application has exactly the same tasks
  • Stateful applications need Pods that belong together in set, but may work on different tasks and different data
  • Much of what we have learned about Deployments can be applied to StatefulSets as well
Introduction to Kubernetes

Stateful Applications

  • Stateful apps:

    • general concept
    • fit well to Kubernetes
    • save some state
  • When interrupted or stopped, a new replica (Pod) can read the saved state and continue operating from this state

  • Example:
    • A database backend (e.g. PostgreSQL) delivers data to a frontend using 3 Pods.
    • Each time we update data using any of the Pods, that data needs to be persisted
    • When a Pod terminates, a new one is created and needs to pick up the saved state
Introduction to Kubernetes

Kubernetes StatefulSets

  • Stateful applications translate to "Kubernetes StatefulSets"
  • A sample manifest consists of the same sections like:
    • apiVersion, kind, metadata, spec, template
  • replicas defines the number of Pods in the StatefulSet
  • More on selector later
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: <deployment name>
  labels:
    app: <a label for the application>
spec:
  replicas: <number of initial replicas>
  selector:
    matchLabels:
      app: <matches the label above>
  template:
    metadata:
      labels:
        app: <label to be given to each pod>
    spec:
      containers:
      - name: <container name>
        image: <the image to be used>
        ports:
        - containerPort: <ports for networking>
Introduction to Kubernetes

Deploying to a Kubernetes Cluster

  • StatefulSet is deployed similar toDeployments: kubectl apply -f <manifest.yml>
  • Once deployed, a StatefulSet is created different than a Deployment:
    • Pods are created one after the other, not all at once like Pods in a Deployment
    • Pods get predictable names like pod-0, pod-1, pod-2. etc.
  • This means: in contrast to the Pods of a Deployment, the Pods of a StatefulSet have an identity, and a state
  • Hence, different Pods of a StatefulSet with different identity can perform different roles in an application
Introduction to Kubernetes

Scaling A StatefulSet

  • Like Deployments, StatefulSets can be scaled up or scaled down:
    • Either change the number of replicas in the Manifest and re-apply,
    • Or use the command kubectl scale statefulsets ...
  • When scaling up, new Pods will be created one after another:
    • e.g, pod-0, pod-1, pod-2 first pod-3, then pod-4 will be added
  • When scaling down, Pods created last will be deleted first:
    • e.g, first pod-4, then pod-3
Introduction to Kubernetes

Monitoring a StatefulSet

  • Like in the case of Deployments, Monitoring enables reactions to all kind of problems, like outages, load spikes, or missing storage
  • Here, we use kubectl for basic monitoring tasks
  • Typical command: same like with Deployments
  • Example 1: kubectl get pods returns all pods in a StatefulSet with their current status
  • Example 2: kubectl get services returns all services that a StatefulSet may use
Introduction to Kubernetes

Let's practice!

Introduction to Kubernetes

Preparing Video For Download...