-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35918 from iocanel/wait-for-config-and-doc
Document init tasks
- Loading branch information
Showing
15 changed files
with
260 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
//// | ||
This guide is maintained in the main Quarkus repository | ||
and pull requests should be submitted there: | ||
https://github.com/quarkusio/quarkus/tree/main/docs/src/main/asciidoc | ||
//// | ||
= Initialization tasks | ||
:categories: initialization | ||
:summary: This reference guide explains how to configure initialization tasks | ||
|
||
There are often initialization tasks performed by Quarkus extensions that are meant to be run once. | ||
For example, Flyway or Liquibase initialization falls into that category. But what happens when the scaling | ||
needs of an application requires more instances of the application to run? Or what happens when the application | ||
restarts ? | ||
|
||
A common environment where both of these cases are pretty common is Kubernetes. To address these challenges, | ||
Quarkus allows externalization of such tasks as Kubernetes https://kubernetes.io/docs/concepts/workloads/controllers/job/[Jobs] and uses https://kubernetes.io/docs/concepts/workloads/pods/init-containers/[init containers] to ensure that an | ||
application instance only starts once the initialization jobs have finished. With this approach even if an | ||
application has multiple replicas, the initialization logic will only run once. | ||
|
||
This approach is reflected in the manifests generated by xref:kubernetes.adoc[Kubernetes extension]. | ||
|
||
== Disabling the feature | ||
|
||
The feature can be explictily disabled per task (enabled by default). | ||
The default behavior can change by setting the following property to `false`: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.kubernetes.init-task-defaults.enabled=false | ||
---- | ||
|
||
or on Openshift: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.openshift.init-task-defaults.enabled=false | ||
---- | ||
|
||
**Note**: All the configuration options in this guide are available on both OpenShift and Kubernetes. The rest of the guide will use Kubernetes(`quarkus.kubernetes` prefix) | ||
configuration prefix, but all the configuration options are also available for OpenShift(`quarkus.openshift` prefix) too. | ||
|
||
In the case where we need to disable a particular task, we can use the following property: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.kubernetes.init-tasks."<task name>".enabled=false | ||
---- | ||
|
||
The task name is the name of the extension that performs the initialization. | ||
Examples: | ||
|
||
For Flyway: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.kubernetes.init-tasks.flyway.enabled=false | ||
---- | ||
|
||
For Liquibase: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.kubernets.init-tasks.liquibase.enabled=false | ||
---- | ||
|
||
For Liquibase Mongodb: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.kubernetes.init-tasks.liquibase-mongodb.enabled=false | ||
---- | ||
|
||
|
||
== Controlling the generated job | ||
|
||
The job container is pretty similar to the application container, and the only thing that changes is the configured environment variables. | ||
More specifically, the following environment variable is added, to tell the job to exit right after initialization. | ||
|
||
[source,properties] | ||
---- | ||
QUARKUS_INIT_AND_EXIT=true | ||
---- | ||
|
||
The image, image pull policy, service account, volumes, mounts and additional environment variables are inherited/copied from the deployment resource. | ||
Any customization to the original deployment resource (via configuration or extension) will also be reflected in the job. | ||
|
||
== Controlling the generated init container | ||
|
||
The name of the generated init container is `wait-for-${task name}` by default. | ||
Given that the init container is part of the same pod as the actual application it will get the same service account (and therefore permissions) and volumes as the application. | ||
Further customization to the container can be done using using the configuration options for init containers (see `quarkus.kubernetes.init-containers` or `quarkus.openshift.init-containers`). | ||
|
||
Examples: | ||
|
||
To set the imagePullPolicy to `IfNotPresent` on the init container that waits for the `flyway` job: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.kubernetes.init-containers.wait-for-flyway.image-pull-policy=IfNotPresent | ||
---- | ||
|
||
To set custom command (say `custom-wait-for`) on the init container that waits for the `flyway` job: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.kubernetes.init-containers.wait-for-flyway.command=custom-wait-for | ||
---- | ||
|
||
|
||
== Orchestration of the initialization tasks | ||
|
||
The deployment resource should not start until the job has been completed. The typical pattern that is used among Kubernetes users is the | ||
use of init containers to achieve this. An init container that `wait for` the job to complete is enough to enforce that requirement. | ||
|
||
=== Using a custom wait-for container image | ||
|
||
To change the `wait-for` image which by default is `groundnuty/k8s-wait-for:no-root-v1.7` you can use: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.kubernetes.init-task-defaults.wait-for-container.image=my/wait-for-image:1.0 | ||
---- | ||
|
||
To change the `wait-for` image for a particular init container (e.g. `wait-for-flway`) you can use: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.kubernetes.init-containers.wait-for-flyway=my/wait-for-image:1.0 | ||
---- | ||
|
||
=== Configuring permissions | ||
|
||
For an init container to be able to perform the `wait for job` it needs to be able to perform `get` operations on the job resource. | ||
This is done automatically and the generated manifests include the required `Role` and `RoleBinding` resources. | ||
|
||
If for any reason additional permissions are required either by the init container or the job, they can be configured with through the xref:deploying-to-kuberentes.adoc#generating-rbac-resources[Kubernetes RBAC configuration]. | ||
|
||
**Note**: The application, the init container and the job use the same `ServiceAccount` and therefore, share the same permissions. | ||
|
||
== Extension providing Initialization Tasks | ||
|
||
Currently, this feature is used by the following extensions: | ||
- xref:flyway.adoc[Flyway] | ||
- xref:liquibase.adoc[Liquibase] | ||
- xref:liquibase-mongodb.adoc[Liquibase MongoDB] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...la/deployment/src/main/java/io/quarkus/kubernetes/deployment/InitTaskContainerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.quarkus.kubernetes.deployment; | ||
|
||
import io.quarkus.runtime.annotations.ConfigGroup; | ||
import io.quarkus.runtime.annotations.ConfigItem; | ||
|
||
@ConfigGroup | ||
public class InitTaskContainerConfig { | ||
|
||
/** | ||
* The init task image to use by the init-container. | ||
*/ | ||
@ConfigItem(defaultValue = "groundnuty/k8s-wait-for:no-root-v1.7") | ||
public String image; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.