Kubernetes的webhook钩子:preStop和postStart k8s
  AWkq7aIjuRwO 2023年11月02日 83 0

preStop和postStart

与探针类似其实叫webhook钩子

我们对我们的pod加一个钩子,两个钩子分别是:preStop和postStart

postStart:在容器创建完成后立即运行的钩子语句,类似操作系统上的rc.local这个文件里面内容

preStop:在容器中止之前立即运行的一个钩子语句,类似于关机之前执行的一个操作

这两个和我们的操作系统很相似,一个preStop和postStart

webhook理解成触发器

查看一下

kubectl explain pod.spec.containers
   lifecycle    <Object>
     Actions that the management system should take in response to container
     lifecycle events. Cannot be updated.
[root@k8s-master1 ~]# kubectl explain pod.spec.containers.lifecycle
KIND:     Pod
VERSION:  v1

RESOURCE: lifecycle <Object>

DESCRIPTION:
     Actions that the management system should take in response to container
     lifecycle events. Cannot be updated.

     Lifecycle describes actions that the management system should take in
     response to container lifecycle events. For the PostStart and PreStop
     lifecycle handlers, management of the container blocks until the action is
     complete, unless the container process fails, in which case the handler is
     aborted.

FIELDS:
   postStart    <Object>
   #容器创造后立即运行
     PostStart is called immediately after a container is created. If the
     handler fails, the container is terminated and restarted according to its
     restart policy. Other management of the container blocks until the hook
     completes. More info:
     https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks

   preStop      <Object>
   #容器结束前运行的
     PreStop is called immediately before a container is terminated due to an
     API request or management event such as liveness/startup probe failure,
     preemption, resource contention, etc. The handler is not called if the
     container crashes or exits. The Pod's termination grace period countdown
     begins before the PreStop hook is executed. Regardless of the outcome of
     the handler, the container will eventually terminate within the Pod's
     termination grace period (unless delayed by finalizers). Other management
     of the container blocks until the hook completes or until the termination
     grace period is reached. More info:
     https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks

一般来说是个命令行

kubectl explain pod.spec.containers.lifecycle.postStart

KIND:     Pod
VERSION:  v1

RESOURCE: postStart <Object>

DESCRIPTION:
     PostStart is called immediately after a container is created. If the
     handler fails, the container is terminated and restarted according to its
     restart policy. Other management of the container blocks until the hook
     completes. More info:
     https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/#container-hooks

     LifecycleHandler defines a specific action that should be taken in a
     lifecycle hook. One and only one of the fields, except TCPSocket must be
     specified.

FIELDS:
   exec <Object>
   #命令行
     Exec specifies the action to take.

   httpGet      <Object>
     HTTPGet specifies the http request to perform.

   tcpSocket    <Object>
     Deprecated. TCPSocket is NOT supported as a LifecycleHandler and kept for
     the backward compatibility. There are no validation of this field and
     lifecycle hooks will fail in runtime when tcp handler is specified.
kubectl explain pod.spec.containers.lifecycle.postStart.exec
KIND:     Pod
VERSION:  v1

RESOURCE: exec <Object>

DESCRIPTION:
     Exec specifies the action to take.

     ExecAction describes a "run in container" action.

FIELDS:
   command      <[]string>
     Command is the command line to execute inside the container, the working
     directory for the command is root ('/') in the container's filesystem. The
     command is simply exec'd, it is not run inside a shell, so traditional
     shell instructions ('|', etc) won't work. To use a shell, you need to
     explicitly call out to that shell. Exit status of 0 is treated as
     live/healthy and non-zero is unhealthy.
[root@k8s-master1 pod]# cat nginx-pod-3.yml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-4
spec:
  containers:
  - name: nginx
    image: images.guoguo.com/apps/ubuntu-nginx:1.24.0
    ports:
    - containerPort: 80
    startupProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 10
      timeoutSeconds: 2
      failureThreshold: 2
      successThreshold: 1
      periodSeconds: 5
    readinessProbe:
      httpGet:
        port: 80
        path: /index.html
      initialDelaySeconds: 10
      timeoutSeconds: 2
      failureThreshold: 2
      successThreshold: 1
      periodSeconds: 5
    livenessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 10
      timeoutSeconds: 2
      failureThreshold: 2
      successThreshold: 1
      periodSeconds: 5
    lifecycle:          #生命周期 也就是这俩钩子的爹
      postStart:        #容器创建后执行的
        exec:           #命令行
          command: ['/bin/bash','-c','mkdir /apps/data']    #必须找个格式 使用/bin/bash -c 用mkdir 创建/apps/data目录
      preStop:          #容器结束前执行的  语法和postStart一样
        exec:
          command: [ '/bin/bash','-c','sleep 10']   #睡眠十秒
  restartPolicy: Always   #pod重启策略  Always 不管咋样 都要重启

创建

[root@k8s-master1 pod]# kubectl apply -f nginx-pod-3.yml
pod/nginx-4 created
[root@k8s-master1 pod]# kubectl get pods
NAME      READY   STATUS    RESTARTS   AGE
nginx-4   1/1     Running   0          25s

然后我们看下pod里面看看/apps下有没有创建data目录

[root@k8s-master1 pod]# kubectl exec nginx-4 -- ls -d /apps/data
/apps/data
#创建了data目录

停止的话就不演示了,设置了sleep 10 睡眠1秒后才进行删除

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

AWkq7aIjuRwO