首页 > 其他分享 >POD的生命周期

POD的生命周期

时间:2022-09-20 17:58:12浏览次数:65  
标签:容器 生命周期 name share nginx html usr POD

POD的生命周期

# 删除所有的pod
[root@k8s-m01 ~]# kubectl delete pod --all
## init container
初始化容器是指,在主容器启动之前,我们可以让他做一些准备工作。
比如:
1.两个容器做了共享存储,那么我们可以让它先启动一个容器,来对目录进行更改用户和授权
2.容器需要连接数据,那么可以让初始化容器检测数据库是否可以正常连接,如果可以再启动主容器

## hook
PostStart:在容器启动创建后,立即执行,但时间不能太长,否则容器不会是running状态
PreStop:在容器停止前,执行一些命令,主要用于优雅关闭程序

## liveness probe
存活探针,用于定义容器内,应用是否满足探针指定状态

## readiness probe
就绪探针,指定何时允许容器进入流量

Pod对容器的封装和应用

那么我们在工作中,一个pod究竟要放几个容器?

在实际工作中我们除了完成任务以外还需要考虑以后扩容的问题,就拿wordpress举例,有以下两种方案:

第一种:全部放一个pod里

第二种:Wordpress和MySQL分开

那么如何扩容呢?如果第一种方案大家会发现没有办法很好的扩容,因为数据库和wordpress已经绑定成一个整体了,扩容wordpress就得扩容mysql。而第二种方案就要灵活的多。

初始化容器

apiVersion: "v1"
kind: "Pod"
metadata:
  labels:
    app: nginx-init
  name: nginx-init-v1
spec:
  volumes:
  - name: code
    emptyDir: {}

  initContainers:
  - name: init
    args: ["/bin/sh", "-c", "echo k8s >> /usr/share/nginx/html/index.html"]
    image: busybox
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: code
      mountPath: /usr/share/nginx/html/

  containers:
  - name: nginx01
    image: nginx:alpine
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: code
      mountPath: /usr/share/nginx/html/
      
[root@k8s-m01 ~]# kubectl apply -f init.nginx.yaml

[root@k8s-n01 ~]# cat /var/lib/kubelet/pods/2c2f21d6-d1c5-4b95-b7e3-44e6a0e877ba/volumes/kubernetes.io~empty-dir/code/index.html 
k8s

hook

# 启动之后钩子和停止钩子
[root@k8s-m01 ~]# kubectl apply -f hook-nginx.yaml 
apiVersion: "v1"
kind: "Pod"
metadata:
  labels:
    app: nginx-hook
  name: nginx-hook-v1
spec:
  volumes:
  - name: code
    hostPath:
      path: /tmp/html

  containers:
  - name: nginx01
    image: nginx:alpine
    imagePullPolicy: IfNotPresent
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo postStart  >> /usr/share/nginx/html/index.html"]
      preStop:
        exec:
          command: ["/bin/sh", "-c", "echo bye >> /usr/share/nginx/html/1.html"]
    volumeMounts:
    - name: code
      mountPath: /usr/share/nginx/html/
      
[root@k8s-n02 ~]# cat /tmp/html/index.html 
postStart

[root@k8s-n02 ~]# cat /tmp/html/1.html 
bye

存活性探针

[root@elkstack01 ~]# vim liveness-nginx.yaml 
apiVersion: "v1"
kind: "Pod"
metadata:
  labels:
    app: nginx-liveness
  name: nginx-liveness-v1
spec:
  volumes:
  - name: code
    hostPath:
      path: /tmp/html

  containers:
  - name: nginx01
    image: nginx:alpine
    imagePullPolicy: IfNotPresent
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo livenessProbe  >> /usr/share/nginx/html/index.html"]
    livenessProbe:
      exec:
        command: ["/bin/sh", "-c" ,"cat /usr/share/nginx/html/index.html"]
      initialDelaySeconds: 3
      periodSeconds: 1
    volumeMounts:
    - name: code
      mountPath: /usr/share/nginx/html/




initialDelaySeconds: 3   // 容器启动之后,等待N秒执行探针指定的命令
      periodSeconds: 1   // 探针执行的间隔时间,默认10s,最小为1s
      
      
## 使用httpGet的方式检测网站是否可以正常访问
[root@elkstack01 ~]# cat liveness-nginx.yaml
apiVersion: "v1"
kind: "Pod"
metadata:
  labels:
    app: nginx-liveness
  name: nginx-liveness-v2
spec:
  volumes:
  - name: code
    hostPath:
      path: /tmp/html

  containers:
  - name: nginx01
    image: nginx:alpine
    imagePullPolicy: IfNotPresent
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo livenessProbe httpGet > /usr/share/nginx/html/index.html"]
    livenessProbe:
      exec:
        #command: ["/bin/sh", "-c" ,"cat /usr/share/nginx/html/index.html"]
      httpGet:
        path: /index.html
        port: 80
      initialDelaySeconds: 3
      periodSeconds: 1
    volumeMounts:
    - name: code
      mountPath: /usr/share/nginx/html/

就绪态探针

apiVersion: v1
kind: Pod
metadata:
  name: readness-nginx
spec:
  containers:
  - name: readness-nginx
    image: nginx:alpine
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo ok > /usr/share/nginx/html/health.html"]
    readinessProbe:
      httpGet:
        path: /zls
        port: 8080
      initialDelaySeconds: 5
      timeoutSeconds: 3
      periodSeconds: 3
      successThreshold: 3
      failureThreshold: 3
    livenessProbe:
      httpGet:
        path: /health.html
        port: 80
      initialDelaySeconds: 3
      periodSeconds: 3
      
initialDelaySeconds: 5    // 容器启动后,等待几秒,执行就绪探针的指令
timeoutSeconds: 3         // 就绪探针执行的超时时间
periodSeconds: 3          // 每个N秒,执行一次就绪态探针指令,默认10s,最小1s
successThreshold: 3       // 如果3次成功,才放行流量
failureThreshold: 3       // 如果3次失败,就彻底失败,一直不放行流量



有时候,应用会暂时性地无法为请求提供服务。 例如,应用在启动时可能需要加载大量的数据或配置文件,或是启动后要依赖等待外部服务。 在这种情况下,既不想杀死应用,也不想给它发送请求。 Kubernetes 提供了就绪探针来发现并缓解这些情况。 容器所在 Pod 上报还未就绪的信息,并且不接受通过 Kubernetes Service 的流量

标签:容器,生命周期,name,share,nginx,html,usr,POD
From: https://www.cnblogs.com/wangchengww/p/16711931.html

相关文章

  • pod内部java.net.UnknownHostException
    周日中午接应用电话,反馈有个应用异常,查看pod日志中,出现以下的报警java.net.UnknownHostException:channel-ndc-imp查看channel-ndc-imp服务,是正常的。尝试登录某个pod,n......
  • Netty 学习(四):ChannelHandler 的事件传播和生命周期
    Netty学习(四):ChannelHandler的事件传播和生命周期作者:Grey原文地址:博客园:Netty学习(四):ChannelHandler的事件传播和生命周期CSDN:Netty学习(四):ChannelHandler的事件......
  • podman+openresty+openssl,https双向认证demo测试
    前言暂不讨论https原理,单论配置的话:1.https单项认证server:server.crt+server.keyclient:server_ca.crt2.https双向认证server:server.crt+server.key......
  • k8s给pod添加hosts
    ###1.背景线上待办功能一直不通,发现正式环境的待办系统域名无法访问,需要配置hosts。因为应用部署在云上,需使用k8s给pod添加域名IP映射。2.实战过程使用yaml方式创......
  • podman学习随笔
    podman基本使用方法一、装包[root@localhost~]#yummoduleinstallpodman二、镜像基本操作2.1配置文件相关[root@localhost~]#vim/etc/containers/registrie......
  • 数据生命周期管理的作用
    数据生命周期管理的作用(1)降低数据安全风险数据在企业内存在损毁、泄露等显性风险,同时也存在由于数据生命周期导致的数据决策管理和数据驱动失误等隐性风险。比如,企业内......
  • 26. Fragment生命周期
    26.Fragment生命周期26.1Fragment生命周期onAttach()/onDetach():绑定/解绑onCreate()/onDestroy():创建/销毁创建时,解析bundleonCreateView()/onDestroyView():对UI......
  • 单例以及模板类的静态成员变量的生命周期
    我们有如下的单例设计模式的实现:template<typenameT>classOnceSingle{public:OnceSingle()=delete;OnceSingle&operator=(constOnceSingle<T>&m)=......
  • 简单定义一个生命周期
    简单定义一个拥有create,willStateUpdate和shouldStateUpdate三个类似生命周期的类,名字随意,不要介意classState{constructor(){this.state={hehe:"9"};......
  • 浅析UE4 Actor&Actor生命周期
    首先说明一下关于UE4中一些对象的名字前缀吧,虽然这个不是这一关于Actor的内容,但是后续都要用到,所以就先说明白。关于Class类前缀:派生自 Actor 的类前缀为A,比如ACont......