Halacs
Halacs

Reputation: 932

Using Kubernetes' hooks

I would like to try Kubernetes' hooks but I didn't find any example how I should do it. As far as I know, with this hooks I can run bash scripts in freshly created containers and prior to terminate them.

I've found just a short documentation which say this is possible but that's all.

Do somebody have an example or something useful info?

Thanks in advance.

Upvotes: 16

Views: 10655

Answers (3)

Shakiba Moshiri
Shakiba Moshiri

Reputation: 23914

official documents

example Define postStart and preStop handlers

apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    image: nginx
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
      preStop:
        exec:
          command: ["/bin/sh","-c","nginx -s quit; while killall -0 nginx; do sleep 1; done"]

Upvotes: 0

Robert Bailey
Robert Bailey

Reputation: 18230

I don't see any examples .yaml files, but Kubernetes API v1 describes the lifecycle events in the same manner. Currently, only PostStart and PreStop are defined and you should be able to use them by adding a lifecycle section to a container in your pod definition.

Based on reading the API definition, something like this should work (disclaimer: I haven't actually tried it myself):

containers:
  - name: lifecycle
    image: busybox
    lifecycle:
      postStart:
        exec:
          command:
            - "touch"
            - "/var/log/lifecycle/post-start"
      preStop:
        httpGet:
          path: "/abort"
          port: 8080

Upvotes: 14

Halacs
Halacs

Reputation: 932

With the above answer I could try postStart hook, and I found a bug which was solved at the end of the last year but not published it yet in Fedora's testing repository just in rawhide repo.

The repos should be updated in the next couple of days.

Further details: https://github.com/kubernetes/kubernetes/issues/3930

Upvotes: 0

Related Questions