Running Flink on Kubernetes by hand means writing deployments for the job manager and task managers, wiring up high availability, and remembering to take a savepoint before every upgrade. The Flink Kubernetes Operator, an Apache Flink sub-project, does that for you: you describe the job as a Kubernetes resource, and the operator makes the cluster match it, including stateful upgrades.
This guide covers what to decide when you adopt it. Configuration keys change between Flink and operator versions, so check the documentation for the versions you run.
The resources
FlinkDeploymentdescribes a Flink cluster, and in application mode, the job it runs: image, Flink version, configuration, job manager and task manager resources, and the job's jar, parallelism and upgrade mode.FlinkSessionJobsubmits a job to an existing session cluster.
apiVersion: flink.apache.org/v1beta1
kind: FlinkDeployment
metadata:
name: sensor-anomalies # an example job
spec:
image: registry.example.test/flink/sensor-anomalies:1.4.0
flinkVersion: v1_20
serviceAccount: flink
flinkConfiguration:
taskmanager.numberOfTaskSlots: "2"
execution.checkpointing.dir: s3://flink-state.example.test/checkpoints
execution.checkpointing.savepoint-dir: s3://flink-state.example.test/savepoints
high-availability.type: kubernetes
high-availability.storageDir: s3://flink-state.example.test/ha
jobManager:
resource: {memory: "2048m", cpu: 1}
taskManager:
resource: {memory: "4096m", cpu: 2}
job:
jarURI: local:///opt/flink/usrlib/sensor-anomalies.jar
parallelism: 8
upgradeMode: savepoint
state: runningA minimal application-mode deployment. Names and paths are examples; adapt versions and settings to your environment.
Application mode or session mode
| Application mode | Session mode | |
|---|---|---|
| Cluster per | Job | Shared by many jobs |
| Isolation | Strong: a failing job affects only itself | Weaker: jobs share job manager and task managers |
| Start-up | Slower: a cluster per deployment | Faster: submit to a running cluster |
| Fits | Production jobs | Many small jobs, development, ad-hoc SQL |
For production we default to application mode: each job gets its own resources, upgrades and failures.
Upgrade modes
When you change the spec (a new image, new configuration, new parallelism), the operator upgrades the job according to its
upgradeMode:
stateless: stop and start from empty state. Only for jobs with no state worth keeping.savepoint: take a savepoint, stop, and start the new version from it. The safest choice for stateful jobs, and the one we use by default.last-state: restart from the latest checkpoint recorded in the HA metadata, without taking a new savepoint. Faster, and it works even when the job is unhealthy and can't take a savepoint, but requires HA and only suits changes that are compatible with the existing state.
Whatever the mode, the job's stateful operators need explicit uid()s so state maps back after code changes; see
checkpoints and savepoints.
High availability and storage
Enable Kubernetes high availability so a job manager restart recovers the running job instead of losing it. Checkpoints, savepoints and HA metadata must live in durable shared storage, usually object storage, accessed with a workload identity rather than keys in the configuration. Set a savepoint history limit so old savepoints are cleaned up.
Autoscaling
The operator includes an autoscaler that watches each operator's processing rate and busy time, computes the parallelism needed to keep up with the incoming rate (with headroom), and rescales the job per operator within the limits you set. It works well for jobs with changing load and no severe skew. Set the maximum parallelism high enough first; see scaling Flink jobs.
Resources and memory
Size task managers from what the job uses, not from defaults. Flink divides a task manager's memory into the JVM heap, managed memory (which RocksDB uses for its cache and write buffers) and network buffers, and the container's memory limit must cover all of it plus JVM overhead. A container killed for exceeding its limit looks like a random restart; check the pod's termination reason before tuning anything else. Prefer fewer, larger task managers for jobs with large state, and set CPU requests so the job isn't throttled at peak.
Moving existing jobs onto the operator
- Take a savepoint of the job on its current cluster, and record its location in durable storage the new cluster can read.
- Write the FlinkDeployment with the same Flink version, the same job code and the same operator ids, and point the job spec's initial savepoint path at the savepoint.
- Start it in a test namespace first against replayed data, and check that state restored as expected: counts, windows and timers carry on rather than start from zero.
- Stop the old job with a final savepoint and start the production deployment from it, so no input is processed twice or skipped.
- Upgrade later: change one thing at a time. Moving platform and Flink version in the same step makes any problem harder to attribute.
Upgrading the operator itself
The operator has its own release cycle, separate from Flink's. Read its compatibility notes for the Flink versions you run, and note that
Helm does not upgrade custom resource definitions on helm upgrade; the CRDs usually need to be applied separately first. Test
operator upgrades in a non-production cluster with real deployments, because the operator manages every job it watches.
Operating checklist
- Application mode for production jobs, one deployment per job.
upgradeMode: savepointfor stateful jobs; HA enabled; durable storage for state.- Resource requests that match what the job uses; task manager memory sized for state (RocksDB's managed memory included).
- Metrics exported to your monitoring system (checkpoint duration and failures, backpressure, lag), with alerts.
- A tested restore: start a copy of the job from a recent savepoint in a test namespace, regularly.
- A plan for Flink and operator upgrades, tested against a savepoint from production.
For finding what limits a job once it runs, see tuning a slow Flink job.
Questions we are often asked
Do we need Kubernetes to run Flink well?
No. Flink runs on YARN, standalone clusters and managed services too. Kubernetes with the operator is a good fit when your organisation already runs services on Kubernetes and wants Flink jobs deployed the same way.
Should every team get its own operator?
Usually one operator per Kubernetes cluster is enough; it can watch the namespaces you choose, and each team deploys into its own namespace with its own quotas and access.
Does the autoscaler replace capacity planning?
It handles changing load within the limits you give it. You still decide the maximum parallelism, the resource limits and the cluster capacity it can scale into.