跳到主要内容

Deep Dive into Kubectl port-forward

· 阅读需 11 分钟

Introduction​

The kubectl port-forward command is a powerful tool in Kubernetes that allows you to forward local ports to ports on a pod or service. This is particularly useful for accessing services running inside a Kubernetes cluster from your local machine, debugging applications, and accessing internal services without exposing them to the public internet. This article provides a deep dive into the kubectl port-forward command, covering various scenarios, best practices, and advanced usage, with a detailed example of accessing a PostgreSQL database deployed in a Kubernetes cluster.


How kubectl port-forward Works​

Overview​

The kubectl port-forward command establishes a TCP connection between your local machine and a specific pod or service in the Kubernetes cluster. This connection is bidirectional, meaning that traffic sent to the local port is forwarded to the pod or service, and vice versa.

Steps​

  1. Identify the Pod or Service: The command specifies the pod or service to which the port forwarding will be established.
  2. Establish a Connection: kubectl establishes a TCP connection to the specified pod or service in the Kubernetes cluster.
  3. Forward Traffic: kubectl listens on the specified local port and forwards incoming traffic to the corresponding port on the pod or service.

Example​

To forward local port 5432 to a PostgreSQL pod running on port 5432:

kubectl port-forward postgresql-0 5432:5432 -n database

In this example:

  • postgresql-0 is the name of the PostgreSQL pod.
  • 5432:5432 specifies that local port 5432 will be forwarded to port 5432 on the pod.
  • -n database specifies the namespace where the pod is located.

Basic Usage​

Forwarding a Single Port​

To forward a local port to a port on a pod, use the following command:

kubectl port-forward <pod-name> <local-port>:<pod-port> -n <namespace>

For example, to forward local port 8080 to port 80 on a pod named my-pod in the default namespace:

kubectl port-forward my-pod 8080:80

Forwarding a Single Port to a Service​

To forward a local port to a port on a service, use the following command:

kubectl port-forward service/<service-name> <local-port>:<service-port> -n <namespace>

For example, to forward local port 8080 to port 80 on a service named my-service in the default namespace:

kubectl port-forward service/my-service 8080:80

Advanced Usage​

Forwarding Multiple Ports​

To forward multiple ports, specify multiple <local-port>:<pod-port> pairs:

kubectl port-forward <pod-name> <local-port1>:<pod-port1> <local-port2>:<pod-port2> -n <namespace>

For example, to forward local ports 8080 and 9090 to ports 80 and 90 on a pod named my-pod:

kubectl port-forward my-pod 8080:80 9090:90

Forwarding to a Specific Pod​

If you have multiple pods with the same label, you can forward to a specific pod by specifying the pod name:

kubectl port-forward <pod-name> <local-port>:<pod-port> -n <namespace>

For example, to forward local port 8080 to port 80 on a specific pod named my-pod-1:

kubectl port-forward my-pod-1 8080:80

Forwarding to a Service​

To forward to a service, use the service/ prefix:

kubectl port-forward service/<service-name> <local-port>:<service-port> -n <namespace>

For example, to forward local port 8080 to port 80 on a service named my-service:

kubectl port-forward service/my-service 8080:80

Common Scenarios​

Accessing a PostgreSQL Database​

To access a PostgreSQL database running inside a Kubernetes cluster, follow these steps:

  1. Identify the PostgreSQL Pod: First, identify the name of the PostgreSQL pod. You can list all pods in the namespace where PostgreSQL is deployed:

    kubectl get pods -n <namespace>

    For example, if the PostgreSQL pod is named postgresql-0 in the database namespace:

    kubectl get pods -n database
  2. Port Forward to the PostgreSQL Pod: Forward the PostgreSQL port (default is 5432) to a local port:

    kubectl port-forward <pod-name> <local-port>:5432 -n <namespace>

    For example, to forward local port 5432 to the PostgreSQL pod:

    kubectl port-forward postgresql-0 5432:5432 -n database
  3. Connect to the PostgreSQL Database: Use a PostgreSQL client to connect to the database using the local port. The JDBC URL to connect to the PostgreSQL database from your local machine would be:

    jdbc:postgresql://localhost:<local-port>/<database-name>

    For example, if the database name is mydb, the JDBC URL would be:

    jdbc:postgresql://localhost:5432/mydb

    You can also specify the username and password in the JDBC URL:

    jdbc:postgresql://localhost:5432/mydb?user=<username>&password=<password>

    Alternatively, you can use a PostgreSQL client like psql to connect:

    psql -h localhost -p 5432 -U <username> -d <database-name>

    For example:

    psql -h localhost -p 5432 -U postgres -d mydb
  4. Underlying Mechanism

The kubectl port-forward command sets up a proxy that listens on the specified local port (e.g., 5432) and forwards incoming traffic to the corresponding port on the PostgreSQL pod in the Kubernetes cluster. This proxy effectively creates a tunnel between your local machine and the Kubernetes cluster, allowing you to use localhost to connect to the database.

Example

When you run the following command:

kubectl port-forward postgresql-0 5432:5432 -n database

kubectl sets up a proxy that listens on localhost:5432 and forwards incoming traffic to postgresql-0:5432 in the database namespace.


Best Practices​

Security Considerations​

  • Limit Access: Only forward ports when necessary and limit access to the forwarded ports.
  • Use Namespaces: Use namespaces to isolate resources and limit the scope of port forwarding.
  • Monitor Access: Monitor access to forwarded ports and review logs for suspicious activity.

Resource Management​

  • Avoid Overloading: Avoid forwarding too many ports simultaneously to prevent overloading the local machine.
  • Use Ephemeral Ports: Use ephemeral ports (e.g., 49152-65535) for local forwarding to avoid conflicts with other services.

Monitoring and Logging​

  • Enable Logging: Enable logging for port forwarding to track access and diagnose issues.
  • Monitor Performance: Monitor the performance of the local machine and the Kubernetes cluster to ensure port forwarding does not impact performance.

Lifecycle of kubectl port-forward​

Running as a Process​

When you run the kubectl port-forward command, kubectl starts a new process on your local machine. This process listens on the specified local port and forwards traffic to the corresponding port on the pod or service in the Kubernetes cluster.

Example​

When you run the following command:

kubectl port-forward postgresql-0 5432:5432 -n database

kubectl starts a new process that listens on localhost:5432 and forwards incoming traffic to postgresql-0:5432 in the database namespace.

Termination​

The kubectl port-forward process runs until you manually terminate it. You can terminate the process by pressing Ctrl+C in the terminal where the command is running.


Temporary Nature of Port Forwarding​

Temporary Connection​

The port forwarding established by kubectl port-forward is temporary. It only lasts as long as the kubectl port-forward process is running. Once you terminate the process (e.g., by pressing Ctrl+C), the port forwarding stops, and you can no longer access the pod or service via the forwarded port.

Impact of Reboot​

If you reboot your local machine, the kubectl port-forward process will be terminated, and the port forwarding will be lost. You will need to rerun the kubectl port-forward command after rebooting to reestablish the connection.


Persisting Port Forwarding​

Using Systemd Service​

To persist port forwarding across reboots, you can create a systemd service that runs the kubectl port-forward command on startup.

Step-by-Step Guide​

  1. Create a Script: Create a script that runs the kubectl port-forward command.

    #!/bin/bash
    kubectl port-forward postgresql-0 5432:5432 -n database

    Save this script as port-forward.sh and make it executable:

    chmod +x port-forward.sh
  2. Create a Systemd Service: Create a systemd service file to run the script on startup.

    [Unit]
    Description=Kubernetes Port Forwarding for PostgreSQL

    [Service]
    ExecStart=/path/to/port-forward.sh
    Restart=always

    [Install]
    WantedBy=multi-user.target

    Save this file as /etc/systemd/system/kubectl-port-forward.service.

  3. Enable and Start the Service: Enable and start the systemd service.

    sudo systemctl enable kubectl-port-forward.service
    sudo systemctl start kubectl-port-forward.service

Using Cron Job​

Another approach is to use a cron job to run the kubectl port-forward command on startup.

Step-by-Step Guide​

  1. Create a Script: Create a script that runs the kubectl port-forward command.

    #!/bin/bash
    kubectl port-forward postgresql-0 5432:5432 -n database

    Save this script as port-forward.sh and make it executable:

    chmod +x port-forward.sh
  2. Create a Cron Job: Create a cron job to run the script on startup.

    @reboot /path/to/port-forward.sh

    Add this line to your crontab by running:

    crontab -e

Security Considerations​

Limiting Access​

  • Only Forward Ports When Necessary: Only forward ports when necessary and limit access to the forwarded ports.
  • Use Namespaces: Use namespaces to isolate resources and limit the scope of port forwarding.
  • Monitor Access: Monitor access to forwarded ports and review logs for suspicious activity.

Resource Management​

  • Avoid Overloading: Avoid forwarding too many ports simultaneously to prevent overloading the local machine.
  • Use Ephemeral Ports: Use ephemeral ports (e.g., 49152-65535) for local forwarding to avoid conflicts with other services.

Monitoring and Logging​

  • Enable Logging: Enable logging for port forwarding to track access and diagnose issues.
  • Monitor Performance: Monitor the performance of the local machine and the Kubernetes cluster to ensure port forwarding does not impact performance.

Conclusion​

The kubectl port-forward command is a versatile and powerful tool for accessing services running inside a Kubernetes cluster from your local machine. By mastering the various options and scenarios covered in this article, you can effectively use kubectl port-forward for debugging applications, accessing databases, and accessing internal services. The detailed example of accessing a PostgreSQL database deployed in a Kubernetes cluster demonstrates how to use kubectl port-forward to connect to a database from your local machine.

The port forwarding established by kubectl port-forward is temporary and only lasts as long as the kubectl port-forward process is running. If you reboot your local machine, the port forwarding will be lost, and you will need to rerun the command to reestablish the connection.

To persist port forwarding across reboots, you can create a systemd service or a cron job to run the kubectl port-forward command on startup.


References​

  1. kubectl port-forward Documentation
  2. Kubernetes Best Practices
  3. Kubernetes Security Best Practices
  4. PostgreSQL JDBC Driver Documentation
  5. Systemd Documentation
  6. Cron Documentation

By following these best practices, you can leverage kubectl port-forward to its full potential and efficiently manage your Kubernetes cluster access and debugging tasks.

Spark in Spring Boot Application

· 阅读需 11 分钟

Introduction​

This article provides a comprehensive guide to developing a Spark application with Spring Boot 3.3.5 to communicate with Kafka and Elasticsearch. The application will handle Avro records in Kafka, JSON data in Elasticsearch, and Parquet files in MinIO using Spark DataFrame and Spark SQL. The article covers the setup, integration, and advanced scenarios for handling large-scale data processing, including the use of Avro Schema Registry when communicating with Kafka.


Prerequisites​

  • Java 17 or later
  • Apache Spark 3.5.4
  • Spring Boot 3.3.5
  • Kafka 3.x
  • Elasticsearch 8.x
  • MinIO
  • Maven or Gradle
  • Confluent Schema Registry

Project Folder Structure​

spark-spring-boot-app/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ ├── com/
│ │ │ │ ├── example/
│ │ │ │ │ ├── config/
│ │ │ │ │ │ ├── SparkConfig.java
│ │ │ │ │ ├── service/
│ │ │ │ │ │ ├── KafkaService.java
│ │ │ │ │ │ ├── ElasticsearchService.java
│ │ │ │ │ │ ├── MinIOService.java
│ │ │ │ │ │ ├── DataFrameService.java
│ │ │ │ │ ├── SparkSpringBootConsoleApplication.java
│ │ ├── resources/
│ │ │ ├── application.properties
│ ├── test/
│ │ ├── java/
│ │ │ ├── com/
│ │ │ │ ├── example/
│ │ │ │ │ ├── service/
│ │ │ │ │ │ ├── KafkaServiceTest.java
│ │ │ │ │ │ ├── ElasticsearchServiceTest.java
│ │ │ │ │ │ ├── MinIOServiceTest.java
│ │ │ │ │ │ ├── DataFrameServiceTest.java
├── pom.xml (or build.gradle)

Setting Up the Spring Boot Application​

Step 1: Create a Spring Boot Project​

Create a new Spring Boot project using Spring Initializr or your preferred IDE. Include the following dependencies:

  • Spring for Apache Kafka
  • Spring Data Elasticsearch
  • Spring for Apache Spark

Step 2: Configure Application Properties​

Configure the application properties for Kafka, Elasticsearch, and MinIO:

# Kafka Configuration
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=my-group
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.ByteArrayDeserializer
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.ByteArraySerializer

# Schema Registry Configuration
schema.registry.url=http://localhost:8081

# Elasticsearch Configuration
spring.elasticsearch.rest.uris=http://localhost:9200

# MinIO Configuration
minio.endpoint=http://localhost:9000
minio.accessKey=your-access-key
minio.secretKey=your-secret-key
minio.bucket=your-bucket

Integrating Spark with Spring Boot​

Step 1: Add Spark Dependencies​

Add the following dependencies to your pom.xml or build.gradle file:

<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_2.12</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-avro_2.12</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql-kafka-0-10_2.12</artifactId>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>io.confluent</groupId>
<artifactId>kafka-avro-serializer</artifactId>
<version>7.0.1</version>
</dependency>

Step 2: Configure Spark​

Configure Spark in your Spring Boot application using annotation-style configuration:

import org.apache.spark.sql.SparkSession;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SparkConfig {

@Bean
public SparkSession sparkSession() {
return SparkSession.builder()
.appName("SparkSpringBootApp")
.master("local[*]")
.config("spark.sql.shuffle.partitions", "10")
.getOrCreate();
}
}

Communicating with Kafka​

Reading Avro Records from Kafka​

To read Avro records from Kafka, use the spark-avro and spark-sql-kafka-0-10 libraries:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class KafkaService {

@Autowired
private SparkSession sparkSession;

public Dataset<Row> readAvroFromKafka(String topic) {
return sparkSession.read()
.format("kafka")
.option("kafka.bootstrap.servers", "localhost:9092")
.option("subscribe", topic)
.load()
.selectExpr("CAST(value AS BINARY)")
.select(org.apache.spark.sql.avro.functions.from_avro(col("value"), "your_avro_schema"));
}
}

Reading Avro Records from Kafka with Schema Registry​

To read Avro records from Kafka using the Schema Registry, use the spark-avro and spark-sql-kafka-0-10 libraries:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class KafkaService {

@Autowired
private SparkSession sparkSession;

@Value("${schema.registry.url}")
private String schemaRegistryUrl;

public Dataset<Row> readAvroFromKafka(String topic) {
return sparkSession.read()
.format("kafka")
.option("kafka.bootstrap.servers", "localhost:9092")
.option("subscribe", topic)
.load()
.selectExpr("CAST(value AS BINARY)")
.select(org.apache.spark.sql.avro.functions.from_avro(col("value"), schemaRegistryUrl + "/subjects/" + topic + "-value/versions/latest"));
}
}

Writing Data to Kafka​

To write data to Kafka, use the spark-sql-kafka-0-10 library:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class KafkaService {

@Autowired
private SparkSession sparkSession;

public void writeToKafka(Dataset<Row> dataset, String topic) {
dataset.selectExpr("to_json(struct(*)) AS value")
.write()
.format("kafka")
.option("kafka.bootstrap.servers", "localhost:9092")
.option("topic", topic)
.save();
}
}

Writing Avro Records to Kafka with Schema Registry​

To write Avro records to Kafka using the Schema Registry, use the spark-avro and spark-sql-kafka-0-10 libraries:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class KafkaService {

@Autowired
private SparkSession sparkSession;

@Value("${schema.registry.url}")
private String schemaRegistryUrl;

public void writeAvroToKafka(Dataset<Row> dataset, String topic) {
dataset.select(org.apache.spark.sql.avro.functions.to_avro(struct("*")).as("value"))
.write()
.format("kafka")
.option("kafka.bootstrap.servers", "localhost:9092")
.option("topic", topic)
.option("schema.registry.url", schemaRegistryUrl)
.save();
}
}

Communicating with Elasticsearch​

Reading JSON Data from Elasticsearch​

To read JSON data from Elasticsearch, use the elasticsearch-hadoop library:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ElasticsearchService {

@Autowired
private SparkSession sparkSession;

public Dataset<Row> readFromElasticsearch(String index) {
return sparkSession.read()
.format("org.elasticsearch.spark.sql")
.option("es.nodes", "localhost")
.option("es.port", "9200")
.option("es.resource", index)
.load();
}
}

Writing Data to Elasticsearch​

To write data to Elasticsearch, use the elasticsearch-hadoop library:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ElasticsearchService {

@Autowired
private SparkSession sparkSession;

public void writeToElasticsearch(Dataset<Row> dataset, String index) {
dataset.write()
.format("org.elasticsearch.spark.sql")
.option("es.nodes", "localhost")
.option("es.port", "9200")
.option("es.resource", index)
.save();
}
}

Handling Parquet Files in MinIO​

Reading Parquet Files from MinIO​

To read Parquet files from MinIO, use the spark-hadoop-cloud library:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MinIOService {

@Autowired
private SparkSession sparkSession;

public Dataset<Row> readParquetFromMinIO(String bucket, String path) {
return sparkSession.read()
.format("parquet")
.option("spark.hadoop.fs.s3a.endpoint", "http://localhost:9000")
.option("spark.hadoop.fs.s3a.access.key", "your-access-key")
.option("spark.hadoop.fs.s3a.secret.key", "your-secret-key")
.option("spark.hadoop.fs.s3a.path.style.access", "true")
.load("s3a://" + bucket + "/" + path);
}
}

Writing Parquet Files to MinIO​

To write Parquet files to MinIO, use the spark-hadoop-cloud library:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MinIOService {

@Autowired
private SparkSession sparkSession;

public void writeParquetToMinIO(Dataset<Row> dataset, String bucket, String path) {
dataset.write()
.format("parquet")
.option("spark.hadoop.fs.s3a.endpoint", "http://localhost:9000")
.option("spark.hadoop.fs.s3a.access.key", "your-access-key")
.option("spark.hadoop.fs.s3a.secret.key", "your-secret-key")
.option("spark.hadoop.fs.s3a.path.style.access", "true")
.save("s3a://" + bucket + "/" + path);
}
}

Using Spark DataFrame and Spark SQL​

Creating DataFrames​

To create DataFrames, use the SparkSession object:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataFrameService {

@Autowired
private SparkSession sparkSession;

public Dataset<Row> createDataFrame() {
return sparkSession.read()
.format("csv")
.option("header", "true")
.load("path/to/csv/file");
}
}

Querying DataFrames with Spark SQL​

To query DataFrames using Spark SQL, register the DataFrame as a temporary view:

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataFrameService {

@Autowired
private SparkSession sparkSession;

public Dataset<Row> queryDataFrame(Dataset<Row> df) {
df.createOrReplaceTempView("my_table");
return sparkSession.sql("SELECT * FROM my_table WHERE column = 'value'");
}
}

Unit Test Cases​

KafkaServiceTest.java​

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
public class KafkaServiceTest {

@Autowired
private KafkaService kafkaService;

@Autowired
private SparkSession sparkSession;

@Test
public void testReadAvroFromKafka() {
Dataset<Row> dataset = kafkaService.readAvroFromKafka("test-topic");
assertNotNull(dataset);
}

@Test
public void testWriteAvroToKafka() {
Dataset<Row> dataset = sparkSession.read().json("path/to/json/file");
kafkaService.writeAvroToKafka(dataset, "test-topic");
}
}

ElasticsearchServiceTest.java​

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
public class ElasticsearchServiceTest {

@Autowired
private ElasticsearchService elasticsearchService;

@Autowired
private SparkSession sparkSession;

@Test
public void testReadFromElasticsearch() {
Dataset<Row> dataset = elasticsearchService.readFromElasticsearch("test-index");
assertNotNull(dataset);
}

@Test
public void testWriteToElasticsearch() {
Dataset<Row> dataset = sparkSession.read().json("path/to/json/file");
elasticsearchService.writeToElasticsearch(dataset, "test-index");
}
}

MinIOServiceTest.java​

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
public class MinIOServiceTest {

@Autowired
private MinIOService minIOService;

@Autowired
private SparkSession sparkSession;

@Test
public void testReadParquetFromMinIO() {
Dataset<Row> dataset = minIOService.readParquetFromMinIO("test-bucket", "test-path");
assertNotNull(dataset);
}

@Test
public void testWriteParquetToMinIO() {
Dataset<Row> dataset = sparkSession.read().json("path/to/json/file");
minIOService.writeParquetToMinIO(dataset, "test-bucket", "test-path");
}
}

DataFrameServiceTest.java​

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
public class DataFrameServiceTest {

@Autowired
private DataFrameService dataFrameService;

@Autowired
private SparkSession sparkSession;

@Test
public void testCreateDataFrame() {
Dataset<Row> dataset = dataFrameService.createDataFrame();
assertNotNull(dataset);
}

@Test
public void testQueryDataFrame() {
Dataset<Row> dataset = sparkSession.read().json("path/to/json/file");
Dataset<Row> result = dataFrameService.queryDataFrame(dataset);
assertNotNull(result);
}
}

Design Diagrams​

Component Diagram​

+-------------------+ +-------------------+ +-------------------+
| | | | | |
| Spring Boot | | Apache Spark | | Kafka |
| | | | | |
+--------+----------+ +--------+----------+ +--------+----------+
| | |
| | |
v v v
+-------------------+ +-------------------+ +-------------------+
| | | | | |
| KafkaService | | Elasticsearch | | MinIOService |
| | | | | |
+--------+----------+ +--------+----------+ +--------+----------+
| | |
| | |
v v v
+--------------------+ +-------------------+ +---------------------------------------+
| | | | | |
| DataFrameService | | SparkConfig | | SparkSpringBootConsoleApplication |
| | | | | |
+--------------------+ +-------------------+ +---------------------------------------+

Sequence Diagram​

+---------+ +---------+ +---------+ +---------+
| | | | | | | |
| Client | | Spring | | Spark | | Kafka |
| | | Boot | | | | |
+---------+ +---------+ +---------+ +---------+
| | | |
| Request | | |
|----------------->| | |
| | | |
| | Read Avro | |
| | from Kafka | |
| |-------------->| |
| | | |
| | | Send Avro |
| | |<------------->|
| | | |
| | Process | |
| | Data | |
| |-------------->| |
| | | |
| | Write to | |
| | Elasticsearch| |
| |-------------->| |
| | | |
| | Write to | |
| | MinIO | |
| |-------------->| |
| | | |
| Response | | |
|<-----------------| | |
| | | |

Advanced Scenarios​

Complex Scenario 1: Flow Control and Backpressure Handling​

In scenarios where your application needs to handle flow control and backpressure, you can use Spark's built-in mechanisms for handling large-scale data processing.

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FlowControlService {

@Autowired
private SparkSession sparkSession;

public void handleFlowControl(Dataset<Row> dataset) {
dataset.writeStream()
.format("console")
.option("checkpointLocation", "path/to/checkpoint")
.start()
.awaitTermination();
}
}

Complex Scenario 2: Handling Large-Scale Data Processing​

For large-scale data processing, use Spark's distributed processing capabilities and optimize resource usage.

import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LargeScaleProcessingService {

@Autowired
private SparkSession sparkSession;

public void processLargeScaleData(Dataset<Row> dataset) {
dataset.repartition(100)
.write()
.format("parquet")
.save("path/to/output");
}
}

Conclusion​

Developing a Spark application with Spring Boot 3.3.5 to communicate with Kafka and Elasticsearch involves setting up the Spring Boot application, integrating Spark, and handling data using Spark DataFrame and Spark SQL. By following the best practices outlined in this article, you can create efficient and scalable data processing applications.


References​

  1. Spring Boot Documentation
  2. Apache Spark Documentation
  3. Apache Kafka Documentation
  4. Elasticsearch Documentation
  5. MinIO Documentation
  6. Confluent Schema Registry Documentation

By following these best practices, you can leverage Spark and Spring Boot to build powerful and scalable data processing applications.

Troubleshooting Pod-to-Pod Communication in Kubernetes

· 阅读需 5 分钟

Introduction​

In a K8S cluster, PodA is experiencing issues when using a service name to access PodB. PodA is on node1, and PodB is on node2. This article will walk through the troubleshooting steps to identify and resolve the issue.

Problem Description​

PodA is unable to access PodB using the service name, and the request is failing. PodA is on node1, and PodB is on node2.

Troubleshooting Steps​

Step 1: Capture Network Traffic​

First, use tcpdump to capture network traffic and observe if there are any anomalies:

[root@node1 ~]# tcpdump -n -i ens192 port 50300
...
13:48:17.630335 IP 177.177.176.150.distinct -> 10.96.22.136.50300: UDP, length 214
13:48:17.630407 IP 192.168.7.21.distinct -> 10.96.22.136.50300: UDP, length 214
...

From the captured packets, the source address and port are 177.177.176.150:50901, and the destination address and port are 10.96.22.136:50300. The destination IP 10.96.22.136 is the IP address obtained by PodA when using the server-svc service name.

Step 2: Verify Service IP​

Check if the IP address 10.96.22.136 is correct:

[root@node1 ~]# kubectl get pod -A -owide|grep server
ss server-xxx-xxx 1/1 Running 0 20h 177.177.176.150 node1
ss server-xxx-xxx 1/1 Running 0 20h 177.177.254.245 node2
ss server-xxx-xxx 1/1 Running 0 20h 177.177.18.152 node3
[root@node1 ~]# kubectl get svc -A -owide|grep server
ss server-svc ClusterIP 10.96.182.195 <none> 50300/UDP

The source IP is correct, but the destination IP does not match the expected server-svc IP 10.96.182.195.

Step 3: Check DNS Resolution​

Since K8S uses CoreDNS for service discovery starting from v1.13, verify if the DNS resolution is correct inside PodA:

[root@node1 ~]# kubectl exec -it -n ss server-xxx-xxx -- cat /etc/resolve.conf
nameserver 10.96.0.10
search ss.svc.cluster.local svc.cluster.local cluster.local
options ndots:5

[root@node1 ~]# kubectl exec -it -n ss server-xxx-xxx -- nslookup server-svc
Server: 10.96.0.10

Name: ss
Address: 10.96.182.195

The DNS resolution is correct, and PodA can resolve server-svc to 10.96.182.195.

Step 4: Investigate Service IP​

Check if 10.96.22.136 is associated with any other service, iptables rules, or routes:

[root@node1 ~]# kubectl get svc -A -owide|grep 10.96.22.136

[root@node1 ~]# iptables-save|grep 10.96.22.136

[root@node1 ~]# ip route|grep 10.96.22.136

The IP 10.96.22.136 does not exist in the cluster.

Step 5: Verify Outgoing Traffic from Pod​

Check the destination IP when the traffic leaves PodA:

[root@node1 ~]# ip route|grep 177.177.176.150
177.177.176.150 dev cali9afa4438787 scope link

[root@node1 ~]# tcpdump -n -i cali9afa4438787 port 50300
...
14:16:40.821511 IP 177.177.176.150.50902 -> 10.96.22.136.50300: UDP, length 214
...

The destination IP is incorrect when leaving PodA.

Step 6: Simulate Traffic from Pod​

Simulate a UDP packet from PodA using nc to verify if the destination IP is correct:

[root@node1 ~]# kubectl exec -it -n ss server-xxx-xxx -- echo “test” | nc -u server-svc 50300 -p 9999

[root@node1 ~]# tcpdump -n -i cali9afa4438787 port 50300
...
15:46:45.871580 IP 177.177.176.150.50902 -> 10.96.182.195.50300: UDP, length 54
...

The simulated traffic has the correct destination IP.

Step 7: Restart Pod​

Restart PodA to see if the issue is resolved:

[root@node1 ~]# docker ps |grep server-xxx-xxx | grep -v POD |awk '{print $1}' |xargs docker restart

[root@node1 ~]# tcpdump -n -i ens192 port 50300
...
15:58:17.150535 IP 177.177.176.150.distinct -> 10.96.182.195.50300: UDP, length 214
15:58:17.150607 IP 192.168.7.21.distinct -> 10.96.182.195.50300: UDP, length 214
...

The issue is resolved after restarting PodA.

Step 8: Identify Java DNS Caching​

Investigate if Java DNS caching is causing the issue:

In Java, DNS caching is used to improve performance by storing DNS lookup results. When InetAddress is used to resolve a domain name, the result is cached by the JVM. This cache can cause stale DNS entries to be used.

To resolve this, adjust the DNS caching settings:

  1. Set the networkaddress.cache.ttl property in the Java security settings.
  2. Modify the java.security file to set the networkaddress.cache.negative.ttl property.

Solution​

Adjust the DNS caching settings in the business application based on the business scenario.

Conclusion​

This article detailed the troubleshooting steps to identify and resolve an issue where PodA was unable to access PodB using a service name in a K8S cluster. The root cause was identified as Java DNS caching, and the issue was resolved by adjusting the DNS caching settings.

Deep Dive into Core Kubernetes Components

· 阅读需 3 分钟

This article dives deep into the core components of Kubernetes, explaining their roles, functions, and interconnections. Textual diagrams illustrate the architecture to offer a clearer understanding of each component's interactions.


API Server​

The API Server is the core entry point for all Kubernetes interactions. It exposes the Kubernetes API, validates requests, and interacts with other components. Key functionalities include:

  • Authentication: Verifies the identity of incoming requests.
  • Authorization: Manages RBAC (Role-Based Access Control) policies.
  • Admission Control: Applies constraints and transformations to objects at request time.
  • API Requests: Handles CRUD (Create, Read, Update, Delete) requests on Kubernetes objects.

Diagram:

+----------------+
| |
| API Server |
| |
+----------------+
|
v
+----------------+
| |
| etcd |
| |
+----------------+

etcd​

etcd is a highly available key-value store used by Kubernetes to persist the cluster state. It maintains the configurations and states of each component and is essential for recovering cluster state in case of a restart. Each component retrieves cluster state through the API Server, which interfaces with etcd.

Diagram:

+----------------+ +----------------+
| | | |
| API Server | <-------> | etcd |
| | | |
+----------------+ +----------------+

Controller Manager​

The Controller Manager includes various controllers (e.g., Node, Replication, Endpoint) that regulate the state of cluster resources. Each controller reconciles the cluster's actual state with the desired state. When discrepancies arise, controllers initiate adjustments (e.g., scaling replicas).

Diagram:

+----------------+
| |
| Controller |
| Manager |
| |
+----------------+
|
v
+----------------+
| |
| API Server |
| |
+----------------+

Scheduler​

The Scheduler assigns Pods to Nodes based on resource needs and constraints. The Scheduler evaluates factors like CPU, memory, and affinity rules before assigning Pods, ensuring optimal workload distribution.

Diagram:

+----------------+
| |
| Scheduler |
| |
+----------------+
|
v
+----------------+
| |
| API Server |
| |
+----------------+

Kubelet​

The Kubelet runs on each Node and is responsible for managing container instances. It communicates with the API Server to receive Pod specifications, ensuring they’re running as defined. It monitors containers, collects resource usage, and sends reports back to the API Server.

Diagram:

+----------------+ +----------------+
| | | |
| Kubelet | <-> | API Server |
| | | |
+----------------+ +----------------+

Kube Proxy​

Kube Proxy manages network communication across Nodes in the cluster, implementing Kubernetes networking policies. It facilitates communication within Pods by maintaining network rules and forwarding requests as needed.

Diagram:

+----------------+ +----------------+
| | | |
| Kube Proxy | <-> | Kubelet |
| | | |
+----------------+ +----------------+

Communication Flow​

In Kubernetes, components communicate through a centralized and secure API Server, with etcd acting as the persistent store. The workflow for scheduling, updating, and scaling applications includes:

  1. User Command: Users issue requests to the API Server.
  2. Scheduler: Schedules new Pods.
  3. Controller Manager: Enforces and monitors the cluster state.
  4. Kubelet and Proxy: Enforce changes and maintain container and network policies.

Overall Diagram:

+----------------+ +----------------+
| | | |
| API Server | <--------> | etcd |
| | | |
+----------------+ +----------------+
| |
| |
v v
+----------------+ +----------------+
| | | |
| Controller | | Scheduler |
| Manager | | |
+----------------+ +----------------+
| |
v v
+----------------+ +----------------+
| | | |
| Kubelet | | Kube Proxy |
| | | |
+----------------+ +----------------+

Each Kubernetes component plays a specific role in cluster operations, and their communication ensures the cluster maintains its desired state and remains responsive to changing demands.

Deep Dive into Kubernetes Ingress

· 阅读需 3 分钟

In Kubernetes, Ingress is a crucial component that manages external access to services within a cluster, typically via HTTP or HTTPS. It provides a centralized entry point for external traffic, applying routing rules to direct requests to appropriate services. This article explores the architecture and responsibilities of Kubernetes Ingress, how it interacts with other components, and provides visual architecture diagrams.


What is Kubernetes Ingress?​

Ingress in Kubernetes is an API object that manages external access to services, usually HTTP. Instead of exposing each service individually through a Service object of type LoadBalancer or NodePort, Ingress offers a central route, managing traffic with a single IP address or DNS name for the entire cluster. Key benefits include:

  • Centralized Traffic Management: Ingress consolidates traffic through a single point of entry, allowing easy route management.
  • Path-Based Routing: Requests can be directed to specific services based on request paths or hostnames.
  • SSL Termination: Ingress can terminate SSL/TLS connections, ensuring secure HTTPS access.

Ingress Types​

Kubernetes supports two main Ingress types:

  • Simple Ingress: For straightforward routing to backend services based on path or hostname.
  • Advanced Ingress: With more complex routing rules, integrating SSL termination, URL rewrites, and custom filtering.

Ingress Controllers​

An Ingress Controller is the implementation that enforces Ingress rules, usually deployed as a Pod within the cluster. Popular Ingress controllers include NGINX, HAProxy, Traefik, and Istio. The Ingress Controller is responsible for:

  1. Monitoring Ingress Resources: Watching for any changes in Ingress resources and updating routing rules accordingly.
  2. Load Balancing and SSL Termination: Providing efficient load balancing for HTTP/HTTPS requests.
  3. Proxying and Forwarding: Accepting requests on behalf of the Ingress and forwarding them to the correct Service and Pod.

The Ingress Controller must be installed in the cluster to interpret and apply Ingress resources, as Kubernetes does not ship with a default controller.


Ingress Rules and Annotations​

Ingress rules define the routing policy for external traffic. These rules can direct traffic based on URL paths or hostnames, and each rule specifies the Service and Port the request should route to. Annotations add configurations like timeouts, request limits, and additional routing options.

Example of an Ingress Rule​

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: example-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: example.com
http:
paths:
- path: /app
pathType: Prefix
backend:
service:
name: app-service
port:
number: 80

In this example, requests to example.com/app are routed to the app-service on port 80.


Ingress Architecture Diagram​

Ingress with Path-Based Routing​

+-----------------------------+
| External User |
+-----------------------------+
|
v
+-----------------------------+
| Ingress Controller | <-- Manages Ingress Rules
+-----------------------------+
|
+-------+------+--------+
| | |
v v v
+-----------+ +-----------+ +-----------+
| Service A | | Service B | | Service C |
+-----------+ +-----------+ +-----------+
| | |
v v v
+-----------+ +-----------+ +-----------+
| Pod A1 | | Pod B1 | | Pod C1 |
+-----------+ +-----------+ +-----------+

In this diagram:

  • Ingress Controller routes based on rules that direct traffic to Services.
  • Services are abstractions that select Pods through labels.
  • Pods run the actual applications and receive traffic forwarded by the Ingress Controller.

This setup allows centralized and managed external access to cluster resources.


By centralizing access with Ingress, Kubernetes can handle complex routing configurations, ensuring that traffic flows efficiently and securely across the cluster.

Deep Dive into Kubernetes Pod and Service Interactions

· 阅读需 3 分钟

In Kubernetes, Pods and Services work together to ensure reliable deployment, scaling, and network communication for containerized applications. This article provides a deep dive into their responsibilities, roles, and interaction processes within the Kubernetes architecture.

Understanding Kubernetes Pods​

A Pod is the smallest deployable unit in Kubernetes. Each Pod represents one or more containers and is typically designed to hold a single application instance. Key responsibilities and characteristics of Pods include:

  • Containerized Workloads: A Pod holds one or more containers that share storage, network, and specifications.
  • Networking: Each Pod gets its own IP address, allowing containers within it to communicate over localhost.
  • Ephemeral Nature: Pods are disposable. When they fail or are deleted, a new Pod with a different IP is often created to replace them.
  • Scalability: Kubernetes replicates Pods based on defined scaling requirements to handle application load.

Pod Lifecycle:

  1. Scheduled: The Scheduler places the Pod on an appropriate Node.
  2. Running: Containers within the Pod are executing.
  3. Terminated: When the Pod is completed or deleted.

Understanding Kubernetes Services​

A Service in Kubernetes is a stable networking endpoint that exposes a set of Pods as a network-accessible service, typically grouped by labels. Services abstract the dynamic nature of Pods and allow continuous network access. The main responsibilities of Services include:

  • Stable Networking: Services maintain a consistent IP address and DNS name despite the ephemeral nature of Pods.
  • Load Balancing: Services automatically distribute incoming traffic across available Pods, balancing load.
  • Discovery: Pods can be dynamically assigned to a Service based on matching labels, enabling flexible scaling and updates.

Types of Services:

  1. ClusterIP (default): Exposes a service within the cluster.
  2. NodePort: Exposes a service on each Node’s IP at a static port.
  3. LoadBalancer: Creates an external load balancer to expose the service externally.
  4. ExternalName: Maps the Service to a DNS name outside of the cluster.

Pod-Service Communication​

Kubernetes leverages label selectors and IP allocation to connect Pods with Services. Here’s how communication works:

  1. Label Selection: Services use labels to select Pods for traffic routing.
  2. Endpoints Object: Kubernetes creates an Endpoint resource to track the IP addresses of Pods associated with the Service.
  3. Service Proxying: kube-proxy intercepts traffic to a Service IP and forwards it to one of the Pod IPs listed in the Endpoints.
  4. Service IP and DNS: Each Service has a unique IP and is discoverable via a DNS name, facilitating easy communication.

Architecture Diagrams​

Pod Structure​

+----------------------------------+
| Pod |
| +-----------+ +-----------+ |
| | Container | | Container | |
| | App | | Sidecar | |
| +-----------+ +-----------+ |
| Network (localhost) |
| Shared Storage |
+----------------------------------+

Service Structure and Flow​

+-----------------+ +------------------+
| Service IP | <------------>| kube-proxy |
+-----------------+ +------------------+
| |
v v
+-----------------+ +------------------+ +------------------+
| Pod 1 | <-----> | Pod 2 | <- | Pod 3 |
+-----------------+ +------------------+ +------------------+

In summary, Kubernetes Pods run containerized workloads, while Services provide stable network access to those workloads, regardless of individual Pod lifetimes. Together, they enable dynamic scaling, fault tolerance, and efficient networking within Kubernetes clusters.

Best Practices of Deploying PostgreSQL in Kubernetes

· 阅读需 3 分钟

This guide walks through deploying PostgreSQL in a Kubernetes Pod with a sidecar container that collects PostgreSQL logs and forwards them to Elasticsearch. We’ll use Helm to manage deployments, including setting up necessary ConfigMaps and Secrets for configuration.

Project Structure​

postgresql-logging/
├── charts/
│ └── postgresql-log-sidecar/
│ ├── Chart.yaml
│ ├── templates/
│ │ ├── configmap.yaml
│ │ ├── deployment.yaml
│ │ ├── secret.yaml
│ │ ├── service.yaml
│ ├── values.yaml
├── README.md

1. Architecture Overview​

The following diagram shows how the PostgreSQL Pod and the sidecar interact for log collection:

+-----------------------------------+
| PostgreSQL Pod |
| |
| +-----------------------------+ |
| | PostgreSQL Container | |
| | - Exposes Port 5432 | |
| +-----------------------------+ |
| |
| +-----------------------------+ |
| | Log Collector | |
| | - Collects Logs | |
| | - Pushes to Elasticsearch | |
| +-----------------------------+ |
+-----------------------------------+

2. Helm Chart Configuration​

Chart.yaml​

apiVersion: v2
name: postgresql-log-sidecar
description: A Helm chart for PostgreSQL with log sidecar
version: 0.1.0
appVersion: "13"

values.yaml​

Define values for PostgreSQL configuration and the sidecar container.

postgresql:
username: admin
password: password123
database: appdb

elasticsearch:
host: "elasticsearch:9200"
index: "postgresql-logs"

sidecar:
image: fluent/fluent-bit
tag: "1.8"
resources:
limits:
memory: "200Mi"
requests:
memory: "100Mi"

3. ConfigMap and Secret​

configmap.yaml​

This ConfigMap includes log configuration for the sidecar container.

apiVersion: v1
kind: ConfigMap
metadata:
name: postgresql-log-config
data:
fluent-bit.conf: |
[SERVICE]
Flush 1
Log_Level info

[INPUT]
Name tail
Path /var/log/postgresql/postgresql.log
Tag postgresql

[OUTPUT]
Name es
Match *
Host {{ .Values.elasticsearch.host }}
Port 9200
Index {{ .Values.elasticsearch.index }}

secret.yaml​

Create a Secret for PostgreSQL credentials.

apiVersion: v1
kind: Secret
metadata:
name: postgresql-secret
type: Opaque
data:
postgresql-username: {{ .Values.postgresql.username | b64enc }}
postgresql-password: {{ .Values.postgresql.password | b64enc }}

4. Deployment Configuration​

deployment.yaml​

The main deployment file sets up PostgreSQL and the Fluent Bit sidecar container.

apiVersion: apps/v1
kind: Deployment
metadata:
name: postgresql-log-sidecar
spec:
replicas: 1
selector:
matchLabels:
app: postgresql
template:
metadata:
labels:
app: postgresql
spec:
containers:
- name: postgresql
image: postgres:13
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: {{ .Values.postgresql.database }}
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: postgresql-secret
key: postgresql-username
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgresql-secret
key: postgresql-password
volumeMounts:
- name: postgres-logs
mountPath: /var/log/postgresql
resources:
requests:
memory: "500Mi"
limits:
memory: "1Gi"

- name: log-collector
image: {{ .Values.sidecar.image }}:{{ .Values.sidecar.tag }}
args: ["-c", "/fluent-bit/etc/fluent-bit.conf"]
volumeMounts:
- name: postgres-logs
mountPath: /var/log/postgresql
- name: config
mountPath: /fluent-bit/etc
subPath: fluent-bit.conf
resources:
limits:
memory: {{ .Values.sidecar.resources.limits.memory }}
requests:
memory: {{ .Values.sidecar.resources.requests.memory }}
volumes:
- name: postgres-logs
emptyDir: {}
- name: config
configMap:
name: postgresql-log-config

service.yaml​

Create a Service to expose PostgreSQL internally within the cluster.

apiVersion: v1
kind: Service
metadata:
name: postgresql
spec:
type: ClusterIP
ports:
- port: 5432
targetPort: 5432
selector:
app: postgresql

5. Deploying the Helm Chart​

Install the chart with:

helm install postgresql-log-sidecar ./charts/postgresql-log-sidecar

Microservice - Spring Cloud Gateway

· 阅读需 16 分钟
  1. Introduction
  2. Why We Need API Gateway
  3. Build API Gateway
  4. Setting Up API Gateway
  5. Route Configuration
  6. Security and JWT Authentication
  7. Dockerfile Configuration
  8. Helm Chart Configuration
  9. Project Structure
  10. API Gateway Architecture
  11. Sequence Diagrams
  12. HTTPS Configuration for API Gateway
  13. Difference Between lb:// and http:// Protocols
  14. Conclusion

Introduction​

In a microservices architecture, an API Gateway acts as a single entry point for client requests, routing them to the appropriate microservices within the Kubernetes cluster. This article will delve into the setup and configuration of a Spring Cloud API Gateway, focusing on JWT authentication validation, route management, and deployment using Docker and Helm.


Why We Need API Gateway​

Single Entry Point​

An API Gateway provides a single entry point for all client requests, simplifying client-side code and reducing the number of endpoints clients need to interact with.

Routing and Load Balancing​

The gateway routes requests to the appropriate microservices and handles load balancing across multiple instances of each service.

Security​

It centralizes security concerns such as authentication and authorization, reducing the need for each microservice to implement these features independently.

Protocol Translation​

The gateway can handle protocol translation, allowing clients to use a single protocol (e.g., HTTP/2) while communicating with microservices using different protocols.


Build API Gateway​

Is there an official API Gateway image?

  • Currently, Spring Cloud Gateway does not offer an official pre-built Docker image, developers build a Spring Boot application with spring-cloud-starter-gateway and custom configurations (routes, JWT filter, etc.).

  • As gateway services are often customized for specific routing, load balancing, security and potentially other cross-cutting concerns and needs. It acts as the main entry point for all external requests to your microservices. The gateway can either pull routing configurations from a service discovery mechanism(like Spring Cloud Kubernetes Discovery) or have the configurations set explicitly.

Steps to Build a Production-Ready API Gateway

  1. Route Configuration: Define routing rules either in application.yml or using Java annotations.
  2. Security Setup: Add JWT validation (e.g., JwtAuthenticationFilter) and configure security policies in the gateway.
  3. Service Discovery: Use Spring Cloud Kubernetes for service discovery, allowing the gateway to dynamically discover services.
  4. Containerization and Deployment: Build a Docker image and deploy it to Kubernetes with a Helm chart for scalability and configuration management.

Setting Up API Gateway​

Step 1: Add Dependencies​

Add the necessary dependencies to your pom.xml file.

<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<!-- Spring Cloud Gateway -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<!-- JWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>

Step 2: Create the Main Application Class​

Create the main application class for the Spring Cloud Gateway.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ApiGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}

Service Discovery and Routing Configuration​

YAML-based Configuration​

Configure routes in the application.yml file.

spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true # Ensures lowercase naming for services
routes:
- id: profile_service
uri: lb://profile-svc
predicates:
- Path=/profile/**
- id: inventory_service
uri: lb://inventory-svc
predicates:
- Path=/inventory/**
- id: order_service
uri: lb://order-svc
predicates:
- Path=/order/**
- id: shipment_service
uri: lb://shipment-svc
predicates:
- Path=/shipment/**

kubernetes:
discovery:
all-namespaces: true # Discover services across all namespaces
service-labels: # Optional: filter based on labels
environment: production
  • lb:// */ scheme: Enables load balancing across multiple instances of each microservice.
  • Service Discovery: The gateway will detect microservices like inventory-service or shipment-service using Kubernetes service discovery, eliminating the need for hardcoding service addresses.

Annotation-based Configuration​

Alternatively, configure routes programmatically using annotations.

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GatewayRoutesConfig {

@Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route("profile_service_route", r -> r.path("/profile/**")
.uri("lb://profile-svc"))
.route("inventory_service_route", r -> r.path("/inventory/**")
.uri("lb://inventory-svc"))
.route("order_service_route", r -> r.path("/order/**")
.uri("lb://order-svc"))
.route("shipment_service_route", r -> r.path("/shipment/**")
.uri("lb://shipment-svc"))
.build();
}
}

Enable Service Discovery To enable service discovery, you can use the @EnableDiscoveryClient annotation. This annotation is typically placed in a configuration class.

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableDiscoveryClient
public class DiscoveryConfig {
// Additional configuration for service discovery can be added here if needed
}

application.yml Configuration for Service Discovery​

Ensure that the following configuration is present in your application.yml or application.properties file to enable service discovery and other settings:

spring:
cloud:
gateway:
discovery:
locator:
enabled: true
lower-case-service-id: true
kubernetes:
discovery:
all-namespaces: true
service-labels:
environment: production

Summary​

  • Annotation-based Configuration: The GatewayRoutesConfig class defines the routes using the RouteLocatorBuilder.
  • Service Discovery: The DiscoveryConfig class enables service discovery using the @EnableDiscoveryClient annotation.
  • YAML Configuration: The application.yml file contains the necessary settings for service discovery and gateway configuration.

Note: The discovery settings, such as enabling discovery locator and configuring Kubernetes discovery, cannot be directly configured using annotations in the configuration class. These settings are typically configured in the application.yml or application.properties file.


Security and JWT Authentication​

Add Dependencies Include spring-boot-starter-oauth2-resource-server and spring-cloud-starter-gateway in your pom.xml:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>

Gateway Application Properties Configuration: Configure application.yml for JWT validation. This includes specifying the JWT issuer URI and enabling authorization.

spring:
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: inventory-service
uri: lb://inventory-service
predicates:
- Path=/inventory/**
filters:
- TokenRelay # Forward the token to the downstream services
security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://<your-auth-provider>/realms/your-realm
  • TokenRelay Filter: Passes the JWT token along with requests to the backend services.
  • Issuer URI: This points to your authentication provider, e.g., an OpenID Connect provider like Keycloak or Auth0.

Enable JWT Authentication: Set up SecurityConfig to enable JWT authentication and configure routes for authorized access.

@Configuration
@EnableWebFluxSecurity
public class SecurityConfig {

@Bean
public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange()
.pathMatchers("/inventory/**", "/shipment/**").authenticated() // Require auth for specified paths
.anyExchange().permitAll()
.and()
.oauth2ResourceServer()
.jwt(); // Enable JWT authentication

return http.build();
}
}
  • This configuration restricts access to /inventory/** and /shipment/** endpoints to authenticated users, while allowing others as specified.

Annotation-based Configuration for Routes and Filters

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;

@Configuration
public class GatewayRoutesConfig {

@Bean
public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route("inventory-service", r -> r.path("/inventory/**")
.filters(f -> f.filter(new JwtAuthenticationFilter("ROLE_USER,ROLE_ADMIN")))
.uri("lb://inventory-service"))
.build();
}

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http
.authorizeExchange()
.anyExchange().authenticated()
.and()
.oauth2ResourceServer()
.jwt()
.jwkSetUri("https://example-issuer.com/.well-known/jwks.json")
.and()
.and()
.csrf().disable();
return http.build();
}
}

>**Note**: **Annotation-based Configuration**: The `GatewayRoutesConfig` class defines the routes and filters using the `RouteLocatorBuilder`. It also configures security settings using Spring Security annotations.

JWT Authentication Filter​

Create a JWT authentication filter to verify incoming requests.

import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.web.server.authentication.AuthenticationWebFilter;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.ReactiveJwtDecoder;

@Configuration
public class SecurityConfig {

@Autowired
private JwtDecoder jwtDecoder;

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
AuthenticationWebFilter jwtAuthFilter = new AuthenticationWebFilter(authenticationManager());
http
.authorizeExchange()
.pathMatchers("/oauth2/**").permitAll()
.pathMatchers("/inventory/**", "/shipment/**").authenticated().permitAll()
.anyExchange().authenticated()
.and().addFilterAt(jwtAuthFilter, SecurityWebFiltersOrder.AUTHENTICATION);

return http.build();
}

@Bean
public AuthenticationManager authenticationManager() {
return new JwtAuthenticationManager(jwtDecoder);
}
}

JWT Decoder Configuration​

Configure the JWT decoder to validate tokens.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;

@Configuration
public class JwtConfig {

@Bean
public JwtDecoder jwtDecoder() {
return NimbusJwtDecoder.withPublicKey(publicKey).build();
}
}

Service-to-Service Communication via Gateway​

To communicate between services, such as shipment-service needing to communicate with inventory-service, microservices can send requests through the API Gateway.

This method simplifies routing, as services only need to know the gateway address and the service paths (e.g., /inventory/**). This approach enhances security and centralizes cross-cutting concerns like authentication, logging, and rate limiting.

Benefits of Gateway-based Communication​

  1. Centralized Security: JWT and other auth mechanisms can be handled at the gateway.
  2. Simplified Routing: Each service interacts with others via a single entry point.
  3. Enhanced Scalability: Kubernetes manages load balancing for each service, and the gateway balances requests across pods.

Service Discovery and Routing Workflow​

[External Client] --> [API Gateway]
|
| (Discovers routes from Kubernetes Discovery)
|
[inventory-service] <--+-- "lb://inventory-service"
[shipment-service] <--+-- "lb://shipment-service"
[other-service] <--+-- Discovered automatically

Sequence Diagram: Inventory and Shipment Communication via Gateway

  1. Shipment Service sends a request to API Gateway at /inventory/items.
  2. API Gateway validates the JWT token, looks up inventory-service route, and forwards the request.
  3. Inventory Service responds with the requested data.
  4. API Gateway relays the response to Shipment Service.
Shipment Service --> API Gateway: Request /inventory/items with JWT
API Gateway --> inventory-service: Validate JWT and route to inventory
inventory-service --> API Gateway: Return data
API Gateway --> Shipment Service: Relay data response

Example: Sending an Authenticated Request to Another Microservice via the Gateway

In shipment-service, use Spring's WebClient to call the inventory-service through the API Gateway.

  1. Define WebClient Bean for Consistency Across Calls

    @Bean
    public WebClient.Builder webClientBuilder() {
    return WebClient.builder();
    }
  2. Send Authenticated Request through Gateway

    @Autowired
    private WebClient.Builder webClientBuilder;

    public InventoryResponse fetchInventoryItems(String token) {
    return webClientBuilder
    .build()
    .get()
    .uri("http://api-gateway/inventory/items")
    .header(HttpHeaders.AUTHORIZATION, "Bearer " + token)
    .retrieve()
    .bodyToMono(InventoryResponse.class)
    .block();
    }
    • Token Forwarding: The token is forwarded with the request, allowing the gateway to validate it before routing to the target service.
    • Gateway URI: Requests go through http://api-gateway/inventory/items, routing based on service discovery.

In this example:

  • The gateway is deployed separately in Kubernetes and handles dynamic service discovery and routing via lb:// URIs.
  • Service-to-service calls leverage the API Gateway for ease of scaling, simplified routing, and centralized security. Each service only needs to know the API Gateway’s address and service paths.

Comparison: Gateway vs. Direct Service-to-Service Calls​

AspectAPI Gateway RoutingDirect Service-to-Service Calls
RoutingCentralized, via API GatewayDirect, each service must know other services
JWT ValidationCentralized in GatewayEach service needs to validate tokens
ScalingGateway handles scaling and balancingEach service scales independently
SecurityJWT validated once at the gatewayJWT validation needed in each service
Use CaseBest for external and internal callsIdeal for internal-only, tightly integrated

Dockerfile Configuration​

Create a Dockerfile for building the API Gateway image.

# Start with the base image for Java
FROM openjdk:17-jdk-slim

# Set working directory
WORKDIR /app

# Copy the API Gateway JAR to the image
COPY target/api-gateway.jar api-gateway.jar

# Expose port 8443 for the API Gateway
EXPOSE 8443

# Run the API Gateway application
ENTRYPOINT ["java", "-jar", "api-gateway.jar"]

Build and Push the Docker Image​

docker build -t myregistry/api-gateway:latest .
docker push myregistry/api-gateway:latest

Helm Chart Configuration​

Helm Chart Structure​

api-gateway/
├── Chart.yaml
├── values.yaml
└── templates/
├── deployment.yaml
├── service.yaml
└── ingress.yaml

Chart.yaml​

apiVersion: v2
name: api-gateway
version: 0.1.0
description: API Gateway for Microservices

values.yaml​

image:
repository: myregistry/api-gateway
tag: latest
pullPolicy: IfNotPresent

replicaCount: 2

service:
type: ClusterIP
port: 8443

ingress:
enabled: true
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true"
hosts:
- host: api.example.com
paths:
- path: /
pathType: Prefix
tls:
- secretName: api-gateway-tls
hosts:
- api.example.com

templates/deployment.yaml​

apiVersion: apps/v1
kind: Deployment
metadata:
name: api-gateway
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: api-gateway
template:
metadata:
labels:
app: api-gateway
spec:
containers:
- name: api-gateway
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
ports:
- containerPort: 8443

templates/service.yaml​

apiVersion: v1
kind: Service
metadata:
name: api-gateway
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: 8443
selector:
app: api-gateway

templates/ingress.yaml​

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: api-gateway-ingress
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true"
spec:
tls:
- hosts:
- api.example.com
secretName: api-gateway-tls
rules:
- host: api.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: api-gateway
port:
number: 8443

Project Structure​

api-gateway/
├── src/
│ ├── main/
│ │ ├── java/
│ │ │ └── com/
│ │ │ └── example/
│ │ │ └── apigateway/
│ │ │ ├── ApiGatewayApplication.java
│ │ │ ├── config/
│ │ │ │ ├── GatewayRoutesConfig.java
│ │ │ │ ├── SecurityConfig.java
│ │ │ │ └── JwtConfig.java
│ │ └── resources/
│ │ └── application.yml
├── Dockerfile
├── pom.xml
└── helm/
└── api-gateway/
├── Chart.yaml
├── values.yaml
└── templates/
├── deployment.yaml
├── service.yaml
└── ingress.yaml

API Gateway Architecture​

graph TD
A[Client] -->|HTTPS| B[API Gateway]
B -->|lb://| C[Profile Service]
B -->|lb://| D[Inventory Service]
B -->|lb://| E[Order Service]
B -->|lb://| F[Shipment Service]
B -->|lb://| G[OAuth2 Service]

Explanation​

  1. Client: Sends requests to the API Gateway over HTTPS.
  2. API Gateway: Routes requests to the appropriate microservice using lb:// protocol.
  3. Microservices: Each service handles requests and returns responses to the API Gateway.

Sequence Diagrams​

Sequence Diagram for Client Request​

sequenceDiagram
participant Client
participant API Gateway
participant Profile Service
participant Inventory Service
participant Order Service
participant Shipment Service

Client->>API Gateway: HTTPS Request
API Gateway->>Profile Service: lb://profile-svc
API Gateway->>Inventory Service: lb://inventory-svc
API Gateway->>Order Service: lb://order-svc
API Gateway->>Shipment Service: lb://shipment-svc
Profile Service-->>API Gateway: Response
Inventory Service-->>API Gateway: Response
Order Service-->>API Gateway: Response
Shipment Service-->>API Gateway: Response
API Gateway-->>Client: HTTPS Response

Sequence Diagram for JWT Authentication​

sequenceDiagram
participant Client
participant API Gateway
participant OAuth2 Service

Client->>API Gateway: HTTPS Request with JWT
API Gateway->>OAuth2 Service: Validate JWT
OAuth2 Service-->>API Gateway: JWT Valid
API Gateway-->>Client: HTTPS Response

HTTPS Configuration for API Gateway​

Step 1: Generate SSL Certificate​

Generate a self-signed SSL certificate or obtain one from a Certificate Authority (CA).

# Generate a self-signed certificate (for testing purposes)
keytool -genkeypair -alias api-gateway -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore api-gateway-keystore.p12 -validity 365

Step 2: Configure HTTPS in application.yml​

Add the keystore settings to application.yml.

server:
port: 8443
ssl:
enabled: true
key-store: classpath:api-gateway-keystore.p12
key-store-password: your_password_here
key-store-type: PKCS12

Step 3: Update Routes to Use lb://​

Ensure that the routes use lb:// to leverage service discovery and load balancing.

spring:
cloud:
gateway:
routes:
- id: profile_service
uri: lb://profile-svc
predicates:
- Path=/profile/**
- id: inventory_service
uri: lb://inventory-svc
predicates:
- Path=/inventory/**
- id: order_service
uri: lb://order-svc
predicates:
- Path=/order/**
- id: shipment_service
uri: lb://shipment-svc
predicates:
- Path=/shipment/**

Step 4: Update Kubernetes Ingress for HTTPS​

Configure the Kubernetes Ingress to redirect traffic to the API Gateway’s HTTPS endpoint.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: api-gateway-ingress
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "true"
spec:
tls:
- hosts:
- api.example.com
secretName: api-gateway-tls
rules:
- host: api.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: api-gateway
port:
number: 8443

Difference Between lb:// and http:// Protocols​

lb:// Protocol​

  • Service Discovery: Uses Kubernetes service discovery to find the appropriate service instance.
  • Load Balancing: Automatically load balances requests across multiple instances of the service.
  • Dynamic Routing: Routes can be dynamically updated based on service availability.

Example:

spring:
cloud:
gateway:
routes:
- id: profile_service
uri: lb://profile-svc
predicates:
- Path=/profile/**

http:// Protocol​

  • Direct Routing: Routes requests directly to a specific URL.
  • No Load Balancing: Does not provide load balancing or service discovery.
  • Static Routing: Routes are static and do not change based on service availability.

Example:

spring:
cloud:
gateway:
routes:
- id: profile_service
uri: http://profile-svc:8080
predicates:
- Path=/profile/**

When to Use Each Protocol​

  • Use lb:// for routes within Kubernetes service discovery, allowing the gateway to leverage load balancing.
  • Use http:// for direct routes to a specific URL or when no service discovery is available.

The other option

Here’s a step-by-step setup for building the API Gateway with JWT authentication and routing configurations:

Step 1: Configure API Gateway with JWT Authentication and Routing​

# application.yml
spring:
cloud:
gateway:
routes:
- id: inventory-service
uri: lb://inventory-service
predicates:
- Path=/inventory/**
filters:
- name: JwtAuthenticationFilter
args:
roles: "ROLE_USER,ROLE_ADMIN" # Add roles if role-based access is needed
discovery:
locator:
enabled: true # Enables dynamic route lookup from discovery service
default-filters:
- StripPrefix=1 # Adjusts the path for downstream services

security:
oauth2:
resourceserver:
jwt:
issuer-uri: https://example-issuer.com # Point to the JWT issuer
jwk-set-uri: https://example-issuer.com/.well-known/jwks.json # Set up for validation

Step 2: Implement JWT Authentication Filter for Gateway​

The custom JWT filter ensures each request includes a valid JWT token.

// JwtAuthenticationFilter.java
@Component
public class JwtAuthenticationFilter implements GatewayFilter {

private final JwtDecoder jwtDecoder;

public JwtAuthenticationFilter(JwtDecoder jwtDecoder) {
this.jwtDecoder = jwtDecoder;
}

@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String authHeader = exchange.getRequest().getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
if (authHeader != null && authHeader.startsWith("Bearer ")) {
String token = authHeader.substring(7);
try {
Jwt jwt = jwtDecoder.decode(token); // Validates the JWT
return chain.filter(exchange); // If valid, proceed with the request
} catch (JwtException e) {
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
}
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
}

Add the filter in application.yml for it to be applied to specific routes (like inventory-service in this case).

Step 3: Building and Deploying the API Gateway as a Docker Image​

  1. Dockerfile for the API Gateway:

    # Dockerfile for api-gateway
    FROM openjdk:17-jdk-slim
    ARG JAR_FILE=target/api-gateway.jar
    COPY ${JAR_FILE} api-gateway.jar
    ENTRYPOINT ["java", "-jar", "/api-gateway.jar"]
  2. Build the Docker Image:

    docker build -t myregistry/api-gateway:latest .
    docker push myregistry/api-gateway:latest # Push to your registry
  3. Kubernetes Deployment Configuration:

    # api-gateway-deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: api-gateway
    labels:
    app: api-gateway
    spec:
    replicas: 2
    selector:
    matchLabels:
    app: api-gateway
    template:
    metadata:
    labels:
    app: api-gateway
    spec:
    containers:
    - name: api-gateway
    image: myregistry/api-gateway:latest
    ports:
    - containerPort: 8080
    env:
    - name: SPRING_PROFILES_ACTIVE
    value: k8s

Step 4: Service-to-Service Communication via Gateway​

Now, other services can call the gateway (using api-gateway service DNS) and append the target service’s route (/inventory/*) to the request path.

Sample Code for a Downstream Service Request Using WebClient with JWT​

// ShipmentService.java
@Service
public class ShipmentService {

private final WebClient webClient;

public ShipmentService(WebClient.Builder webClientBuilder) {
this.webClient = webClientBuilder
.baseUrl("http://api-gateway") // Gateway URL in Kubernetes DNS
.defaultHeader(HttpHeaders.AUTHORIZATION, "Bearer " + jwtToken)
.build();
}

public Inventory getInventory(String id) {
return webClient.get()
.uri("/inventory/" + id)
.retrieve()
.bodyToMono(Inventory.class)
.block();
}
}

Comparison: Gateway vs. Interceptor-Based JWT Validation​

AspectGateway with JWT FilterService-Level JWT Interceptors
Centralized ControlAll JWT validation centralized at gatewayEach service has independent JWT validation
Configuration ComplexityEasier for microservices, managed in gatewayMore complex across multiple services
Performance OverheadCan cause bottleneck at gateway if overloadedDistributed load, but duplicate processing
ResilienceSingle point of failure without redundancyMore resilient, distributed across services
Authorization FlexibilityOffers consistent authentication policyEach service can implement role-based restrictions

Understanding Kubernetes Pods and Services

· 阅读需 5 分钟

In Kubernetes, Pods and Services are fundamental components, each with distinct responsibilities that work together to provide scalable, resilient applications. Here’s an in-depth look into how these units interact, their roles, and how they communicate.

1. Kubernetes Pod: The Basic Unit of Deployment​

A Pod is the smallest deployable unit in Kubernetes, representing one or more containers. Containers within the same Pod:

  • Share resources: Same IP address, storage volumes, and network namespace.
  • Enable inter-container communication: Containers in a Pod can access each other on localhost.
  • Are ephemeral: When a Pod fails, it is replaced by a new Pod instance, often with a new IP address.

Pod Architecture​

Each Pod contains one or more containers with shared resources. Below is a textual representation of a typical Pod structure:

+-----------------------+
| Pod |
| IP: 10.244.1.5 |
| |
| +------------------+ |
| | Container A | |
| | - Application | |
| +------------------+ |
| |
| +------------------+ |
| | Container B | |
| | - Sidecar Proxy | |
| +------------------+ |
+-----------------------+

In this structure, Container A might run the main application, while Container B is a helper (e.g., proxy or logging container). These containers share an IP address and can communicate directly.

2. Real Scenario Explaination​

In this setup, the sidecar container collects logs from the PostgreSQL container by sharing a directory between them. Here’s a breakdown of how it works:

  1. Shared Volume: A shared emptyDir volume, postgres-logs, is mounted in both the PostgreSQL and sidecar containers. PostgreSQL is configured to output logs to /var/log/postgresql within its filesystem, which maps to the shared volume.

  2. Log Collector Configuration: The sidecar container, running Fluent Bit, is configured to read from /var/log/postgresql (also mapped to the shared volume). Fluent Bit’s configuration specifies the tail input plugin to read new entries as they are appended.

  3. Log Processing and Output: Fluent Bit processes the logs, then sends them to Elasticsearch through its output configuration, allowing centralized log analysis and monitoring.

This method ensures that logs are constantly accessible to the sidecar without modifying the PostgreSQL container directly.

Example Fluent Bit Configuration in configmap.yaml​

apiVersion: v1
kind: ConfigMap
metadata:
name: postgresql-log-config
data:
fluent-bit.conf: |
[SERVICE]
Flush 1
Log_Level info

[INPUT]
Name tail
Path /var/log/postgresql/postgresql.log
Tag postgresql

[OUTPUT]
Name es
Match *
Host {{ .Values.elasticsearch.host }}
Port 9200
Index {{ .Values.elasticsearch.index }}

This setup allows the sidecar to monitor log updates efficiently, pushing each entry to Elasticsearch as it appears in the log file.

2. Kubernetes Service: Stable Networking for Dynamic Pods​

A Service is an abstraction that provides a stable, permanent network interface for a set of Pods. Since Pod IP addresses are temporary, a Service routes requests to Pods and handles the load balancing across them.

Key Service Types​

  • ClusterIP: Exposes the Service only within the cluster.
  • NodePort: Exposes the Service on each node’s IP at a specific port.
  • LoadBalancer: Exposes the Service externally, often through a cloud provider’s load balancer.
  • Headless Service: Exposes each Pod individually, without load balancing, often used for direct client-to-Pod connections.

Example Service Architecture​

The Service selects Pods with specific labels and routes traffic to them dynamically. Here’s a simplified text diagram for a Service:

+-------------------+ +------------------+
| Client Request |------------>| Service |
+-------------------+ | (ClusterIP) |
+--------+---------+
|
|
+------------+-----------+
| |
| |
+-------v------+ +---------v------+
| Pod | | Pod |
| IP: Dynamic | | IP: Dynamic |
+--------------+ +----------------+

The Service maintains a list of Pods based on matching labels and routes incoming requests to any healthy Pod in the group, providing an abstraction layer for easy access.

3. Communication Between Pods and Services​

Kubernetes manages Pod-to-Service and Service-to-Pod communication using:

  1. Labels and Selectors: Services target Pods by their labels.
  2. Cluster DNS: Each Service receives a unique hostname, which can be used within the cluster to resolve the Service IP.
  3. Load Balancing: Services distribute requests across Pods, using internal load balancing mechanisms (IP tables, kube-proxy).

When a client inside the cluster calls a Service, Kubernetes DNS resolves the Service name to a virtual IP, and the Service routes the request to one of the associated Pods.

Dynamic Pod Communication Example​

The following diagram illustrates how a Service enables stable access to multiple Pods, even if the Pods are added or removed over time:

+-------------------+ +-----------------+
| Client |----------------->| Service: my-app |
+-------------------+ | IP: 10.96.0.1 |
+-------+---------+
|
+-------------------+-------------------+
| |
+--------v-----------+ +--------v------------+
| Pod | | Pod |
| Label: app=my-app | | Label: app=my-app |
+--------------------+ +---------------------+

Here, the client accesses my-app, and the Service dynamically routes the request to an available Pod with the matching label.

4. Summary​

  • Pods: Contain containers, share IP addresses, ephemeral in nature.
  • Services: Provide stable, permanent access to a group of Pods with specific labels, enable load balancing, and ensure that applications can consistently reach the backend Pods, even with dynamic scaling.

By decoupling Pod IP addresses from the client-facing endpoint, Services ensure reliable, scalable, and high-availability communication within Kubernetes applications.

Example Fluent Bit Configuration in configmap.yaml​

apiVersion: v1
kind: ConfigMap
metadata:
name: postgresql-log-config
data:
fluent-bit.conf: |
[SERVICE]
Flush 1
Log_Level info

[INPUT]
Name tail
Path /var/log/postgresql/postgresql.log
Tag postgresql

[OUTPUT]
Name es
Match *
Host {{ .Values.elasticsearch.host }}
Port 9200
Index {{ .Values.elasticsearch.index }}

This setup allows the sidecar to monitor log updates efficiently, pushing each entry to Elasticsearch as it appears in the log file.

Deep Dive into Executors and ThreadPoolExecutor

· 阅读需 15 分钟

Scaling a thread pool efficiently is essential to balance resource usage with workload demands, ensuring performance without over-provisioning or exhausting system resources. Below are common scaling strategies, including scenarios for using each and detailed production code examples with comments.

1. Overview of Thread Pool Scaling Strategies​

In thread pool management, the primary goal is to allocate just enough threads to handle tasks effectively without overloading the CPU or running into resource constraints. The Executor framework in Java provides tools for dynamic scaling via multiple built-in pool types and strategies, such as FixedThreadPool for predictable workloads and CachedThreadPool for on-demand scaling.


2. FixedThreadPool - Predictable Task Loads​

For workloads where the number of concurrent tasks is consistent or predictable, using a FixedThreadPool is efficient. The pool is created with a predefined number of threads, ensuring no new threads are added or removed during runtime. This strategy is suitable for batch processing or tasks with steady arrival rates.

Sample Code:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FixedThreadPoolExample {
// Define a pool with a fixed number of threads for predictable task load
private static final int THREAD_POOL_SIZE = 5;
private static final ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);

public static void main(String[] args) {
// Simulate task submission to the fixed thread pool
for (int i = 0; i < 10; i++) {
final int taskId = i;
executorService.submit(() -> processTask(taskId));
}

// Shutdown the executor service to release resources after processing
executorService.shutdown();
}

private static void processTask(int taskId) {
System.out.println("Processing Task ID: " + taskId + " with thread " + Thread.currentThread().getName());
try {
// Simulating task processing duration
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

Output:

Processing Task ID: 0 with thread pool-1-thread-1
Processing Task ID: 1 with thread pool-1-thread-2
Processing Task ID: 2 with thread pool-1-thread-3
Processing Task ID: 3 with thread pool-1-thread-4
Processing Task ID: 4 with thread pool-1-thread-5
Processing Task ID: 5 with thread pool-1-thread-1
Processing Task ID: 6 with thread pool-1-thread-2
Processing Task ID: 7 with thread pool-1-thread-3
Processing Task ID: 8 with thread pool-1-thread-4
Processing Task ID: 9 with thread pool-1-thread-5

Explanation:

  1. Thread Pool Initialization:

    • A fixed number of threads (5) handle the tasks sequentially.
    • The thread pool is initialized with 5 threads, named pool-1-thread-1 to pool-1-thread-5.
  2. Task Submission:

    • The for loop submits 10 tasks to the thread pool.
    • Each task is assigned a unique taskId from 0 to 9.
  3. Task Execution:

    • The tasks are executed by the available threads in the thread pool.
    • Since there are 5 threads, the first 5 tasks (0 to 4) are executed concurrently by the 5 threads.
    • After the first 5 tasks are completed, the next 5 tasks (5 to 9) are executed by the same threads, but the order of execution may vary.
  4. Thread Reuse:

    • The executor will reuse threads, limiting thread creation overhead.
    • The threads in the thread pool are reused to execute subsequent tasks. For example, pool-1-thread-1 might execute task 0 and then task 5.
  5. The shutdown method releases resources once all tasks complete.

Advantages: Predictable memory usage and better performance control for tasks with a steady rate.


3. CachedThreadPool - Unpredictable, Short-Lived Tasks​

A CachedThreadPool dynamically adjusts the pool size depending on task demand. This pool is ideal for handling bursty or sporadic loads. Threads are created as needed and, if idle, removed after a timeout period, allowing the system to scale back.

Sample Code:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CachedThreadPoolExample {
// Using CachedThreadPool for sporadic and short-lived tasks
private static final ExecutorService executorService = Executors.newCachedThreadPool();

public static void main(String[] args) {
// Simulate submitting bursty tasks
for (int i = 0; i < 10; i++) {
final int taskId = i;
executorService.submit(() -> handleBurstTask(taskId));
}

// Shutdown executor once all tasks are completed
executorService.shutdown();
}

private static void handleBurstTask(int taskId) {
System.out.println("Handling Burst Task ID: " + taskId + " with thread " + Thread.currentThread().getName());
try {
// Simulating brief processing
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

Output:

Handling Burst Task ID: 0 with thread pool-1-thread-1
Handling Burst Task ID: 1 with thread pool-1-thread-2
Handling Burst Task ID: 2 with thread pool-1-thread-3
Handling Burst Task ID: 3 with thread pool-1-thread-4
Handling Burst Task ID: 4 with thread pool-1-thread-5
Handling Burst Task ID: 5 with thread pool-1-thread-6
Handling Burst Task ID: 6 with thread pool-1-thread-7
Handling Burst Task ID: 7 with thread pool-1-thread-8
Handling Burst Task ID: 8 with thread pool-1-thread-9
Handling Burst Task ID: 9 with thread pool-1-thread-10

Explanation:

  1. Thread Pool Initialization:

    • The CachedThreadPool adds threads on demand up to Integer.MAX_VALUE, suitable for short-lived tasks.
    • The CachedThreadPool dynamically creates new threads as needed and reuses previously constructed threads when they are available.
  2. Task Submission:

    • The for loop submits 10 tasks to the thread pool.
    • Each task is assigned a unique taskId from 0 to 9.
  3. Task Execution:

    • The tasks are executed by the available threads in the thread pool.
    • Since the CachedThreadPool creates new threads as needed, it will create up to 10 threads to handle the 10 tasks.
  4. Thread Creation and Reuse:

    • Threads that are idle for 60 seconds are removed, preventing memory buildup.
    • Ideal for systems that handle irregular spikes without knowing task demand in advance.
    • The CachedThreadPool creates new threads for each task initially because no threads are available.
    • The threads are named pool-1-thread-1 to pool-1-thread-10.

Advantages: Scalability and flexibility for dynamic task arrival rates.


4. Dynamic Scaling with Flexible Configuration​

In a production environment, scaling thread pools dynamically may involve using the ThreadPoolExecutor with customized parameters, such as adjustable core pool size and task queue types. This allows for on-the-fly adjustments based on real-time performance metrics.

Sample Code:

import java.util.concurrent.*;

public class DynamicThreadPoolScaling {
public static void main(String[] args) {
// Custom ThreadPoolExecutor with flexible parameters for dynamic scaling
ThreadPoolExecutor executor = new ThreadPoolExecutor(
2, // corePoolSize
10, // maximumPoolSize
60L, // keepAliveTime
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(5), // workQueue
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy() // Rejection policy
);

for (int i = 0; i < 20; i++) {
final int taskId = i;
executor.submit(() -> performDynamicTask(taskId));
}

// Shut down the executor after tasks completion
executor.shutdown();
}

private static void performDynamicTask(int taskId) {
System.out.println("Performing Dynamic Task ID: " + taskId + " with thread " + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

Explanation of Parameters:

  • corePoolSize: Defines the minimum number of threads.
  • maximumPoolSize: Sets a scalable limit for threads.
  • LinkedBlockingQueue: Limits task queue to prevent memory overflows.
  • CallerRunsPolicy: Ensures tasks run in the caller's thread if limits are reached, providing backpressure control.

Output:

Performing Dynamic Task ID: 0 with thread pool-1-thread-1
Performing Dynamic Task ID: 1 with thread pool-1-thread-2
Performing Dynamic Task ID: 2 with thread pool-1-thread-1
Performing Dynamic Task ID: 3 with thread pool-1-thread-2
Performing Dynamic Task ID: 4 with thread pool-1-thread-1
Performing Dynamic Task ID: 5 with thread pool-1-thread-2
Performing Dynamic Task ID: 6 with thread pool-1-thread-1
Performing Dynamic Task ID: 7 with thread pool-1-thread-2
Performing Dynamic Task ID: 8 with thread pool-1-thread-1
Performing Dynamic Task ID: 9 with thread pool-1-thread-2
Performing Dynamic Task ID: 10 with thread pool-1-thread-3
Performing Dynamic Task ID: 11 with thread pool-1-thread-4
Performing Dynamic Task ID: 12 with thread pool-1-thread-5
Performing Dynamic Task ID: 13 with thread pool-1-thread-6
Performing Dynamic Task ID: 14 with thread pool-1-thread-7
Performing Dynamic Task ID: 15 with thread pool-1-thread-8
Performing Dynamic Task ID: 16 with thread pool-1-thread-9
Performing Dynamic Task ID: 17 with thread pool-1-thread-10
Performing Dynamic Task ID: 18 with thread main
Performing Dynamic Task ID: 19 with thread main

Explaination of Output:

  1. ThreadPoolExecutor Initialization:

    • The ThreadPoolExecutor is initialized with:
      • corePoolSize: 2
      • maximumPoolSize: 10
      • keepAliveTime: 60 seconds
      • workQueue: LinkedBlockingQueue with a capacity of 5
      • threadFactory: Default thread factory
      • rejectionPolicy: CallerRunsPolicy
  2. Task Submission:

    • The for loop submits 20 tasks to the thread pool.
    • Each task is assigned a unique taskId from 0 to 19.
  3. Task Execution:

    • Initially, the core threads (pool-1-thread-1 and pool-1-thread-2) handle the first 2 tasks.
    • The next 5 tasks are queued in the LinkedBlockingQueue.
    • As the core threads finish their tasks, they pick up tasks from the queue.
    • When the queue is full (5 tasks), new threads are created up to the maximumPoolSize (10 threads) to handle the remaining tasks.
    • When all 10 threads are busy and the queue is full, the CallerRunsPolicy kicks in, and the remaining tasks are executed by the main thread (main).

Detailed Steps​

  1. Task 0 to 1 Execution:

    • Task 0 is executed by pool-1-thread-1.
    • Task 1 is executed by pool-1-thread-2.
  2. Task 2 to 6 Queued:

    • Tasks 2 to 6 are queued in the LinkedBlockingQueue.
  3. Task 2 to 6 Execution:

    • Tasks 2 to 6 are executed by pool-1-thread-1 and pool-1-thread-2 as they finish their initial tasks.
  4. Task 7 to 11 Queued:

    • Tasks 7 to 11 are queued in the LinkedBlockingQueue.
  5. Task 7 to 11 Execution:

    • Tasks 7 to 11 are executed by pool-1-thread-1 and pool-1-thread-2 as they finish their previous tasks.
  6. Task 12 to 16 Execution:

    • New threads (pool-1-thread-3 to pool-1-thread-7) are created to handle tasks 12 to 16.
  7. Task 17 to 19 Execution:

    • New threads (pool-1-thread-8 to pool-1-thread-10) are created to handle tasks 17 to 19.
  8. Task 18 and 19 Execution by Main Thread:

    • Since all threads are busy and the queue is full, the CallerRunsPolicy causes the main thread to execute tasks 18 and 19.

5. Thread Pool Strategy Decision​

+-------------------------+
| Task Load Predictable? |
+-----------+-------------+
|
|
Yes | No
+----------+------------+
| |
+-------v----------+ +------v------------+
| FixedThreadPool | | CachedThreadPool |
+-------+----------+ +-----+-------------+
| |
| |
Use if tasks are steady Use if tasks are bursty or
and concurrent unpredictable

Using Executors to create thread pools (e.g., via Executors.newFixedThreadPool() or Executors.newCachedThreadPool()) is convenient but introduces hidden risks and drawbacks in certain production scenarios. Let's look at the specific drawbacks and explore best practices for mitigating these issues.


6. Drawbacks of Using Executors to Create Thread Pools​

  1. Unbounded Queue with newFixedThreadPool:

    • The newFixedThreadPool(int nThreads) method uses a LinkedBlockingQueue without a maximum size limit for its task queue. This unbounded queue can lead to memory exhaustion if tasks are submitted faster than they can be processed.
    • When a large number of tasks are added, the queue will keep growing, which can lead to OutOfMemoryError and system instability.
  2. Unbounded Thread Creation in newCachedThreadPool:

    • The newCachedThreadPool() uses a SynchronousQueue that directly hands off tasks to threads. If no threads are idle to handle new tasks, it creates new threads up to Integer.MAX_VALUE.
    • With a high load, this pool may create excessive threads, leading to CPU saturation and potential memory exhaustion, especially in long-running applications.
  3. Lack of Control Over Rejection Policy:

    • The methods in Executors do not allow explicit configuration of rejection policies for when tasks exceed pool capacity. Default policies can fail to handle high-load scenarios gracefully, often leading to performance degradation and unhandled task rejections.
  4. Harder to Manage Idle Threads:

    • For pools like newCachedThreadPool, idle threads remain for a default 60 seconds before being terminated. While this may be appropriate for some workloads, it can lead to inefficiencies in scenarios where shorter idle times would conserve resources.

7. Best Practices Solution: Using ThreadPoolExecutor Directly​

To avoid these pitfalls, it's best to directly instantiate a ThreadPoolExecutor where you can explicitly configure the task queue size, rejection policies, and timeout values. Here’s how to address each issue with recommended configurations:

A. Configure Bounded Queues to Prevent Memory Leaks:

  • Use a bounded task queue, like ArrayBlockingQueue or a sized LinkedBlockingQueue, to control the number of waiting tasks and prevent out-of-memory errors.
  • This limits the number of tasks waiting in the queue, ensuring system stability by capping memory use.
new ThreadPoolExecutor(
corePoolSize,
maxPoolSize,
keepAliveTime,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(queueSize)
);

B. Limit Thread Growth to Prevent Resource Exhaustion:

  • Use a capped maximumPoolSize when tasks can surge unexpectedly. Setting a limit, e.g., based on the number of available CPU cores, helps prevent CPU saturation.

    When tasks can surge unexpectedly, it's crucial to set a capped maximumPoolSize to prevent the thread pool from creating an excessive number of threads. An unbounded maximumPoolSize can lead to CPU saturation, excessive memory usage, and degraded performance due to excessive context switching.

    CPU Saturation CPU saturation occurs when the CPU is overwhelmed with too many threads, leading to:

    • High CPU Utilization: The CPU spends most of its time executing threads, leaving little time for other system processes.
    • Context Switching Overhead: Frequent context switching between threads consumes CPU cycles, reducing overall efficiency.
    • Memory Pressure: Each thread consumes memory, and a large number of threads can lead to memory exhaustion.

    To prevent CPU saturation, it's recommended to set a maximumPoolSize based on the number of available CPU cores. This ensures that the thread pool can handle a reasonable number of concurrent tasks without overwhelming the CPU.

    A common approach is to set the maximumPoolSize to a value slightly higher than the number of CPU cores to account for both CPU-bound and I/O-bound tasks. A simple formula is maximumPoolSize = N * U

    Where:

    • N: Number of available CPU cores.
    • U: Utilization factor (typically between 1.0 and 2.0).
      • U = 1.0: For CPU-bound tasks where each thread is expected to fully utilize a CPU core.
      • U = 1.5 to 2.0: For mixed CPU-bound and I/O-bound tasks, where some threads may be waiting on I/O operations.

    Example Calculation Suppose you have a system with 8 CPU cores and you expect a mix of CPU-bound and I/O-bound tasks. You can set the maximumPoolSize as follows maximumPoolSize = 8 * 1.5 = 12

  • For highly concurrent applications, you can use dynamic sizing strategies with monitoring metrics (like CPU and memory usage) to inform adjustments to pool parameters.

C. Explicitly Set Rejection Policies:

  • When tasks exceed the pool’s capacity, control the pool’s behavior with rejection policies. ThreadPoolExecutor provides options like CallerRunsPolicy, DiscardPolicy, and DiscardOldestPolicy:
    • CallerRunsPolicy: Executes tasks in the caller thread, which helps slow down task submissions under heavy load.
    • DiscardOldestPolicy: Removes the oldest queued task, making room for newer, high-priority tasks.
  • This choice depends on your application’s tolerance for unprocessed tasks.
new ThreadPoolExecutor(
corePoolSize,
maxPoolSize,
keepAliveTime,
TimeUnit.SECONDS,
new LinkedBlockingQueue<>(queueSize),
new ThreadPoolExecutor.CallerRunsPolicy()
);

Adjust Idle Timeout for Cached Threads:

  • For pools similar to CachedThreadPool, customize the idle timeout to conserve resources by releasing threads sooner when they are not needed. ThreadPoolExecutor allows control over idle time with setKeepAliveTime.
ThreadPoolExecutor cachedPool = new ThreadPoolExecutor(
0, Integer.MAX_VALUE,
30L, TimeUnit.SECONDS, // Reduced keep-alive time
new SynchronousQueue<>(),
Executors.defaultThreadFactory()
);

8. ThreadPoolExecutor with Best Practice Configurations​

Below is a best practice setup using ThreadPoolExecutor that incorporates bounded queues, a limited thread count, a suitable rejection policy, and a controlled idle timeout:

import java.util.concurrent.*;

public class BestPracticeThreadPool {

public static ExecutorService createThreadPool() {
int corePoolSize = 4;
int maxPoolSize = 10;
long keepAliveTime = 30L;
int queueCapacity = 50;

return new ThreadPoolExecutor(
corePoolSize,
maxPoolSize,
keepAliveTime,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(queueCapacity),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.CallerRunsPolicy() // Rejection policy to handle overloads
);
}

public static void main(String[] args) {
ExecutorService threadPool = createThreadPool();

// Submitting example tasks
for (int i = 0; i < 100; i++) {
final int taskId = i;
threadPool.submit(() -> processTask(taskId));
}

// Properly shutdown to free resources
threadPool.shutdown();
}

private static void processTask(int taskId) {
System.out.println("Processing Task ID: " + taskId + " with thread " + Thread.currentThread().getName());
try {
Thread.sleep(500); // Simulate work
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}

Explanation of Configurations:

  • ArrayBlockingQueue limits queue size to 50, managing memory consumption.
  • CallerRunsPolicy applies backpressure to slow down submissions.
  • corePoolSize and maxPoolSize control CPU utilization.
  • keepAliveTime of 30 seconds clears idle threads efficiently.

9. Summary​

Using ThreadPoolExecutor directly provides complete control over pool configurations:

  • Bounded queues prevent memory overflows.
  • Limited threads avoid CPU exhaustion.
  • Rejection policies ensure controlled behavior under load.
  • Idle timeouts conserve system resources.

These adjustments create a resilient and scalable thread pool setup suitable for high-performance, resource-intensive applications.