之前的文章 Kubernetes 的 Health Check 一文中提到的健康检查的方式有三种,仅仅只用了 exec
一种方式来演示。
本文将使用一个 yaml 文件,展示 httpGet
和 tcpSocket
两种方式怎么使用,两者区别就是修改健康检查部分就可以
httpGet
如下是我的yaml文件的部分内容,这里没有展示完全我的文件,感兴趣的可以下载看完整的yaml文件: https://www.feiyiblog.com/files/practise/nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
livenessProbe:
httpGet:
scheme: HTTP
path: /healthz
port: 80
initialDelaySeconds: 10
periodSeconds: 5
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html/index.html
readOnly: True
subPath: index.html
- name: html
mountPath: /usr/share/nginx/html/healthz/index.html
readOnly: True
subPath: healthz.html
volumes:
- name: html
configMap:
name: nginx-cm
将健康检查部分拿出来单独注释一遍
# ------------------------------------------------------
# 因为readiness和liveness使用方法一样,这里以liveness为例
# 使用HTTP的方式去访问,http://localhost/healthz
# 访问成功健康检查通过,失败则按照策略重复检查
livenessProbe:
httpGet:
scheme: HTTP
path: /healthz # 该路径是真实存在在你的服务目录下的
# 如:nginx的页面根目录为/usr/share/nginx/html,在html目录下必须存在healthz,否则健康检查必失败
port: 80
initialDelaySeconds: 10
periodSeconds: 5
# ------------------------------------------------------
将资源部署成功后,可以通过 describe
查看,得到以下信息
Liveness: http-get http://:80/healthz delay=10s timeout=1s period=5s #success=1 #failure=3
tcpSocket
只需要更改 livenessProbe
部分即可
livenessProbe:
tcpSocket:
port: 80 # 容器内启动服务的tcp端口号
initialDelaySeconds: 10
periodSeconds: 5
只要服务端口起来了,就没问题。以上~这么简单!
Liveness: tcp-socket :80 delay=10s timeout=1s period=5s #success=1 #failure=3
以后遇到复杂的用法我再来更新。