由发起者对容器进行周期性健康状态检测。 容器1周期检测相当于人类的周期性体检每次检测相当于人类每次体检的内容
docker health check实现方式:
1.在docker-compose实现探针:
root@k8s-master:~/yaml/1226/2.PodProbe-case/case2-docker-container-healthy-check# cat 1.docker-compose.yaml
version: '3.6'
services:
nginx-service:
image: nginx:1.20.2
container_name: nginx-web1
expose:
- 80
- 443
ports:
- "80:80"
- "443:443"
restart: always
healthcheck: #添加服务健康状态检查
test: ["CMD", "curl", "-f", "http://localhost"]
interval: 5s #健康状态检查的间隔时间,默认为30s
timeout: 5s #单次检查的失败超时时间,默认为30s
retries: 3 #连续失败次数默认3次,当连续失败retries次数后将容器置为unhealthy状态
start_period: 60s #60s后每间隔interval的时间检查一次,连续retries次后才将容器置为unhealthy状态, 但是start_period时间内检查成功就认为是检查成功并装容器置于healthy状态
2.在dockerfile实现健康状态检测:
root@k8s-master:~/yaml/1226/2.PodProbe-case/case2-docker-container-healthy-check# cat 2.Dockerfile
FROM nginx:1.20.2
maintainer "test@test.com"
HEALTHCHECK --interval=5s --timeout=2s --retries=3
CMD curl --silent --fail localhost:80 || exit 1
pod的生命周期,从start后可以配置postStart检测,运行过程中可以配置livenessProbe和readinessProbe,最后在 stop前可以配置preStop操作。
Pod重启策略:
Pod一旦配置了探针,在检测失败时候,会基于restartPolicy对Pod进行下一步操作:
restartPolicy (容器重启策略):
imagePullPolicy (镜像拉取策略):
3.1 探针介绍
探针是由 kubelet 对容器执行的定期诊断,以保证Pod的状态始终处于运行状态,要执行诊断,kubelet 调用由容器实现的Handler(处理程序),也称为Hook(钩子)。
有三种类型的处理程序:
每次探测都将获得以下三种结果之一:
3.2 探针类型
3.3 探针通用配置参数:
3.4 探针HTTP配置参数
HTTP 探测器可以在 httpGet 上配置额外的字段:
3.5 探针示例
case3-Probe# kubectl apply -f 5-startupProbe-livenessProbe-readinessProbe.yaml
root@k8s-master:~/yaml/1226/2.PodProbe-case/case3-Probe# cat 5-startupProbe-livenessProbe-readinessProbe.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myserver-myapp-frontend-deployment
namespace: myserver
spec:
replicas: 1
selector:
matchLabels: #rs or deployment
app: myserver-myapp-frontend-label
#matchExpressions:
# - {key: app, operator: In, values: [myserver-myapp-frontend,ng-rs-81]}
template:
metadata:
labels:
app: myserver-myapp-frontend-label
spec:
terminationGracePeriodSeconds: 60
containers:
- name: myserver-myapp-frontend-label
image: nginx:1.20.2
ports:
- containerPort: 80
startupProbe:
httpGet:
#path: /monitor/index.html
path: /index.html
port: 80
initialDelaySeconds: 5 #首次检测延迟5s
failureThreshold: 3 #从成功转为失败的次数
periodSeconds: 3 #探测间隔周期
readinessProbe:
httpGet:
#path: /monitor/monitor.html
path: /index.html
port: 80
initialDelaySeconds: 5
periodSeconds: 3
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
livenessProbe:
httpGet:
#path: /monitor/monitor.html
path: /index.html
port: 80
initialDelaySeconds: 5
periodSeconds: 3
timeoutSeconds: 5
successThreshold: 1
failureThreshold: 3
---
apiVersion: v1
kind: Service
metadata:
name: myserver-myapp-frontend-service
namespace: myserver
spec:
ports:
- name: http
port: 81
targetPort: 80
nodePort: 30012
protocol: TCP
type: NodePort
selector:
app: myserver-myapp-frontend-label
describe查看pods的运行情况
3.6 postStart、preStop简介
postStart 和 preStop处理函数:
Pod的终止流程
1.创建pod
2.删除pod
示例代码:
case4-postStart-preStop# kubectl apply -f 1-myserver-myapp1-postStart-preStop.yaml
root@k8s-master:~/yaml/1226/2.PodProbe-case/case4-postStart-preStop# cat 1-myserver-myapp1-postStart-preStop.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myserver-myapp1-lifecycle
labels:
app: myserver-myapp1-lifecycle
namespace: myserver
spec:
replicas: 1
selector:
matchLabels:
app: myserver-myapp1-lifecycle-label
template:
metadata:
labels:
app: myserver-myapp1-lifecycle-label
spec:
terminationGracePeriodSeconds: 60 #可选终止等待期,如果有设置删除宽限时间,则等待宽限时间到期,否则最多等待30s,
containers:
- name: myserver-myapp1-lifecycle-label
image: tomcat:7.0.94-alpine
lifecycle:
postStart:
exec:
#command: 把自己注册到注册在中心
command: ["/bin/sh", "-c", "echo 'Hello from the postStart handler' >> /usr/local/tomcat/webapps/ROOT/index.html"]
#httpGet:
# #path: /monitor/monitor.html
# host: www.magedu.com
# port: 80
# scheme: HTTP
# path: index.html
preStop:
exec:
#command: 把自己从注册中心移除
command: ["/usr/local/tomcat/bin/catalina.sh","stop"]
ports:
- name: http
containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: myserver-myapp1-lifecycle-service
namespace: myserver
spec:
ports:
- name: http
port: 80
targetPort: 8080
nodePort: 30012
protocol: TCP
type: NodePort
selector:
app: myserver-myapp1-lifecycle-label
页面更新:2024-05-27
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号