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 的流量
posted @ 2022-09-20 17:43  Gabydawei  阅读(29)  评论(0编辑  收藏  举报