Skip to main content

GCP Managed Kafka Setup

This guide covers configuring Zenoo Hub to use Google Cloud Managed Service for Apache Kafka. While the main GCP Cloud Provider documentation covers Firestore and Secret Manager, this dedicated guide focuses on Kafka configuration, authentication, IAM permissions, and troubleshooting specific to GCP Managed Kafka.

Overview

Google Cloud Managed Service for Apache Kafka provides a fully managed, Apache Kafka-compatible event streaming service. It eliminates the operational overhead of running and maintaining Kafka clusters while providing:
  • Automatic scaling - Scales capacity based on throughput
  • High availability - Multi-zone redundancy and automatic failover
  • IAM integration - Native GCP authentication and authorization
  • Security - Encryption at rest and in transit by default
  • Monitoring - Built-in Cloud Monitoring integration
Why This Separate Guide? GCP Managed Kafka has unique authentication and authorization requirements compared to self-hosted Kafka or AWS MSK. This guide addresses:
  • OAuth/SASL_SSL authentication setup (recommended)
  • mTLS authentication (alternative)
  • IAM permission configuration
  • Kafka Streams consumer group coordination
  • Common deployment issues and solutions

Prerequisites

Before proceeding, ensure you have:
  • GCP Managed Kafka cluster created and running
  • Hub application with GCP cloud provider configured (see GCP Provider)
  • Service account with Kafka IAM permissions (see IAM Permissions)
  • Network connectivity from Hub deployment to Managed Kafka cluster
Create Managed Kafka Cluster:

Authentication Methods

GCP Managed Kafka supports two authentication methods. OAuth is strongly recommended for new deployments. OAuth authentication uses GCP IAM for both authentication and authorization, providing the simplest and most secure approach.

Benefits

  • No manual ACL management - Permissions controlled via IAM roles
  • Automatic credential rotation - Uses Application Default Credentials
  • Integrated with GCP ecosystem - Consistent with other GCP services
  • No certificate management - No keystores or truststores required
  • Seamless authentication - Works with service accounts, Workload Identity, etc.

Required Dependency

Add the GCP Managed Kafka authentication library to your build.gradle:

Configuration

Configure OAuth authentication in application-gcp.yml:
Key Configuration Notes:
  • Port 9092 - OAuth uses the standard Kafka port (not 9192 for mTLS)
  • Timeouts - Set to 300000ms (5 minutes) for GCP Managed Kafka latency characteristics
  • Bootstrap Address - Get from cluster details:

mTLS (Alternative)

Mutual TLS (mTLS) authentication uses client certificates for authentication. While supported, OAuth is strongly recommended for new deployments.

Benefits

  • Certificate-based authentication - No IAM dependency
  • Explicit trust model - Client certificates verify identity

Challenges

  • Manual ACL management - Must configure Kafka ACLs via API
  • Certificate rotation overhead - Regular keystore/truststore updates required
  • GCP Managed Kafka ACL limitations - Known issues with ACL API (see troubleshooting)
  • Operational complexity - More moving parts to manage

Configuration

mTLS Setup:
  1. Generate certificate signing request (CSR)
  2. Sign CSR with Managed Kafka CA
  3. Import signed certificate and CA into keystores
  4. Configure ACLs via Managed Kafka API (see limitations)
Recommendation: Use OAuth unless you have specific requirements for certificate-based authentication.

IAM Permissions for OAuth

When using OAuth authentication, GCP Managed Kafka uses IAM roles for authorization. The service account running your Hub application requires specific IAM roles.

Required Roles

Permissions Breakdown

roles/managedkafka.client includes:
  • managedkafka.clusters.connect - Connect to Managed Kafka cluster
  • managedkafka.topics.read - Read messages from topics
  • managedkafka.topics.write - Write messages to topics
roles/managedkafka.consumerGroupEditor includes:
  • managedkafka.consumerGroups.update - Join consumer groups and commit offsets
Critical for Kafka Streams: Kafka Streams applications require both roles. Without consumerGroupEditor, Kafka Streams will remain stuck in REBALANCING state with 0 assigned partitions.

Verify Permissions

Hub Configuration for Kafka

Complete Hub configuration example integrating GCP cloud provider with Managed Kafka.

Complete application-gcp.yml

Environment Variables

Override configuration via environment variables:

Profile-Based Configuration

Use Spring profiles for different environments:

Kafka Streams Configuration

Critical: Hub uses Apache Kafka Streams for stateful stream processing. Ensure ALL Kafka properties are passed to Kafka Streams configuration.

Verification

The Hub’s KafkaConfig.java must include this pattern:
Why This Matters: If props.putAll(kafkaProperties.getProperties()) is missing, Kafka Streams will not receive OAuth authentication properties and will fail to connect or remain stuck in REBALANCING.

Common Mistake

Incorrect (causes OAuth failure):
Correct:

Deployment

Deploy Hub with GCP Managed Kafka on various GCP compute platforms.

GCE VM Deployment

Deploy on Google Compute Engine with attached service account:

GKE Deployment

Deploy on Google Kubernetes Engine with Workload Identity: 1. Configure Workload Identity:
2. Kubernetes Manifests:
3. Deploy:

Cloud Run Deployment

Deploy on Cloud Run (serverless):
Note: Cloud Run may require VPC egress for Managed Kafka connectivity.

Verification

Verify Kafka Streams and Kafka connectivity after deployment.

Check Kafka Streams State

Kafka Streams should transition to RUNNING state within 30-60 seconds of application startup.
Expected Output:
Warning Signs:
  • state: REBALANCING (stuck indefinitely)
  • state: ERROR
  • status: DOWN

Verify Partition Assignment

Check that consumer partitions are assigned:
Expected Output:
Warning: If value: 0.0, partitions are not assigned (authorization issue or configuration problem).

Check Connection Stability

Verify stable Kafka connections:
Expected Output:
Monitor for 5 minutes - connection count should remain stable (not constantly increasing/decreasing).

Check for Errors

Monitor application logs for errors:
No errors expected - OAuth authentication should be silent when working correctly.

Troubleshooting

Common issues and solutions when deploying Hub with GCP Managed Kafka.

Kafka Streams Stuck in REBALANCING

Symptoms:
  • Health check shows state: REBALANCING indefinitely (> 2 minutes)
  • Actuator metrics show 0 assigned partitions
  • Failed rebalance count increasing
Causes and Solutions:

Cause 1: Missing IAM Permissions

Most common issue. Service account lacks required Managed Kafka IAM roles. Diagnosis:
Solution:
Verification: After granting roles, Kafka Streams should transition to RUNNING within 30 seconds.

Cause 2: Kafka Properties Not Passed to Streams

OAuth properties not reaching Kafka Streams configuration. Diagnosis: Check application logs for OAuth token errors:
Solution: Verify KafkaConfig.kStreamsConfigs() includes:

Cause 3: Timeout Configuration Too Short

Consumer group coordination timing out due to short timeouts. Diagnosis: Check if timeouts are configured:
Solution: Ensure application-gcp.yml has adequate timeouts:
GCP Managed Kafka has higher latencies than self-hosted Kafka. 5-minute timeouts (300000ms) are recommended.

Authentication Failures

Symptoms:
  • Application fails to start
  • Logs show OAuth token errors
  • Connection refused errors

Problem: OAuth Library Missing

Error Message:
Cause: OAuth authentication library not in classpath. Solution: Add dependency to build.gradle:
Rebuild and redeploy:

Problem: Application Default Credentials Not Found

Error Message:
Cause: No credentials available in environment. Solution: For GCE/GKE, attach service account:
For local testing:

Problem: Service Account Lacks managedkafka.client Role

Error Message:
Cause: Service account can authenticate but lacks permission to connect to cluster. Solution:

Connection Issues

Symptoms:
  • Cannot connect to bootstrap servers
  • Timeout errors
  • Network unreachable

Problem: Wrong Bootstrap Address

Error Message:
Cause: Incorrect bootstrap server address or port. Solution: Verify bootstrap address:
OAuth uses port 9092 (not 9192):

Problem: Network Firewall Rules

Error Message:
Cause: Firewall blocking egress to Managed Kafka. Solution: Check VPC firewall rules:
Allow egress to Managed Kafka (default is allow):

Problem: Private Cluster Without Private Google Access

Cause: Managed Kafka cluster in VPC without Private Google Access enabled. Solution: Enable Private Google Access on subnet:

Problem: DNS Resolution Failure

Error Message:
Cause: DNS not configured correctly. Solution: Test DNS resolution:
Ensure VM uses Google’s DNS (169.254.169.254 for metadata server).

Performance Issues

Problem: High Consumer Lag

Symptoms:
  • Messages not processed in time
  • Consumer lag increasing
Diagnosis:
Solutions:
  1. Increase parallelism:
  1. Increase partition count:
  1. Scale horizontally (add more Hub instances)

Problem: Slow Rebalancing

Symptoms:
  • Rebalancing takes several minutes
  • Frequent rebalances disrupt processing
Solution: Increase rebalance timeout:
And ensure max.poll.interval.ms is adequate:

Best Practices

Use OAuth Over mTLS

Recommendation: Always use OAuth/SASL_SSL authentication for new deployments. Reasons:
  • Simpler operations: No certificate rotation or keystore management
  • IAM integration: Consistent permission model across GCP services
  • Automatic credential refresh: No manual key rotation
  • Better security: Short-lived OAuth tokens vs long-lived certificates
When to use mTLS:
  • Compliance requirements mandate certificate-based authentication
  • Migrating from existing mTLS setup (consider migrating to OAuth)

Configure Appropriate Timeouts

GCP Managed Kafka has different latency characteristics than self-hosted Kafka. Recommended Timeouts:
Why Higher Timeouts:
  • Multi-zone replication may introduce latency
  • OAuth token acquisition overhead
  • Managed service network path

Monitor Consumer Lag

Regularly monitor consumer lag to detect processing issues:
Set up Cloud Monitoring alerts for high lag:

Enable Topic Auto-Creation

Let Hub auto-create topics on startup:
This ensures topics exist before processing starts, preventing startup errors.

Use Separate Clusters per Environment

Create dedicated Managed Kafka clusters for each environment:
  • hub-dev-kafka - Development
  • hub-staging-kafka - Staging
  • hub-prod-kafka - Production
Benefits:
  • Isolation prevents cross-environment issues
  • Cost optimization (smaller dev clusters)
  • Independent scaling and configuration

Secure Bootstrap Address

Store bootstrap address as environment variable, not hardcoded:
Set via:
  • GCE metadata
  • Kubernetes ConfigMap/Secret
  • Cloud Run environment variables

Regular IAM Audits

Quarterly review IAM permissions:
Remove unused service accounts and roles.

See Also