### K8S探针种类
#### 存活探针(Liveness Probe)
存活探针用于检查容器是否仍在运行。如果存活探针失败,K8S将重启容器。存活探针通常用于检测容器内部的故障,如死锁或无限循环。
#### 就绪探针(Readiness Probe)
就绪探针用于检查容器是否准备好接收流量。如果就绪探针失败,K8S将停止将流量转发到该容器。就绪探针通常用于确保应用程序在启动过程中完全初始化。
#### 启动探针(Startup Probe)
启动探针用于检查容器是否已经准备好接收流量。与就绪探针类似,但启动探针仅在容器启动时运行一次。如果启动探针失败,K8S将重启容器。启动探针对于在容器启动期间的检查非常有用。
### 示例代码
#### 存活探针示例
```yaml
apiVersion: v1
kind: Pod
metadata:
name: liveness-probe-demo
spec:
containers:
- name: liveness-probe-container
image: nginx
livenessProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 15
periodSeconds: 5
```
在上面的示例中,我们定义了一个存活探针,使用HTTP GET请求检查路径`/index.html`,端口80。初始延迟15秒,每5秒运行一次检查。
#### 就绪探针示例
```yaml
apiVersion: v1
kind: Pod
metadata:
name: readiness-probe-demo
spec:
containers:
- name: readiness-probe-container
image: nginx
readinessProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 10
periodSeconds: 3
```
在上面的示例中,我们定义了一个就绪探针,使用HTTP GET请求检查路径`/index.html`,端口80。初始延迟10秒,每3秒运行一次检查。
#### 启动探针示例
```yaml
apiVersion: v1
kind: Pod
metadata:
name: startup-probe-demo
spec:
containers:
- name: startup-probe-container
image: nginx
startupProbe:
httpGet:
path: /index.html
port: 80
initialDelaySeconds: 5
periodSeconds: 2
failureThreshold: 10
```
在上面的示例中,我们定义了一个启动探针,使用HTTP GET请求检查路径`/index.html`,端口80。初始延迟5秒,每2秒运行一次检查。如果连续10次检查失败,则容器将被重启。
通过以上示例,你可以了解K8S中不同类型探针的用法和配置方式。探针可以帮助你更好地监控和管理Kubernetes中的容器,确保应用程序的健康运行。希望这篇文章可以帮助你更好地理解和应用K8S探针。