-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve usability of gRPC probes in Kubernetes #33437
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,92 @@ | ||
//// | ||
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 | ||
//// | ||
= Deploying your gRPC Service in Kubernetes | ||
include::_attributes.adoc[] | ||
:categories: serialization | ||
:summary: This guide explains how to deploy your gRPC services in Quarkus to Kubernetes. | ||
|
||
This page explains how to deploy your gRPC service in Quarkus in Kubernetes. | ||
We'll continue with the example from xref:grpc-getting-started.adoc[the Getting Started gRPC guide]. | ||
|
||
== Configuring your project to use the Quarkus Kubernetes extension | ||
|
||
Add the Quarkus Kubernetes extension to your build file: | ||
|
||
[source,xml,role="primary asciidoc-tabs-target-sync-cli asciidoc-tabs-target-sync-maven"] | ||
.pom.xml | ||
---- | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-kubernetes</artifactId> | ||
</dependency> | ||
---- | ||
|
||
[source,gradle,role="secondary asciidoc-tabs-target-sync-gradle"] | ||
.build.gradle | ||
---- | ||
implementation("io.quarkus:quarkus-kubernetes") | ||
---- | ||
|
||
Next, we want to expose our application using the Kubernetes Ingress resource: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.kubernetes.ingress.expose=true | ||
---- | ||
|
||
The Quarkus Kubernetes will bind the HTTP server using the port name `http` and the gRPC server using the port name `grpc`. By default, the Quarkus application will only expose the port name `http`, so only the HTTP server will be publicly accessible. To expose the gRPC server instead, set the `quarkus.kubernetes.ingress.target-port=grpc` property in your application.properties: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.kubernetes.ingress.target-port=grpc | ||
---- | ||
|
||
TIP: If you configure Quarkus to use the same port for both HTTP and gRPC servers with the property `quarkus.grpc.server.use-separate-server=false`, then you don't need to change the default `target-port`. | ||
|
||
Finally, we need to generate the Kubernetes manifests by running the command in a terminal: | ||
|
||
include::{includes}/devtools/build.adoc[] | ||
|
||
Once generated, you can look at the `target/kubernetes` directory: | ||
|
||
[source,txt] | ||
---- | ||
target/kubernetes | ||
└── kubernetes.json | ||
└── kubernetes.yml | ||
---- | ||
|
||
You can find more information about how to deploy the application in Kubernetes in the xref:deploying-to-kubernetes.adoc#deployment[the Kubernetes guide]. | ||
|
||
== Using gRPC Health probes | ||
|
||
By default, the Kubernetes resources do not contain readiness and liveness probes. To add them, import the Smallrye Health extension to your build file: | ||
|
||
[source,xml,role="primary asciidoc-tabs-target-sync-cli asciidoc-tabs-target-sync-maven"] | ||
.pom.xml | ||
---- | ||
<dependency> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-smallrye-health</artifactId> | ||
</dependency> | ||
---- | ||
|
||
[source,gradle,role="secondary asciidoc-tabs-target-sync-gradle"] | ||
.build.gradle | ||
---- | ||
implementation("io.quarkus:quarkus-smallrye-health") | ||
---- | ||
|
||
TIP: More information about the health extension can be found in xref:microprofile-health.adoc[the Microprofile Health guide]. | ||
|
||
By the default, this extension will configure the probes to use the HTTP server (which is provided by some extensions like the Quarkus RESTEasy reactive extension). Internally, this probe will also use xref:grpc-service-implementation.adoc#health[the generated gRPC Health services]. | ||
|
||
If your application does not use any Quarkus extension that exposes an HTTP server, you can still configure the probes to directly use the gRPC Health service by adding the property `quarkus.kubernetes.readiness-probe.grpc-action-enabled=true` into your configuration: | ||
|
||
[source,properties] | ||
---- | ||
quarkus.kubernetes.readiness-probe.grpc-action-enabled=true | ||
---- |
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
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
20 changes: 16 additions & 4 deletions
20
...tes/vanilla/deployment/src/main/java/io/quarkus/kubernetes/deployment/ProbeConverter.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 |
---|---|---|
@@ -1,26 +1,38 @@ | ||
|
||
package io.quarkus.kubernetes.deployment; | ||
|
||
import org.eclipse.microprofile.config.ConfigProvider; | ||
|
||
import io.dekorate.kubernetes.config.Probe; | ||
import io.dekorate.kubernetes.config.ProbeBuilder; | ||
|
||
public class ProbeConverter { | ||
|
||
public static Probe convert(ProbeConfig probe) { | ||
return builder(probe).build(); | ||
public static Probe convert(String name, ProbeConfig probe) { | ||
return builder(name, probe).build(); | ||
} | ||
|
||
public static ProbeBuilder builder(ProbeConfig probe) { | ||
public static ProbeBuilder builder(String name, ProbeConfig probe) { | ||
ProbeBuilder b = new ProbeBuilder(); | ||
probe.httpActionPath.ifPresent(b::withHttpActionPath); | ||
probe.execAction.ifPresent(b::withExecAction); | ||
probe.tcpSocketAction.ifPresent(b::withTcpSocketAction); | ||
probe.grpcAction.ifPresent(b::withGrpcAction); | ||
if (probe.grpcAction.isPresent()) { | ||
b.withGrpcAction(probe.grpcAction.get()); | ||
} else if (probe.grpcActionEnabled) { | ||
b.withGrpcAction(getQuarkusGrpcPort() + ":" + name); | ||
} | ||
|
||
b.withInitialDelaySeconds((int) probe.initialDelay.getSeconds()); | ||
b.withPeriodSeconds((int) probe.period.getSeconds()); | ||
b.withTimeoutSeconds((int) probe.timeout.getSeconds()); | ||
b.withSuccessThreshold(probe.successThreshold); | ||
b.withFailureThreshold(probe.failureThreshold); | ||
return b; | ||
} | ||
|
||
private static int getQuarkusGrpcPort() { | ||
return ConfigProvider.getConfig().getOptionalValue("quarkus.grpc.server.port", Integer.class) | ||
.orElse(9000); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...andard-way/src/test/java/io/quarkus/it/kubernetes/KubernetesWithGrpcProbeEnabledTest.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,68 @@ | ||
package io.quarkus.it.kubernetes; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
import io.quarkus.builder.Version; | ||
import io.quarkus.maven.dependency.Dependency; | ||
import io.quarkus.test.ProdBuildResults; | ||
import io.quarkus.test.ProdModeTestResults; | ||
import io.quarkus.test.QuarkusProdModeTest; | ||
|
||
public class KubernetesWithGrpcProbeEnabledTest { | ||
|
||
private static final String APP_NAME = "kubernetes-with-grpc-probe"; | ||
|
||
@RegisterExtension | ||
static final QuarkusProdModeTest config = new QuarkusProdModeTest() | ||
.withApplicationRoot((jar) -> jar.addClasses(GreetingResource.class)) | ||
.setApplicationName(APP_NAME) | ||
.setApplicationVersion("0.1-SNAPSHOT") | ||
.overrideConfigKey("quarkus.kubernetes.readiness-probe.grpc-action-enabled", "true") | ||
.setLogFileName("k8s.log") | ||
.setForcedDependencies(List.of( | ||
Dependency.of("io.quarkus", "quarkus-kubernetes", Version.getVersion()), | ||
Dependency.of("io.quarkus", "quarkus-smallrye-health", Version.getVersion()))); | ||
|
||
@ProdBuildResults | ||
private ProdModeTestResults prodModeTestResults; | ||
|
||
@Test | ||
public void assertGeneratedResources() throws IOException { | ||
|
||
final Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes"); | ||
assertThat(kubernetesDir) | ||
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.json")) | ||
.isDirectoryContaining(p -> p.getFileName().endsWith("kubernetes.yml")); | ||
List<HasMetadata> kubernetesList = DeserializationUtil | ||
.deserializeAsList(kubernetesDir.resolve("kubernetes.yml")); | ||
assertThat(kubernetesList.get(0)).isInstanceOfSatisfying(Deployment.class, d -> { | ||
assertThat(d.getMetadata()).satisfies(m -> { | ||
assertThat(m.getName()).isEqualTo(APP_NAME); | ||
}); | ||
|
||
assertThat(d.getSpec()).satisfies(deploymentSpec -> { | ||
assertThat(deploymentSpec.getTemplate()).satisfies(t -> { | ||
assertThat(t.getSpec()).satisfies(podSpec -> { | ||
assertThat(podSpec.getContainers()).singleElement() | ||
.satisfies(container -> { | ||
assertThat(container.getReadinessProbe()).isNotNull().satisfies(p -> { | ||
assertEquals(p.getGrpc().getPort().intValue(), 9000); | ||
assertEquals(p.getGrpc().getService(), APP_NAME); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does that work with the new server where both http and gRPC are served on the same port?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you share more details about this? But I guess that if both HTTP and gRPC are handing by the same port, it will work fine using the default
http
port name.Though, if the port name
grpc
won't be generated any longer, we might need to revisit this statement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the old gRPC server, http and gRPC are served on different ports.
With the new gRPC server, HTTP and gRPC are served on the same port (which is very likely going to be https).
Both mode are supported at the moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alesj do you think we could add tests for both cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took a deeper look into this that was implemented as part of #28654.
So, by default, it creates two servers: one for HTTP and another one for gRPC.
If
quarkus.grpc.server.use-separate-server
isfalse
, then the gRPC port won't be listening.Note that the
grpc
port is still bound to the generated Deployment/Service resources even thoughquarkus.grpc.server.use-separate-server
is false. Becausequarkus.grpc.server.use-separate-server
is a runtime property, this might make sense. So, ultimately the user would need to select the target port (eitherhttp
orgrpc
) of the generated resource.We could again follow a best-effort approach, so if users use
quarkus.grpc.server.use-separate-server=false
at build time, we could remove the binding of thegrpc
port in the generated resources and usehttp
by default as the target port. Wdyt @cescoffier ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 or having an explicit property on the kubernetes side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have an explicit property on K8s side which is
quarkus.kubernetes.ingress.target-port
.I'd prefer doing
We could again follow a best-effort approach, so if users use quarkus.grpc.server.use-separate-server=false at build time, we could remove the binding of the grpc port in the generated resources and use http by default as the target port.
as part of another pull request after merging this pull request.So, if you're ok with the rest of the changes, can we proceed with merging this pull request?