如何配置Vagrant,以便主机可以与在VM上运行的kafka-docker进行通信?

时间:2018-05-06 21:26:58

标签: apache-kafka vagrant docker-compose

我在Vagrant Ubuntu VM上运行kafka-docker。我可以使用在VM上运行的NodeJs程序来生成和使用消息。

然而,我无法使用Windows主机上的相同NodeJs程序,程序似乎达不到但没有收到答案。

这是我的docker-compose.yml文件:

 version: '2'
 services:
   zookeeper:
     image: "wurstmeister/zookeeper:latest"
     network_mode: "host"
     ports:
       - "2181:2181"
   kafka:
     image: "wurstmeister/kafka:latest"
     network_mode: "host"
     ports:
       - 9092:9092
     environment:
       KAFKA_LISTENERS: PLAINTEXT://:9092
       KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://172.17.0.1:9092
       KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
       KAFKA_CREATE_TOPICS: "BertTopic:3:1"

这是我的Vagrantfile:

SRCMOUNT = "/hyperledger"
LOCALDEV = "/local-dev"

$script = <<SCRIPT
set -x

echo "127.0.0.1 couchdb" | tee -a /etc/hosts

cd #{SRCMOUNT}/fabric/devenv
./setup.sh

SCRIPT

Vagrant.require_version ">= 1.7.4"
Vagrant.configure('2') do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.network :forwarded_port, guest: 2181, host: 2181, id: "zookeeper", host_ip: "localhost", auto_correct: true # zookeeper 
  config.vm.network :forwarded_port, guest: 9092, host: 9092, id: "kafka", host_ip: "localhost", auto_correct: true # kafka
  config.vm.network :forwarded_port, guest: 7050, host: 7050, id: "orderer", host_ip: "localhost", auto_correct: true # fabric orderer service
  config.vm.network :forwarded_port, guest: 7051, host: 7051, id: "peer", host_ip: "localhost", auto_correct: true # fabric peer service
  config.vm.network :forwarded_port, guest: 7053, host: 7053, id: "peer_event", host_ip: "localhost", auto_correct: true # fabric peer event service
  config.vm.network :forwarded_port, guest: 7054, host: 7054, id: "ca", host_ip: "localhost", auto_correct: true # fabric-ca service
  config.vm.network :forwarded_port, guest: 5984, host: 15984, id: "couchdb", host_ip: "localhost", auto_correct: true # CouchDB service
  config.vm.synced_folder "../..", "#{SRCMOUNT}"
  config.vm.synced_folder "../..", "/opt/gopath/src/github.com/hyperledger"
  config.vm.synced_folder ENV.fetch('LOCALDEVDIR', "../.."), "#{LOCALDEV}"
  config.vm.provider :virtualbox do |vb|
    vb.name = "kafkadocker"
    vb.customize ['modifyvm', :id, '--memory', '4096']
    vb.cpus = 2

    storage_backend = ENV['DOCKER_STORAGE_BACKEND']
    case storage_backend
    when nil,"","aufs","AUFS"
      # No extra work to be done
    when "btrfs","BTRFS"
      # Add a second disk for the btrfs volume
      IO.popen("VBoxManage list systemproperties") { |f|

        success = false
        while line = f.gets do
          # Find the directory where the machine images are stored
          machine_folder = line.sub(/^Default machine folder:\s*/,"")

          if line != machine_folder
            btrfs_disk = File.join(machine_folder, vb.name, 'btrfs.vdi')

            unless File.exist?(btrfs_disk)
              # Create the disk if it doesn't already exist
              vb.customize ['createhd', '--filename', btrfs_disk, '--format', 'VDI', '--size', 20 * 1024]
            end

            # Add the disk to the VM
            vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', btrfs_disk]
            success = true

            break
          end
        end
        raise Vagrant::Errors::VagrantError.new, "Could not provision btrfs disk" if !success
      }
    else
      raise Vagrant::Errors::VagrantError.new, "Unknown storage backend type: #{storage_backend}"
    end

  end

  config.vm.provision :shell, inline: $script
end

我在Linux VM上的ifconfig显示:

docker0   Link encap:Ethernet  HWaddr 02:42:11:89:57:c8
          inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0

这是我在Ubuntu上的/ etc / hosts文件:

127.0.0.1       localhost
127.0.0.1               zookeeper
127.0.0.1               kafkadocker

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

最后想出了如何超越&#34;错误:连接ETIMEDOUT&#34;当我看到Windows中的nodejs程序连接但未能完成时。

这是简单的食谱。

首先,在Vagrantfile中,除了上面列出的转发端口行之外,还要为Oracle Virtualbox vm提供一个正确的名称:

# after line: config.vm.provider :virtualbox do |vb|
    vb.name = "kafkadocker"

使用以下命令启动虚拟机:

vagrant up

接下来,在Windows命令提示符下,执行:

ipconfig

请注意Oracle Virtualbox vm的IP地址,对我来说重要的是:

Ethernet adapter VirtualBox Host-Only Network:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::9900:a662:107f:3420%11
   IPv4 Address. . . . . . . . . . . : 192.168.56.1

接下来,我们需要配置Windows以了解何时告知它何时连接到服务器&#34; kafkadocker&#34;它需要转到IP地址192.168.56.1,因此打开Notepad ++作为管理员并将以下行添加到文件C:\ Windows \ System32 \ drivers \ etc \ hosts:

192.168.56.1    kafkadocker

接下来,在vm中,编辑docker-compose.yml并将其更改为:

version: '2'
services:
  zookeeper:
    image: "wurstmeister/zookeeper:latest"
    network_mode: "host"
    ports:
      - "2181:2181"
  kafka:
    image: "wurstmeister/kafka:latest"
    network_mode: "host"
    ports:
      - 9092
    environment:
      KAFKA_LISTENERS: PLAINTEXT://:9092
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafkadocker:9092
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_CREATE_TOPICS: "BertTopic:3:1"

重要的一行是KAFKA_ADVERTISED_LISTENERS。在连接时,Kafka会告诉Windows上的客户端passthru到服务器&#34; kafkadocker&#34;。现在,由于Windows主机文件,节点程序将知道要转到哪个IP。

现在使用以下命令启动VM中的kafka-docker:

 docker-compose up -d

接下来在Windows中打开Visual Studio代码:

cd c:\nodecode
code .

运行nodejs程序。对于制作人:

var kafka = require('kafka-node'),
    Producer = kafka.Producer,
    KeyedMessage = kafka.KeyedMessage,
    client = new kafka.Client(),
    producer = new Producer(client),
    km = new KeyedMessage('key', 'message'),
    payloads = [
        { topic: 'BertTopic', messages: 'first message', partition: 0 },
        { topic: 'BertTopic', messages: 'second message', partition: 0 },
        { topic: 'BertTopic', messages: 'third message', partition: 0 }
    ];
producer.on('ready', function () {
    producer.send(payloads, function (err, data) {
        console.log(data);
        process.exit(0);
    });
});

producer.on('error', function (err) {
console.log('ERROR: ' + err.toString());
});

对于消费者(从头开始阅读所有消息):

var kafka = require('kafka-node'),
    Consumer = kafka.Consumer,
    client = new kafka.Client(),
    consumer = new Consumer(
        client,
        [
              { topic: 'BertTopic', partition: 0, offset: 0 }
        ],
        { fromOffset: true }         
    );

consumer.on('message', function (message) 
{
    console.log(message);
});

consumer.on('error', function (err) 
{
    console.log('ERROR ' + err.toString());
});

希望这有助于某人。