随笔 - 91  文章 - 0  评论 - 2  阅读 - 44255

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

               在前台执行 指定家目录

posted on   SZ_文彬  阅读(261)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示