Assuming you've already got VirtualBox and Vagrant already installed, let's create a Vagrant box on which to run some of these nodes and get its IP address:
$ cd
$ mkdir foo-vagrant
$ cd foo-vagrant
$ vagrant init bento/ubuntu-18.04
$ vagrant up
$ vagrant ssh
Note: the init
step does nothing more than create a minimalist Vagrantfile
in the current directory for the specified box - you could just as well create this file by hand.
Once you've ssh-ed onto the machine get its IP address:
$ ip -4 -br address show
In my case the address was 10.0.2.15
- this is just an address that's been dynamically assigned by Vagrant and isn't really visible to anything beyond the Vagrant tools. What we'd like is a fixed IP address, as we want to include it in configuration files and so don't want it changing over time, that's visible to everything on the host (e.g. web browsers etc.) but not visible beyond the host.
Note: Vagrant does also support giving its boxes publicly visible IP addresses if that's what you want.
Vagrant don't provide any guidance on choosing static IP addresses such that they don't collide with the ranges that Vagrant uses for dynamic addresses. I just took the dynamic address and changed the second octet from 0 to 1 on the assumption that Vagrant is unlikely to just randomly jump around within the 10.x.x.x address range for dynamic addresses.
So exit from the Vagrant box and add something like the following to Vagrantfile
:
config.vm.network "private_network", ip: "10.1.2.15"
Now you'll need to destroy and recreate the box (I thought reload --provision
would be enough but it wasn't):
$ vagrant destroy
$ vagrant up
Note: if you ssh onto the box and get its IP address as before you'll see it now has two non-local interfaces - eth0
as before has a dynamically assigned IP address while the new eth1
has the configured static IP address. I presume it's possible to tell Vagrant to do away with eth0
or give it the static IP address.
Now on the host machine get its IP address:
$ ip -4 -br address show
Now let's make some local data available such that we can see it on the Vagrant box, e.g. a directory beta
that's a subdirectory of some directory alpha
:
$ cd .../alpha
$ cp -r beta ~/foo-vagrant
Note: when working on the Vagrant box be sure to always distinguish between localhost (127.0.0.1), i.e. the loopback address, that isn't visible externally, and 0.0.0.0 which corresponds to all interfaces and is the easiest address to listen on if you want to accept incoming connections.
When you ssh onto the Vagrant box the directory on the host that contains the Vagrantfile
is visible on the guest as /vagrant
.
$ vagrant ssh
$ ls /vagrant
If you copied over some files into the same directory as your Vagrantfile
, e.g. beta
as above, you should see them now in the ls
output.
Let's install some extra packages:
$ cd ~/foo-vagrant
$ vagrant ssh
$ sudo apt update
$ sudo apt install --yes openjdk-8-jre-headless
Note: without the sudo apt update
the sudo apt install
would fail for me as the install asked for dependencies that the un-updated apt
indexes didn't know about.
Note: some overly smart services can try to autodetect the systems public IP and end up getting the address of your cable modem or such like. In such cases you may need to disable this behavior and tell the service to use the static IP that you assigned to the Vagrant box.
If you mess things up on the Vagrant box it's nice to be able to destroy and recreate it. But it'd also be nice to have Java 8 automatically installed as part of the process (rather than repeating the apt
commands seen above). You can do this by adding the following section to the Vagrantfile
:
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt install --yes openjdk-8-jre-headless
SHELL
Once this is done you can destroy and recreate the box and immediately run java
etc.:
$ vagrant destroy
$ vagrant up
$ vagrant ssh
$ java -version
The Bento images are fairly lightweight and aren't setup with all the shell cleverness that tells you what package you need if you try a command which isn't currently installed.
So if you're missing something try this answer from Unix SE...
$ sudo apt-get install apt-file
$ sudo apt-file update
$ apt-file search --regexp '/java$'
This is very slow so it's clearly not what the shell is doing on a full install.
In an earlier section we made various modifications to the Vagrantfile
- for more details on these see the Vagrant site:
- The provisioning page covers adding additional steps, like installing Java, to creating a box.
- The networking page covers setting up port forwarding.
- The static IP section of the private network page covers assigning the box a fixed static IP address.