# stateful set

Kubernetes StatefulSets are used to deploy and manage stateful applications in a reliable and scalable way. Unlike Deployments, which are suitable for stateless applications, StatefulSets provide guarantees about the ordering and uniqueness of Pods. Here's a guide on how to use Kubernetes StatefulSets:

#### 1. **Understanding StatefulSets:**

* **Unique Network Identifiers:** StatefulSets assign unique network identifiers (hostname) to each Pod, ensuring stable network identities across restarts and rescheduling.
* **Ordered Deployment:** StatefulSets deploy Pods in a predictable order, starting from 0 and incrementing. This is crucial for stateful applications that rely on a stable identity and order.
* **Persistent Storage:** StatefulSets are often used with Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) to ensure data persistence across Pod restarts.

#### 2. **Creating a StatefulSet:**

Here's an example of a simple StatefulSet manifest:

```yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
            - containerPort: 80
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: "standard"
        resources:
          requests:
            storage: 1Gi
```

In this example:

* The StatefulSet named "web" will deploy three replicas of the Nginx container.
* Pods will have the labels `app: nginx`.
* Each Pod will have its own Persistent Volume Claim (PVC) template named "data" for persistent storage.

#### 3. **Deploying the StatefulSet:**

Apply the StatefulSet manifest using `kubectl apply`:

```bash
kubectl apply -f statefulset.yaml
```

#### 4. **Scaling the StatefulSet:**

You can scale the StatefulSet using the `kubectl scale` command:

```bash
kubectl scale statefulset web --replicas=5
```

#### 5. **Accessing StatefulSet Pods:**

Pods in a StatefulSet can be accessed using their stable network identifiers (hostnames). For example, the Pods in the "web" StatefulSet will be named "web-0", "web-1", and so on.

```bash
kubectl exec -it web-0 -- /bin/bash
```

#### 6. **Deleting the StatefulSet:**

Deleting a StatefulSet will also delete the associated Pods and PVCs:

```bash
kubectl delete statefulset web
```

#### 7. **Updating the StatefulSet:**

To update the StatefulSet, modify the manifest file and apply the changes:

```bash
kubectl apply -f updated-statefulset.yaml
```

#### 8. **Additional Considerations:**

* **Headless Service:** StatefulSets are often used with a headless Service to provide stable network identities to the Pods. The Service name is typically set in the `serviceName` field of the StatefulSet.
* **Init Containers:** You can use init containers to perform initialization tasks before the main container starts.
* **Pod Disruption Budgets:** For high availability, consider configuring Pod Disruption Budgets to limit the number of simultaneously disrupted Pods during updates.
* **Stateful Applications:** StatefulSets are suitable for stateful applications like databases, caching systems, and other systems that require stable identities and persistent storage.
* **Affinity and Anti-Affinity:** Use Pod affinity and anti-affinity rules to influence the scheduling of Pods.
* **Rolling Updates:** StatefulSets support rolling updates, which allow updates to be performed with minimal disruption to the application.

Refer to the [official Kubernetes documentation on StatefulSets](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/) for detailed information and advanced configurations. StatefulSets are a powerful feature for deploying and managing stateful applications in a Kubernetes cluster.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://a7246c5516ab4c80cdfe21ca2be3e40c.gitbook.io/kubernetes-basics/stateful-set.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
