就绪检测&存活检测---实验

admin
2020-11-25 / 6 评论 / 295 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年01月31日,已超过235天没有更新,若内容或图片失效,请留言反馈。

[hide]

检查探针---就绪检测

readinessProbe-httpget

创建资源清单

[root@k8s-master ~]# vim read.yaml

apiVersion: v1
kind: Pod
metadata:
  name: readiness-httpget-pod
  namespace: default
spec:
  containers:
    - name: readiness-httpget-container
      image: nginx:1.17
      imagePullPolicy: IfNotPresent
      readinessProbe:
        httpGet:
          port: 80
          path: /yz.html
        initialDelaySeconds: 1
        periodSeconds: 3

创建Pod

[root@k8s-master ~]# kubectl create -f read.yaml

pod/readiness-httpget-pod created

查看Pod,虽然显示运行状态,但是准备未就绪

[root@k8s-master ~]# kubectl get pods

NAME                         READY   STATUS    RESTARTS   AGE
myapp-pod                    2/2     Running   0          2d20h
mysql-rc-jjsmp               1/1     Running   2          50d
mysql-rc-lc22m               1/1     Running   2          55d
mysql-rc-nkdrh               1/1     Running   2          50d
nginx-app-756ffb5cc8-52hdh   1/1     Running   0          7d13h
nginx-app-756ffb5cc8-bgcbh   1/1     Running   0          7d13h
nginx-app-756ffb5cc8-smztw   1/1     Running   0          7d13h
readiness-httpget-pod        0/1     Running   0          5s
yzapp-pod                    1/1     Running   19         19h

查看Pod的描述信息

[root@k8s-master ~]# kubectl describe pods readiness-httpget-pod

Name:         readiness-httpget-pod
Namespace:    default
Priority:     0
Node:         k8s-node2/42.51.227.115
Start Time:   Tue, 24 Nov 2020 03:32:54 +0000
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.244.3.83
IPs:
  IP:  10.244.3.83
Containers:
  readiness-httpget-container:
    Container ID:   docker://55adb8df8776d8a6d3a16db8467dde734148a961a5e5ae0ccdf4191fb4109e79
    Image:          nginx:1.17
    Image ID:       docker-pullable://nginx@sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 24 Nov 2020 03:32:57 +0000
    Ready:          False
    Restart Count:  0
    Readiness:      http-get http://:80/yz.html delay=1s timeout=1s period=3s #success=1 #failure=3
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-hvlcv (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  default-token-hvlcv:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-hvlcv
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason     Age                From                Message

----     ------     ----               ----                -------

  Normal   Scheduled  <unknown>          default-scheduler   Successfully assigned default/readiness-httpget-pod to k8s-node2
  Normal   Pulled     37s                kubelet, k8s-node2  Container image "nginx:1.17" already present on machine
  Normal   Created    37s                kubelet, k8s-node2  Created container readiness-httpget-container
  Normal   Started    37s                kubelet, k8s-node2  Started container readiness-httpget-container
  Warning  Unhealthy  1s (x12 over 34s)  kubelet, k8s-node2  Readiness probe failed: HTTP probe failed with statuscode: 404

进入到Pod里的容器,创建yz.html文件

[root@k8s-master ~]# kubectl exec readiness-httpget-pod -it -- /bin/sh

# cd /usr/share/nginx/html
# ls
# 50x.html  index.html
# echo "I am YuanZhang" > yz.html
# exit

再次查看Pod,显示已经准备就绪了

[root@k8s-master ~]# kubectl get pods

NAME                         READY   STATUS    RESTARTS   AGE
myapp-pod                    2/2     Running   0          2d20h
mysql-rc-jjsmp               1/1     Running   2          50d
mysql-rc-lc22m               1/1     Running   2          55d
mysql-rc-nkdrh               1/1     Running   2          50d
nginx-app-756ffb5cc8-52hdh   1/1     Running   0          7d13h
nginx-app-756ffb5cc8-bgcbh   1/1     Running   0          7d13h
nginx-app-756ffb5cc8-smztw   1/1     Running   0          7d13h
readiness-httpget-pod        1/1     Running   0          2m36s
yzapp-pod                    1/1     Running   19         19h

检查探针---三种存活检测

livenessProbe-exec

创建资源清单

[root@k8s-master ~]# vim live-exec.yaml

apiVersion: v1
kind: Pod
metadata:
  namespace: default
  name: liveness-exec-pod
spec:
  containers:
    - name: liveness-exec-container
      image: busybox
      imagePullPolicy: IfNotPresent
      command: ["/bin/sh","-c","touch /tmp/live; sleep 60; rm -rf /tmp/live; sleep 3600"]
      livenessProbe:
        exec:
          command: ["test","-e","/tmp/live"]
        initialDelaySeconds: 1
        periodSeconds: 3

创建资源

[root@k8s-master ~]# kubectl create -f live-exec.yaml

pod/liveness-exec-pod created

查看Pod信息,已经是在运行中了

[root@k8s-master ~]# kubectl get pods

NAME                         READY   STATUS      RESTARTS   AGE
liveness-exec-pod             1/1     Running     0          4s
myapp-pod                    2/2     Running     0          3d
mysql-rc-jjsmp               1/1     Running     2          51d
mysql-rc-lc22m               1/1     Running     2          55d
mysql-rc-nkdrh               1/1     Running     2          51d
nginx-app-756ffb5cc8-52hdh   1/1     Running     0          7d17h
nginx-app-756ffb5cc8-bgcbh   1/1     Running     0          7d17h
nginx-app-756ffb5cc8-smztw   1/1     Running     0          7d17h
readiness-httpget-pod        1/1     Running     0          3h33m
yzapp-pod                    0/1     Completed   22         23h

我们持续查看Pod的运行状态

我们发现Pod会重启,因为我们上面的清单里写的命令

  command: ["/bin/sh","-c","touch /tmp/live; sleep 60; rm -rf /tmp/live; sleep 3600"]
  livenessProbe:
    exec:
      command: ["test","-e","/tmp/live"]
    initialDelaySeconds: 1
    periodSeconds: 3
意思就是容器创建后会创建一个文件 /tmp/live,然后休眠60秒,然后删除这个文件,然后再休眠6分钟
在创建容器后的一分钟内文件是存在的,然后一分钟后文件删除了,我们检测不到了这个文件,那么该容器就被杀死了,因为Pod里只有一个容器,那么容器死掉意味着Pod也会被重启,继续执行这个流程,会一直重启。

[root@k8s-master ~]# kubectl get pods -w

NAME                         READY   STATUS    RESTARTS   AGE
liveness-exec-pod             1/1     Running   0          12s
myapp-pod                    2/2     Running   0          3d
mysql-rc-jjsmp               1/1     Running   2          51d
mysql-rc-lc22m               1/1     Running   2          55d
mysql-rc-nkdrh               1/1     Running   2          51d
nginx-app-756ffb5cc8-52hdh   1/1     Running   0          7d17h
nginx-app-756ffb5cc8-bgcbh   1/1     Running   0          7d17h
nginx-app-756ffb5cc8-smztw   1/1     Running   0          7d17h
readiness-httpget-pod        1/1     Running   0          3h33m
yzapp-pod                    1/1     Running   23         23h
livenes-exec-pod             1/1     Running   1          99s
livenes-exec-pod             1/1     Running   2          3m19s

livenessProbe-httpget

创建资源清单

[root@k8s-master ~]# vim live-http.yaml

apiVersion: v1
kind: Pod
metadata:
  name: liveness-httpget-pod
  namespace: default
spec:
  containers:
    - name: liveness-httpget-container
      image: nginx:1.17
      imagePullPolicy: IfNotPresent
      ports:
        - containerPort: 80
          name: http
      livenessProbe:
        httpGet:
          port: http
          path: /index.html
        initialDelaySeconds: 1
        periodSeconds: 3

创建Pod资源

[root@k8s-master ~]# kubectl create -f live-http.yaml

pod/liveness-httpget-pod created

查看Pod信息,显示已经正在运行

[root@k8s-master ~]# kubectl get pods

NAME                         READY   STATUS             RESTARTS   AGE
liveness-exec-pod             0/1     CrashLoopBackOff   11         33m
liveness-httpget-pod         1/1     Running            0          4s
myapp-pod                    2/2     Running            0          3d
mysql-rc-jjsmp               1/1     Running            2          51d
mysql-rc-lc22m               1/1     Running            2          55d
mysql-rc-nkdrh               1/1     Running            2          51d
nginx-app-756ffb5cc8-52hdh   1/1     Running            0          7d17h
nginx-app-756ffb5cc8-bgcbh   1/1     Running            0          7d17h
nginx-app-756ffb5cc8-smztw   1/1     Running            0          7d17h
readiness-httpget-pod        1/1     Running            0          4h6m
yzapp-pod                    1/1     Running            23         23h

查看default命名空间下的Pod的详细信息,可以看到我们创建Pod的IP为:10.244.3.84

[root@k8s-master ~]# kubectl get pods -o wide

NAME                         READY   STATUS             RESTARTS   AGE     IP             NODE        NOMINATED NODE   READINESS GATES
liveness-exec-pod             0/1     CrashLoopBackOff   11         33m     10.244.2.111   k8s-node3   <none>           <none>
liveness-httpget-pod         1/1     Running            0          15s     10.244.3.84    k8s-node2   <none>           <none>
myapp-pod                    2/2     Running            0          3d      10.244.3.82    k8s-node2   <none>           <none>
mysql-rc-jjsmp               1/1     Running            2          51d     10.244.1.72    k8s-node1   <none>           <none>
mysql-rc-lc22m               1/1     Running            2          55d     10.244.2.80    k8s-node3   <none>           <none>
mysql-rc-nkdrh               1/1     Running            2          51d     10.244.3.65    k8s-node2   <none>           <none>
nginx-app-756ffb5cc8-52hdh   1/1     Running            0          7d17h   10.244.2.98    k8s-node3   <none>           <none>
nginx-app-756ffb5cc8-bgcbh   1/1     Running            0          7d17h   10.244.2.100   k8s-node3   <none>           <none>
nginx-app-756ffb5cc8-smztw   1/1     Running            0          7d17h   10.244.3.73    k8s-node2   <none>           <none>
readiness-httpget-pod        1/1     Running            0          4h6m    10.244.3.83    k8s-node2   <none>           <none>
yzapp-pod                    1/1     Running            23         23h     10.244.2.110   k8s-node3   <none>           <none>

我们访问Pod的nginx,可以看到访问正常

[root@k8s-master ~]# curl 10.244.3.84/index.html

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

删除Pod的容器里的usr/share/nginx/html/index.html文件

[root@k8s-master ~]# kubectl exec liveness-httpget-pod -it -- rm -rf /usr/share/nginx/html/index.html

删除后查看Pod信息,发现Pod已经被重启了

[root@k8s-master ~]# kubectl get pods

NAME                         READY   STATUS             RESTARTS   AGE
liveness-exec-pod             0/1     CrashLoopBackOff   15         51m
liveness-httpget-pod         1/1     Running            1          17m
myapp-pod                    2/2     Running            0          3d1h
mysql-rc-jjsmp               1/1     Running            2          51d
mysql-rc-lc22m               1/1     Running            2          55d
mysql-rc-nkdrh               1/1     Running            2          51d
nginx-app-756ffb5cc8-52hdh   1/1     Running            0          7d17h
nginx-app-756ffb5cc8-bgcbh   1/1     Running            0          7d17h
nginx-app-756ffb5cc8-smztw   1/1     Running            0          7d17h
readiness-httpget-pod        1/1     Running            0          4h24m
yzapp-pod                    1/1     Running            23         24h

livenessProbe-tcp

创建资源清单

为了方便测试效果,我们这里把80端口写成8080,这样可以直接看到实验效果

[root@k8s-master ~]# vim liveness-tcp.yaml

apiVersion: v1
kind: Pod
metadata:
  name: liveness-tcp-pod
  namespace: default
spec:
  containers:
    - name: liveness-tcp-container
      image: nginx:1.17
      imagePullPolicy: IfNotPresent
      livenessProbe:
        initialDelaySeconds: 5
        timeoutSeconds: 1
        tcpSocket:
          port: 8080
        periodSeconds: 3

5秒以后开始检测,检测时候发起连接我们的8080端口,但是端口肯定是不通的,然后一秒以后自己知道是失败了,然后重启Pod

创建Pod资源

[root@k8s-master ~]# kubectl create -f liveness-tcp.yaml

pod/liveness-tcp-pod created

持续查看Pod信息,发现已经被重启

[root@k8s-master ~]# kubectl get pods -w

NAME                         READY   STATUS             RESTARTS   AGE
livenes-exec-pod             0/1     CrashLoopBackOff   19         68m
liveness-httpget-pod         1/1     Running            1          35m
liveness-tcp-pod             1/1     Running            0          8s
myapp-pod                    2/2     Running            0          3d1h
mysql-rc-jjsmp               1/1     Running            2          51d
mysql-rc-lc22m               1/1     Running            2          55d
mysql-rc-nkdrh               1/1     Running            2          51d
nginx-app-756ffb5cc8-52hdh   1/1     Running            0          7d18h
nginx-app-756ffb5cc8-bgcbh   1/1     Running            0          7d18h
nginx-app-756ffb5cc8-smztw   1/1     Running            0          7d18h
readiness-httpget-pod        1/1     Running            0          4h41m
yzapp-pod                    1/1     Running            24         24h
liveness-tcp-pod             1/1     Running            1          13s
liveness-tcp-pod             1/1     Running            2          26s

[/hide]

3

评论 (6)

取消
  1. 头像
    frsfrsr
    Windows 10 · Google Chrome

    heian黑暗

    回复
  2. 头像
    就这吧
    Android · Google Chrome

    画图

    回复
  3. 头像
    zpf
    Windows 10 · Google Chrome

    qq群来的。帮顶

    回复
  4. 头像
    小白001
    Windows 7 · QQ Browser

    科技三

    回复
  5. 头像
    lsd
    Windows 10 · Google Chrome

    大哥牛逼

    回复
  6. 头像
    warlock
    Windows 10 · Google Chrome

    看看

    回复

院长正在为您加急渲染,请耐心等待......