Last active
August 29, 2015 14:20
-
-
Save amokan/3e8223615d64c4ed807c to your computer and use it in GitHub Desktop.
Vagrantfile that runs NSQ in a vagrant VM under docker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
BOX_NAME = ENV['BOX_NAME'] || 'phusion-open-ubuntu-14.04-amd64' | |
BOX_URI = ENV['BOX_URI'] || 'https://oss-binaries.phusionpassenger.com/vagrant/boxes/latest/ubuntu-14.04-amd64-vbox.box' | |
VMWARE_BOX_URI = ENV['BOX_URI'] || 'https://oss-binaries.phusionpassenger.com/vagrant/boxes/latest/ubuntu-14.04-amd64-vmwarefusion.box' | |
AWS_REGION = ENV['AWS_REGION'] | |
AWS_AMI = ENV['AWS_AMI'] | |
APPS = '/mnt/apps' | |
# Not sure if this changes. Setup as a variable just in case. | |
DOCKER_IP = '172.17.42.1' | |
VAGRANTFILE_API_VERSION = '2' | |
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| | |
# Setup virtual machine box. This VM configuration code is always executed. | |
config.vm.box = BOX_NAME | |
config.vm.box_url = BOX_URI | |
# aws provider | |
config.vm.provider :aws do |aws, override| | |
aws.access_key_id = ENV['AWS_ACCESS_KEY_ID'] | |
aws.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY'] | |
aws.keypair_name = ENV['AWS_KEYPAIR_NAME'] | |
override.ssh.private_key_path = ENV['AWS_SSH_PRIVKEY'] | |
override.ssh.username = 'ubuntu' | |
aws.region = AWS_REGION | |
aws.ami = AWS_AMI | |
aws.instance_type = 'm1.xlarge' | |
end | |
# vmware fusion provider | |
config.vm.provider :vmware_fusion do |f, override| | |
override.vm.box = BOX_NAME | |
override.vm.box_url = VMWARE_BOX_URI | |
f.vmx['displayName'] = 'docker_playground' | |
end | |
# virtualbox provider | |
config.vm.provider :virtualbox do |vb| | |
config.vm.box = BOX_NAME | |
config.vm.box_url = BOX_URI | |
#memory | |
vb.customize ['modifyvm', :id, '--memory', '4096'] | |
end | |
### Port Forwarding ### | |
# Docker UI | |
config.vm.network 'forwarded_port', guest: 9000, host: 9000 | |
# nsqlookupd | |
config.vm.network 'forwarded_port', guest: 4160, host: 4160 | |
config.vm.network 'forwarded_port', guest: 4161, host: 4161 | |
# nsqd | |
config.vm.network 'forwarded_port', guest: 4150, host: 4150 | |
config.vm.network 'forwarded_port', guest: 4151, host: 4151 | |
# nsqadmin | |
config.vm.network 'forwarded_port', guest: 4171, host: 4171 | |
### Shell Provisioning ### | |
# install docker and configure vagrant user | |
if Dir.glob("#{File.dirname(__FILE__)}/.vagrant/machines/default/*/id").empty? | |
# Install Docker | |
pkg_cmd = 'wget -q -O - https://get.docker.io/gpg | apt-key add -;' \ | |
'echo deb http://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list;' \ | |
'apt-get update -qq; apt-get install -q -y --force-yes lxc-docker; ' | |
# Add vagrant user to the docker group | |
pkg_cmd << 'usermod -a -G docker vagrant; ' | |
config.vm.provision :shell, :inline => pkg_cmd | |
end | |
# setup shared directories for docker containers | |
config.vm.provision 'shell' do |s| | |
s.inline = "mkdir -p #{APPS}" | |
s.inline = "mkdir -p #{APPS}/nsqd/data" | |
end | |
### Docker Container Provisioning ### | |
config.vm.provision 'docker' do |d| | |
d.run 'nsqlookupd', image: 'nsqio/nsq', daemonize: true, cmd: "/nsqlookupd -broadcast-address='#{DOCKER_IP}'", args: "-p 4160:4160 -p 4161:4161" | |
d.run 'nsqd', image: 'nsqio/nsq', daemonize: true, cmd: "/nsqd -broadcast-address='#{DOCKER_IP}' -lookupd-tcp-address='#{DOCKER_IP}:4160' -data-path='/data'", args: "-p 4150:4150 -p 4151:4151 -v '#{APPS}/nsqd/data:/data'" | |
d.run 'nsqadmin', image: 'nsqio/nsq', daemonize: true, cmd: "/nsqadmin -lookupd-http-address='#{DOCKER_IP}:4160' -http-address='0.0.0.0:4171'", args: "-p 4171:4171" | |
d.run 'dockerui', image: 'crosbymichael/dockerui', daemonize: true, args: '-p 9000:9000 -v /var/run/docker.sock:/docker.sock crosbymichael/dockerui -e /docker.sock' | |
end | |
end |
Note: I have only attempted to run this using the vagrant VMware plugin. It should work fine with VBox or on EC2, but you may need to adjust the file a bit.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should give you four running docker containers, three of which for NSQ (
nsqlookupd
,nsqd
, andnsqadmin
). The other is the helpful dockerui image.There are some docs available on the nsq.io site for docker if you feel like tweaking things more - http://nsq.io/deployment/docker.html
After a
vagrant up
, you should be able to go to http://localhost:9000 to verify all the containers are running and, if so, hit http://localhost:4171 to view the NSQ admin UI.