diff --git a/roles/mongodb/tasks/configure-mongodb-numa.yml b/roles/mongodb/tasks/configure-mongodb-numa.yml new file mode 100644 index 00000000..d4fd8ca7 --- /dev/null +++ b/roles/mongodb/tasks/configure-mongodb-numa.yml @@ -0,0 +1,96 @@ +# Copyright (c) 2025, Itential, Inc +# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt) +--- + +- name: Detect NUMA nodes + ansible.builtin.shell: + cmd: | + set -o pipefail + if [ -d /sys/devices/system/node ]; then + ls -d /sys/devices/system/node/node[0-9]* 2>/dev/null | wc -l + else + echo 0 + fi + args: + executable: /bin/bash + register: mongodb_numa_nodes + changed_when: false + +- name: Set NUMA enabled fact + ansible.builtin.set_fact: + mongodb_numa_enabled: "{{ (mongodb_numa_nodes.stdout | int) > 1 }}" + +- name: Enable NUMA settings if multiple NUMA nodes detected + when: mongodb_numa_enabled + block: + - name: Ensure numactl is installed when NUMA is enabled + ansible.builtin.package: + name: numactl + state: present + + - name: Discover numactl path + ansible.builtin.shell: + cmd: "command -v numactl || true" + args: + executable: /bin/bash + register: mongodb_numactl_path_result + changed_when: false + become: true + + - name: Discover mongod path + ansible.builtin.shell: + cmd: "command -v mongod || true" + args: + executable: /bin/bash + register: mongodb_mongod_path_result + changed_when: false + + - name: Set NUMA helper facts + ansible.builtin.set_fact: + mongodb_numactl_path: "{{ mongodb_numactl_path_result.stdout | default('', true) }}" + mongodb_mongod_path: "{{ mongodb_mongod_path_result.stdout | default('/usr/bin/mongod', true) }}" + mongodb_numactl_present: "{{ (mongodb_numactl_path_result.stdout | default('', true) | length) > 0 }}" + + - name: Apply NUMA sysctl settings + ansible.posix.sysctl: + name: vm.zone_reclaim_mode + value: "{{ mongodb_vm_zone_reclaim_mode }}" + state: present + sysctl_file: "{{ mongodb_sysctl_file }}" + reload: true + + - name: Disable kernel NUMA balancing + ansible.posix.sysctl: + name: kernel.numa_balancing + value: "0" + state: present + sysctl_file: "{{ mongodb_sysctl_file }}" + reload: true + + - name: Ensure mongod systemd drop-in directory exists + ansible.builtin.file: + path: /etc/systemd/system/mongod.service.d + state: directory + mode: "0755" + when: mongodb_numactl_present + + - name: Configure mongod to run with numactl interleave + ansible.builtin.copy: + dest: /etc/systemd/system/mongod.service.d/numa.conf + mode: "0644" + content: | + [Service] + ExecStart= + ExecStart={{ mongodb_numactl_path }} --interleave=all {{ mongodb_mongod_path }} --config {{ mongodb_conf_file }} + register: mongodb_numa_dropin + when: mongodb_numactl_present + + - name: Reload systemd and restart mongod to apply NUMA settings + ansible.builtin.systemd: + name: mongod + state: restarted + daemon_reload: true + when: + - mongodb_numactl_present + - mongodb_numa_dropin is defined + - mongodb_numa_dropin.changed diff --git a/roles/mongodb/tasks/install-mongodb.yml b/roles/mongodb/tasks/install-mongodb.yml index d2d1784b..82ed07cb 100644 --- a/roles/mongodb/tasks/install-mongodb.yml +++ b/roles/mongodb/tasks/install-mongodb.yml @@ -68,6 +68,11 @@ file: configure-mongodb-logrotate.yml tags: configure_logrotate +- name: Configure NUMA for MongoDB + ansible.builtin.include_tasks: + file: configure-mongodb-numa.yml + tags: configure_numa + # Check if firewalld is running, if it is then open the appropriate ports - name: Gather service facts ansible.builtin.service_facts: