• k8sd之pod生命周期


    pod生命周期:

      状态:pending 挂起

    没有节点满足条件

    running 运行 

    Failed

    sucess

    unkonwn

    pod生命周期中的重要行为:

    初始化容器

    容器探测:liveness probe  探测容器是否活着 探测容器 存活性探针

              readindess probe 探测容器中主程序是否正常提供服务 就绪性探针

    restartPolicy:容器重启策略

            Always,OnFailure,Never,Default to always

    pod终止过程:发送指令给容器并有等待机制

    查看liveness, readindess

    kubectl explain pods.spec.containers

    探针类型有三种:探测容器

        ExecAction:

    TCPSocketAction:套接字探针

     HTTPGetAction:

          

    liveness Probe探测情况

    kubectl explain pods.spec.containers.livenessProbe

    例如

    kubectl explain pods.spec.containers.livenessProbe.exec

    liveness Probe探针类型:三种只用其中一种

    exec <Object>

    tcpSocket          <Object>

    httpGet    <Object>

    附加属性

    failureThreshold       <integer> 探测几次失败,才认为失败

    periodSeconds <integer>   探测间隔时间 单位秒

    timeoutSeconds       <integer> 每次探测超时时间响应

    initialDelaySeconds <integer>  容器初始化后延迟的探测时间,容器第一次探测时间 默认容器一启动就开始探测

    readinessProbe就绪行探测的属性与livenessProbe一样

    kubectl explain pods.spec.containers.readinessProbe

    livenessProbe实例:

    exec<Object>

    kubectl explain pods.spec.containers.livenessProbe.exec

    实例:

    vim  leveness-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:   exec探针 执行

            command: ["test","-e","/tmp/healthy"]  探测命令 返回true false 返回false的话restartPolicy容器重启策略执行 restart

          initialDelaySeconds: 2 容器初始化后2秒探测

          periodSeconds: 3  探针返回true 间隔3秒再探测

    kubectl create -f leveness-exec.yaml

    kubectl get pods –w

    kubectl describe pods liveness-exec-pod

    livenessProbe

    tcpSocket探针

    kubectl explain pods.spec.containers.livenessProbe.tcpSocket

    livenessProbe

    httpGet探针

    kubectl explain pods.spec.containers.livenessProbe.httpGet

    实例:

    cp leveness-exec.yaml liveness-httpdget.yaml

    vim liveness-httpdget.yaml

    apiVersion: v1

    kind: Pod

    metadata:

      name: liveness-httpget-pod

      namespace: default

    spec:

      containers:

      - name: liveness-httpget-container

        image: ikubernetes/myapp:v1

        imagePullPolicy: IfNotPresent

        ports:

        - name: http

          containerPort: 80

        livenessProbe:

          httpGet:  探针

            port: http  port的name

            path: /index.html  请求的网页 网页请求不到,restartPolicy重启策略执行restart,重启之后 容器文件会重置

          initialDelaySeconds: 1 容器初始化1秒后探测

          periodSeconds: 3 间隔3秒探测

    kubectl create -f liveness-httpdget.yaml

    kubectl get pods –w

    kubectl describe pods liveness-httpget-pod

    查看Liveness:结果

    实验过程:

    kubectl exec -it liveness-httpget-pod -- /bin/sh   进入容器

    rm -rf /usr/share/nginx/html/index.html

    kubectl describe pods liveness-httpget-pod  显示 restart count

    就绪性探测实例:

    cp liveness-httpdget.yaml readiness-httpget.yaml

    vim readiness-httpget.yaml

    apiVersion: v1

    kind: Pod

    metadata:

      name: readiness-httpget-pod

      namespace: default

    spec:

      containers:

      - name: readiness-httpget-container

        image: ikubernetes/myapp:v1

        imagePullPolicy: IfNotPresent

        ports:

        - name: http

          containerPort: 80

        readinessProbe:

          httpGet:

            port: http

            path: /index.html 能够访问就会显示就绪 ready 1,访问不到就会显示不就绪 ready为0

          initialDelaySeconds: 1

          periodSeconds: 3

    kubectl create -f readiness-httpget.yaml

    kubectl get pods

    实验过程

    kubectl exec -it readiness-httpget-pod -- /bin/sh

    rm -rf /usr/share/nginx/html/index.html

    kubectl get pods

    kubectl get pods 此时显示ready状态为0

    kubectl exec -it readiness-httpget-pod -- /bin/sh

    touch   /usr/share/nginx/html/index.html

    kubectl get pods  此时ready为1  因为已经能够探测到了

    kubectl describe pods readiness-httpget-pod  查看Readiness

    容器启动后勾子,结束前勾子

    kubectl explain pods.spec.containers.lifecycle

    postStart <Object>

    kubectl explain pods.spec.containers.lifecycle.postStart

    实例:

    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: ["/bin/sh","-c","mkdir -p /data/web/html;echo Home_Page>> /data/web/html/index.html"] 容器初始化后执行

          command: ["/bin/sh",”-c”,”sleep 3600”] 容器启动 时执行  既容器初始化前就已经执行 先执行

    kubectl create  -f  poststart-pod.yaml

    kubectl get pods

    登录检查勾子执行的结果

    kubectl exec -it poststart-pod -- /bin/sh

    ls /data/web/html/

    cat  /data/web/html/index.html

    preStop    <Object>

    kubectl explain pods.spec.containers.lifecycle.preStop

    注释:/bin/httpd  -f  -h /data/web/html

                   在前台执行 指定家目录

  • 相关阅读:
    spring cloud的消费服务ribbon(踩着坑往前爬)
    spring cloud注册服务与发现(踩着坑往前爬)
    springboot配置多数据源
    ssh免密登入
    mybatais面试题(复习篇)
    ssh全注解整合
    Vue项目中使用axios配置请求拦截
    Vue后台的路由和权限校验
    CSS3新增属性(4)
    CSS3新增属性(3)
  • 原文地址:https://www.cnblogs.com/leiwenbin627/p/11291806.html
Copyright © 2020-2023  润新知