赞
踩
apiVersion: v1
kind: Pod
metadata:
name: nainx
spec:
nodeSelector: master1 # 指定pod需要调度的节点
containers:
- name: nginx
image: nginx
apiVersion: v1
kind: Pod
metadata:
name: nainx
spec:
nodeName: # 指定调度到标签env为dev的节点上
env: dev
containers:
- name: nginx
image: nginx
watch apiServer
,获取spec.nodeName为空的pod,即判断pod.spec.Node == null? 若为null,表示这个Pod请求是新的,需要创建,因此先进行调度计算(共计2步:1、过滤不满足条件的,2、选择优先级高的),找到合适的node,然后将信息在etcd数据库中更新分配结果:pod.spec.Node = nodeA (设置一个具体的节点)post start
之类的(启动后钩子函数)操作post start
执行之后就开始进行健康检查pre stop
操作(终止前钩子函数)apiVersion: v1
kind: Pod
metadata:
name: post-start-test
spec:
containers:
- name: poststart
image: nginx
lifecycle: # 生命周期事件
postStart:
exec:
command: ["mkdir", "-p", "/usr/share/nginx/post_start_test"] # 创建pod后创建文件夹
preStop:
exec:
command: ["/bin/sh", "-c", "sleep 1000000000"] # 容器异常终止大概需要30s以上
kubectl exec -it post-start-test -- ls -l /usr/share/nginx/
total 8
drwxr-xr-x 2 root root 4096 Dec 29 2021 html
drwxr-xr-x 2 root root 4096 Aug 22 05:09 post_start_test
kubectl delete -f lifecycle.yaml
pod "post-start-test" deleted # 删除的光标会持续将近20秒才结束,或者可以写个shel死循环,每隔一秒输出一个计数,看看用了多少计算
Success
。Failure
。 如果容器不提供就绪态探针,则默认状态为 Success
。注:检查后不健康,将容器设置为Notready;如果使用service来访问,流量不会转发给此种状态的podkubelet
将杀死容器,而容器依其进行重启。 如果容器没有提供启动探测,则默认状态为 Success
。SERVING
,则认为诊断成功。gRPC 探针是一个 alpha 特性,只有在你启用了 GRPCContainerProbe
特性门控时才能使用。apiVersion: v1
kind: Pod
metadata:
name: liveness-exec
spec:
containers:
- name: liveness
image: busybox
imagePullPolicy: IfNotPresent
args:
- /bin/sh
- -c
- touch /tmp/health; sleep 30; rm -rf /tmp/health; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/health
initialDelaySeconds: 5 # 启动后5秒开始检测
periodSeconds: 5 # 检测间隔,5秒检测一次
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 83s default-scheduler Successfully assigned default/liveness-exec to docker-desktop
Normal Pulling 82s kubelet Pulling image "busybox"
Normal Pulled 65s kubelet Successfully pulled image "busybox" in 17.1068002s
Normal Created 65s kubelet Created container liveness
Normal Started 65s kubelet Started container liveness
Warning Unhealthy 23s (x3 over 33s) kubelet Liveness probe failed: cat: can't open '/tmp/health': No such file or directory
Normal Killing 23s kubelet Container liveness failed liveness probe, will be restarted
apiVersion: v1
kind: Pod
metadata:
name: liveness-httpget
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /indexhtml
initialDelaySeconds: 5
periodSeconds: 5
kubectl get pod
NAME READY STATUS RESTARTS AGE
liveness-httpget 0/1 ImagePullBackOff 0 44s
kubectl exec -it liveness-httpget -- rm -rf /usr/share/nginx/html/index.html
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 30s default-scheduler Successfully assigned default/liveness-httpget to docker-desktop
Normal Killing 11s kubelet Container nginx failed liveness probe, will be restarted
Normal Pulled 10s (x2 over 30s) kubelet Container image "nginx" already present on machine
Normal Created 10s (x2 over 30s) kubelet Created container nginx
Normal Started 10s (x2 over 30s) kubelet Started container nginx
Warning Unhealthy 1s (x4 over 21s) kubelet Liveness probe failed: HTTP probe
Normal Killing 9s (x2 over 29s) kubelet Container nginx failed liveness probe, will be restarted
Normal Created 8s (x3 over 48s) kubelet Created container nginx
Normal Started 8s (x3 over 48s) kubelet Started container nginx
failed with statuscode: 404
apiVersion: v1
kind: Pod
metadata:
name: readiness-httpget
spec:
containers:
- name: readiness
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
readinessProbe: # 这里由liveness换成了readiness
httpGet:
port: http
path: /index.html
initialDelaySeconds: 3
periodSeconds: 5
kubectl exec -it liveness-httpget -- rm -rf /usr/share/nginx/html/index.html
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m57s default-scheduler Successfully assigned default/readiness-httpget to docker-desktop
Normal Pulled 2m57s kubelet Container image "nginx" already present on machine
Normal Created 2m57s kubelet Created container readiness
Normal Started 2m57s kubelet Started container readiness
Warning Unhealthy 3s (x13 over 63s) kubelet Readiness probe failed: HTTP probe failed with statuscode: 404
apiVersion: v1
kind: Pod
metadata:
name: readiness-liveness-httpget
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
livenessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 1
periodSeconds: 3
readinessProbe:
httpGet:
port: http
path: /index.html
initialDelaySeconds: 5
periodSeconds: 5
kubectl exec -it liveness-httpget -- rm -rf /usr/share/nginx/html/index.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。