Docker & Kubernetes for Developers · 课时

使用 StatefulSets 管理有状态应用

使用 StatefulSets 部署和管理数据库等有状态应用,确保稳定的网络标识和持久化存储。

第 2 / 4 课11 个步骤

使用 StatefulSets 管理有状态应用 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Stateful Applications in K8s

When deploying applications in Kubernetes, some applications are stateless, meaning they don't store data locally and can be easily scaled or replaced.

However, many critical applications, like databases or message queues, are stateful. They need persistent storage and stable network identities.

This lesson introduces StatefulSets, a Kubernetes API object designed specifically to manage these stateful workloads.

Why StatefulSets?

Traditional Kubernetes Deployments are great for stateless apps. They create Pods with arbitrary names and can replace them freely.

Stateful applications, however, require:

  • Stable, unique network identities: Each instance needs a consistent name.
  • Stable, persistent storage: Data must survive Pod restarts or rescheduling.
  • Ordered deployment and scaling: Sometimes, instances need to start or stop in a specific sequence.

StatefulSets provide these crucial capabilities.

Key Features of StatefulSets

StatefulSets offer several powerful features for managing stateful applications:

  • Stable Network ID: Each Pod gets a predictable name (e.g., web-0, web-1) and DNS hostname.
  • Stable Persistent Storage: Integrates with Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) to give each Pod its own persistent storage.
  • Ordered Guarantees: Ensures Pods are created, scaled, and deleted in a strict, ordinal order.
  • Graceful Deployment: Pods are only created after their storage is ready, and only deleted after they've gracefully shut down.

StatefulSet Components

A StatefulSet typically works in conjunction with other Kubernetes resources:

  • Headless Service: Provides stable network identities for the Pods. It doesn't load-balance traffic, but creates unique DNS entries for each Pod.
  • Pod Template: Defines the specification for the Pods, including containers, resources, etc.
  • Volume Claim Templates: Generates a unique Persistent Volume Claim (PVC) for each Pod, ensuring dedicated storage.

Defining a Headless Service

Before creating a StatefulSet, you often define a Headless Service. This service is crucial for assigning stable network identities to your stateful Pods.

Notice clusterIP: None, which tells Kubernetes not to assign a cluster IP, making it 'headless'.

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
  labels:
    app: my-app
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None # This makes it a Headless Service
  selector:
    app: my-app

Creating a Basic StatefulSet

Now, let's look at a basic StatefulSet definition. It links to our Headless Service using serviceName and defines a volumeClaimTemplates for persistent storage.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-app
spec:
  serviceName: "my-app-service" # Link to Headless Service
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

Understanding Pod Identity

When the StatefulSet from the previous scene is deployed, it will create three Pods named my-app-0, my-app-1, and my-app-2.

Each Pod also gets its own stable DNS entry, like my-app-0.my-app-service.default.svc.cluster.local. This allows other services to reliably connect to specific instances.

Crucially, each Pod will also get its own unique Persistent Volume Claim (e.g., www-my-app-0), ensuring dedicated storage.

Ordered Deployment & Scaling

StatefulSets enforce a strict order for Pod creation, scaling, and deletion. This is vital for applications where sequence matters (e.g., a primary database and its replicas).

  • Creation: Pods are created in ascending ordinal order (my-app-0, then my-app-1, etc.). Each Pod is fully running and ready before the next is created.
  • Deletion: Pods are terminated in descending ordinal order (my-app-2, then my-app-1, etc.). Each Pod is fully shut down before the next is deleted.
  • Scaling: Similarly, scaling up adds Pods in order, and scaling down removes them in reverse order.

Updating StatefulSets

StatefulSets support controlled updates to their Pods, typically used for rolling out new versions of your application.

  • RollingUpdate (default): Pods are updated in reverse ordinal order (my-app-2, then my-app-1, etc.). Each Pod is updated and ready before the next one starts.
  • OnDelete: This strategy requires manual intervention. The StatefulSet controller will not automatically update Pods. You must manually delete Pods for the new template to take effect.

Rolling updates ensure minimal downtime and maintain the application's state during upgrades.

StatefulSet Features Check

Which of the following are key characteristics or requirements for Kubernetes StatefulSets?

StatefulSets Recap

Congratulations! You've learned about Kubernetes StatefulSets.

  • StatefulSets manage stateful applications, ensuring stable identities and persistent storage.
  • They provide stable network IDs, ordered operations, and leverage Volume Claim Templates for dedicated storage.
  • A Headless Service is often used to provide stable DNS entries for StatefulSet Pods.
  • They support controlled updates with strategies like RollingUpdate.

StatefulSets are essential for running databases, message queues, and other stateful services reliably on Kubernetes.

免费开始

用 AI 导师学习 Docker & Kubernetes for Developers — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「使用 StatefulSets 管理有状态应用」课时是免费的吗?

是的 — 「使用 StatefulSets 管理有状态应用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。

「使用 StatefulSets 管理有状态应用」这节课中我会学到什么?

使用 StatefulSets 部署和管理数据库等有状态应用,确保稳定的网络标识和持久化存储。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Docker & Kubernetes for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「使用 StatefulSets 管理有状态应用」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?

能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 持久卷与持久卷声明
  2. 使用 StatefulSets 管理有状态应用
  3. 使用 ConfigMaps 与 Secret 管理配置
  4. 存储类与动态供给
← 返回 Docker & Kubernetes for Developers