361 Configure loadbalance

Use case

Use the role vbotka.freebsd.network to configure loadbalance.

Tree

shell> tree .
.
├── ansible.cfg
├── host_vars
│   └── iocage_03.yml
├── iocage.ini
├── pb-network.yml
└── pb-postinstall.yml

Synopsis

  • The Ansible controller connects to the iocage host iocage_03 at IP 10.1.0.17 configured in /etc/rc.conf of the managed node:

    defaultrouter="10.1.0.10"
    gateway_enable="YES"
    cloned_interfaces="bridge0"
    create_args_bridge0="addm em0"
    ifconfig_bridge0="inet 10.1.0.17/24"
    ifconfig_em0="up -tso -vlanhwtso"
    
  • The nameserver is 10.1.0.1:

    shell> cat /etc/resolv.conf
    nameserver 10.1.0.1
    
    shell> cat /etc/resolvconf.conf
    resolvconf="NO"
    
  • In the playbook pb-postinstall.yml on iocage_03, ensure the nameserver is set to 10.1.0.1.

  • In the playbook pb-network.yml on iocage_03, configure load balancing across two NICs.

Requirements

Notes

The USB NICs ue0 and ue1 are used here for testing. Using them in production is not recommended. See the FreeBSD Forum thread rc.d netif restart lagg0 to learn about issues with USB NICs.

Note

vbotka.freebsd_network is the role freebsd_network in the namespace vbotka.

ansible.cfg

[defaults]
callback_result_format = yaml
display_skipped_hosts = False
gathering = explicit
interpreter_python = auto_silent
log_path = /var/log/ansible.log
deprecation_warnings = False

[connection]
pipelining = true

Inventory iocage.ini

iocage_02 ansible_host=10.1.0.73
iocage_03 ansible_host=10.1.0.17
iocage_04 ansible_host=10.1.0.29

[iocage]
iocage_03

[iocage:vars]
ansible_user=admin
ansible_become=true
ansible_python_interpreter=auto_silent

host_vars

host_vars/iocage_03.yml
# role network
fn_gateway_enable: true
fn_defaultrouter: 10.1.0.10

fn_cloned_interfaces:
  - {interface: lagg0, state: value_present}
fn_interfaces:
  - {interface: lagg0, options: inet 10.1.0.28/24, create: laggproto loadbalance laggport ue0 laggport ue1}
  - {interface: ue0, options: -tso -vlanhwtso}
  - {interface: ue1, options: -tso -vlanhwtso}

fn_ethname: true
fn_ethname_enable: true
fn_ethname_cmd_enable: true
fn_ethname_cmd: start
fn_ethname_install: false
fn_ethname_name_mac:
  ue0: 3c:33:32:c7:20:19
  ue1: 40:86:cb:d2:53:90

# role postinstall
fp_resolvconf: true

fp_resolvconf_conf_clean: false
fp_resolvconf_conf:
  - {key: resolvconf, value: "NO"}

fp_resolv_conf_clean: false
fp_resolv_conf:
  - {key: nameserver, value: 10.1.0.1}

Playbook pb-postinstall.yml

- name: Test role vbotka.freebsd.network
  hosts: iocage
  gather_facts: true

  roles:
    - vbotka.freebsd.postinstall

Playbook output - Configure resolv.conf

The tasks fp_resolvconf configure /etc/resolvconf.conf and /etc/resolv.conf using the modules community.general.sysrc and ansible.builtin.lineinfile respectively. If you are not sure about the contents of these files, you may want to clean them before applying the configuration:

fp_resolvconf_conf_clean: true
fp_resolv_conf_clean: true

This makes the play non-idempotent. The defaults are false. To keep the play idempotent, omit these variables.

(env) > ansible-playbook -i iocage.ini \
                         -t fp_resolvconf \
                         -e fp_resolvconf_conf_clean=true \
                         -e fp_resolv_conf_clean=true \
                         pb-postinstall.yml
PLAY [Test role vbotka.freebsd.network] ****************************************

TASK [Gathering Facts] *********************************************************
ok: [iocage_03]

TASK [vbotka.freebsd.postinstall : Sanity: Unknown run tags] *******************
ok: [iocage_03]

TASK [vbotka.freebsd.postinstall : Resolvconf: Stat file /etc/resolv.conf] *****
ok: [iocage_03]

TASK [vbotka.freebsd.postinstall : Resolvconf: Clean file /etc/resolv.conf] ****
changed: [iocage_03]

TASK [vbotka.freebsd.postinstall : Resolvconf: Present lines in /etc/resolv.conf] ***
changed: [iocage_03] => (item={'key': 'nameserver', 'value': '10.1.0.1'})

TASK [vbotka.freebsd.postinstall : Resolvconf: Stat file /etc/resolvconf.conf] ***
ok: [iocage_03]

TASK [vbotka.freebsd.postinstall : Resolvconf: Clean file /etc/resolvconf.conf] ***
changed: [iocage_03]

TASK [vbotka.freebsd.postinstall : Resolvconf: Present lines in /etc/resolvconf.conf] ***
changed: [iocage_03] => (item={'key': 'resolvconf', 'value': 'NO'})

RUNNING HANDLER [vbotka.freebsd.postinstall : Resolvconf apply] ****************
changed: [iocage_03]

PLAY RECAP *********************************************************************
iocage_03                  : ok=9    changed=5    unreachable=0    failed=0    skipped=8    rescued=0    ignored=0   

Playbook pb-network.yml

- name: Test role vbotka.freebsd.network
  hosts: iocage
  gather_facts: true

  roles:
    - vbotka.freebsd.network

Playbook output - Configure loadbalance

(env) > ansible-playbook -i iocage.ini pb-network.yml
PLAY [Test role vbotka.freebsd.network] ****************************************

TASK [Gathering Facts] *********************************************************
ok: [iocage_03]

TASK [vbotka.freebsd.network : Interfaces: Configure interfaces create_args in /etc/rc.conf] ***
ok: [iocage_03] => (item={'interface': 'lagg0', 'options': 'inet 10.1.0.28/24', 'create': 'laggproto loadbalance laggport ue0 laggport ue1'})

TASK [vbotka.freebsd.network : Interfaces: Configure interfaces options in /etc/rc.conf] ***
ok: [iocage_03] => (item={'interface': 'lagg0', 'options': 'inet 10.1.0.28/24', 'create': 'laggproto loadbalance laggport ue0 laggport ue1'})
ok: [iocage_03] => (item={'interface': 'ue0', 'options': '-tso -vlanhwtso'})
ok: [iocage_03] => (item={'interface': 'ue1', 'options': '-tso -vlanhwtso'})

TASK [vbotka.freebsd.network : Cloned-interfaces: Configure cloned_interfaces in /etc/rc.conf] ***
ok: [iocage_03] => (item={'interface': 'lagg0', 'state': 'value_present'})

TASK [vbotka.freebsd.network : Routing: Set defaultrouter in /etc/rc.conf] *****
ok: [iocage_03]

TASK [vbotka.freebsd.network : Routing: Set gateway_enable=YES in /etc/rc.conf] ***
ok: [iocage_03]

TASK [vbotka.freebsd.network : Routing: Remove list of static routes from /etc/rc.conf] ***
ok: [iocage_03]

TASK [vbotka.freebsd.network : Ethname: Set ethname_enable=YES in /etc/rc.conf] ***
ok: [iocage_03]

TASK [vbotka.freebsd.network : Ethname: Remove ethname_names from /etc/rc.conf] ***
ok: [iocage_03]

TASK [vbotka.freebsd.network : Ethname: Set ethname_*_mac in /etc/rc.conf] *****
ok: [iocage_03] => (item={'key': 'ue0', 'value': '3c:33:32:c7:20:19'})
ok: [iocage_03] => (item={'key': 'ue1', 'value': '40:86:cb:d2:53:90'})

TASK [vbotka.freebsd.network : Ethname: service ethname <fn_ethname_cmd>] ******
ok: [iocage_03]

PLAY RECAP *********************************************************************
iocage_03                  : ok=11   changed=0    unreachable=0    failed=0    skipped=12   rescued=0    ignored=0   

Result

MAC addresses are sanitized.

shell> ssh admin@10.1.0.17 ifconfig lagg0
lagg0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
	options=800000<HWSTATS>
	ether 11:22:33:44:55:66
	inet 10.1.0.28 netmask 0xffffff00 broadcast 10.1.0.255
	laggproto loadbalance lagghash l2,l3,l4
	groups: lagg
	media: Ethernet autoselect
	status: no carrier
	nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>