---
# System prepended metadata

title: Liveness & Readiness
tags: [K8S]

---

# Liveness & Readiness
* Liveness 用來檢查 container 是否仍然正常運行，而 Readiness 用來檢查 container 是否準備好接收流量。
* Liveness 探針的失敗會導致 container 重啟，而 Readiness 探針的失敗並不會重啟 container。
## Liveness
* 如果 Liveness 探針失敗，Kubernetes 將會重建 container，不是 pod，但只要 container 或 pod 重建 restart 次數都會增加。
```
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: liveness
  name: liveness-exec
spec:
  containers:
  - name: liveness
    image: registry.k8s.io/busybox
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 600
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5
```


* 如果探針發現檔案不見 container 會 restart，因為 restart 次數增加，但 pod ip 並不會改變。
```
$ kg pod liveness-exec -o wide --watch
NAME            READY   STATUS    RESTARTS   AGE   IP              NODE            NOMINATED NODE   READINESS GATES
liveness-exec   1/1     Running   0          7s    10.244.225.10   tkdev-worker1   <none>           <none>
liveness-exec   1/1     Running   1 (1s ago)   67s   10.244.225.10   tkdev-worker1   <none>           <none>
```
* describe 可以看到 Liveness probe 的訊息
```
$ kubectl describe pod liveness-exec
......
Events:
  Type     Reason     Age                   From               Message
  ----     ------     ----                  ----               -------
  Normal   Scheduled  4m28s                 default-scheduler  Successfully assigned default/liveness-exec to cilium1
  Normal   Pulled     4m26s                 kubelet            Successfully pulled image "registry.k8s.io/busybox" in 1.287880562s (1.287905133s including waiting)
  Normal   Pulled     3m12s                 kubelet            Successfully pulled image "registry.k8s.io/busybox" in 598.209367ms (598.231273ms including waiting)
  Normal   Created    117s (x3 over 4m26s)  kubelet            Created container liveness
  Normal   Started    117s (x3 over 4m26s)  kubelet            Started container liveness
  Normal   Pulled     117s                  kubelet            Successfully pulled image "registry.k8s.io/busybox" in 637.29397ms (637.337828ms including waiting)
  Warning  Unhealthy  73s (x9 over 3m53s)   kubelet            Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory
  Normal   Killing    73s (x3 over 3m43s)   kubelet            Container liveness failed liveness probe, will be restarted
  Normal   Pulling    43s (x4 over 4m28s)   kubelet            Pulling image "registry.k8s.io/busybox"
```


## readiness

* 如果 Readiness 探針失敗，Kubernetes 將會將容器標記為不可用，並將其從服務中移除。
* 例如，一個 web 應用程式可能需要在啟動後花費幾秒鐘來加載其數據庫。在這種情況下，您可以使用 Readiness 探針來確保 web 應用程式在加載完數據庫之前不會接收流量。
```
apiVersion: v1
kind: Pod
metadata:
  labels:
    test: readiness
  name: readiness-exec
spec:
  containers:
  - name: readiness
    image: nginx
    ports:
    - containerPort: 80
    readinessProbe:
      exec:
        command:
        - cat
        - /usr/share/nginx/html/index.html
      initialDelaySeconds: 5
      periodSeconds: 5
---
apiVersion: v1
kind: Service
metadata:
  name: readiness-svc
spec:
  selector:
    test: readiness
  type: NodePort
  ports:
  - protocol: TCP
    port: 80
```
* 刪除 html 檔案
```
$ kubectl exec readiness-exec -- rm -f /usr/share/nginx/html/index.html
```

* 狀態為 Running，但服務本身不會重啟
```
$ kubectl get po
NAME                     READY   STATUS    RESTARTS   AGE
readiness-exec           0/1     Running   0          117s
```

```
$ kubectl describe po readiness-exec
......
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  87s                default-scheduler  Successfully assigned default/readiness-exec to cilium1
  Normal   Pulling    87s                kubelet            Pulling image "registry.k8s.io/busybox"
  Normal   Pulled     86s                kubelet            Successfully pulled image "registry.k8s.io/busybox" in 633.371896ms (633.40657ms including waiting)
  Normal   Created    86s                kubelet            Created container readiness
  Normal   Started    86s                kubelet            Started container readiness
  Warning  Unhealthy  1s (x13 over 52s)  kubelet            Readiness probe failed: cat: can't open '/tmp/healthy': No such file or directory
```

