Poststart hook

As the name suggests, this hook allows you to execute some logic after the container is started
There are three key points to explain

  • Poststart and the main thread of the container execute asynchronously. They are not started at the same time after the main thread is started
  • The container will be in the state of postpending before it runs
  • The semantics of the hook contains the meaning of “execute at least once”. Therefore, repeated execution of the hook will affect the system, which needs careful consideration

For the first question

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        lifecycle:
          postStart:
            exec:
              command:
              - sh
              - -c
              - date "+%Y-%m-%d %H:%M:%S" > /datetmp.txt && sleep 30
        ports:
        - containerPort: 80
        livenessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 30
        readinessProbe:
          httpGet:
            path: /
            port: 80
            scheme: HTTP
          initialDelaySeconds: 30

You can see that the start time of poststart is the same as that of the container
Because my current system time is UTC + 8, it is 9:23 in datetmp and 17:23 in nginx console log, but they do start at the same time instead of waiting until after the main thread

For the second question

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        lifecycle:
          postStart:
            exec:
              command:
              - sh
              - -c
              - echo "postStart postStart postStart" && sleep infinity
        ports:
        - containerPort: 80
        livenessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 30
        readinessProbe:
          httpGet:
            path: /
            port: 80
            scheme: HTTP
          initialDelaySeconds: 30

The difference between the waiting state of the poststart hook and the ready probe is
It does not acknowledge that the container has started
Note that the start status is false, while the start status of the ready probe is true

When a single pod is installed in three containers, the poststart hooks of the three containers and the three mainlines and six scripts of the container will be started at the same time

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        lifecycle:
          postStart:
            exec:
              command:
              - sh
              - -c
              - echo "postStart postStart postStart" && sleep 30
      - name: u1
        command:
        - sleep
        - infinity
        image: ubuntu
        lifecycle:
          postStart:
            exec:
              command:
              - sh
              - -c
              - date "+%Y-%m-%d %H:%M:%S" >> /datetmp.txt
      - name: u2
        command:
        - sleep
        - infinity
        image: ubuntu
        lifecycle:
          postStart:
            exec:
              command:
              - sh
              - -c
              - date "+%Y-%m-%d %H:%M:%S" >> /datetmp.txt

Here, the Ubuntu container is blocked for 30 seconds, but the nginx is blocked forever. As a result, the three containers on this pod cannot be started successfully at the same time

Therefore, although the poststart hook acts on the container, it will affect other containers on the pod
Change nginx blocking to 30 seconds and Ubuntu to 5 seconds
You can see that after 5 seconds and about 10 seconds, the three containers are still in the starting state

You can also see that on the two Ubuntu, the poststart hook is asynchronous and started at the same time

Send a Message