mirror of
https://github.com/openshift/openshift-docs.git
synced 2026-02-06 06:46:26 +01:00
112 lines
3.2 KiB
Plaintext
112 lines
3.2 KiB
Plaintext
// Module included in the following assemblies:
|
|
//
|
|
// * applications/deployments/deployment-strategies.adoc
|
|
|
|
[id="deployments-lifecycle-hooks_{context}"]
|
|
= Lifecycle hooks
|
|
|
|
The Rolling and Recreate strategies support _lifecycle hooks_, or deployment
|
|
hooks, which allow behavior to be injected into the deployment process at
|
|
predefined points within the strategy:
|
|
|
|
.Example `pre` lifecycle hook
|
|
[source,yaml]
|
|
----
|
|
pre:
|
|
failurePolicy: Abort
|
|
execNewPod: {} <1>
|
|
----
|
|
<1> `execNewPod` is a Pod-based lifecycle hook.
|
|
|
|
Every hook has a `failurePolicy`, which defines the action the strategy should
|
|
take when a hook failure is encountered:
|
|
|
|
[cols="2,8"]
|
|
|===
|
|
|
|
.^|`Abort`
|
|
|The deployment process will be considered a failure if the hook fails.
|
|
|
|
.^|`Retry`
|
|
|The hook execution should be retried until it succeeds.
|
|
|
|
.^|`Ignore`
|
|
|Any hook failure should be ignored and the deployment should proceed.
|
|
|===
|
|
|
|
Hooks have a type-specific field that describes how to execute the hook.
|
|
Currently, Pod-based hooks are the only supported hook type, specified by the
|
|
`execNewPod` field.
|
|
|
|
[discrete]
|
|
==== Pod-based lifecycle hook
|
|
|
|
Pod-based lifecycle hooks execute hook code in a new Pod derived from the
|
|
template in a DeploymentConfig.
|
|
|
|
The following simplified example DeploymentConfig uses the Rolling strategy.
|
|
Triggers and some other minor details are omitted for brevity:
|
|
|
|
[source,yaml]
|
|
----
|
|
kind: DeploymentConfig
|
|
apiVersion: v1
|
|
metadata:
|
|
name: frontend
|
|
spec:
|
|
template:
|
|
metadata:
|
|
labels:
|
|
name: frontend
|
|
spec:
|
|
containers:
|
|
- name: helloworld
|
|
image: openshift/origin-ruby-sample
|
|
replicas: 5
|
|
selector:
|
|
name: frontend
|
|
strategy:
|
|
type: Rolling
|
|
rollingParams:
|
|
pre:
|
|
failurePolicy: Abort
|
|
execNewPod:
|
|
containerName: helloworld <1>
|
|
command: [ "/usr/bin/command", "arg1", "arg2" ] <2>
|
|
env: <3>
|
|
- name: CUSTOM_VAR1
|
|
value: custom_value1
|
|
volumes:
|
|
- data <4>
|
|
----
|
|
<1> The `helloworld` name refers to `spec.template.spec.containers[0].name`.
|
|
<2> This `command` overrides any `ENTRYPOINT` defined by the `openshift/origin-ruby-sample` image.
|
|
<3> `env` is an optional set of environment variables for the hook container.
|
|
<4> `volumes` is an optional set of volume references for the hook container.
|
|
|
|
In this example, the `pre` hook will be executed in a new Pod using the
|
|
`openshift/origin-ruby-sample` image from the `helloworld` container. The hook
|
|
Pod has the following properties:
|
|
|
|
* The hook command is `/usr/bin/command arg1 arg2`.
|
|
* The hook container has the `CUSTOM_VAR1=custom_value1` environment variable.
|
|
* The hook failure policy is `Abort`, meaning the deployment process fails if the hook fails.
|
|
* The hook Pod inherits the `data` volume from the DeploymentConfig Pod.
|
|
|
|
[id="deployments-setting-lifecycle-hooks_{context}"]
|
|
== Setting lifecycle hooks
|
|
|
|
You can set lifecycle hooks, or deployment hooks, for a DeploymentConfig using
|
|
the CLI.
|
|
|
|
.Procedure
|
|
|
|
. Use the `oc set deployment-hook` command to set the type of hook you want:
|
|
`--pre`, `--mid`, or `--post`. For example, to set a pre-deployment hook:
|
|
+
|
|
----
|
|
$ oc set deployment-hook dc/frontend \
|
|
--pre -c helloworld -e CUSTOM_VAR1=custom_value1 \
|
|
-v data --failure-policy=abort -- /usr/bin/command arg1 arg2
|
|
----
|