Quarkus - Generating Kubernetes resources

    To complete this guide, you need:

    • roughly 10 minutes

    • an IDE

    • JDK 1.8+ installed with configured appropriately

    • Apache Maven 3.5.3+

    • access to a Kubernetes or cluster (Minikube is a viable options)

    Creating the Maven project

    First, we need a new project that contains the Kubernetes extension. This can be done using the following command:

    Quarkus offers the ability to automatically generate Kubernetes resources based on sane defaults and user supplied configuration. The implementation that takes careof generating the actual Kubernetes resources is provided by . Currently it supports the generation of resources forvanilla Kubernetes and OpenShift.

    When we added the kubernetes extension to the command line invocation above, the following dependency was added to the pom.xml

    1. <dependency>
    2. <groupId>io.quarkus</groupId>
    3. <artifactId>quarkus-kubernetes</artifactId>
    4. </dependency>

    By using the following configuration for example:

    1. kubernetes.group=yourDockerUsername # this is optional and defaults to your username if not set.
    2. quarkus.application.name=test-quarkus-app # this is also optional and defaults to the project name if not set

    and following the execution of ./mvnw package you will notice amongst the other files that are created, two files namedkubernetes.json and kubernetes.yaml in the target/kubernetes/ directory.

    If you look at either file you will see that it contains both a Kubernetes Deployment and a Service.

    The full source of the kubernetes.json file looks something like this:

    1. {
    2. "apiVersion" : "v1",
    3. "items" : [ {
    4. "apiVersion" : "apps/v1",
    5. "kind" : "Deployment",
    6. "metadata" : {
    7. "labels" : {
    8. "app" : "test-quarkus-app",
    9. "version" : "1.0-SNAPSHOT",
    10. "group" : "yourDockerUsername"
    11. },
    12. "name" : "test-quarkus-app"
    13. "spec" : {
    14. "replicas" : 1,
    15. "selector" : {
    16. "matchLabels" : {
    17. "app" : "test-quarkus-app",
    18. "version" : "1.0-SNAPSHOT",
    19. "group" : "yourDockerUsername"
    20. }
    21. },
    22. "template" : {
    23. "metadata" : {
    24. "labels" : {
    25. "app" : "test-quarkus-app",
    26. "version" : "1.0-SNAPSHOT",
    27. "group" : "yourDockerUsername"
    28. }
    29. "spec" : {
    30. "containers" : [ {
    31. "env" : [ {
    32. "name" : "KUBERNETES_NAMESPACE",
    33. "valueFrom" : {
    34. "fieldRef" : {
    35. "fieldPath" : "metadata.namespace"
    36. }
    37. }
    38. } ],
    39. "image" : "yourDockerUsername/test-quarkus-app:1.0-SNAPSHOT",
    40. "imagePullPolicy" : "IfNotPresent",
    41. "name" : "test-quarkus-app"
    42. } ]
    43. }
    44. }
    45. } ]
    46. }

    An important thing to note about the Deployment is that is uses yourDockerUsername/test-quarkus-app:1.0-SNAPSHOT as the Docker image of the Pod.

    Also the Service is configured to use container port 8080 (which is automatically picked up by the standard Quarkus configuration).

    By default the Kubernetes resources do not contain readiness and liveness probes in the generated Deployment. Adding them however is just a matter of adding the Smallrye Health extension like so:

    The values of the generated probes will be determined by the configured health properties: quarkus.smallrye-health.root-path, quarkus.smallrye-health.liveness-path and quarkus.smallrye-health.readiness-path.More information about the health extension can be found in the relevant guide.

    Tuning the generated resources using application.properties

    The Kubernetes extension allows tuning the generated manifest, using the application.properties file.Here are some examples:

    Changing the number of replicas:

    1. kubernetes.replicas=3

    The docker registry and the user of the docker image can be specified, with the following properties:

    1. kubernetes.group=myUser
    2. kubernetes.registry=http://my.docker-registry.net

    Note: These options used to be quarkus.kubernetes.docker.registry and quarkus.kubernetes.group respectively.

    Adding labels:

    To add a new label to all generated resources, say foo=bar:

    1. kubernetes.labels[0].key=foo
    2. kubernetes.labels[0].value=bar

    To set the initial delay of the probe to 20 seconds and the period to 45:

    Here you can find a complete reference to all the available configuration options:

    Configuration options

    The table below describe all the available configuration options.

    Properties that use non standard types, can be referenced by expanding the property.For example to define a kubernetes-readiness-probe which is of type Probe:

    1. kubernetes.readiness-probe.initial-delay-seconds=20
    2. kubernetes.readiness-probe.period-seconds=45

    In this example initial-delay and period-seconds are fields of the type Probe.Below you will find tables describing all available types.

    Basic Types
    Table 2. Annotation
    PropertyTypeDescriptionDefault Value
    keyString
    valueString
    Table 3. Label
    PropertyTypeDescriptionDefault Value
    keyString
    valueString
    Table 4. Env
    PropertyTypeDescriptionDefault Value
    nameString
    valueString
    secretString
    configmapString
    fieldString
    Table 5. Probe
    PropertyTypeDescriptionDefault Value
    —————————————————————+———————-http-action-pathString
    exec-actionString
    tcp-socket-actionString
    initial-delay-secondsint
    0period-secondsint
    30timeout-secondsint
    Table 6. Port
    PropertyTypeDescriptionDefault Value
    nameString
    container-portint
    hostPortint0
    pathString/
    protocolProtocolTCP
    Table 8. Mount
    PropertyTypeDescriptionDefault Value
    nameString
    pathString
    sub-pathString
    read-onlybooleanfalse
    Table 9. ConfigMapVolume
    PropertyTypeDescriptionDefault Value
    volume-nameString
    config-map-nameString
    default-modeint384
    optionalbooleanfalse
    Table 10. SecretVolume
    PropertyTypeDescriptionDefault Value
    volume-nameString
    secret-nameString
    default-modeint384
    optionalbooleanfalse
    Table 11. AzureDiskVolume
    PropertyTypeDescriptionDefault Value
    volume-nameString
    disk-nameString
    disk-uriString
    kindStringManaged
    caching-modeStringReadWrite
    fs-typeStringext4
    read-onlybooleanfalse
    Table 12. AwsElasticBlockStoreVolume
    PropertyTypeDescriptionDefault Value
    volume-nameString
    volume-idString
    partitionint
    fs-typeStringext4
    read-onlybooleanfalse
    Table 14. PersistentVolumeClaimVolume
    PropertyTypeDescriptionDefault Value
    volume-nameString
    claim-nameString
    read-onlybooleanfalse
    Table 15. AzureFileVolume
    PropertyTypeDescriptionDefault Value
    volume-nameString
    share-nameString
    secret-nameString
    read-onlybooleanfalse

    Docker

    Table 16. Docker
    PropertyTypeDescriptionDefault Value
    docker-fileStringDockerfile
    registryString
    auto-push-enabledboolean
    falseauto-build-enabledboolean

    OpenShift support

    To enable the generation of OpenShift resources, you need to include OpenShift in the target platforms:

    1. kubernetes.deployment.target=openshift

      The OpenShift resources can be customized in a similar approach with Kubernetes.

      Table 17. Openshift
      PropertyTypeDescriptionDefault Value
      openshift.groupString
      openshift.nameString
      openshift.versionString
      openshift.init-containersContainer[]
      openshift.labelsLabel[]
      openshift.annotationsAnnotation[]
      openshift.env-varsEnv[]
      openshift.working-dirString
      openshift.commandString[]
      openshift.argumentsString[]
      openshift.replicasint1
      openshift.service-accountString
      openshift.hostString
      openshift.portsPort[]
      openshift.service-typeServiceTypeClusterIP
      openshift.pvc-volumesPersistentVolumeClaimVolume[]
      openshift.secret-volumesSecretVolume[]
      openshift.config-map-volumesConfigMapVolume[]
      openshift.git-repo-volumesGitRepoVolume[]
      openshift.aws-elastic-block-store-volumesAwsElasticBlockStoreVolume[]
      openshift.azure-disk-volumesAzureDiskVolume[]
      openshift.azure-file-volumesAzureFileVolume[]
      openshift.mountsMount[]
      openshift.image-pull-policyImagePullPolicyIfNotPresent
      openshift.image-pull-secretsString[]
      openshift.liveness-probeProbe( see Probe )
      openshift.readiness-probeProbe( see Probe )
      openshift.sidecarsContainer[]
      openshift.exposebooleanfalse
      openshift.auto-deploy-enabledbooleanfalse
      Table 18. S2i
      PropertyTypeDescriptionDefault Value
      s2i.enabledbooleantrue
      s2i.registryString
      s2i.builder-imageStringfabric8/s2i-java:2.3
      s2i.build-env-varsEnv[]
      s2i.auto-push-enabledbooleanfalse
      s2i.auto-build-enabledbooleanfalse