All Products
Search
Document Center

Container Service for Kubernetes:Configure port probing in Knative

Last Updated:Nov 06, 2024

In cloud-native applications, microservices architectures, or containerized applications, you can configure liveness probes and readiness probes to ensure that applications are healthy and ready to receive traffic. Compared with probing policies in open-source Kubernetes, Knative adopts more frequent and active probes in order to reduce the cold start time of pods and accelerate the transition of pods from startup to running state.

How it works

You can configure liveness probes and readiness probes to ensure that your applications are healthy and ready to receive traffic.

  • Liveness probes: used to monitor the health status of containers. If a container is in the Failed state or the service in the container fails to launch, the liveness probe restarts the container.

  • Readiness probes: used to efficiently manage the auto scaling of applications to ensure that only pods in the Ready state can receive traffic. This improves the stability and response speeds of the service.

The following figure shows how probes work in Knative.

image
  1. Define readiness probes or liveness probes in a Knative Service CustomResource (CR).

  2. The kubelet sends liveness probes to probe the specified containers.

  3. Knative rewrites and sends readiness probes to the queue-proxy container. The probing starts from the Activator component or the queue-proxy container to ensure that the entire network link is configured and ready.

    Note

    When no probes are defined, Knative defines default readiness probes for the primary container. The default readiness probes check the TCP sockets on the ports of Knative services.

  4. Knative defines readiness probes for the queue-proxy container. The status of queue-proxy aggregates the results of readiness probes rewritten by all containers, including the primary container and sidecar containers. When the queue-proxy container returns successful responses and the Knative network layer is configured, Knative considers that the pods are healthy and ready to accept traffic.

Prerequisites

Knative has been deployed in your cluster. For more information, see Deploy Knative.

Configure custom listening ports

Configuration method

Add the readinessProbe or livenessProbe field to a Knative Service to configure readiness probes and liveness probes, respectively. The configuration method is the same as that used in Kubernetes. For more information, see Configure Liveness, Readiness and Startup Probes.

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: runtime
  namespace: default
spec:
  template:
    spec:
      containers:
      - name: first-container
        image: <YOUR-IMAGE>      # Replace <YOUR-IMAGE> with the actual image name. 
        ports:
          - containerPort: 8080
        readinessProbe:       # Check when a container is ready after the container starts up. 
          httpGet:            # The port that is used for liveness probes through TCP socket connections. 
            port: 8080        # Specify a port other than the containerPort for readiness probes. 
            path: "/health"
        livenessProbe:            # Check whether a container is alive. 
          tcpSocket:
            port: 8080
      - name: second-container
        image: <YOUR-IMAGE>
        readinessProbe:             
          httpGet:
            port: 8089
            path: "/health"
        livenessProbe:              
          tcpSocket:
            port: 8089

Supported probe types

Probe type

Description

httpGet

Send HTTP GET requests to check the health status and liveness of services, and confirm whether the probes are successful based on the returned status codes.

tcpSocket

Attempt to create a TCP connection to check the health status and liveness of services.

exec

Run specific commands in containers and confirm whether the probes are successful based on the exit codes.

grpc

Call methods defined by gRPC Health Checking Protocol to check the health status and liveness of services.

Note

Custom PreStop probes are not supported because a built-in PreStop hook is already provided by Knative to support graceful shutdown.

References

You can configure a certificate to access Services over HTTPS through a custom domain name. For more information, see Configure a certificate to access Services over HTTPS.