DC/DR with MirrorMaker 2
Replicate topics and consumer offsets to a DR site with MirrorMaker 2, and fail over and back without posting a payment twice.
A second broker in the same cluster is not a second site. When the site is gone, the cluster is gone with it. MirrorMaker 2 copies topics from a primary cluster to a DR cluster, and it can copy consumer offsets so a group resumed on DR continues from the records it had finished.
This lab runs active-passive. Applications write to one site. The other site is a copy. Writing to both is how you get two ledgers.
Prerequisites
- Two Streams for Apache Kafka clusters,
labinkafkaandlabinkafka-dr, each built as in Kafka on OpenShift - Network from the DR project to the primary bootstrap, and TLS trust for both clusters once security is on
- The same topic names on both sides. Identity replication, below, depends on that
- Consumers you can stop. Offset sync does not update a group that is running on the target
Mirror the payments topics
MirrorMaker 2 is a Connect cluster plus three connectors: source copies records, checkpoint copies offsets, heartbeat shows the link is alive. IdentityReplicationPolicy keeps the topic name payments on both sides. The default policy prefixes the source alias, and every consumer config changes during a failover. Use identity on all three connectors or the offset copy lands on names the consumers will not read.
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaMirrorMaker2
metadata:
name: to-dr
namespace: kafka-dr
spec:
version: 4.2.0
replicas: 2
connectCluster: dr
clusters:
- alias: primary
bootstrapServers: lab-kafka-bootstrap.kafka.svc:9093
tls:
trustedCertificates:
- secretName: lab-cluster-ca-cert
certificate: ca.crt
- alias: dr
bootstrapServers: lab-kafka-bootstrap.kafka-dr.svc:9093
tls:
trustedCertificates:
- secretName: lab-cluster-ca-cert
certificate: ca.crt
config:
config.storage.replication.factor: 3
offset.storage.replication.factor: 3
status.storage.replication.factor: 3
mirrors:
- sourceCluster: primary
targetCluster: dr
topicsPattern: "payments|payments.dlt|payment-totals|payment-reviews"
groupsPattern: "posting|fraud|indexing"
sourceConnector:
tasksMax: 2
config:
replication.factor: 3
offset-syncs.topic.location: target
offset-syncs.topic.replication.factor: 3
refresh.topics.interval.seconds: 60
sync.topic.acls.enabled: "false"
replication.policy.class: org.apache.kafka.connect.mirror.IdentityReplicationPolicy
heartbeatConnector:
config:
heartbeats.topic.replication.factor: 3
replication.policy.class: org.apache.kafka.connect.mirror.IdentityReplicationPolicy
checkpointConnector:
config:
checkpoints.topic.replication.factor: 3
sync.group.offsets.enabled: "true"
sync.group.offsets.interval.seconds: 60
emit.checkpoints.interval.seconds: 60
refresh.groups.interval.seconds: 60
offset.lag.max: 0
replication.policy.class: org.apache.kafka.connect.mirror.IdentityReplicationPolicy
topicsPattern is the business topics, not .*. A pattern of .* copies internal topics, including MirrorMaker's own, and the two clusters fight. offset.lag.max: 0 syncs small lags. The default leaves the DR group up to a hundred records behind a consumer that was keeping up, which is a hundred payments to think about during failover. sync.topic.acls.enabled stays false so a DR experiment does not import primary ACLs you have not reviewed. Create the DR KafkaUser resources on purpose, as in the security lab.
oc apply -f mm2.yaml
oc get kafkamirrormaker2 to-dr -n kafka-dr
Produce on the primary and read the same key on DR. Stop the posting group on the primary, wait one sync interval, and confirm the group offset on DR with kafka-consumer-groups.sh --describe. A group that is still running on DR will not be updated. That is why the passive site has no consumers until the day you fail over.
Fail over
- Stop producers and consumers on the primary. If the site is down, they are already stopped. Confirm they cannot reconnect halfway through and write to both.
- Read MirrorMaker lag. Whatever has not been copied is a gap you are accepting. Write the last mirrored offset down.
- Wait one
sync.group.offsets.interval.secondsafter the consumers are stopped, so the last offset lands. - Start consumers on DR with the same group ids. They resume from the synced offset. The ledger's unique payment id, from delivery guarantees, makes a repeated record at the boundary harmless.
- Start producers on DR. Update the bootstrap they use. A DNS name that moves is less error-prone than a config change in every service, and it still needs a person to move it.
Do not start the primary producers again while DR producers are running.
Fail back
Failback is a new mirror in the other direction, of the topics DR accepted while it was primary, then the same five steps in reverse. Build that KafkaMirrorMaker2 only when you are ready to return. A mirror in both directions at once, on the same topics, copies records back and forth. Stop writers on DR, let the return mirror catch up, stop it, then start writers on the primary.
The registry topic from schema registry has to be on the DR side as well, or consumers can read bytes they cannot decode. Either mirror kafkasql-journal or run a registry that both sites can reach. Test a failover while both sites are healthy. The first time you read a synced offset should not be the day a site is on fire.
What's next
- The unique key that makes step 4 safe is in delivery guarantees.
- Broker lag and under-replication on both clusters use the alerts in monitoring.
- A cluster that is merely too small, rather than in another site, is scaling and upgrades.