Force a private IP during Load Balancer creation in OCI

This post is also available in: Português

Those days, I needed to recreate a Load Balancer in Oracle Cloud Infrastructure for a specific reason. It was a private Load Balancer. The problem is that when you create a Load Balancer, you can't specify the Private IP address in the Subnet that you want to use.. Oracle will pick automatically 3 IPs from that subnet, according oficial doc below:

Link: https://docs.cloud.oracle.com/iaas/Content/Balance/Concepts/balanceoverview.htm

That was my Load Balancer:

It assigned floating private IP address 10.72.16.11 in the subnet + 2 IPs to host both the primary and standby load balancers: 10.72.16.5 and 10.72.16.10.

However, after I've dropped the Load Balancer, when I have recreated it Oracle used 3 different IPs Addresses on the target Subnet, which I couldn't accept as the Load Balancer private IP was bounded to some applications. I started to try my luck, closing some IP gaps with dummy instances.. but it didn't solve the issue. The IPs returned were getting higher on each drop/create and the free IPs in the beginning of the subnet range were totally being ignored... maybe some sequence counter was being used in background.

Damn it.. so I came with  a solution to force the use of the IPs I wanted...

I've created a shellscript tool that uses oci-cli and jq in the backend and that will associate ALL (but the 3) IPs addresses available in the subnet with a dummy instance. So when I ask to create my Load Balancer again, it will use the 3 IPs that I want because they are the only ones remaining. =]

The tool is public available here: https://github.com/dbarj/oci-scripts/blob/master/oci_fill_subnet_ips.sh

How does it work?

Simply call oci_fill_subnet_ips.sh passing as first parameter the Subnet OCID that you want to use all IPs. You can also optionally pass as a second parameter with the IPs that you want to keep available (comma separated). As the example above, I passed the 3 IPs that I want to use in my Load Balancer.

The script will perform the following actions:

  1. Calculate how many IPs are still available in your Subnet.
  2. Check which instance shape is more suitable to fill your gap:
    1. VM.Standard2.1 can hold up to 64 IPs.
    2. VM.Standard2.4 can hold up to 128 IPs.
    3. VM.Standard2.8 can hold up to 256 IPs.
    4. VM.Standard2.16 can hold up to 512 IPs.
    5. VM.Standard2.24 can hold up to 768 IPs.
  3. It will create the instance above and keep it stopped to avoid utilization costs.
  4. It will than associate all the IPs as secondary IPs in the instance VNICs.
  5. If there are still gaps, it will go back to step 1 and create as many instances are needed to close it.

 

P.S: Don't forget to remove the instance after you use the remaining IPs.

You can check the ful output here:

AMAC02SJ3M7G8WN:oci-scripts rodrigo.jorge$ ./oci_fill_subnet_ips.sh  ocid1.subnet.oc1.phx.xxxxxxxx 10.72.16.5,10.72.16.10,10.72.16.11
The script will create temporary machines to allocate      244 IPs.
Type "YES" to execute it and apply the changes: YES
Total IPs to burn:      244
Creating Dummy instance to hold your IPs.
This instace is able to hold up to 256 IPs.
Action completed. Waiting until the resource has entered state: RUNNING
Adding VNIC 2 to this compute.
Adding VNIC 3 to this compute.
Adding VNIC 4 to this compute.
Adding VNIC 5 to this compute.
Adding VNIC 6 to this compute.
Adding VNIC 7 to this compute.
Adding VNIC 8 to this compute.
Stopping Dummy instance.
Action completed. Waiting until the resource has entered state: STOPPED
Adding 10.72.16.19
Adding 10.72.16.20
Adding 10.72.16.21
Adding 10.72.16.22
Adding 10.72.16.23
Adding 10.72.16.24
Adding 10.72.16.25
Adding 10.72.16.26
Adding 10.72.16.27
Adding 10.72.16.28
Adding 10.72.16.29
Adding 10.72.16.30
...
Adding 10.72.16.254
All IPs burned.

This script can also be used if you find any other OCI functionality that don't give you the possibility to specify the desired Private IP.

Hope you enjoyed.

Have you enjoyed? Please leave a comment or give a 👍!

 

1 comments

    • kuman on January 1, 2024 at 09:25
    • Reply

    If there're already used IPs in the VCN, will script skips them or have to put them as part of second param?

Leave a Reply

Your email address will not be published.