Using Kubernetes' hooks
Asked Answered
W

3

16

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.

Woodall answered 26/1, 2015 at 14:22 Comment(0)
B
14

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
Beech answered 27/1, 2015 at 6:40 Comment(1)
Thanks it helped me, but it just almost works yet. My pod become to be in running state but in a little time after it fails with these message on the minion site: "kubelet.go:811] Error running pod apache.default.etcd container master: failed to call event handler: docker server version missing from server version output - &[Arch=amd64 GitCommit=5bc2ff8/1.4.1 GoVersion=go1.3.3 KernelVersion=3.11.10-301.fc20.x86_64 Os=linux Version=1.4.1 ApiVersion=1.16]" It can be a bug maybe? Here is my pod definitions:pastebin.com/VVqRQpZmWoodall
W
0

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

Woodall answered 30/1, 2015 at 11:55 Comment(0)
F
0

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"]
Femur answered 6/8 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.