---
# System prepended metadata

title: StatefulSet
tags: [K8S]

---

---
title: StatefulSet # 簡報的名稱
tags: K8s   # 簡報的標籤

---

# StatefulSet

> [name=李俊毅]
> [time=Wed, Feb 5, 2020 1:04 PM]

---

## Agenda

* 介紹
* 範例
* StatefulSet與Service的關係
* 常用的CLI

---

## 介紹

----

StatefulSet，可縮寫為sts，管理有狀態應用，可以控制Pod與管理Pod擴展和伸縮，Pod Name為有序命名，命名規則為 Pod Name- 加上 UID

---

## 範例

----

hello-world.yaml

```
apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    app.kubernetes.io/name: load-balancer-example
  name: hello-world
spec:
  replicas: 5
  selector:
    matchLabels:
      app.kubernetes.io/name: load-balancer-example
  serviceName: hello-world
  podManagementPolicy: Parallel
  template:
    metadata:
      labels:
        app.kubernetes.io/name: load-balancer-example
    spec:
      containers:
        - image: gcr.io/google-samples/node-hello:1.0
          name: hello-world
          ports:
            - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: hello-world
spec:
  type: ClusterIP
  clusterIP: None
  selector:
    app.kubernetes.io/name: load-balancer-example
  ports:
    - port: 8080
```

---

## StatefulSet 與 Service 的關係

----

StatefulSet 在建立時，需要搭配 Service 才能建成功，因 StatefulSet 為有狀態，搭配的 Service 為 Headless Service ，透過此 Service 可以做到指定Pod為服務對象

----

![](https://i.imgur.com/Bx72KMG.png)

![](https://i.imgur.com/NuLuK6h.png)

---

## 常用的CLI

----

### 查詢 statefulset

----

```
kubectl get sts
```

![](https://i.imgur.com/SuDuMMD.png)

----

### 確認 statefulset 狀況

----

```
kubectl describe sts hello-world
```

![](https://i.imgur.com/vMPiB6h.png)

----

### 查看或修改運行中 statefulset

----

```
kubectl edit sts hello-world
```

![](https://i.imgur.com/gXbKEVe.png)

----

### 查看 Pod

```
kubectl get pods -o wide
```

----

![](https://i.imgur.com/mOgXKCx.png)


