k8s 的pod进阶

容器探测的具体实现方法;三种探针类型

ExecAction、TCPSocketAction、HTTPGetAction

1
2
3
4
5
6
7
8
9
10
11
12
lifecycle    <Object>
  Actions that the management system should take in response to container
  lifecycle events. Cannot be updated.
 
livenessProbe    <Object>
  Periodic probe of container liveness. Container will be restarted if the
  probe fails. Cannot be updated. More info:
  https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
readinessProbe   <Object>
  Periodic probe of container service readiness. Container will be removed
  from service endpoints if the probe fails. Cannot be updated. More info:
  https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

  livenessProbe 介绍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[root@master manifests]# kubectl explain pods.spec.containers.livenessProbe
KIND:     Pod
VERSION:  v1
 
RESOURCE: livenessProbe <Object>
 
DESCRIPTION:
     Periodic probe of container liveness. Container will be restarted if the
     probe fails. Cannot be updated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
 
     Probe describes a health check to be performed against a container to
     determine whether it is alive or ready to receive traffic.
 
FIELDS:
   exec <Object>   探针
     One and only one of the following should be specified. Exec specifies the
     action to take.
 
   failureThreshold <integer>  失败次数
     Minimum consecutive failures for the probe to be considered failed after
     having succeeded. Defaults to 3. Minimum value is 1.
 
   httpGet  <Object>  http探针
     HTTPGet specifies the http request to perform.
 
   initialDelaySeconds  <integer>  启动后等待多长时间,开始探测
     Number of seconds after the container has started before liveness probes
     are initiated. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes
 
   periodSeconds    <integer>  每一次间隔时间;默认10
     How often (in seconds) to perform the probe. Default to 10 seconds. Minimum
     value is 1.
 
   successThreshold <integer>  成功次数
     Minimum consecutive successes for the probe to be considered successful
     after having failed. Defaults to 1. Must be 1 for liveness. Minimum value
     is 1.
 
   tcpSocket    <Object>   tcp探针
     TCPSocket specifies an action involving a TCP port. TCP hooks not yet
     supported
 
   timeoutSeconds   <integer>  每一超时时间;默认1
     Number of seconds after which the probe times out. Defaults to 1 second.
     Minimum value is 1. More info:
     https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle#container-probes

  exec探测方法介绍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@master manifests]# kubectl explain pods.spec.containers.livenessProbe.exec
KIND:     Pod
VERSION:  v1
 
RESOURCE: exec <Object>
 
DESCRIPTION:
     One and only one of the following should be specified. Exec specifies the
     action to take.
 
     ExecAction describes a "run in container" action.
 
FIELDS:
   command  <[]string>  #指定运行的命令,容器里必须支持
     Command is the command line to execute inside the container, the working
     directory for the command is root ('/') in the container's filesystem. The
     command is simply exec'd, it is not run inside a shell, so traditional
     shell instructions ('|', etc) won't work. To use a shell, you need to
     explicitly call out to that shell. Exit status of 0 is treated as
     live/healthy and non-zero is unhealthy.

  编写测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@master manifests]# cat chenxi-exec.yaml
apiVersion: v1
kind: Pod
metadata:
        name: liveness-exec-pod
        namespace: default
spec:
        containers:
        - name: liveness-exec-container
          image: busybox:latest
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh","-c","touch /tmp/chenxi; sleep 60; rm -rf /tmp/chenxi; sleep 3600"]
          livenessProbe:
             exec:
                command: ["test","-e","/tmp/chenxi"]
             initialDelaySeconds: 2
        restartPolicy: OnFailure

  启动这个pod

1
2
[root@master manifests]# kubectl create -f chenxi-exec.yaml
pod/liveness-exec-pod created

  测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@master manifests]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
liveness-exec-pod        1/1     Running   0          2m
myapp-84cd4b7f95-g6ldp   1/1     Running   3          10d
nginx-5896f46c8-zblcs    1/1     Running   3          10d
pod-demo                 2/2     Running   0          162m
[root@master manifests]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
liveness-exec-pod        1/1     Running   1          2m1s
myapp-84cd4b7f95-g6ldp   1/1     Running   3          10d
nginx-5896f46c8-zblcs    1/1     Running   3          10d
pod-demo                 2/2     Running   0          162m
[root@master manifests]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
liveness-exec-pod        1/1     Running   1          2m2s
myapp-84cd4b7f95-g6ldp   1/1     Running   3          10d
nginx-5896f46c8-zblcs    1/1     Running   3          10d
pod-demo                 2/2     Running   0          162m

  容器创建后,终止前操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@master manifests]# kubectl explain pods.spec.containers.lifecycle
KIND:     Pod
VERSION:  v1
 
RESOURCE: lifecycle <Object>
 
DESCRIPTION:
     Actions that the management system should take in response to container
     lifecycle events. Cannot be updated.
 
     Lifecycle describes actions that the management system should take in
     response to container lifecycle events. For the PostStart and PreStop
     lifecycle handlers, management of the container blocks until the action is
     complete, unless the container process fails, in which case the handler is
     aborted.
 
FIELDS:
   postStart    <Object>
     PostStart is called immediately after a container is created. If the
     handler fails, the container is terminated and restarted according to its
     restart policy. Other management of the container blocks until the hook
     completes. More info:
     https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
 
   preStop  <Object>
     PreStop is called immediately before a container is terminated due to an
     API request or management event such as liveness probe failure, preemption,
     resource contention, etc. The handler is not called if the container
     crashes or exits. The reason for termination is passed to the handler. The
     Pod's termination grace period countdown begins before the PreStop hooked
     is executed. Regardless of the outcome of the handler, the container will
     eventually terminate within the Pod's termination grace period. Other
     management of the container blocks until the hook completes or until the
     termination grace period is reached. More info:
     https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks

  创建后操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@master manifests]# kubectl explain pods.spec.containers.lifecycle.postStart
KIND:     Pod
VERSION:  v1
 
RESOURCE: postStart <Object>
 
DESCRIPTION:
     PostStart is called immediately after a container is created. If the
     handler fails, the container is terminated and restarted according to its
     restart policy. Other management of the container blocks until the hook
     completes. More info:
     https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
 
     Handler defines a specific action that should be taken
 
FIELDS:
   exec <Object>
     One and only one of the following should be specified. Exec specifies the
     action to take.
 
   httpGet  <Object>
     HTTPGet specifies the http request to perform.
 
   tcpSocket    <Object>
     TCPSocket specifies an action involving a TCP port. TCP hooks not yet
     supported

  停止前操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@master manifests]# kubectl explain pods.spec.containers.lifecycle.preStop
KIND:     Pod
VERSION:  v1
 
RESOURCE: preStop <Object>
 
DESCRIPTION:
     PreStop is called immediately before a container is terminated due to an
     API request or management event such as liveness probe failure, preemption,
     resource contention, etc. The handler is not called if the container
     crashes or exits. The reason for termination is passed to the handler. The
     Pod's termination grace period countdown begins before the PreStop hooked
     is executed. Regardless of the outcome of the handler, the container will
     eventually terminate within the Pod's termination grace period. Other
     management of the container blocks until the hook completes or until the
     termination grace period is reached. More info:
     https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks
 
     Handler defines a specific action that should be taken
 
FIELDS:
   exec <Object>
     One and only one of the following should be specified. Exec specifies the
     action to take.
 
   httpGet  <Object>
     HTTPGet specifies the http request to perform.
 
   tcpSocket    <Object>
     TCPSocket specifies an action involving a TCP port. TCP hooks not yet
     supported

  编写podyaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@master manifests]# cat pot.yaml
apiVersion: v1
kind: Pod
metadata:
        name: poststart-pod
        namespace: default
spec:
        containers:
        - name: busybox-httpd
          image: busybox:latest
          imagePullPolicy: IfNotPresent
          lifecycle:
            postStart:
              exec:
                command: ['/bin/sh','-c','echo Home_Page >> /tmp/index.html']
          #command: ['/bin/sh','-c','sleep 3600']
          command: ["/bin/httpd"]
          args: ["-f","-h /tmp"]

  

  

 

posted @   烟雨楼台,行云流水  阅读(494)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
点击右上角即可分享
微信分享提示