Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Kubernetes' hooks

Tags:

kubernetes

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.

like image 239
Halacs Avatar asked Jan 26 '15 14:01

Halacs


People also ask

What is hook in Kubernetes?

There are two types of hook handlers that can be implemented for Containers: Exec - Executes a specific command, such as pre-stop.sh , inside the cgroups and namespaces of the Container. Resources consumed by the command are counted against the Container.

What is Handler in Kubernetes?

There are two types of handlers that you can attach to a lifecycle hook: exec : It executes the specified command in the container's main process. The command is executed in parallel with the container's ENTRYPOINT instruction. If the hook takes too long or fails, the kubelet process will restart the container.

What is Kubernetes Terminationgraceperiodseconds?

4 - Kubernetes waits for a grace period At this point, Kubernetes waits for a specified time called the termination grace period. By default, this is 30 seconds. It's important to note that this happens in parallel to the preStop hook and the SIGTERM signal. Kubernetes does not wait for the preStop hook to finish.

How do pods communicate with each other?

A Pod can communicate with another Pod by directly addressing its IP address, but the recommended way is to use Services. A Service is a set of Pods, which can be reached by a single, fixed DNS name or IP address. In reality, most applications on Kubernetes use Services as a way to communicate with each other.


1 Answers

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
like image 65
Robert Bailey Avatar answered Sep 17 '22 01:09

Robert Bailey