当前位置: 首页 > news >正文

85、 探针

一、pod的进阶

pod的进阶:

1.1、pod的生命周期当中的状态:

1、Running运行中,pod已经分配到节点上且pod内的容器正常运行。正常状态(ready 1/1)。

2、complete:完成之后退出,容器内的返回码是0,echo $?(表示容器正常运行结束)

3、pending:挂起状态,pod已经创建好了,但是没有被分配到节点上。

面试题:出现pending状态如何解决?

  • 1、节点上的资源不足 nginx------node1 node1---->换个节点
  • 2、污点,节点上设置了污点标签,导致节点不可部署
  • 3、pv,节点上没有合适的pv挂载点(手动),创建pv失败。(手动,自动)
  • 4、网络原因,防火墙导致节点不可用。
  • 5、swap没有关闭,k8s禁止使用交换分区。
  • 6、HostPort已经被占用,NodePort节点上的端口被占用,也会pending。
  • 7、ImagePullBackOff:镜像拉取失败
  • 8、CrashLoopBackOff:容器已经启动了,但是异常退出了,可以看日志,或者查看详细信息情况。
  • 9、error:pod启动过程中报错,日志可以查询。
  • 10、PodInitializing:初始化中(pod内部有初始化init容器)
  • 11、Evicte:pod被驱逐。
pod状态一览:
CrashLoopBackOff:    容器退出,kubelet正在将它重启
InvalidImageName:    无法解析镜像名称
ImageInspectError:   无法校验镜像
ErrImageNeverPull:   策略禁止拉取镜像
ImagePullBackOff:    正在重试拉取
RegistryUnavailable: 连接不到镜像中心
ErrImagePull:        通用的拉取镜像出错
CreateContainerConfigError: 不能创建kubelet使用的容器配置
CreateContainerError: 创建容器失败
m.internalLifecycle.PreStartContainer 执行hook报错
RunContainerError:   启动容器失败
PostStartHookError:   执行hook报错
ContainersNotInitialized: 容器没有初始化完毕
ContainersNotReady:   容器没有准备完毕
ContainerCreating:    容器创建中
PodInitializing:pod   初始化中
DockerDaemonNotReady:  docker还没有完全启动
NetworkPluginNotReady: 网络插件还没有完全启动
Evicte:     pod被驱赶

4、Failed:失败:容器内的返回码是非0状态退出,进入失败状态。

logs -f 可以查看pod的日志 describe pod 查看pod的详细情况,也可以查询到错误原因。

5、Terminating(终止中)

pod正在删除中。

6、Unknown:未知

集群出现问题了,API出现了问题,或者是API server和调度器之间通信有问题(证书过期)。

1.2、资源限制

docker cpu 100000

​ 内存 m g

k8s cpu 最小单位 100m 0.1 一个cpu的10%

​ 1000m 1 沾满一个cpu

​ 500m 0.5 占cpu的50%

​ 2 占2个cpu

​ 内存:单位 Ki Mi Gi Ti

1、cpu和内存做资源限制

[root@master01 k8s-yaml]# vim test1.yml#定义api接口的版本
apiVersion: v1
kind: Pod
metadata:name: nginx1labels:app: nginx1
spec:containers:- name: nginximage: nginx:1.22resources:requests:cpu: "0.5"memory: "512Mi"
#软限制,最低的要求,可以不做limits:cpu: "1"memory: "1Gi"
#硬策略,最多使用这么多[root@master01 k8s-yaml]# kubectl apply -f test1.yml 
pod/nginx1 created[root@master01 k8s-yaml]# kubectl describe pod nginx1 Limits:cpu:     1memory:  1GiRequests:cpu:        500mmemory:     512Mi

二、探针probe:(面试必问)

探针是对容器执行定期的检查。

2.1、启动探针

探针:启动探针,在容器启动时,根据条件判断容器是否成功。如果有启动探针和其他探针并列。只有启动探针执行完毕(成功),后续的探针才会执行,启动探针失败,整个容器判定为失败,pod也会进入失败状态。

在整个容器的生命周期当中,只有启动探针在启动时执行,执行成功之后,后续不再执行。

startupProbe

2.2、存活探针

存活探针:livenessProbe 探测容器是否正常运行(Running),如果探测失败,会根据pod的重启策略来决定是否重启。

将伴随整个pod的生命周期。

2.3、就绪探针

就绪探针:readinessProbe 探测pod的状态是否进入ready,如果进入ready状态失败,service将会把这个pod的ip从转发中移除。

service不会把请求转发到这个pod

nginx1 --3 node1 node2 node3

nginx1-pod 10.244.0.10

nginx1-pod2 10.244.0.11-------------service----------NodePort 192.168.168.81:30001

nginx1-pod3 10.244.0.12----------ready 0/1

就绪探针没有检测成功或者失败,pod可能是running,但是ready一定是0/1。

存活探针一般用于容器内的配置文件或者是关键组件是否正常。

就绪探针一般用于指定端口的服务,需要对外提供访问的业务。

这两个探针都会伴随整个pod的生命周期。

2.4、probe的检测方法:

1、exec方法:就是进入容器内,指定命令,命令的返回码是0就是成功,非0都是失败。

在容器内使用自定义命令来检测容器内的健康状况,判断关键配置文件是否存在,依赖环境是否完整等等。

2、tcpSocket方法:对容器的ip地址进行tcp检查(三次握手),和指定的端口进行连接,如果三次握手和端口通信建立连接正常。

则认为成功,判断容器的端口是否正常启动,端口是否处于监听状态。

3、httpGet方法:对容器内的ip+端口进行http请求,请求的方式是get。响应码大于等于200且小于400,都是成功。

200=<x<400,主要应用于web容器。

结果:

1、成功。

2、失败。定义了容器的重启策略,容器会进行重启。

3、未知。探针失败,但是不会采取任何行动。

2.5、lifecycle字段:

2.5、容器钩子:

1、postStart----启动钩子

postStart:容器启动时立即执行的命令,执行容器内需要执行的初始化命令,等待依赖环境。

2、preStop----停止钩子

preStop:停止之前执行的任务,清理任务,同步文件等等(导出容器内的数据)。

1、启动任务失败,pod能否进入正常状态

2、停止任务失败,pod能否退出

3、如果和探针一起,启动失败的任务影响探针嘛?

影响,优先级大于启动探针

容器的启动的钩子----->启动探针-------->存活和就绪探针

存活探针

[root@master01 k8s-yaml]# vim test2.yamlapiVersion: apps/v1
kind: Deployment
metadata:name: centoslabels:app: centos7
spec:replicas: 1selector:matchLabels:app: centos7template:metadata:labels:app: centos7spec:
#定义pod的容器参数containers:- name: centos7image: centos:7command: ["/bin/bash","-c","touch /opt/123.txt && sleep 3600"]livenessProbe:exec:command: ["/usr/bin/test","-e","/opt/123.txt"]initialDelaySeconds: 1
#initialDelaySeconds: 表示容器启动之后多少秒开始第一次探测,1是秒,要等待应用程
#序准备好之后再探测。以避免结果有误。periodSeconds: 3
#在pod的生命周期内,探针的检测时间间隔是3秒 ,也没有固定的范围,根据业务容器的>情况来看,比较敏感的检测时间,可以缩短时间间隔   
#在pod的生命周期内,探针的检测时间间隔是3秒,也没有固定的范围,根据业务容器的情
况来看,比较敏感的检测时间,可以缩短时间间隔failureThreshold: 2
#表示次数,表示探针检测容器失败几次就把容器标记为不健康。timeoutSeconds: 1
#timeoutSeconds的时间必须小于periodSeconds,表示探针在多少时间之内完成探测。 successThreshold: 1
#只要探针成功一次,就把容器标记为健康,这个值只能是1,默认也是1,可以不写。[root@master01 k8s-yaml]# kubectl apply -f test2.yaml 
deployment.apps/centos created
[root@master01 k8s-yaml]# kubectl get pod
[root@master01 k8s-yaml]# kubectl describe pod centos-6746885856-pvd88 Liveness:       exec [/usr/bin/test -e /opt/123.txt] delay=1s timeout=1s period=3s #success=1 #failure=2##删除文件,触发存活探针
[root@master01 k8s-yaml]# kubectl exec -it centos-6746885856-nvndp 
error: you must specify at least one command for the container
[root@master01 k8s-yaml]# kubectl exec -it centos-6746885856-nvndp bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
[root@centos-6746885856-nvndp /]# cd /opt/
[root@centos-6746885856-nvndp opt]# ls
123.txt
[root@centos-6746885856-nvndp opt]# rm -rf *
[root@centos-6746885856-nvndp opt]# exit
exit
[root@master01 k8s-yaml]# kubectl describe pod centos-6746885856-nvndp  Events:Type     Reason     Age              From               Message----     ------     ----             ----               -------Normal   Scheduled  71s              default-scheduler  Successfully assigned default/centos-6746885856-nvndp to node02Normal   Pulled     70s              kubelet            Container image "centos:7" already present on machineNormal   Created    70s              kubelet            Created container centos7Normal   Started    70s              kubelet            Started container centos7Warning  Unhealthy  1s (x2 over 4s)  kubelet            Liveness probe failed:Normal   Killing    1s               kubelet            Container centos7 failed liveness probe, will be restarted

探针的优化(面试)

  initialDelaySeconds: 1
#initialDelaySeconds: 表示容器启动之后多少秒开始第一次探测,1是秒,要等待应用程
#序准备好之后再探测。以避免结果有误。periodSeconds: 3
#在pod的生命周期内,探针的检测时间间隔是3秒 ,也没有固定的范围,根据业务容器的>情况来看,比较敏感的检测时间,可以缩短时间间隔   
#在pod的生命周期内,探针的检测时间间隔是3秒,也没有固定的范围,根据业务容器的情
况来看,比较敏感的检测时间,可以缩短时间间隔failureThreshold: 2
#表示次数,表示探针检测容器失败几次就把容器标记为不健康。timeoutSeconds: 1
#timeoutSeconds的时间必须小于periodSeconds,表示探针在多少时间之内完成探测。 successThreshold: 1
#只要探针成功一次,就把容器标记为健康,这个值只能是1,默认也是1,可以不写。

存活探针不设置参数

[root@master01 k8s-yaml]# vim test3.yaml apiVersion: apps/v1
kind: Deployment
metadata:name: centoslabels:app: centos7
spec:replicas: 1selector:matchLabels:app: centos7template:metadata:labels:app: centos7spec:
#定义pod的容器参数containers:- name: centos7image: centos:7command: ["/bin/bash","-c","touch /opt/123.txt && sleep 3600"]livenessProbe:exec:command: ["/usr/bin/test","-e","/opt/123.txt"]#      initialDelaySeconds: 1
#initialDelaySeconds: 表示容器启动之后多少秒开始第一次探测,1是秒,要等待应用程
#序准备好之后再探测。以避免结果有误。#       periodSeconds: 3
#在pod的生命周期内,探针的检测时间间隔是3秒 ,也没有固定的范围,根据业务容器的>情况来看,比较敏感的检测时间,可以缩短时间间隔   
#在pod的生命周期内,探针的检测时间间隔是3秒,也没有固定的范围,根据业务容器的情
况来看,比较敏感的检测时间,可以缩短时间间隔#        failureThreshold: 2
#表示次数,表示探针检测容器失败几次就把容器标记为不健康。#         timeoutSeconds: 1
#timeoutSeconds的时间必须小于periodSeconds,表示探针在多少时间之内完成探测。 
#          successThreshold: 1
#只要探针成功一次,就把容器标记为健康,这个值只能是1,默认也是1,可以不写。[root@master01 k8s-yaml]# kubectl get pod
[root@master01 k8s-yaml]# kubectl describe pod centos-596c98dd98-wwfbl Liveness:       exec [/usr/bin/test -e /opt/123.txt] delay=0s timeout=1s period=10s #success=1 #failure=3

存活探针检测方法为tcp-80

[root@master01 k8s-yaml]# vim test3.yaml apiVersion: apps/v1
kind: Deployment
metadata:name: nginxlabels:app: centos7
spec:replicas: 1selector:matchLabels:app: centos7template:metadata:labels:app: centos7spec:
#定义pod的容器参数containers:- name: nginximage: nginx:1.22livenessProbe:tcpSocket:port: 80
[root@master01 k8s-yaml]# kubectl apply -f test3.yaml 
deployment.apps/nginx created
[root@master01 k8s-yaml]# kubectl get pod
[root@master01 k8s-yaml]# kubectl describe pod nginx-585c6b6f4b-kfrjv Liveness:       tcp-socket :80 delay=0s timeout=1s period=10s #success=1 #failure=3

存活探针检测方法为tcp-81

[root@master01 k8s-yaml]# vim test3.yaml apiVersion: apps/v1
kind: Deployment
metadata:name: nginxlabels:app: centos7
spec:replicas: 1selector:matchLabels:app: centos7template:metadata:labels:app: centos7spec:
#定义pod的容器参数containers:- name: nginximage: nginx:1.22livenessProbe:tcpSocket:port: 81[root@master01 k8s-yaml]# kubectl get pod -o wide
nginx-654cfc659-nlkzh     0/1     CrashLoopBackOff   4          2m28s   10.244.2.91   node02   <none> [root@master01 k8s-yaml]# kubectl describe pod nginx-654cfc659-nlkzh 

存活探针检测方法为http-81

[root@master01 k8s-yaml]# vim test3.yaml apiVersion: apps/v1
kind: Deployment
metadata:name: nginxlabels:app: centos7
spec:replicas: 1selector:matchLabels:app: centos7template:metadata:labels:app: centos7spec:
#定义pod的容器参数containers:- name: nginximage: nginx:1.22livenessProbe:httpGet:port: 80scheme: HTTPpath: /index.html[root@master01 k8s-yaml]# kubectl apply -f test3.yaml 
[root@master01 k8s-yaml]# kubectl get pod -o wide
[root@master01 k8s-yaml]# kubectl describe pod nginx-5859bfdf9f-xbqnn Liveness:       http-get http://:80/index.html delay=0s timeout=1s period=10s #success=1 #failure=3

启动探针检测方法为http-80+就绪探针–exec指定命令检测文件是否存在

[root@master01 k8s-yaml]# vim test3.yaml apiVersion: apps/v1
kind: Deployment
metadata:name: nginxlabels:app: centos7
spec:replicas: 1selector:matchLabels:app: centos7template:metadata:labels:app: centos7spec:
#定义pod的容器参数containers:- name: nginximage: nginx:1.22livenessProbe:startupProbe:httpGet:port: 80scheme: HTTPpath: /index.htmlreadinessProbe:exec: command: ["/usr/bin/test","-e","/etc/passwd"][root@master01 k8s-yaml]# kubectl apply -f test3.yaml 
[root@master01 k8s-yaml]# kubectl get pod -o wide
[root@master01 k8s-yaml]# kubectl describe pod nginx-76f8b6d4f7-xt4mp#scheme:调用的协议(http)
#path:path: /index.html curl 192.168.168.81    
#      initialDelaySeconds: 1
#initialDelaySeconds: 表示容器启动之后多少秒开始第一次探测,1是秒,要等待应用程
#序准备好之后再探测。以避免结果有误。#       periodSeconds: 3
#在pod的生命周期内,探针的检测时间间隔是3秒 ,也没有固定的范围,根据业务容器的>情况来看,比较敏感的检测时间,可以缩短时间间隔   
#在pod的生命周期内,探针的检测时间间隔是3秒,也没有固定的范围,根据业务容器的情
况来看,比较敏感的检测时间,可以缩短时间间隔#        failureThreshold: 2
#表示次数,表示探针检测容器失败几次就把容器标记为不健康。#         timeoutSeconds: 1
#timeoutSeconds的时间必须小于periodSeconds,表示探针在多少时间之内完成探测。 
#          successThreshold: 1
#只要探针成功一次,就把容器标记为健康,这个值只能是1,默认也是1,可以不写。
[root@master01 k8s-yaml]# vim test3.yaml apiVersion: apps/v1
kind: Deployment
metadata:name: nginxlabels:app: centos7
spec:replicas: 1selector:matchLabels:app: centos7template:metadata:labels:app: centos7spec:
#定义pod的容器参数containers:- name: nginximage: nginx:1.22livenessProbe:startupProbe:httpGet:port: 80scheme: HTTPpath: /index.htmlreadinessProbe:exec:command: ["/usr/bin/test","-e","/etc/passwd"]
---
#表示分段,上一个yml结束,下一个新的yml
apiVersion: v1
kind: Service
metadata:name: nginx-1
# namespacelabels:app: nginx1
spec:type: NodePortports:- port: 80targetPort: 80nodePort: 30000selector:app: centos7[root@master01 k8s-yaml]# kubectl apply -f test3.yaml [root@master01 k8s-yaml]# kubectl get pod 
NAME                     READY   STATUS    RESTARTS   AGE
nfs1-76f66b958-68wpl     1/1     Running   0          12h
nginx-76f8b6d4f7-hhvdq   1/1     Running   0          41s
[root@master01 k8s-yaml]# kubectl exec -it nginx-76f8b6d4f7-hhvdq bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-76f8b6d4f7-hhvdq:/# cd /usr/share/nginx/html/
root@nginx-76f8b6d4f7-hhvdq:/usr/share/nginx/html# ls
50x.html  index.html
root@nginx-76f8b6d4f7-hhvdq:/usr/share/nginx/html# cat index.html 
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@nginx-76f8b6d4f7-hhvdq:/usr/share/nginx/html# echo 123456 > index.html 
root@nginx-76f8b6d4f7-hhvdq:/usr/share/nginx/html# cat index.html 
123456
root@nginx-76f8b6d4f7-hhvdq:/usr/share/nginx/html# exit
exit
[root@master01 k8s-yaml]# curl 192.168.168.81
curl: (7) Failed connect to 192.168.168.81:80; 拒绝连接
[root@master01 k8s-yaml]# curl 192.168.168.81:30000
123456[root@master01 k8s-yaml]# vim test3.yaml 
[root@master01 k8s-yaml]# kubectl exec -it nginx-76f8b6d4f7-hhvdq bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-76f8b6d4f7-hhvdq:/# rm -rf /etc/passwd
root@nginx-76f8b6d4f7-hhvdq:/# exit
exit
[root@master01 k8s-yaml]# kubectl get pod 
NAME                     READY   STATUS    RESTARTS   AGE
nfs1-76f66b958-68wpl     1/1     Running   0          12h
nginx-76f8b6d4f7-hhvdq   1/1     Running   0          5m48s
[root@master01 k8s-yaml]# kubectl get pod 
NAME                     READY   STATUS    RESTARTS   AGE
nfs1-76f66b958-68wpl     1/1     Running   0          12h
nginx-76f8b6d4f7-hhvdq   0/1     Running   0          8m18s就绪探针生效,文件丢失,ready变化

启动钩子—指定命令检测文件不存在,导致后续容器启动失败

[root@master01 k8s-yaml]# vim test3.yaml apiVersion: apps/v1
kind: Deployment
metadata:name: nginxlabels:app: centos7
spec:replicas: 1selector:matchLabels:app: centos7template:metadata:labels:app: centos7spec:
#定义pod的容器参数containers:- name: nginximage: nginx:1.22livenessProbe:startupProbe:httpGet:port: 80scheme: HTTPpath: /index.htmlreadinessProbe:exec:command: ["/usr/bin/test","-e","/etc/passwd"]lifecycle:postStart:exec:command: ["/bin/bash","-c","cat /opt/123.txt"]
---
#表示分段,上一个yml结束,下一个新的yml
apiVersion: v1
kind: Service
metadata:name: nginx-1
# namespacelabels:app: nginx1
spec:type: NodePortports:- port: 80targetPort: 80[root@master01 k8s-yaml]# kubectl apply -f test3.yaml --force
[root@master01 k8s-yaml]# kubectl get pod 
NAME                    READY   STATUS    RESTARTS   AGE
nfs1-76f66b958-68wpl    1/1     Running   0          12h
nginx-df44cb667-kt4xj   0/1     PostStartHookError: command '/bin/bash -c cat /opt/123.txt' exited with 1: cat: /opt/123.txt: No such file or directory1       3s[root@master01 k8s-yaml]# kubectl describe pod nginx-df44cb667-p4vspWarning  FailedPostStartHook  4m49s (x4 over 5m33s)  kubelet            Exec lifecycle hook ([/bin/bash -c cat /opt/123.txt]) for Container "nginx" in Pod "nginx-df44cb667-p4vsp_default(8ee9249a-e118-4e25-8b10-cb53e8cc189f)" failed - error: command '/bin/bash -c cat /opt/123.txt' exited with 1: cat: /opt/123.txt: No such file or directory
, message: "cat: /opt/123.txt: No such file or directory\n"Normal   Killing  4m49s (x4 over 5m33s)  kubelet  FailedPostStartHookWarning  BackOff  25s (x27 over 5m31s)   kubelet  Back-off restarting failed container

lifecycle字段:

容器钩子:

postStart:容器启动时立即执行的命令,执行容器内需要执行的初始化命令,等待依赖环境。

preStop:停止之前执行的任务,清理任务,同步文件等等(导出容器内的数据)。

1、启动任务失败,pod能否进入正常状态

2、停止任务失败,pod能否退出

3、如果和探针一起,启动失败的任务影响探针嘛?

影响,优先级大于启动探针

容器的启动的钩子----->启动探针-------->存活和就绪探针

4、容器的钩子,不论是启动还是停止之前的命令,只能使用exec。

容器的钩子,不论是启动还是停止之前的命令,只能使用exec。

[root@master01 k8s-yaml]# vim test3.yaml apiVersion: apps/v1
kind: Deployment
metadata:name: nginxlabels:app: centos7
spec:replicas: 3selector:matchLabels:app: centos7template:metadata:labels:app: centos7spec:
#定义pod的容器参数containers:- name: nginximage: nginx:1.22livenessProbe:startupProbe:httpGet:port: 81scheme: HTTPpath: /index.htmlreadinessProbe:exec:command: ["/usr/bin/test","-e","/etc/passwd"]lifecycle:postStart:tcpSocket:port: 80---
#表示分段,上一个yml结束,下一个新的yml
apiVersion: v1
kind: Service
metadata:name: nginx-1
# namespacelabels:app: nginx1
spec:type: NodePortports:- port: 80targetPort: 80
[root@master01 k8s-yaml]# kubectl apply -f test3.yaml 
[root@master01 k8s-yaml]# kubectl get pod 
NAME                     READY   STATUS             RESTARTS   AGE
nfs1-76f66b958-68wpl     1/1     Running            0          13h
nginx-56f444c575-qq29d   0/1     CrashLoopBackOff   5          7m17s
[root@master01 k8s-yaml]# kubectl describe pod nginx-56f444c575-qq29d Warning  FailedPostStartHook  4m6s (x4 over 6m21s)  kubelet            Cannot run handler: invalid handler: &Handler{Exec:nil,HTTPGet:nil,TCPSocket:&TCPSocketAction{Port:{0 80 },Host:,},}Normal   Killing              4m6s (x4 over 6m21s)  kubelet            FailedPostStartHookWarning  BackOff              66s (x17 over 5m19s)  kubelet            Back-off restarting failed container容器的钩子,不论是启动还是停止之前的命令,只能使用exec。

容器的钩子,不论是启动还是停止之前的命令,只能使用exec。

[root@master01 k8s-yaml]# vim test3.yaml apiVersion: apps/v1
kind: Deployment
metadata:name: nginxlabels:app: centos7
spec:replicas: 1selector:matchLabels:app: centos7template:metadata:labels:app: centos7spec:
#定义pod的容器参数containers:- name: nginximage: nginx:1.22livenessProbe:startupProbe:httpGet:port: 81scheme: HTTPpath: /index.htmlreadinessProbe:exec:command: ["/usr/bin/test","-e","/etc/passwd"]lifecycle:postStart:httpGet:port: 80scheme: HTTPpath: /index.html
---
#表示分段,上一个yml结束,下一个新的yml
apiVersion: v1
kind: Service
metadata:name: nginx-1
# namespacelabels:app: nginx1
spec:type: NodePortports:- port: 80targetPort: 80[root@master01 k8s-yaml]# kubectl apply -f test3.yaml 
deployment.apps/nginx created
service/nginx-1 unchanged
[root@master01 k8s-yaml]# kubectl get pod 
NAME                    READY   STATUS                                                                                                                RESTARTS   AGE
nfs1-76f66b958-68wpl    1/1     Running                                                                                                               0          13h
nginx-cbf59dc9f-s4tx6   0/1     PostStartHookError: Get "http://10.244.2.213:80//index.html": dial tcp 10.244.2.213:80: connect: connection refused   0          2s[root@master01 k8s-yaml]# kubectl describe pod nginx-cbf59dc9f-s4tx6 Warning  FailedPostStartHook  24s (x3 over 38s)  kubelet            Http lifecycle hook (/index.html) for Container "nginx" in Pod "nginx-cbf59dc9f-s4tx6_default(ab801d25-2c1f-4ede-a9ac-351d843b8067)" failed - error: Get "http://10.244.2.213:80//index.html": dial tcp 10.244.2.213:80: connect: connection refused, message: ""容器的钩子,不论是启动还是停止之前的命令,只能使用exec。

启动及停止exec指定命令写入文件

[root@master01 k8s-yaml]# vim test4.yamlapiVersion: v1
kind: Pod
metadata:name: centos1labels:app: centos1
spec:containers:- name: centosimage: centos:7command: ["/bin/bash","-c","sleep 30"]lifecycle:postStart:exec:command: ["/bin/bash","-c","echo start > /opt/123.txt && sleep 10"]preStop:exec:command: ["/bin/bash","-c","echo stop > /opt/123.txt && sleep 10"]

启动及停止exec指定命令写入文件,通过挂载目录查看

[root@master01 k8s-yaml]# vim test4.yamlapiVersion: v1
kind: Pod
metadata:name: centos1labels:app: centos1
spec:containers:- name: centosimage: centos:7command: ["/bin/bash","-c","sleep 30"]volumeMounts:- name: data-vmountPath: /opt/test1
#容器内的目录,挂载卷的名称lifecycle:postStart:exec:command: ["/bin/bash","-c","echo start >> /opt/test1/123.txt && sleep 10"]preStop:exec:command: ["/bin/bash","-c","echo stop >> /opt/test1/456.txt && sleep 10"]volumes:- name: data-vhostPath:path: /opt/testtype: DirectoryOrCreate[root@master01 k8s-yaml]# kubectl apply -f test4.yaml --force
pod/centos1 configured
[root@master01 k8s-yaml]# kubectl exec -it centos1 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
[root@centos1 /]# cd /opt/test1/
[root@centos1 test1]# ls
123.txt  456.txt
[root@centos1 test1]# command terminated with exit code 137
[root@master01 k8s-yaml]# kubectl exec -it centos1 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
[root@centos1 /]# cd /opt/test1/
[root@centos1 test1]# ls
123.txt  456.txt
[root@centos1 test1]# cat 456.txt 
stop
[root@centos1 test1]# cat 123.txt 
start
启动及停止exec指定命令写入文件
[root@master01 k8s-yaml]# vim test4.yamlapiVersion: v1
kind: Pod
metadata:name: centos1labels:app: centos1
spec:containers:- name: centosimage: centos:7command: ["/bin/bash","-c","sleep 30"]volumeMounts:- name: data-vmountPath: /opt/test1
#容器内的目录,挂载卷的名称lifecycle:postStart:exec:command: ["/bin/bash","-c","echo start >> /opt/test1/123.txt && sleep 10"]preStop:exec:command: ["/bin/bash","-c","echo stop >> /opt/test1/321.txt && sleep 10"]volumes:- name: data-vhostPath:path: /opt/testtype: DirectoryOrCreate[root@master01 k8s-yaml]# kubectl apply -f test4.yaml 
pod/centos1 created
[root@master01 k8s-yaml]# kubectl exec -it centos1 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
[root@centos1 /]# cd /opt/test1/
[root@centos1 test1]# ls
123.txt
[root@centos1 test1]# command terminated with exit code 137
[root@master01 k8s-yaml]# kubectl delete pod centos1
pod "centos1" deleted[root@master01 k8s-yaml]# kubectl get pod -o wide
centos1                   0/1     ContainerCreating   0          2s      <none>         node02   <none>           <none>[root@node02 test]# cat 321.txt 
stop

探针三种:

启动 : 启动执行完毕之后,后续不再执行。

存活 :

就绪

存活和就绪会伴随整个pod的生命周期

三种方法:

exec

tcpSocket

httpGet

作业:

启动钩子和退出钩子

和节点挂载:/usr/share/nginx/html 节点: /opt/node

exec执行 要能在目录中看到开始和打印的结果

包含探针:

1、启动探针:

方法:exec 检测 /usr/share/nginx/html/index.html 文件是否存在

2、存活探针

方法:httpGET

访问验证返回码是否正确

3、就绪探针

tcpSocket

监听容器的80端口是否正常

启动探针、存活探针、就绪探针、启动及停止钩子通过exec指定命令写入文件
[root@master01 k8s-yaml]# vim test5.yamlapiVersion: v1
kind: Pod
metadata:name: nginx1labels:app: nginx1
spec:containers:- name: nignx1image: nginx:1.22volumeMounts:- name: data-vmountPath: /usr/share/nginx/html
#设置容器钩子lifecycle:
#设置启动钩子postStart:exec:command: ["/bin/bash","-c","echo start >> /usr/share/nginx/html/index.html"]
#设置停止钩子preStop:exec:command: ["/bin/bash","-c","echo stop >> /usr/share/nginx/html/error.html"]startupProbe:exec:command: ["/usr/bin/test","-e","/usr/share/nginx/html/index.html"]livenessProbe:httpGet:port: 80scheme: HTTPpath: /index.htmlreadinessProbe:tcpSocket:port: 80volumes:- name: data-vhostPath:path: /opt/nodetype: DirectoryOrCreate[root@master01 k8s-yaml]# kubectl apply -f test5.yaml [root@master01 k8s-yaml]# kubectl get pod -o wide
NAME     READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
nginx1   1/1     Running   0          51s   10.244.1.66   node01   <none>           <none>[root@master01 k8s-yaml]# kubectl describe pod nginx1 Liveness:       http-get http://:80/index.html delay=0s timeout=1s period=10s #success=1 #failure=3Readiness:      tcp-socket :80 delay=0s timeout=1s period=10s #success=1 #failure=3Startup:        exec [/usr/bin/test -e /usr/share/nginx/html/index.html] delay=0s timeout=1s period=10s #success=1 #failure=3Events:Type    Reason     Age   From               Message----    ------     ----  ----               -------Normal  Scheduled  63s   default-scheduler  Successfully assigned default/nginx1 to node01Normal  Pulled     63s   kubelet            Container image "nginx:1.22" already present on machineNormal  Created    63s   kubelet            Created container nignx1Normal  Started    63s   kubelet            Started container nignx1[root@node01 node]# cat /opt/node/index.html 
start[root@master01 k8s-yaml]# kubectl delete pod nginx1 
pod "nginx1" deleted[root@node01 node]# cat error.html 
stop

相关文章:

85、 探针

一、pod的进阶 pod的进阶&#xff1a; 1.1、pod的生命周期当中的状态&#xff1a; 1、Running运行中&#xff0c;pod已经分配到节点上且pod内的容器正常运行。正常状态&#xff08;ready 1/1&#xff09;。 2、complete&#xff1a;完成之后退出&#xff0c;容器内的返回码…...

2024全国大学省数学建模竞赛A题-原创参考论文(部分+第一问代码)

一问题重述 1.1 问题背景 "板凳龙"&#xff0c;又称"盘龙"&#xff0c;是浙闽地区的传统地方民俗文化活动。这种独特的表演艺术形式融合了中国传统龙舞的精髓和地方特色&#xff0c;展现了人们对美好生活的向往和对传统文化的传承。 在板凳龙表演中&am…...

在VScode上写网页(html)

一、首先点进VScode&#xff0c;下载3个插件。 VScode安装&#xff1a;VScode 教程 | 菜鸟教程 二、新建 HTML 文件 作者运行的代码来自&#xff1a;http://t.csdnimg.cn/vIAQi 把代码复制粘贴进去&#xff0c;然后点击文件→另存为→选择html格式。 三、运行代码...

C#中LINQ的Cast<T>与OfType<T>

在C#中&#xff0c;Cast() 方法是LINQ&#xff08;Language Integrated Query&#xff09;的一部分&#xff0c;它位于 System.Linq 命名空间中。这个方法用于将 IEnumerable 集合&#xff08;或任何实现了 IEnumerable 接口的集合&#xff09;的元素转换为指定类型 T 的集合。…...

小阿轩yx-Kubernertes日志收集

小阿轩yx-Kubernertes日志收集 前言 在 Kubernetes 集群中如何通过不同的技术栈收集容器的日志&#xff0c;包括程序直接输出到控制台日志、自定义文件日志等 有哪些日志需要收集 日志收集与分析很重要&#xff0c;为了更加方便的处理异常 简单总结一些比较重要的需要收集…...

0to1使用Redis实现“登录验证”次数限制

1 引言 系统为了避免密码遭到暴力破解&#xff0c;通常情况下需要在登录时&#xff0c;限制用户验证账号密码的次数&#xff0c;当达到一定的验证次数后&#xff0c;在一段时间内锁定该账号&#xff0c;不再验证。本章将用几行代码实现该功能&#xff0c;完整代码链接在文章最…...

ARM----时钟

时钟频率可以是由晶振提供的&#xff0c;我们需要高频率&#xff0c;但是外部接高的晶振会不稳定&#xff0c;所有使用PLL&#xff08;锁相环&#xff09;来放大频率。接下来就让我们学习用外部晶振提供的频率来配置时钟频率。 一.时钟源的选择 在这里我们选择外部晶振作为时钟…...

NISP 一级 —— 考证笔记合集

该笔记为导航目录&#xff0c;在接下来一段事件内&#xff0c;我会每天发布我关于考取该证书的相关笔记。 当更新完成后&#xff0c;此条注释会被删除。 第一章 信息安全概述 1.1 信息与信息安全1.2 信息安全威胁1.3 信息安全发展阶段与形式1.4 信息安全保障1.5 信息系统安全保…...

C++三位状态比较排序

数组相同元素个数及按序 void 交换3个数升(int& A, int& B, int& C, bool& k) {int J 0;if (B > A&&A > C)J C, C B, B A, A J, k true;//231else if (C > A&&A > B)J A, A B, B J, k true;//213else if (A > B&a…...

麒麟系统安装GPU驱动

1.nvidia 1.1显卡驱动 本机显卡型号:nvidia rtx 3090 1.1.1下载驱动 打开 https://www.nvidia.cn/geforce/drivers/ 也可以直接使用下面这个地址下载 https://www.nvidia.com/download/driverResults.aspx/205464/en-us/ 1.1.3安装驱动 右击&#xff0c;为run文件添加可…...

IDEA 安装lombok插件不兼容的问题及解决方法

解决&#xff1a;IDEA 安装lombok插件不兼容问题&#xff0c;plugin xxxx is incompatible 一、去官网下载最新的2024版本 地址传送通道&#xff1a; lombok插件官网地址https://plugins.jetbrains.com/plugin/6317-lombok/versions/stable 二、修改参数的配置 在压缩包路径…...

聊聊说话的习惯

1 在日常生活中&#xff0c;每个人都有固定的说话习惯。心理学研究表明&#xff0c;通过一个人的说话习惯,也可以分析出他的性格特点。对于每一个人来讲&#xff0c;说话习惯已经融为他们生活中的一部分。在社交活动中&#xff0c;一些不良的说话习惯很可能会给他们带来麻烦。…...

当水泵遇上物联网:智能水务新时代的浪漫交响

在当代科技的宏伟乐章中&#xff0c;物联网&#xff08;IoT&#xff09;技术宛如一位技艺高超的指挥家&#xff0c;引领着各行各业迈向智能化的新纪元。当这股创新浪潮涌向古老的水务行业时&#xff0c;一场前所未有的“智能水务”革命便悄然上演&#xff0c;而水泵——这一传统…...

【Canvas与钟表】干支表盘

【成图】 【代码】 <!DOCTYPE html> <html lang"utf-8"> <meta http-equiv"Content-Type" content"text/html; charsetutf-8"/> <head><title>387.干支表盘</title><style type"text/css">…...

分布式项目中使用雪花算法提前获取对象主键ID

hello&#xff0c;大家好&#xff0c;我是灰小猿&#xff01; 在做分布式项目开发进行数据表结构设计时&#xff0c;有时候为了提高查询性能&#xff0c;在进行数据库表设计时&#xff0c;会使用自增ID来代替UUID作为数据的主键ID&#xff0c;但是这样就会有一个问题&#xff…...

小程序多个set-cookie无法处理

1、情景&#xff1a; 项目中遇到一个问题&#xff0c;客户的服务器上了华为云的防火墙&#xff0c;导致小程序请求头中携带了3个set- cookie&#xff08;有两个是华为云给自动加的&#xff09;&#xff0c;而小程序端不知道用哪个来 处理&#xff0c;结果选了个错误的进行处理…...

Mybatis【分页插件,缓存,一级缓存,二级缓存,常见缓存面试题】

文章目录 MyBatis缓存分页延迟加载和立即加载什么是立即加载&#xff1f;什么是延迟加载&#xff1f;延迟加载/懒加载的配置 缓存什么是缓存&#xff1f;缓存的术语什么是MyBatis 缓存&#xff1f;缓存的适用性缓存的分类一级缓存引入案例一级缓存的配置一级缓存的工作流程一级…...

【Qt开发】QT6.5.3安装方法(使用国内源)亲测可行!!!

目录 &#x1f315;下载在线安装包&#x1f315; 把安装包放到系统盘&#x1f315;开始安装&#x1f315;参考文章 &#x1f315;下载在线安装包 https://mirrors.nju.edu.cn/qt/official_releases/online_installers/ &#x1f315; 把安装包放到系统盘 我的系统盘是G盘&…...

springblade-JWT认证缺陷漏洞CVE-2021-44910

漏洞成因 SpringBlade前端通过webpack打包发布的&#xff0c;可以从其中找到app.js获取大量接口 然后直接访问接口&#xff1a;api/blade-log/api/list 直接搜索“请求未授权”&#xff0c;定位到认证文件&#xff1a;springblade/gateway/filter/AuthFilter.java 后面的代码…...

Chapter 12 Vue CLI脚手架组件化开发

欢迎大家订阅【Vue2Vue3】入门到实践 专栏&#xff0c;开启你的 Vue 学习之旅&#xff01; 文章目录 前言一、项目目录结构二、组件化开发1. 组件化2. Vue 组件的基本结构3. 依赖包less & less-loader 前言 组件化开发是Vue.js的核心理念之一&#xff0c;Vue CLI为开发者提…...

MPNet:旋转机械轻量化故障诊断模型详解python代码复现

目录 一、问题背景与挑战 二、MPNet核心架构 2.1 多分支特征融合模块(MBFM) 2.2 残差注意力金字塔模块(RAPM) 2.2.1 空间金字塔注意力(SPA) 2.2.2 金字塔残差块(PRBlock) 2.3 分类器设计 三、关键技术突破 3.1 多尺度特征融合 3.2 轻量化设计策略 3.3 抗噪声…...

Vue记事本应用实现教程

文章目录 1. 项目介绍2. 开发环境准备3. 设计应用界面4. 创建Vue实例和数据模型5. 实现记事本功能5.1 添加新记事项5.2 删除记事项5.3 清空所有记事 6. 添加样式7. 功能扩展&#xff1a;显示创建时间8. 功能扩展&#xff1a;记事项搜索9. 完整代码10. Vue知识点解析10.1 数据绑…...

Python:操作 Excel 折叠

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 Python 操作 Excel 系列 读取单元格数据按行写入设置行高和列宽自动调整行高和列宽水平…...

Java-41 深入浅出 Spring - 声明式事务的支持 事务配置 XML模式 XML+注解模式

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持续更新中&#xff01;&#xff08;长期更新&#xff09; 目前2025年06月05日更新到&#xff1a; AI炼丹日志-28 - Aud…...

学习STC51单片机32(芯片为STC89C52RCRC)OLED显示屏2

每日一言 今天的每一份坚持&#xff0c;都是在为未来积攒底气。 案例&#xff1a;OLED显示一个A 这边观察到一个点&#xff0c;怎么雪花了就是都是乱七八糟的占满了屏幕。。 解释 &#xff1a; 如果代码里信号切换太快&#xff08;比如 SDA 刚变&#xff0c;SCL 立刻变&#…...

2023赣州旅游投资集团

单选题 1.“不登高山&#xff0c;不知天之高也&#xff1b;不临深溪&#xff0c;不知地之厚也。”这句话说明_____。 A、人的意识具有创造性 B、人的认识是独立于实践之外的 C、实践在认识过程中具有决定作用 D、人的一切知识都是从直接经验中获得的 参考答案: C 本题解…...

佰力博科技与您探讨热释电测量的几种方法

热释电的测量主要涉及热释电系数的测定&#xff0c;这是表征热释电材料性能的重要参数。热释电系数的测量方法主要包括静态法、动态法和积分电荷法。其中&#xff0c;积分电荷法最为常用&#xff0c;其原理是通过测量在电容器上积累的热释电电荷&#xff0c;从而确定热释电系数…...

Java毕业设计:WML信息查询与后端信息发布系统开发

JAVAWML信息查询与后端信息发布系统实现 一、系统概述 本系统基于Java和WML(无线标记语言)技术开发&#xff0c;实现了移动设备上的信息查询与后端信息发布功能。系统采用B/S架构&#xff0c;服务器端使用Java Servlet处理请求&#xff0c;数据库采用MySQL存储信息&#xff0…...

LangChain知识库管理后端接口:数据库操作详解—— 构建本地知识库系统的基础《二》

这段 Python 代码是一个完整的 知识库数据库操作模块&#xff0c;用于对本地知识库系统中的知识库进行增删改查&#xff08;CRUD&#xff09;操作。它基于 SQLAlchemy ORM 框架 和一个自定义的装饰器 with_session 实现数据库会话管理。 &#x1f4d8; 一、整体功能概述 该模块…...

GitFlow 工作模式(详解)

今天再学项目的过程中遇到使用gitflow模式管理代码&#xff0c;因此进行学习并且发布关于gitflow的一些思考 Git与GitFlow模式 我们在写代码的时候通常会进行网上保存&#xff0c;无论是github还是gittee&#xff0c;都是一种基于git去保存代码的形式&#xff0c;这样保存代码…...