Mafengwo's Flink on K8s Migration: 500% Efficiency, 50% Cost Cut via StreamPark
Mafengwo migrated hundreds of Flink jobs from YARN to Kubernetes using Apache StreamPark, solving multi-tenancy, image reuse, and batch migration challenges while integrating Yunikorn for queue scheduling, achieving 500% development efficiency improvement, 150% resource utilization increase, and 50% cost reduction, completing the project a month ahead of schedule.
Reading font
Business Background and Challenges
As Mafengwo's business and big data processing demands grew, compute requirements and machine costs increased. The data platform ran multiple engines on disparate resources: StarRocks on physical machines, Flink and Spark on YARN. This fragmented resource usage led to high operational costs and low utilization. A unified resource management and scheduling solution was needed.
After research, Kubernetes was chosen as the unified management platform for big data resources due to its container management capabilities, support for mixed deployment, improved resource utilization, isolation, security, and existing technical maturity at Mafengwo. Spark, Presto, and StarRocks clusters had already been migrated; Flink on Kubernetes was the next major task.
Choosing Apache StreamPark
Before StreamPark, the team maintained a custom real-time computing management platform (DP) that met basic Flink job management needs but lacked cloud-native support for Flink on Kubernetes. Two options were considered: extend DP or adopt an open-source project. Evaluation showed StreamPark's functionality closely matched requirements, covering DP's existing features while offering advantages in core functionality, extensibility, and operational experience. Crucially, StreamPark supports Flink on Kubernetes deployment, reducing the complexity and cost of adding Kubernetes support to DP. The decision was to adopt Apache StreamPark to simplify the cloud-native migration and lower migration costs.
StreamPark Implementation Practices
1. Multi-tenant Management
Many business lines develop real-time jobs at Mafengwo. To ensure isolation, the team introduced tenant management using StreamPark's built-in support. Teams were created corresponding to business departments with a one-to-one mapping. System accounts are divided into two types: Team accounts (tenant accounts) allowing team members to manage their jobs (CRUD) ensuring business independence and security; and System administrators (Admin accounts) with a global view of job execution and resource usage across business lines, facilitating cost allocation and rapid troubleshooting.
2. Image Reuse
In Flink on Kubernetes mode, Docker images provide job JAR dependencies. The default StreamPark approach merges a base image with user JARs into a fat image, requiring rebuild on every version update. Mafengwo optimized this: all StreamPark jobs now share a single base Docker image. At container startup, an init container dynamically pulls the job's JAR from shared storage (HDFS) and mounts it to the main container via an emptyDir volume.
Implementation steps: (1) User-uploaded JARs in StreamPark are automatically synced to an HDFS path prefixed with the job name. (2) Flink code was modified to dynamically mount required resources at job startup. A custom init container in the Kubernetes Deployment downloads the user job JAR and dependencies from HDFS and places them in the emptyDir volume for the main container.
This optimization eliminated frequent job image builds, enabled image reuse, accelerated job deployment, and reduced image repository management overhead and storage pressure.
3. Batch Job Migration
Migrating historical jobs from the DP platform to StreamPark manually would incur high labor costs. The team developed an automated migration tool that leverages job metadata stored in DP, transforms it, and automatically creates jobs in StreamPark via its open APIs.
The two main job types are Flink SQL and JAR jobs. For JAR jobs , the improved image reuse allows uploading user JARs from DP to a unified HDFS job library path. Flink on Kubernetes pods pull JARs directly from HDFS. The tool converts DP job metadata (Flink version, job name, parallelism, etc.) into StreamPark JAR job metadata and writes it to StreamPark's metadata database, automatically creating the job.
For Flink SQL jobs , similar metadata conversion is performed. Additionally, Flink SQL jobs depend on Flink Connector JARs and UDF JARs. High-frequency Connector JARs are pre-packaged into the Flink base image, requiring no extra handling during migration. For SQL jobs using UDF JARs, the same process as JAR jobs applies: UDFs are uploaded to HDFS with consistent naming rules so metadata conversion records the correct JAR paths.
Using StreamPark's open job APIs, the tool automates the entire migration flow:
Deploy application: /flink/pipe/build Start application: /flink/app/start Create application: /flink/app/create Get application status:
/flink/app/get4. Job Queues with Yunikorn
Kubernetes' default scheduler lacks flexibility and extensibility. The team wanted YARN-like capabilities on Kubernetes: multi-queue scheduling, priority scheduling, and resource preemption to reduce learning and usage costs. They adopted Yunikorn , a lightweight universal resource scheduler providing fine-grained scheduling for Kubernetes. Yunikorn's queue management resembles Hadoop YARN, enabling seamless developer onboarding. It became the scheduler for the current Kubernetes environment.
Business queues were created per business line with resource guarantees configured based on usage. To improve utilization, resource contention and queue priorities were configured for online and offline workload queues.
kind: ConfigMap
metadata:
name: yunikorn-configs
data:
queues.yaml: |
partitions:
- name: default
queues:
- name: department1
submitacl: '*'
resources:
max:
{ memory: 1000Mi, vcore: 1000m }
guaranteed:
{ memory: 300Mi, vcore: 300m }
- name: department2
submitacl: '*'
resources:
max:
{ memory: 1000Mi, vcore: 1000m }
guaranteed:
{ memory: 300Mi, vcore: 300m }
...apiVersion: v1
kind: ConfigMap
metadata:
name: yunikorn-configs
data:
queues.yaml: |
partitions:
- name: default
queues:
- name: department1
properties:
preemption.policy: fence
preemption.delay: 10s
submitacl: '*'
resources:
max:
{ memory: 1000Mi, vcore: 1000m }
queues:
- name: online-workload
submitacl: '*'
resources:
guaranteed:
{ memory: 300Mi, vcore: 300m }
properties:
priority.offset: "0"
- name: offline-workload
submitacl: '*'
resources:
guaranteed:
{ memory: 300Mi, vcore: 300m }
properties:
priority.offset: "1000"
.......Since teams are organized by business line, they map directly to Yunikorn's business queues. To integrate Flink with Yunikorn, StreamPark was modified: at job submission, the system dynamically retrieves the team's queue name and automatically adds labels to JobManager and TaskManager via dynamic properties, e.g., -Dkubernetes.jobmanager.labels=queue:root.dm.flink and -Dkubernetes.taskmanager.labels=queue:root.dm.flink, ensuring Flink jobs are submitted to the correct business queue.
5. Monitoring and Alerting
Mafengwo enhanced StreamPark's alerting with an "Immediate Restart" feature, allowing job maintainers to quickly respond and recover jobs.
Alerting was also extended to integrate with the internal data platform alerting system. Integration steps:
On the internal alert platform, configure dispatch information for specific alert personnel.
In StreamPark, configure the internal alert platform's callback URL to connect the two systems.
Problems Encountered
1. Network Connection Issues
Using CNI or Kubernetes network plugins introduces unavoidable network performance overhead. In production, network issues frequently caused job errors, manifesting as Akka framework request timeout exceptions, e.g.,
akka.pattern.AskTimeoutException: Ask timed out on [Actor[akka://xxx]] after [xxx ms].
To mitigate, Flink cluster network parameters were tuned to enhance fault tolerance: increasing akka.ask.timeout, taskmanager.network.request-backoff.max, and heartbeat.timeout values, reducing job failures due to network latency or instability.
2. UNKNOWN HOST
In Flink on Kubernetes, internal node communication and external interactions (e.g., reading/writing external storage) require Kubernetes to resolve external hostnames to real IPs. The team chose CoreDNS with hot-reload support, a flexible, modular, high-performance DNS server. A ConfigMap defines a Hostsfile storing hostname-to-IP mappings for all external big data machines, synced on machine changes.
apiVersion: v1
kind: ConfigMap
metadata:
name: coredns
namespace: kube-system
labels:
addonmanager.kubernetes.io/mode: EnsureExists
data:
Hostsfile: |
192.168.xx.11 host1
192.168.xx.12 host2
192.168.xx.13 host2
192.168.xx.14 host3
.......Kubernetes automatically mounts the updated ConfigMap into the DNS container, and CoreDNS quickly detects and reloads the configuration, making new settings effective immediately without manual intervention or service restarts.
3. Log Collection
Log inspection is critical for Flink job troubleshooting. StreamPark provides startup log viewing, but when jobs restart or fail causing Kubernetes pod termination, logs disappear with the pod, making post-failure analysis impossible.
To solve this, a hostPath volume solution was adopted: a specific directory on the Kubernetes node's host is mounted to the Flink log directory in the pod. The host directory uses the job name as a prefix to distinguish logs across jobs, simplifying management and collection.
Host-based Flink logs are automatically collected by Filebeat, shipped to Kafka, and written to Elasticsearch, ensuring logs remain searchable and queryable even after abnormal job termination.
Benefits Achieved
StreamPark platform significantly improved overall job development-to-maintenance efficiency by 500% , with significant positive improvements in functionality, usability, and maintainability over the original DP platform. User satisfaction reached 100% via survey feedback.
Through Flink on Kubernetes architecture adjustments, removing strong Hadoop ecosystem binding reduced deployment and operational costs. Unified cloud-native resource request and scheduling enabled mixed online/offline deployment. Combined with HPA, preemptive resource scheduling, and staggered workload strategies, machine resource utilization for real-time computing scenarios increased by 150% , and machine costs decreased by 50% .
Introduction of StreamPark platform and batch migration tool greatly improved Flink on Kubernetes cloud-native implementation efficiency, completing the project one month ahead of schedule .
After nearly half a year of observation and validation, StreamPark and Flink jobs running on Kubernetes have demonstrated stability exceeding expectations.
Future Plans
The team plans to migrate more compute components to the Kubernetes cluster for unified management and resource optimization. Under unified Kubernetes scheduling, they aim to improve resource utilization and operational efficiency by intelligently utilizing idle periods for job execution, leveraging resource preemption, and implementing staggered strategies for different workloads.
Special thanks to the StreamPark community for strong support in usage and issue tracking. The team will continue close collaboration with the community and actively participate in co-building, hoping StreamPark graduates to a top-level Apache project soon.
For questions about attribution, corrections or content rights, please contactand we will review it promptly.