K8S - 探针 - readinessProbe

readinessProbe: 指示容器是否准备好服务请求。如果就绪探针失败,端点控制器将从Pod匹配的所有Service的端点中删除该pod的IP地址。初始延迟之前的就绪状态默认为Failure。如果容器不提供就绪探针,则默认状态为Success。

实验
检测nginx容器的/usr/share/nginx/html/kone.html是否存在

[root@k8s-master01 k8s-test]# cat readiness.yaml
apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-pod
  namespace: default
spec:
  containers: 
  - name: readiness-httpget-container
    image: kone.com/library/nginx
    imagePullPolicy: IfNotPresent
    readinessProbe:
      httpGet:
        port: 80
        path: /kone.html
      initialDelaySeconds: 1
      periodSeconds: 3

1. 当前/usr/share/nginx/html/kone.html在容器中不存在
创建容器kubectl create -f readiness.yaml
2. 查看容器状态


[root@k8s-master01 k8s-test]# kubectl get pod -owide
NAME                               READY   STATUS    RESTARTS   AGE   IP            NODE         NOMINATED NODE   READINESS GATES
readiness-httpget-pod              0/1     Running   0          35s   10.244.2.12   k8s-node01              

看到readiness-httpget-pod的READY状态为0/1,此时容器虽然在running,但实际为未就绪状态
3. kubectl describe pod readiness-httpget-pod查看容器
查看最后的event信息Readiness probe failed: HTTP probe failed with statuscode: 404

Events:
  Type     Reason     Age                    From                 Message
  ----     ------     ----                   ----                 -------
  Normal   Scheduled  2m55s                  default-scheduler    Successfully assigned default/readiness-httpget-pod to k8s-node01
  Normal   Pulled     2m54s                  kubelet, k8s-node01  Container image "kone.com/library/nginx" already present on machine
  Normal   Created    2m54s                  kubelet, k8s-node01  Created container readiness-httpget-container
  Normal   Started    2m54s                  kubelet, k8s-node01  Started container readiness-httpget-container
  Warning  Unhealthy  110s (x22 over 2m53s)  kubelet, k8s-node01  Readiness probe failed: HTTP probe failed with statuscode: 404

4. 在容器中新建/usr/share/nginx/html/kone.html
执行命令进入容器:kubectl exec -it readiness-httpget-pod -- /bin/sh,创建kone.html,然后退出容器

[root@k8s-master01 k8s-test]# kubectl exec -it readiness-httpget-pod -- /bin/sh
# cd /usr/share/nginx/html
# ls
50x.html  index.html
# echo 'kone readinessProbe test' > kone.html
# cat kone.html
kone readinessProbe test
# exit
[root@k8s-master01 k8s-test]# 

5. 查看容器状态为ready

[root@k8s-master01 k8s-test]# kubectl get pods
NAME                               READY   STATUS    RESTARTS   AGE
readiness-httpget-pod              1/1     Running   0          14m

你可能感兴趣的:(K8S)