Connecting to an HDInsight Kafka Cluster from a Local Machine via Python Script

Hanafi Ons 0 Reputation points
2025-07-01T12:55:51.3266667+00:00

Hello,

I've configured my Kafka cluster on Azure HDInsight with a virtual network (VNet) that includes a NAT gateway and a Network Security Group (NSG). Now, I’m trying to connect to this cluster from my local Windows machine using a Python script to send messages.

I am unsure how to correctly configure the bootstrap.servers parameter in my script. Could you provide guidance or examples on the appropriate format or settings for this scenario?

Thank you!

Azure HDInsight
Azure HDInsight
An Azure managed cluster service for open-source analytics.
231 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Venkat Reddy Navari 3,060 Reputation points Microsoft External Staff Moderator
    2025-07-01T13:30:10.97+00:00

    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)

    1. Deploy an edge node or use a VM in the same VNet as the Kafka cluster.
    2. SSH into that node and forward a local port to a broker port using:
         
         ssh -i path/to/your/key.pem -L 9093:<broker-hostname>:9093 sshuser@<edge-node-public-ip>
      
      This tunnels your local port 9093 to the Kafka broker inside the VNet.
    3. In your Python script, set bootstrap.servers to localhost: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.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.