ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
为了确保容器在部署后确实处在正常运行状态,Kubernetes 提供了两种探针(Probe)来探测容器的状态: * LivenessProbe:探测应用是否处于健康状态,如果不健康则删除并根据 Pod 的重启策略 restartPolicy 来重新创建容器。 * ReadinessProbe:探测应用是否启动完成并且处于正常服务状态,如果不正常则不会接收来自 Kubernetes Service 的流量,即将该 Pod 从 Service 的 endpoint 中移除。 Kubernetes 支持三种方式来执行探针: * exec:在容器中执行一个命令,如果[命令退出码](http://www.tldp.org/LDP/abs/html/exitcodes.html)返回`0`则表示探测成功,否则表示失败 * tcpSocket:对指定的容器 IP 及端口执行一个 TCP 检查,如果端口是开放的则表示探测成功,否则表示失败 * httpGet:对指定的容器 IP、端口及路径执行一个 HTTP Get 请求,如果返回的[状态码](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes)在`[200,400)`之间则表示探测成功,否则表示失败 ```yaml apiVersion: v1 kind: Pod metadata: labels: app: nginx name: nginx spec: containers: - image: nginx imagePullPolicy: Always name: http livenessProbe: httpGet: path: / port: 80 httpHeaders: - name: X-Custom-Header value: Awesome initialDelaySeconds: 15 timeoutSeconds: 1 readinessProbe: exec: command: - cat - /usr/share/nginx/html/index.html initialDelaySeconds: 5 timeoutSeconds: 1 - name: goproxy image: gcr.io/google_containers/goproxy:0.1 ports: - containerPort: 8080 readinessProbe: tcpSocket: port: 8080 initialDelaySeconds: 5 periodSeconds: 10 livenessProbe: tcpSocket: port: 8080 initialDelaySeconds: 15 periodSeconds: 20 ```