07-Kubernetes Pod控制器应用进阶二

一、Pod生命周期中的liveness probe(存活状态探测)和readiness probe(就绪状态探测)

1、探测行为有三种:

  1. 执行自定义命令
  2. 向指定的tcp 套接字发送请求
  3. 向指定的http 服务发送请求(Get请求)

2、探针类型有三种:

  1. EXECAction探针
  2. TCPSocketAction探针
  3. HTTPGetAction探针
[root@master ~]# kubectl explain pod.spec.containers
    livenessProbe                #Pod生命周期中的liveness probe
    readinessProbe               #Pod生命周期中的readiness probe
    lifecycle          #Pod生命周期中的post start和pre stop


[root@master ~]# kubectl explain pod.spec.containers.livenessProbe
    exec      #EXEXAction探针
    failureThreshold         #探测失败次数的阈值,默认值是3,最小值是1。(即连着探测3次都失败,则认为失败)
    httpGet          #HTTPGETAction探针
    initialDelaySeconds     #指定容器启动后的多长时间,再开始探测,默认值是0
    periodSeconds           #每次探测之间相隔的时间,默认值是10s,最小值是1s
    successThreshold        #探测成功次数的阈值,默认值是1,最小值是1。(即连着探测1次成功,则认为成功)
    tcpSocket        #TCPSocketAction探针
    timeoutSeconds            #每次探测多长时间没有回应,则认为探测失败。默认值是1s,最小值是1s

 
  

3、EXECAtion探针实例演示—liveness probe

[root@master ~]# vim manifests/liveness-exec.yaml
apiVersion: v1
kind: Pod
metadata:
        name: liveness-exec-pod
        namespace: default
spec:
        containers:
        - name: liveness-exec-container
          image: busybox:latest
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh","-c","touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 3600"]
          livenessProbe:
                exec:
                        command: ["test","-e","/tmp/healthy"]
                initialDelaySeconds: 1
                periodSeconds: 3


[root@master manifests]# kubectl get pods -w  #使用监控的方式查看Pod的状态,可以看到Pod的状态和重启次数一直在发生变化。

4、HTTPGetAction探针实例演示—liveness probe

[root@master manifests]# vim liveness-httpget.yaml 
apiVersion: v1
kind: Pod
metadata:
        name: liveness-httpget-pod
        namespace: default
spec:
        containers:
        - name: liveness-httpget-container
          image: nginx
          imagePullPolicy: IfNotPresent
          ports:
          - name: http
            containerPort: 80
          livenessProbe:
                httpGet:
                        port: http
                        path: /index.html
                initialDelaySeconds: 1
                periodSeconds: 3


[root@master manifests]# kubectl describe pods liveness-httpget-pod  #可以看到重启次数为0


[root@master manifests]# kubectl exec -it liveness-httpget-pod -- /bin/sh
# rm /usr/share/nginx/html/index.html   #删除index.html文件
按下"Ctrl+D"退出该Pod


[root@master manifests]# kubectl describe pods liveness-httpget-pod  #可以看到重启次数为1,这是因为第一次HTTPGet探测失败之后,Pod会重启,重启时,Pod中的容器又会生成index.html文件。所以,看到的重启次数为1。

5、HTTPGetAction探针实例演示—readiness probe

[root@master manifests]# vim readiness-httpget.yaml 
apiVersion: v1
kind: Pod
metadata:
        name: readiness-httpget-pod
        namespace: default
spec:
        containers:
        - name: readiness-httpget-container
          image: nginx
          imagePullPolicy: IfNotPresent
          ports:
          - name: http
            containerPort: 80
          readinessProbe:
                httpGet:
                        port: http
                        path: /index.html
                initialDelaySeconds: 1
                periodSeconds: 3


[root@master manifests]# kubectl exec -it readiness-httpget-pod -- /bin/sh
# rm /usr/share/nginx/html/index.html
按下"Ctrl+d"退出容器

[root@master manifests]# kubectl get pods readiness-httpget-pod   #上面删除readiness-httpget-pod容器中的index.html文件,9秒(3*3)之后,可以看到该Pod的就绪状态为0。因为是就绪状态探测失败,而不是存活状态探测失败,所以,Pod不会自动重启。所以,想要恢复Pod的就绪状态个数,就需要手动将index.html文件添加回来。
NAME                    READY   STATUS    RESTARTS   AGE
readiness-httpget-pod   0/1     Running   0          4m


[root@master manifests]# kubectl exec -it readiness-httpget-pod -- /bin/sh
# echo nginx > /usr/share/nginx/html/index.html
按下"Ctrl+d"退出容器
[root@master ~]# kubectl get pods readiness-httpget-pod   #可以看到就绪状态为1
NAME                    READY   STATUS    RESTARTS   AGE
readiness-httpget-pod   1/1     Running   0          18m

 

二、Pod生命周期中的post start和pre stop

[root@master ~]# kubectl explain pod.spec.containers.lifecycle
    postStart    
    preStop      

[root@master ~]# kubectl explain pod.spec.containers.lifecycle.postStart
    exec 
    httpGet      
    tcpSocket     
  

1、post start实例演示

[root@master manifests]# vim poststart-pod.yaml
apiVersion: v1
kind: Pod
metadata:
        name: poststart-pod
        namespace: default
spec:
        containers:
        - name: busybox-httpd
          image: busybox:latest
          imagePullPolicy: IfNotPresent
          lifecycle:
                postStart:
                        exec:
                                command: ["mkdir","-p","/data/web/html"]
          command: ["/bin/sh","-c","sleep 3600"]


[root@master manifests]# kubectl get pods
NAME            READY   STATUS    RESTARTS   AGE
poststart-pod   1/1     Running   0          5m14s


[root@master manifests]# kubectl exec -it poststart-pod -- /bin/sh
/ # ls /data/web/html/     #可以看到成功创建了/data/web/html文件

2、pre stop的用法与上面的post start相似

你可能感兴趣的:(kubernetes)