Friday, July 21, 2017

Oracle Linux - using vagrant boxes with a static IP

Vagrant is an open-source software product build by HashiCorp for building and maintaining portable virtual development environments. The core idea behind its creation lies in the fact that the environment maintenance becomes increasingly difficult in a large project with multiple technical stacks. Vagrant manages all the necessary configurations for the developers in order to avoid the unnecessary maintenance and setup time, and increases development productivity. Vagrant is written in the Ruby language, but its ecosystem supports development in almost all major languages.

I use Vagrant a lot, really a lot, and especially in combination with Oracle Linux. Oracle ships a number of default vagrant boxes from within which speeds up the development, test and experimental way of working a lot. Without having the need to manually maintain local clones of Oracle virtualbox images you can now use vagrant to extremely fast run Oracle Linux instances.  A short guide on how to get started with vagrant can be found in this specific blogpost on my blog.

The main confusion on ports and ip addresses 
When I talk to people about Vagrant and running Oracle Linux, or any other box, in this system the main confusion comes from the networking side of things. In general the first confusion is how to be able to access ports running in the box from within your local machine. In effect Vagrant will do a port mapping of ports available on the operating system in your box to a specified port on localhost. That is, when you configure this in your Vagrantfile configuration file. (which I will dedicate another post on to explain).

The second confusion comes when people need to communicate between boxes. In those cases it would be very convenient. For example, if you would have one box running with an Oracle database while a secondary box would be running your application server you would like to be able to establish connectivity to the both of them.

Giving each box an external IP
the solution to this issue is providing each Vagrant box running your Oracle Linux instance an external IP address. A hint is already given in the Vagrantfile configuration file which resides in the directory where you gave a "vagrant init" command. If you read the file you will find a comment above a commented configuration line stating : "Create a private network, which allows host-only access to the machine using a specific IP.

I my example I wanted to give a specific box a specific IP address in a static manner. In this specific case the address needed to be to be precise. This IP would become part of a private network which will only be accessible on my Macbook and can be accessed from my Macbook directly or from any other Vagrant box running on it. While you can choose any IP you would like, you should use an IP from the reserved private address space. These IPs are guaranteed to never be publicly routable, and most routers actually block traffic from going to them from the outside world.

To ensure my specific box would always run on I had to uncomment the line and ensure that it would read as the line below: "private_network", ip: ""

This binds the box via the to a private network with the specific IP we needed. If we now try to ping the box on this address it will respond and if I ping it. Also if I go into another box, for example a box with and will try to ping it will respond. Meaning, issue resolved and I have now two boxes who can freely communicate with each other without any issue.

Showing it in Oracle Linux
Now, if we have a look at the Oracle Linux operating system within the running box we can see we have a new interface for this specific address, as shown below:

eth1      Link encap:Ethernet  HWaddr 08:00:27:3D:A5:49  
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::a00:27ff:fe3d:a549/64 Scope:Link
          RX packets:86 errors:0 dropped:0 overruns:0 frame:0
          TX packets:20 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:7328 (7.1 KiB)  TX bytes:1482 (1.4 KiB)

If we want to know how it gets the IP address inside of the Oracle Linux operating system and if you are wondering if this is done with some "hidden" DHCP server that binds to a specific virtual MAC address you can check the configuration by looking into the /etc/sysconfig/network-scripts/ifcfg-eth1 config file within the Oracle Linux operating system that runs within the Vagrant box. The content of the file is shown below:

# The contents below are automatically generated by Vagrant. Do not modify.

As you can see the file is generated by vagrant itself and no "hidden" DHCP trick is required. To push the generated file Vagrant is using parts of its own provisioning solution, which can be used for a lot more interesting things. 

No comments: