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   <none>           <none>

看到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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章