SkillTect provides result-driven consulting and cloud training for tech companies and IT professionals.

SkillTect Technologies Pvt Ltd

Mohan Estate,
Delhi, India 110044,

What is StatefulSet in Kubernetes? What Is The Stateful Application in Kubernetes?

There are some unique challenges from stateless applications to deploying stateful applications in a Kubernetes Environment. Stateful applications, like databases, manage data as persistently, during pod restarts or redeployments requiring a consistent identity and storage across pods. Kubernetes fulfils this need with the StatefulSets, This is a well-designed controller to manage Stateful applications that manage deployments and scaling. This article will help you to understand ‘What is StatefulSet in Kubernetes’, detailing their advantages, usage, and a step-by-step guide to deploying a MySQL database as a stateful application within a Kubernetes cluster.

What Are The Stateful Application in Kubernetes?

Stateful applications are those that save and manage data over time. Unlike stateless applications, which treat each request as an independent transaction without the need for previously stored data, stateful applications such as MySQL, Oracle, and PostgreSQL maintain a continuous state across sessions. This persistent data management is crucial for applications that require historical data access, such as databases that store user information, transactions, or any other data that needs to be retrieved or updated over time.

What Is The Role of StatefulSet in Kubernetes?

StatefulSets is a Kubernetes controller specifically designed for stateful applications. They provide each pod in the application with a unique, persistent identifier that it maintains across rescheduling. This identity is reflected in a stable network identifier, stable storage, and ordered, graceful deployment and scaling. StatefulSets make it feasible to run complex stateful applications on Kubernetes, offering predictable pod names and persistent storage that ensure data persists across pod restarts and updates.

When to Use StatefulSets

StatefulSets are particularly useful when you need to maintain a stable network identity and persistent storage for each pod. They are ideal for primary-replica database architectures, where a primary database handles write operations and replicas handle read operations, with data being synchronised across all instances. StatefulSets ensure that each replica has a consistent identity and access to persisted data, making them essential for databases, clustered applications, and any other application requiring stable, persistent storage.

Deploying MySQL with StatefulSets

Deploying a MySQL database using StatefulSets involves several key steps, from creating necessary Kubernetes objects like Secrets and PersistentVolumes to defining the StatefulSet itself. Here’s a step-by-step guide to deploying a MySQL database as a stateful application:

1. Create a Secret for MySQL Credentials

Secrets in Kubernetes are used to store and manage sensitive information, such as passwords or tokens. For a MySQL database, you’ll need to create a Secret to store the database root password:

apiVersion: v1
kind: Secret
  name: mysql-password
type: opaque
  MYSQL_ROOT_PASSWORD: <base64-encoded-password>

2. Define Persistent Volume Claims (PVCs)

StatefulSets use PVCs to provide stable storage for stateful applications. Define a PVC that requests the necessary storage for your MySQL database. Kubernetes will dynamically provision a PersistentVolume that satisfies the claim, ensuring data persistence.

3. Create the StatefulSet

The StatefulSet definition for MySQL includes specifications for the desired replicas, container images, ports, volume mounts, and the previously created secret for authentication. It should look something like this:

apiVersion: apps/v1
kind: StatefulSet
  name: mysql
  app: mysql
  serviceName: "mysql"
  replicas: 3
    app: mysql
  - name: mysql
    image: mysql:5.7
    - containerPort: 3306
    - name: mysql-storage
      mountPath: /var/lib/mysql
          name: mysql-password
          key: MYSQL_ROOT_PASSWORD
  - metadata:
  name: mysql-storage
  accessModes: [ "ReadWriteOnce" ]
      storage: 10Gi

4. Create a Service for MySQL

A headless service is required to provide a network identity to the pods. The service selects pods managed by the StatefulSet and provides a DNS entry that can be used to communicate with the MySQL database directly.

apiVersion: v1
kind: Service
  name: mysql
  - port: 3306
  clusterIP: None
app: mysql

5. Accessing and Managing the MySQL Database

Once the StatefulSet and its associated services are deployed, you can access the MySQL database using a MySQL client installed within a pod in the same Kubernetes cluster. This allows direct interaction with the database for management tasks such as creating schemas, inserting data, or performing maintenance operations.

Best Practices for Stateful Applications in Kubernetes

  • Namespace Isolation: Deploy stateful applications in their dedicated namespaces to isolate resources and manage permissions effectively.
  • Storage Considerations: Use high-quality, reliable storage solutions that support the access modes your application requires.
  • Backup and Recovery: Implement regular backup and recovery procedures to ensure data durability and minimize downtime.
  • Monitoring and Logging: Employ comprehensive monitoring and logging solutions to track the health and performance of your stateful applications.


StatefulSets in Kubernetes offer a powerful abstraction for running stateful applications like databases with predictable and stable management of persistent data and identities. By following the guidelines and steps outlined in this article, you can deploy, manage, and scale stateful applications within your Kubernetes cluster, leveraging the full potential of containerized environments for both stateless and stateful workloads.

Related Articles:
CCNA and CCIE Certifications for Your IT Career
Google Kubernetes Engine (GKE) a Complete Learning Guide!

Article by Harsh Shrivastav

Leave a Reply