【深入pod】

在这里插入图片描述

1、Pod配置文件


apiVersion: v1  #api文档版本
kind: Pod  #资源对象类型,也可以配置为像Dep loyment、StatefulSet.这类的对象
metadata: #Pod用关的元城据,用于描述Pod的数据
  name: nginx-po  #Pod的名称
  labels:  #定义Pod的标签
    type: app  #自定义label标签,名字为type,值为app
    test: l.0.0  #自定义label标签,描述Pod版本写
  namespace: 'default'  #命名空间的配間
spec:  #期望Pod按照这里面的描述进行创建
  containers:  #对于Pod中的容器描述
  - name: nginx  #容器的名称
    image: nginx:1.20   #指定容器的镜像
    imagePullPolicy: IfNotPresent    #镜像拉取策略,指定如果本抛有就用本地的,I果没有就拉取远程的
    startupProbe:  #应用启动探针配置
      httpGet:   #探测方式,基于http请求探测
        path: /index.html #http请求路循
        port: 80  #请求端口
      failureThreshold: 3  #失败多少次才算真止失败
      periodSeconds: 10  #间隔时间
      successThreshold: 1  #多少次监测成功算成功
      timeoutSeconds: 5  #请求的超时时间
    command:  #指定容器眉动时执行的偷令
    - nginx
    - -g
    - 'daemon off;'    #nginx -g 'daemon off;'
    workingDir: /usr/share/nginx/html    #定义容器启动后的工作目录
    ports:
    - name: http   #端口名称
      containerPort: 80    #描述容器内要暴路什么端口
      protocol: TCP    #描述该端口是基于哪种协议通信的
    env:  #坏境变量
    - name: JVM_OPTS   #坏境变量名
      value: '-Xms128m -Xmx128m'  #坏境变量的值
    resources:
      requests:   #最少需要多少资源
        cpu: 100m    #限制cpu最少使用0.1个核心
        memory: 128Mi  #限制内存最少使用128兆
      limits:    #最多可以用多少资源
        cpu: 200m    #限制cpu最多使用0.2个核心
        memory: 256Mi   #限削最多使用256兆
  restartPolicy: OnFailure  # 重启策略,只有失败的情况才会重启

2、Pod探针

2.1 探针的类型

2.1.1 StartupProbe 启动探针

k8s v1.16版本新增的深针,用于判新应用程序是否已经启动了。

当配置了startupProbe后,会先禁用其他探针,直到startupProbe成功后,其他探针才会继续。

作用:由于有时候不准确预估应用一定是多长时间启动成功,因此配置另外两种方式不方便配置初始化时长来检测。而配置了statupProbe后,只有在应用启动成功了,才会执行另外两种探针,可以更加方便的结合使用另外两种探针使用。

  • 这个启动探针需要排它性,这个如果还未成功,剩下两个探针是关闭状态
startupProbe:
  httpGet:
  path: /api/startup
  port: 80

2.1.2 LivenessProbe 重启探针

用于探测容器中的应用是否运行,如果深测失败,kubelet会根据配置的重启策路进行重启,若没有配置,默认就认为容器启动成功。不会执行重启策路。

  • 存在问题:多长时间去检测应用,如果设置10s,应用正好是11s启动,那么在第10s的时候,应用还没启起来,又会去重启pod。
livenessProbe:
  failureThreshold: 5
  httpGet:
    path: /health
    port: 8080
    scheme: HTTP
  initialDelaySeconds: 60
  penodSeconds: 10
  successThreshold: 1 
  timeoutSeconds: 2  

2.1.3 ReadinessProbe 就绪探针

用于探测容器内的程序是否健康,它的返回值如果返回success,那么就认为该容器已经完全启动,并且该容器是可以接收外部流量的。

  • 存在问题:间隔多长时间以后去检测,启动时间长,应用初始化时间长,我么你在启动的时候有没有必要去检测这个服务启动完?
readinessProbe:
  failureThreshold: 3 #错误次数
  httpGet:
    path: /ready  #路径
    port: 8181
    scheme: HTTP
  periodSeconds: 10 #间隔时间
  successThreshold: 1
  timeoutSeconds: 2  

2.2 探测方式

2.2.1 ExecAction 通过命令的方式检测

在容器内部执行一个命令,如果返回值为0,则任务容器是健康的。如果返回值不是0,则表明启动失败。(使用场景:查看文件的场景)

livenessProbe:
  exec:
    command:  # 执行什么命令
    - cat
    - /health

2.2.2 TCPSocketAction 通过tcp检测端口方式检测

通过tcp连接监测容器内端口是否开放,如果开放则证明该容器健康。(使用场景:nginx服务)

livenessProbe:
  tcpSocket:
    port: 80

2.2.3 HTTPGetAction 通过http请求方式检测

生产环境用的较多的方式,发送HTTP请求到容器内的应用程序,如果按口返回的状态码在200~400之间。则认为容器健康。(使用场景:Java应用)

livenessProbe:
  failureThreshold: 5
  httpGet:
    path: /health  # 请求路径
    port: 8080
    scheme: HTTP
    httpHeaders:  # 请求头
    - name: xxx
      value: xxx

2.3 参数配置

initialDelaySeconds: 60  # 初始化时间,只有在这个时间之后LivenessProbe 或者 ReadinessProbe才会执行。
timeoutSeconds: 2  # 超时时间,不管使用命令行、tcp或者http,超过这个时间就算失败。
periodSeconds: 5  # 监测间隔时间,当上一次执行失败后,间隔多久再进行一次执行。
successThreshold: 1  # 成功阈值:检查1次成功就表示成功
failureThreshold: 2  # 失败阈值:监测失败2次就表示失败

2.4 探针的使用应用

2.4.1 StartupProbe的使用

2.4.1.1 使用http请求的探测方式检测pod

使用上文pod配置文件夹中的配置,配置中包含了StartupProbe启动探针,使用http请求的方式检测。因为/index.html这个文件是存在的,故创建pod是可以正常启动。

在这里插入图片描述

[root@k8s-master ~]# kubectl create  -f nginx-po.yml
pod/nginx-po created
[root@k8s-master ~]# kubectl describe po  nginx-po
Name:             nginx-po
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s-node-02/10.10.10.113
Start Time:       Fri, 23 Feb 2024 00:09:33 +0800
Labels:           test=l.0.0
                  type=app
Annotations:      <none>
Status:           Running
IP:               10.2.1.13
IPs:
  IP:  10.2.1.13
Containers:
  nginx:
    Container ID:  docker://39d108ab9c2da1414af8b0f1bb288f38179d44ba791a76f86a5504827704b7de
    Image:         nginx:1.20
    Image ID:      docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
    Port:          80/TCP
    Host Port:     0/TCP
    Command:
      nginx
      -g
      daemon off;
    State:          Running
      Started:      Fri, 23 Feb 2024 00:09:34 +0800
    Ready:          True
    Restart Count:  0
    Limits:
      cpu:     200m
      memory:  256Mi
    Requests:
      cpu:     100m
      memory:  128Mi
      
   ## 启动的时候通过http请求的方式检测
    Startup:   http-get http://:80/index.html delay=0s timeout=5s period=10s #success=1 #failure=3
    Environment:
      JVM_OPTS:  -Xms128m -Xmx128m
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-9w9zb (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-9w9zb:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  21s   default-scheduler  Successfully assigned default/nginx-po to k8s-node-02
  Normal  Pulled     21s   kubelet            Container image "nginx:1.20" already present on machine
  Normal  Created    21s   kubelet            Created container nginx
  Normal  Started    21s   kubelet            Started container nginx

[root@k8s-master ~]# kubectl get pod
NAME       READY   STATUS    RESTARTS   AGE
nginx-po   1/1     Running   0          112s

现在把http请求的方式改一下,该一个不存在的接口,我们在检测下,看是什么情况,如下图

在这里插入图片描述

[root@k8s-master ~]# kubectl delete po nginx-po
pod "nginx-po" deleted

[root@k8s-master ~]# kubectl create -f  nginx-po.yml
pod/nginx-po created

[root@k8s-master ~]# kubectl describe po  nginx-po
Name:             nginx-po
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s-node-02/10.10.10.113
Start Time:       Fri, 23 Feb 2024 00:16:45 +0800
Labels:           test=l.0.0
                  type=app
Annotations:      <none>
Status:           Running
IP:               10.2.1.14
IPs:
  IP:  10.2.1.14
Containers:
  nginx:
    Container ID:  docker://6b233c24af075db71790eac3ae19ce179e42e93aba9397e93d3b76d7bb1f6be5
    Image:         nginx:1.20
    Image ID:      docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
    Port:          80/TCP
    Host Port:     0/TCP
    Command:
      nginx
      -g
      daemon off;
    State:          Running
      Started:      Fri, 23 Feb 2024 00:16:46 +0800
    Ready:          False
    Restart Count:  0
    Limits:
      cpu:     200m
      memory:  256Mi
    Requests:
      cpu:     100m
      memory:  128Mi

## 此处显示探针的探测方式
    Startup:   http-get http://:80/api/ delay=0s timeout=5s period=10s #success=1 #failure=3
    Environment:
      JVM_OPTS:  -Xms128m -Xmx128m
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-fm927 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  kube-api-access-fm927:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age   From               Message
  ----     ------     ----  ----               -------
  Normal   Scheduled  18s   default-scheduler  Successfully assigned default/nginx-po to k8s-node-02
  Normal   Pulled     19s   kubelet            Container image "nginx:1.20" already present on machine
  Normal   Created    19s   kubelet            Created container nginx
  Normal   Started    19s   kubelet            Started container nginx
  Warning  Unhealthy  9s    kubelet            Startup probe failed: HTTP probe failed with statuscode: 404

# 因为探测http请求的接口不存在,所以容器启动后,探针探测失败又重启了pod,所以就会出现如下情况: restart 的状态是3次
[root@k8s-master ~]# kubectl get po nginx-po
NAME       READY   STATUS    RESTARTS      AGE
nginx-po   0/1     Running   3 (13s ago)   103s


# 超过3次失败代表pod启动失败,如下可以看到完成,但是pod没有启动起来
[root@k8s-master ~]# kubectl get po nginx-po
NAME       READY   STATUS      RESTARTS   AGE
nginx-po   0/1     Completed   4          3m38s
2.4.1.2 使用tcp请求的探测方式检测pod

探测方式,改成tcpSocket方式,再来测试下

在这里插入图片描述

[root@k8s-master ~]# kubectl delete po nginx-po
pod "nginx-po" deleted

[root@k8s-master ~]# kubectl create -f nginx-po.yml
pod/nginx-po created

[root@k8s-master ~]# kubectl describe po  nginx-po.yml
Error from server (NotFound): pods "nginx-po.yml" not found
[root@k8s-master ~]# kubectl describe po  nginx-po
Name:             nginx-po
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s-node-02/10.10.10.113
Start Time:       Fri, 23 Feb 2024 00:25:01 +0800
Labels:           test=l.0.0
                  type=app
Annotations:      <none>
Status:           Running
IP:               10.2.1.15
IPs:
  IP:  10.2.1.15
Containers:
  nginx:
    Container ID:  docker://711ffce625d048d2adb97d787aef11bc50f195a2b1b53a788e7165510f33c62f
    Image:         nginx:1.20
    Image ID:      docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
    Port:          80/TCP
    Host Port:     0/TCP
    Command:
      nginx
      -g
      daemon off;
    State:          Running
      Started:      Fri, 23 Feb 2024 00:25:02 +0800
    Ready:          True
    Restart Count:  0
    Limits:
      cpu:     200m
      memory:  256Mi
    Requests:
      cpu:     100m
      memory:  128Mi

## 此处可以看到探针方式是tcp
    Startup:   tcp-socket :80 delay=0s timeout=5s period=10s #success=1 #failure=3
    Environment:
      JVM_OPTS:  -Xms128m -Xmx128m
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-lwbbz (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-lwbbz:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  16s   default-scheduler  Successfully assigned default/nginx-po to k8s-node-02
  Normal  Pulled     17s   kubelet            Container image "nginx:1.20" already present on machine
  Normal  Created    17s   kubelet            Created container nginx
  Normal  Started    17s   kubelet            Started container nginx

[root@k8s-master ~]# kubectl get po nginx-po
NAME       READY   STATUS    RESTARTS   AGE
nginx-po   1/1     Running   0          26s
2.4.1.3 使用命令的探测方式检测pod

此处我们改为命令的方式去做探针探测
在这里插入图片描述

[root@k8s-master ~]# kubectl create -f nginx-po.yml
pod/nginx-po created

[root@k8s-master ~]# kubectl describe po  nginx-po
Name:             nginx-po
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s-node-02/10.10.10.113
Start Time:       Fri, 23 Feb 2024 00:34:07 +0800
Labels:           test=l.0.0
                  type=app
Annotations:      <none>
Status:           Running
IP:               10.2.1.16
IPs:
  IP:  10.2.1.16
Containers:
  nginx:
    Container ID:  docker://f03f904f0d25cedac772362a9944a40d5dd780bec0c2d814196d068f679c2d59
    Image:         nginx:1.20
    Image ID:      docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
    Port:          80/TCP
    Host Port:     0/TCP
    Command:
      nginx
      -g
      daemon off;
    State:          Running
      Started:      Fri, 23 Feb 2024 00:34:08 +0800
    Ready:          False
    Restart Count:  0
    Limits:
      cpu:     200m
      memory:  256Mi
    Requests:
      cpu:     100m
      memory:  128Mi

## 这里可以看到是命令的方式做为探针 
    Startup:   exec [sh -c echo "这个是命令探针" > /test.log] delay=0s timeout=5s period=10s #success=1 #failure=3
    Environment:
      JVM_OPTS:  -Xms128m -Xmx128m
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-s4x9g (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  kube-api-access-s4x9g:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  8s    default-scheduler  Successfully assigned default/nginx-po to k8s-node-02
  Normal  Pulled     8s    kubelet            Container image "nginx:1.20" already present on machine
  Normal  Created    8s    kubelet            Created container nginx
  Normal  Started    8s    kubelet            Started container nginx
  
[root@k8s-master ~]# kubectl get po nginx-po
NAME       READY   STATUS    RESTARTS   AGE
nginx-po   1/1     Running   0          18s

# 通过exec的方式进入到容器中可以查看到这个文件信息
[root@k8s-master ~]# kubectl exec nginx-po -it -c nginx -- cat /test.log
这个是命令探针
  • 进入容器查看容器内文件信息方式 \color{#FF0000}{进入容器查看容器内文件信息方式} 进入容器查看容器内文件信息方式
  • kubectl exec nginx-po -it -c nginx – cat /test.log
  • kubectl exec 【指定pod名称】 -it -c 【指定容器名称】 − − \color{#FF0000}{--} 【执行的命令】

2.4.2 LivenessProbe 的使用

重启探针,我想实现这样一个需求,nginx如果10s中还没有这个文件,那么会重启pod,同时添加了StartupProbe和LivenessProbe这两个探针类型。

在这里插入图片描述

[root@k8s-master ~]# kubectl create -f nginx-po.yml
pod/nginx-liveness-po created

# 首先启动成功由于LivenessProbe探测不到文件,所以又重启了pod。
root@k8s-master ~]# kubectl get pod nginx-liveness-po
NAME                READY   STATUS    RESTARTS      AGE
nginx-liveness-po   0/1     Running   1 (11s ago)   51s

# 在这重启pod成功
[root@k8s-master ~]# kubectl get pod nginx-liveness-po
NAME                READY   STATUS    RESTARTS      AGE
nginx-liveness-po   1/1     Running   1 (26s ago)   66s



[root@k8s-master ~]# kubectl describe po nginx-liveness-po
Name:             nginx-liveness-po
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s-node-02/10.10.10.113
Start Time:       Fri, 23 Feb 2024 00:55:44 +0800
Labels:           test=l.0.0
                  type=app
Annotations:      <none>
Status:           Running
IP:               10.2.1.17
IPs:
  IP:  10.2.1.17
Containers:
  nginx:
    Container ID:  docker://0b4851536a55afcefea805f7011a94523338d71f92ef3a11ca1f804063db5192
    Image:         nginx:1.20
    Image ID:      docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
    Port:          80/TCP
    Host Port:     0/TCP
    Command:
      nginx
      -g
      daemon off;
    State:          Running
      Started:      Fri, 23 Feb 2024 00:56:25 +0800
    Last State:     Terminated
      Reason:       Completed
      Exit Code:    0
      Started:      Fri, 23 Feb 2024 00:55:45 +0800
      Finished:     Fri, 23 Feb 2024 00:56:25 +0800
    Ready:          True
    Restart Count:  1
    Limits:
      cpu:     200m
      memory:  256Mi
    Requests:
      cpu:     100m
      memory:  128Mi

# 这里是LivenessProbe探针的信息
    Liveness:  http-get http://:80/started.html delay=0s timeout=5s period=10s #success=1 #failure=3
    
# 这里是startupProbe探针的信息
    Startup:   exec [sh -c sleep 3;echo "这个是命令探针" > /test.log] delay=0s timeout=5s period=10s #success=1 #failure=3

    Environment:
      JVM_OPTS:  -Xms128m -Xmx128m
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-km6bb (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-km6bb:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  77s                default-scheduler  Successfully assigned default/nginx-liveness-po to k8s-node-02
  Normal   Pulled     38s (x2 over 78s)  kubelet            Container image "nginx:1.20" already present on machine
  Normal   Created    38s (x2 over 78s)  kubelet            Created container nginx
  Normal   Started    38s (x2 over 78s)  kubelet            Started container nginx
  Normal   Killing    38s                kubelet            Container nginx failed liveness probe, will be restarted

# 这里有Liveness的状态是检测失败了
  Warning  Unhealthy  8s (x5 over 58s)   kubelet            Liveness probe failed: HTTP probe failed with statuscode: 404

# 这里可以看到pod的状态还未就绪
[root@k8s-master ~]# kubectl get pod nginx-liveness-po
NAME                READY   STATUS    RESTARTS     AGE
nginx-liveness-po   0/1     Running   2 (2s ago)   82s

在设想一个问题,刚才我们liveness判断的这个文件是不存在的, 如果我们创建一个started.html文件,然后把这个文件放到容器中,pod是否会恢复正常。实际的工作场景中,是否可以通过这样的方式来进行pod的自我恢复。

[root@k8s-master ~]# touch started.html

[root@k8s-master ~]# kubectl cp started.html  nginx-liveness-po:/usr/share/nginx/html/

[root@k8s-master ~]# kubectl get po nginx-liveness-po
NAME                READY   STATUS    RESTARTS       AGE
nginx-liveness-po   1/1     Running   2 (3m9s ago)   4m29s

2.4.3 ReadinessProbe 的使用

Readiness 是就绪探针,也就是说我们他去检测,如果检测正常,pod可以接受外部流量,如果不正常,pod不可以接受外部流量。
在这里插入图片描述

[root@k8s-master ~]# kubectl create -f nginx-readiness-po.yml
pod/nginx-liveness-po created
[root@k8s-master ~]# kubectl get pod nginx-liveness-po
NAME                READY   STATUS    RESTARTS   AGE
nginx-liveness-po   0/1     Running   0          6s
[root@k8s-master ~]# kubectl describe  pod nginx-liveness-po
Name:             nginx-liveness-po
Namespace:        default
Priority:         0
Service Account:  default
Node:             k8s-node-02/10.10.10.113
Start Time:       Fri, 23 Feb 2024 01:32:17 +0800
Labels:           test=l.0.0
                  type=app
Annotations:      <none>
Status:           Running
IP:               10.2.1.19
IPs:
  IP:  10.2.1.19
Containers:
  nginx:
    Container ID:  docker://30ba14e4759d735b148259c45db01eaf24c89d75f5aa150efbc19da127815dde
    Image:         nginx:1.20
    Image ID:      docker-pullable://nginx@sha256:03f3cb0afb7bd5c76e01bfec0ce08803c495348dccce37bcb82c347b4853c00b
    Port:          80/TCP
    Host Port:     0/TCP
    Command:
      nginx
      -g
      daemon off;
    State:          Running
      Started:      Fri, 23 Feb 2024 01:32:18 +0800
    Ready:          False
    Restart Count:  0
    Limits:
      cpu:     200m
      memory:  256Mi
    Requests:
      cpu:      100m
      memory:   128Mi
    Readiness:  http-get http://:80/started.html delay=0s timeout=3s period=10s #success=1 #failure=5
    Startup:    exec [sh -c sleep 3;echo "这个是命令探针" > /test.log] delay=0s timeout=5s period=10s #success=1 #failure=3
    Environment:
      JVM_OPTS:  -Xms128m -Xmx128m
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-pfcnk (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  kube-api-access-pfcnk:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age   From               Message
  ----     ------     ----  ----               -------
  Normal   Scheduled  19s   default-scheduler  Successfully assigned default/nginx-liveness-po to k8s-node-02
  Normal   Pulled     19s   kubelet            Container image "nginx:1.20" already present on machine
  Normal   Created    19s   kubelet            Created container nginx
  Normal   Started    19s   kubelet            Started container nginx
  Warning  Unhealthy  7s    kubelet            Readiness probe failed: HTTP probe failed with statuscode: 404
[root@k8s-master ~]#



## 映射端口
[root@k8s-master ~]# kubectl expose po nginx-readiness-po   --port 80 --type=NodePort
service/nginx-readiness-po exposed
[root@k8s-master ~]# kubectl get  pod,svc  -owide
NAME                     READY   STATUS    RESTARTS   AGE   IP          NODE          NOMINATED NODE   READINESS GATES
pod/nginx-readiness-po   0/1     Running   0          69s   10.2.1.20   k8s-node-02   <none>           <none>

NAME                         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE    SELECTOR
service/kubernetes           ClusterIP   10.1.0.1       <none>        443/TCP        3d3h   <none>
service/nginx-readiness-po   NodePort    10.1.135.241   <none>        80:31815/TCP   14s    test=l.0.0,type=app
[root@k8s-master ~]# curl 10.10.10.100:31815
curl: (7) Failed connect to 10.10.10.100:31815; 拒绝连接
[root@k8s-master ~]#



# 流量放行
[root@k8s-master ~]# kubectl cp started.html nginx-readiness-po:/usr/share/nginx/html
[root@k8s-master ~]# kubectl get po nginx-readiness-po
NAME                 READY   STATUS    RESTARTS   AGE
nginx-readiness-po   1/1     Running   0          4m
[root@k8s-master ~]# curl 10.10.10.100:31815
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>

3、生命周期

在这里插入图片描述


lifecycle:
  postStart:  # 容创速完成后执行的动作,不能保证该操作一定在容器的command之前执行,一般不使用
    exec:    # 可以是 exec/httpGet/tcpSocket
      command:
      - sh
      - -c
      - 'mkdir /data'
  preStop: #在客器停止前执行的动f作
    httpGet: #发送个http语求
    path: /
    port:80

3.1 Pod的退出流程

  1. 首先删除pod相关的网络信息,如果有暴露端口,删除pod的时候会删除这个POD暴露IP信息,会把我们的pod变成一个Terminating的的状态
  2. 变成Terminating中这个状态的时候,会给pod一个宽限期,让pod去执行一些清理或销毁操作。
    • 配置参数:作用与pod中的所有容器
    • terminationGracePeriodSeconds:30
    • containers:
    • -xxx

3.2 PreStop的应用

在这里插入图片描述

同样使用上文的nginx-po的yaml文件,修改spec的配置如上。我们的prestop运行的命令 sleep 50,睡眠50s,但是pod的默认销毁时间是30,由上文的 terminationGracePeriodSeconds 这个参数控制,也就是说,prestop的命令还未执行呢,pod就已经删除了, 实际工作中,如果删除pod的时候,有数据需要持久化,那么需要配置 terminationGracePeriodSeconds 这个参数来控制pod删除时间。

[root@k8s-master ~]# kubectl create -f nginx-prestop-po.yml
pod/nginx-liveness-po created

[root@k8s-master ~]# kubectl get pod nginx-prestoop-po  -owide
NAME                READY   STATUS    RESTARTS   AGE   IP          NODE          NOMINATED NODE   READINESS GATES
nginx-prestoop-po   1/1     Running   0          13s   10.2.1.23   k8s-node-02   <none>           <none>

[root@k8s-master ~]# curl 10.2.1.23/life.html
Fri Feb 23 09:07:35 UTC 2024 poststart

[root@k8s-master ~]# time kubectl delete po nginx-prestoop-po
pod "nginx-prestoop-po" deleted

real	0m31.183s
user	0m0.067s
sys	0m0.065s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值