应用部署、运行与管理(三)

容器就绪状态检测

[root@k8s-master01 yaml]# cat readiness-httpget-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-demo
  namespace: dev
spec:
  containers:
  - name: demo
    image: ikubernetes/demoapp:v1.0
    imagePullPolicy: IfNotPresent
    readinessProbe:
      httpGet:
        path: '/readyz'
        port: 80
        scheme: HTTP
      initialDelaySeconds: 15
      timeoutSeconds: 2
      periodSeconds: 5
      failureThreshold: 3
  restartPolicy: Always 

上面的配置清单,demoapp应用程序通过/ready暴露了用于就绪状态检测的接口,它与程序启动约15秒后能够以200状态码响应,以OK为响应结果,也正常用户使用POST请求方法通过ready参数传递自定义的响应内容,不过所有非OK的响应内容都被响应以5xx的状态码

 

[root@k8s-master01 yaml]# kubectl apply -f readiness-httpget-demo.yaml -n dev
pod/readiness-httpget-demo created
[root@k8s-master01 yaml]# kubectl get pods -n dev
NAME                                READY   STATUS    RESTARTS   AGE
pod-demo-with-cmd-and-args          1/1     Running   0          10h
pod-nginx-68dc879f46-fcwxh          1/1     Running   0          42h
pod-nginx-68dc879f46-htp2z          1/1     Running   0          42h
pod-nginx-68dc879f46-kzhb5          1/1     Running   0          42h
pod-nginx-68dc879f46-rblxg          1/1     Running   0          42h
pod-using-hostnetwork               1/1     Running   0          39h
pod-using-hostport                  1/1     Running   0          40h
readiness-httpget-demo              0/1     Running   0          10s
securitycontext-capabilities-demo   1/1     Running   0          10h
securitycontext-runasuser-demo      1/1     Running   0          36h

上面代码显示,由于还没有到15S,所以read还是0/1,状态是running


运行 kubectl  get  -w 命令监视其资源变动信息

[root@k8s-master01 yaml]# kubectl get pods/readiness-httpget-demo -w -n dev
NAME                     READY   STATUS    RESTARTS   AGE
readiness-httpget-demo   0/1     Running   0          5s
readiness-httpget-demo   1/1     Running   0          33s

 

查看pod描述信息

[root@k8s-master01 ~]# kubectl describe pods/readiness-httpget-demo -n dev
Name:         readiness-httpget-demo
Namespace:    dev
Priority:     0
Node:         k8s-node02/10.122.138.246
Start Time:   Tue, 17 Aug 2021 07:28:20 +0800
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.244.1.9
IPs:
  IP:  10.244.1.9
Containers:
  demo:
    Container ID:   docker://e75e2bd5ae91c3cf9adf728c9cf27712b5037e232ef3f595651df970a183b375
    Image:          ikubernetes/demoapp:v1.0
    Image ID:       docker-pullable://ikubernetes/demoapp@sha256:6698b205eb18fb0171398927f3a35fe27676c6bf5757ef57a35a4b055badf2c3
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 17 Aug 2021 07:28:22 +0800
    Ready:          True
    Restart Count:  0
    Readiness:      http-get http://:80/readyz delay=15s timeout=2s period=5s #success=1 #failure=3
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-wt6qh (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-wt6qh:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-wt6qh
    Optional:    false
QoS Class:       BestEffort
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  72s                                     Successfully assigned dev/readiness-httpget-demo to k8s-node02
  Normal   Pulled     72s                kubelet, k8s-node02  Container image "ikubernetes/demoapp:v1.0" already present on machine
  Normal   Created    72s                kubelet, k8s-node02  Created container demo
  Normal   Started    71s                kubelet, k8s-node02  Started container demo
  Warning  Unhealthy  43s (x3 over 53s)  kubelet, k8s-node02  Readiness probe failed: Get "http://10.244.1.9:80/readyz": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

为了测试就绪检测效果,下面修改/ready以响应非OK内容

[root@k8s-master01 ~]# kubectl exec readiness-httpget-demo -n dev -- curl -s -XPOST -d 'readyz=FALL' 127.0.0.1/readyz
[root@k8s-master01 ~]# kubectl describe pods/readiness-httpget-demo -n dev
Name:         readiness-httpget-demo
Namespace:    dev
Priority:     0
Node:         k8s-node02/10.122.138.246
Start Time:   Tue, 17 Aug 2021 07:28:20 +0800
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.244.1.9
IPs:
  IP:  10.244.1.9
Containers:
  demo:
    Container ID:   docker://e75e2bd5ae91c3cf9adf728c9cf27712b5037e232ef3f595651df970a183b375
    Image:          ikubernetes/demoapp:v1.0
    Image ID:       docker-pullable://ikubernetes/demoapp@sha256:6698b205eb18fb0171398927f3a35fe27676c6bf5757ef57a35a4b055badf2c3
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 17 Aug 2021 07:28:22 +0800
    Ready:          True
    Restart Count:  0
    Readiness:      http-get http://:80/readyz delay=15s timeout=2s period=5s #success=1 #failure=3
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-wt6qh (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-wt6qh:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-wt6qh
    Optional:    false
QoS Class:       BestEffort
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  6m26s                                      Successfully assigned dev/readiness-httpget-demo to k8s-node02
  Normal   Pulled     6m26s                 kubelet, k8s-node02  Container image "ikubernetes/demoapp:v1.0" already present on machine
  Normal   Created    6m26s                 kubelet, k8s-node02  Created container demo
  Normal   Started    6m25s                 kubelet, k8s-node02  Started container demo
  Warning  Unhealthy  5m57s (x3 over 6m7s)  kubelet, k8s-node02  Readiness probe failed: Get "http://10.244.1.9:80/readyz": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
  Warning  Unhealthy  4s                    kubelet, k8s-node02  Readiness probe failed: HTTP probe failed with statuscode: 507

可以看到最后的状态码是507

接着用-w查看信息

[root@k8s-master01 yaml]# kubectl get pods/readiness-httpget-demo -w -n dev
NAME                     READY   STATUS    RESTARTS   AGE
readiness-httpget-demo   1/1     Running   0          5m59s
readiness-httpget-demo   0/1     Running   0          6m33s

又变成未就绪状态

 

posted @ 2021-08-16 22:18  拥抱大海,面向天空  阅读(56)  评论(0)    收藏  举报