Hi Hanafi Ons Since your HDInsight Kafka cluster is deployed inside a VNet with a NAT Gateway and NSG, connecting directly from your local machine isn't straightforward the Kafka brokers have internal IPs not accessible externally by default.
Option 1: SSH Tunnel via Edge Node or VM (Recommended for Dev/Test)
- Deploy an edge node or use a VM in the same VNet as the Kafka cluster.
- SSH into that node and forward a local port to a broker port using:
This tunnels your local port 9093 to the Kafka broker inside the VNet.ssh -i path/to/your/key.pem -L 9093:<broker-hostname>:9093 sshuser@<edge-node-public-ip>
- In your Python script, set
bootstrap.servers
tolocalhost:9093
:
Copy
from kafka import KafkaProducer
producer = KafkaProducer(
bootstrap_servers='localhost:9093',
security_protocol='SSL', # if SSL is enabled on HDInsight Kafka
ssl_cafile='ca-cert.pem',
ssl_certfile='client-cert.pem',
ssl_keyfile='client-key.pem'
)
Option 2: Public Access via Load Balancer (Use with Caution)
If security policies allow, expose the Kafka brokers via a public IP or Azure Load Balancer:
- Update NSG rules to allow inbound access to Kafka ports (e.g., 9093).
- Set the Kafka brokers’
advertised.listeners
to use the public IP. - Then use:
bootstrap_servers='<public-ip>:9093'
This is not recommended for production due to security implications unless secured properly with SSL and authentication.
Option 3: Connect via VPN or ExpressRoute
If your local environment is connected to the same VNet via a site-to-site VPN or ExpressRoute, you can access the private IPs of the Kafka brokers directly.
I hope this information helps. Please do let us know if you have any further queries.
Kindly consider upvoting the comment if the information provided is helpful. This can assist other community members in resolving similar issues.