-
Notifications
You must be signed in to change notification settings - Fork 2
/
cluster-up
executable file
·55 lines (45 loc) · 1.24 KB
/
cluster-up
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env ruby
require 'yaml'
IMAGE = 'andschroeder/local-kafka-cluster'
# default cluster size is 3.
cluster_size = ARGV.empty? ? 3 : ARGV[0].to_i
if `uname`.strip == 'Darwin'
HOST_IP = `ipconfig getifaddr en0 || ipconfig getifaddr en1`.strip
else
interface=`route -n | grep '^0.0.0.0'`.split("\n")[0].split(" ")[-1]
HOST_IP = `ip -4 addr show scope global dev #{interface} | grep inet`.split(" ")[1][0..-4]
end
puts "Host IP used for Kafka Brokers is #{HOST_IP}"
COMPOSE = {
'version' => '2',
'networks' => { 'cluster' => { 'driver' => 'bridge' } },
'services' => {
'zookeeper' => {
'image' => IMAGE,
'command' => 'zookeeper',
'ports' => [ "2181:2181" ],
'networks' => [ 'cluster' ],
}
}
}
def add_broker(id)
name = "kafka_#{id}"
port = 9091 + id
COMPOSE['services'][name] = {
'image' => IMAGE,
'environment' => {
'kafka_port' => port,
'kafka_id' => id,
'kafka_ip' => HOST_IP
},
'hostname' => name,
'ports' => [ "#{port}:#{port}" ],
'networks' => [ 'cluster' ],
'links' => [ "zookeeper:zookeeper" ]
}
end
(1..cluster_size).each do |i|
add_broker(i)
end
File.write('docker-compose.yml', COMPOSE.to_yaml)
`docker-compose --project-name local up -d`