Compare commits

...

28 Commits

Author SHA1 Message Date
gyptazy
71d373eedb Merge pull request #375 from gyptazy/release/update-versions-release-1.1.10
release: Create release 1.1.10 and update versions
2025-11-25 08:40:08 +01:00
Florian Paul Azim Hoberg
040eeb9f13 release: Create release 1.1.10 and update versions 2025-11-25 08:34:24 +01:00
gyptazy
4ef1e92aad Merge pull request #374 from gyptazy/prepare/release1.1.10
release: Create release 1.1.10
2025-11-25 08:01:26 +01:00
Florian Paul Azim Hoberg
7e5fe13dfe release: Create release 1.1.10
Fixes: #371
2025-11-24 16:54:23 +01:00
gyptazy
66c2ab6570 Merge pull request #370 from gyptazy/fix/missing-py-req
fix: Add missing 'packaging' dependency
2025-11-18 13:19:24 +01:00
gyptazy
ba63514896 Merge pull request #369 from gyptazy/fix/368-crash-including-storage-in-pools
fix(pools): Fixes a crash during PVE resource pool enumeration by ski…
2025-11-18 13:15:53 +01:00
Florian Paul Azim Hoberg
571025a8a6 fix: Add missing 'packaging' dependency
Sponsored-by: Stefan Oettl <stefan.oettl@isarnet.de> (@stefanoettl)
2025-11-18 13:14:02 +01:00
Florian Paul Azim Hoberg
dd13181cf9 fix(pools): Fixes a crash during PVE resource pool enumeration by skipping
members not having a 'name' property (i.e. 'storage' members)

Fixes: #368
Sponsored-by: Stefan Oettl <stefan.oettl@isarnet.de> (@stefanoettl)
2025-11-18 13:08:23 +01:00
gyptazy
37d19a6a2d Merge pull request #365 from gyptazy/fix/335-validate-instead-enforcing-affinity-rules
feature: Prevent unnecessary rebalancing by validating existing affinity enforcement before taking actions
2025-11-17 16:33:55 +01:00
gyptazy
fe333749ce feature: Prevent unnecessary rebalancing by validating existing affinity enforcement before taking actions.
Fixes: #335
2025-11-12 10:03:28 +01:00
gyptazy
8f9bcfcdcf Merge pull request #364 from gyptazy/fix/359-avoid-pve8-users-using-conntrack-state-migrations
fix: Add safety-guard for PVE 8 users when activating `conntrack-aware` migrations mistakenly.
2025-11-11 19:43:39 +01:00
gyptazy
ff5fd2f7f1 fix: Add safety-guard for PVE 8 users when activating conntrack-aware migrations mistakenly.
Fixes: #359
2025-11-11 19:40:13 +01:00
gyptazy
1f6576ecd6 Merge pull request #363 from gyptazy/fix/361-false-positive-proxmox-api-validation
fix: Fix the Proxmox API connection validation which returned a false-positive logging message of timeouts.
2025-11-11 15:25:34 +01:00
gyptazy
46bbe01141 fix: Fix the Proxmox API connection validation which returned a false-positive logging message of timeouts.
Fixes: #361
2025-11-11 10:41:30 +01:00
gyptazy
07ed12fcb7 Merge pull request #358 from gyptazy/cicd/354-add-old-stable-tests
cicd: Add integration test for Debian Bookworm
2025-11-05 11:33:07 +01:00
gyptazy
546fbc7d73 cicd: Add integration test for Debian Bookworm
* Ensure we also run integration tests for Debian Bookworm (PVE 8)

Fixes: #354
2025-11-05 11:21:58 +01:00
gyptazy
15436c431f Merge pull request #355 from gyptazy/release/1.1.9.1
release: Create hotfix release 1.1.9.1
2025-10-30 17:53:14 +01:00
gyptazy
33f6ff8db0 release: Create hotfix release 1.1.9.1
Fixes: #352
2025-10-30 17:46:54 +01:00
gyptazy
84628f232e Merge pull request #353 from gyptazy/fix/352-syntax-fix
fix: Adjust quoting of f-strings
2025-10-30 17:39:42 +01:00
gyptazy
6a91afd405 fix: Adjust quoting of f-strings
Fixes: #352
2025-10-30 17:18:17 +01:00
gyptazy
909643a09f Merge pull request #351 from gyptazy/release/1.1.9final
release: Create final 1.1.9 Release
2025-10-30 07:55:45 +01:00
Florian Paul Azim Hoberg
7de1ba366b release: Create final 1.1.9 Release
Fixes: #350
Sponsored-by: credativ GmbH <https://credativ.de>
2025-10-30 07:50:25 +01:00
gyptazy
0cb19fab34 Merge pull request #344 from gyptazy/release/1.1.9b
Release/1.1.9b
2025-10-27 16:47:48 +01:00
gyptazy
972b10b7e5 Merge pull request #349 from gyptazy/fix/343-config-validation
fix: Make pool based configuration more robust
2025-10-27 14:53:35 +01:00
Florian Paul Azim Hoberg
7fa110e465 fix: Make pool based configuration more robust
Fixes: #343
2025-10-27 14:49:40 +01:00
gyptazy
948df0316b Merge pull request #347 from gyptazy/feature/343-affinity-rules-by-pools
feature(Pools): Add affinity/anti-affinity support by pools
2025-10-27 09:50:50 +01:00
gyptazy
016378e37c feature(Pools): Add affinity/anti-affinity support by pools
Fixes: #343
2025-10-27 09:37:12 +01:00
gyptazy
8a193b9891 Merge pull request #346 from gyptazy/feature/memory-balancing-threshold
feature(balancing): Add an optional threshold in percent for balancing
2025-10-23 12:13:49 +02:00
31 changed files with 701 additions and 117 deletions

View File

@@ -0,0 +1,2 @@
added:
- Prevent redundant rebalancing by validating existing affinity enforcement before taking actions (@gyptazy). [#335]

View File

@@ -0,0 +1,2 @@
added:
- Add safety-guard for PVE 8 users when activating conntrack-aware migrations mistakenly (@gyptazy). [#359]

View File

@@ -0,0 +1,3 @@
fixed:
- Fixed the Proxmox API connection validation which returned a false-positive logging message of timeouts (@gyptazy). [#361]
- Refactored Proxmox API connection functions

View File

@@ -0,0 +1,2 @@
fixed:
- Fixed a crash during PVE resource pool enumeration by skipping members not having a 'name' property (@stefanoettl). [#368]

View File

@@ -0,0 +1 @@
date: 2025-11-25

View File

@@ -1,5 +1,5 @@
added:
- Add pressure (PSI) based balancing for memory, cpu, disk (req. PVE9 or greater) (@gyptazy). [#337|
- Add pressure (PSI) based balancing for memory, cpu, disk (req. PVE9 or greater) (@gyptazy). [#337]
- Pressure (PSI) based balancing for nodes
- Pressure (PSI) based balancing for guests
- Add PVE version evaluation

View File

@@ -0,0 +1,2 @@
added:
- Add affinity/anti-affinity support by pools (@gyptazy). [#343]

View File

@@ -1 +1 @@
date: TBD
date: 2025-10-30

View File

@@ -58,6 +58,10 @@ jobs:
integration-test-debian:
needs: build-package-debian
runs-on: ubuntu-latest
strategy:
matrix:
debian_version: [bookworm, trixie]
name: Integration Test on Debian ${{ matrix.debian_version }}
steps:
- name: Download Debian package artifact
uses: actions/download-artifact@v4
@@ -66,13 +70,18 @@ jobs:
path: package/
- name: Set up Docker with Debian image
run: docker pull debian:latest
run: docker pull debian:${{ matrix.debian_version }}
- name: Install and test Debian package in Docker container
run: |
docker run --rm -v $(pwd)/package:/package -w /package debian:latest bash -c "
apt-get update && \
apt-get install -y systemd && \
apt-get install -y ./proxlb*.deb && \
python3 -c 'import proxlb; print(\"OK: Debian package successfully installed.\")'
"
docker run --rm \
-v "$(pwd)/package:/package" \
-w /package \
debian:${{ matrix.debian_version }} \
bash -c "
set -e
apt-get update
apt-get install -y python3 systemd
apt-get install -y ./proxlb*.deb
python3 -c 'import proxlb; print(\"OK: Debian package successfully installed on ${{ matrix.debian_version }}.\")'
"

View File

@@ -5,6 +5,35 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.1.10] - 2025-11-25
### Added
- Prevent redundant rebalancing by validating existing affinity enforcement before taking actions (@gyptazy). [#335]
- Add safety-guard for PVE 8 users when activating conntrack-aware migrations mistakenly (@gyptazy). [#359]
### Fixed
- Fix the Proxmox API connection validation which returned a false-positive logging message of timeouts (@gyptazy). [#361]
- Refactored Proxmox API connection functions (@gyptazy). [#361]
- Fix a crash during PVE resource pool enumeration by skipping members not having a 'name' property (@stefanoettl). [#368]
## [1.1.9.1] - 2025-10-30
### Fixed
- Fix quoting in f-strings which may cause issues on PVE 8 / Debian Bookworm systems (@gyptazy). [#352]
## [1.1.9] - 2025-10-30
### Added
- Add an optional memory balancing threshold (@gyptazy). [#342]
- Add affinity/anti-affinity support by pools (@gyptazy). [#343]
- Add pressure (PSI) based balancing for memory, cpu, disk (req. PVE9 or greater) (@gyptazy). [#337]
- Pressure (PSI) based balancing for nodes
- Pressure (PSI) based balancing for guests
- Add PVE version evaluation
## [1.1.8] - 2025-10-09

View File

@@ -142,7 +142,7 @@ wget -O /etc/apt/trusted.gpg.d/proxlb.asc https://repo.gyptazy.com/repository.gp
#### Debian Packages (.deb files)
If you do not want to use the repository you can also find the debian packages as a .deb file on gyptazy's CDN at:
* https://cdn.gyptazy.com/debian/
* https://cdn.gyptazy.com/debian/proxlb/
Afterwards, you can simply install the package by running:
```bash
@@ -173,6 +173,8 @@ docker run -it --rm -v $(pwd)/proxlb.yaml:/etc/proxlb/proxlb.yaml proxlb
| Version | Image |
|------|:------:|
| latest | cr.gyptazy.com/proxlb/proxlb:latest |
| v1.1.10 | cr.gyptazy.com/proxlb/proxlb:v1.1.10 |
| v1.1.9.1 | cr.gyptazy.com/proxlb/proxlb:v1.1.9.1 |
| v1.1.9 | cr.gyptazy.com/proxlb/proxlb:v1.1.9 |
| v1.1.8 | cr.gyptazy.com/proxlb/proxlb:v1.1.8 |
| v1.1.7 | cr.gyptazy.com/proxlb/proxlb:v1.1.7 |
@@ -285,7 +287,8 @@ The following options can be set in the configuration file `proxlb.yaml`:
| | memory_threshold | | 75 | `Int` | The maximum threshold (in percent) that needs to be hit to perform balancing actions. (Optional) |
| | method | | memory | `Str` | The balancing method that should be used. [values: `memory` (default), `cpu`, `disk`]|
| | mode | | used | `Str` | The balancing mode that should be used. [values: `used` (default), `assigned`, `psi` (pressure)] |
| | psi | | { nodes: { memory: { pressure_full: 0.20, pressure_some: 0.20, pressure_spikes: 1.00 } } } | `Dict` | A dict of PSI based thresholds for nodes and guests |
| | psi | | { nodes: { memory: { pressure_full: 0.20, pressure_some: 0.20, pressure_spikes: 1.00 }}} | `Dict` | A dict of PSI based thresholds for nodes and guests |
| | pools | | pools: { dev: { type: affinity }, de-nbg01-db: { type: anti-affinity }} | `Dict` | A dict of pool names and their type for creating affinity/anti-affinity rules |
| `service` | | | | | |
| | daemon | | True | `Bool` | If daemon mode should be activated. |
| | `schedule` | | | `Dict` | Schedule config block for rebalancing. |
@@ -360,6 +363,14 @@ balancing:
# pressure_full: 0.20
# pressure_some: 0.20
# pressure_spikes: 1.00
pools:
dev:
type: affinity
de-nbg01-db
type: anti-affinity
pin:
- virt66
- virt77
service:
daemon: True
@@ -390,19 +401,33 @@ ProxLB provides an advanced mechanism to define affinity and anti-affinity rules
ProxLB implements affinity and anti-affinity rules through a tag-based system within the Proxmox web interface. Each guest (virtual machine or container) can be assigned specific tags, which then dictate its placement behavior. This method maintains a streamlined and secure approach to managing VM relationships while preserving Proxmoxs inherent permission model.
### Affinity Rules
<img align="left" src="https://cdn.gyptazy.com/img/proxlb-affinity-rules.jpg"/> Affinity rules are used to group certain VMs together, ensuring that they run on the same host whenever possible. This can be beneficial for workloads requiring low-latency communication, such as clustered databases or application servers that frequently exchange data.
<img align="left" src="https://cdn.gyptazy.com/img/proxlb-affinity-rules.jpg"/> Affinity rules are used to group certain VMs together, ensuring that they run on the same host whenever possible. This can be beneficial for workloads requiring low-latency communication, such as clustered databases or application servers that frequently exchange data. In general, there're two ways to manage affinity rules:
#### Affinity Rules by Tags
To define an affinity rule which keeps all guests assigned to this tag together on a node, users assign a tag with the prefix `plb_affinity_$TAG`:
#### Example for Screenshot
```
plb_affinity_talos
```
As a result, ProxLB will attempt to place all VMs with the `plb_affinity_web` tag on the same host (see also the attached screenshot with the same node).
### Anti-Affinity Rules
<img align="left" src="https://cdn.gyptazy.com/img/proxlb-anti-affinity-rules.jpg"/> Conversely, anti-affinity rules ensure that designated VMs do not run on the same physical host. This is particularly useful for high-availability setups, where redundancy is crucial. Ensuring that critical services are distributed across multiple hosts reduces the risk of a single point of failure.
#### Affinity Rules by Pools
Antoher approach is by using pools in Proxmox. This way, it can easily also combined with other resources like backup jobs. However, in this approach you need to modify the ProxLB config file to your needs. Within the `balancing` section you can create a dict of pools, including the pool name and the affinity type. Please see the example for further details:
**Example Config**
```
balancing:
[...]
pools: # Optional: Define affinity/anti-affinity rules per pool
dev: # Pool name: dev
type: affinity # Type: affinity (keeping VMs together)
pin: # Pin VMs to Nodes
- virt77 # Pinning to 'virt77' which is maybe an older system for dev labs
```
### Anti-Affinity Rules by Tags
<img align="left" src="https://cdn.gyptazy.com/img/proxlb-anti-affinity-rules.jpg"/> Conversely, anti-affinity rules ensure that designated VMs do not run on the same physical host. This is particularly useful for high-availability setups, where redundancy is crucial. Ensuring that critical services are distributed across multiple hosts reduces the risk of a single point of failure. In general, there're two ways to manage anti-affinity rules:
To define an anti-affinity rule that ensures to not move systems within this group to the same node, users assign a tag with the prefix:
@@ -413,6 +438,19 @@ plb_anti_affinity_ntp
As a result, ProxLB will try to place the VMs with the `plb_anti_affinity_ntp` tag on different hosts (see also the attached screenshot with the different nodes).
#### Anti-Affinity Rules by Pools
Antoher approach is by using pools in Proxmox. This way, it can easily also combined with other resources like backup jobs. However, in this approach you need to modify the ProxLB config file to your needs. Within the `balancing` section you can create a dict of pools, including the pool name and the affinity type. Please see the example for further details:
**Example Config**
```
balancing:
[...]
pools: # Optional: Define affinity/anti-affinity rules per pool
de-nbg01-db: # Pool name: de-nbg01-db
type: anti-affinity # Type: anti-affinity (spreading VMs apart)
```
**Note:** While this ensures that ProxLB tries distribute these VMs across different physical hosts within the Proxmox cluster this may not always work. If you have more guests attached to the group than nodes in the cluster, we still need to run them anywhere. If this case occurs, the next one with the most free resources will be selected.
### Ignore VMs
@@ -432,6 +470,7 @@ As a result, ProxLB will not migrate this guest with the `plb_ignore_dev` tag to
### Pin VMs to Specific Hypervisor Nodes
<img align="left" src="https://cdn.gyptazy.com/img/proxlb-tag-node-pinning.jpg"/> Guests, such as VMs or CTs, can also be pinned to specific (and multiple) nodes in the cluster. This might be usefull when running applications with some special licensing requirements that are only fulfilled on certain nodes. It might also be interesting, when some physical hardware is attached to a node, that is not available in general within the cluster.
#### Pinning VMs to (a) specific Hypervisor Node(s) by Tag
To pin a guest to a specific cluster node, users assign a tag with the prefix `plb_pin_$nodename` to the desired guest:
#### Example for Screenshot
@@ -441,6 +480,22 @@ plb_pin_node03
As a result, ProxLB will pin the guest `dev-vm01` to the node `virt03`.
#### Pinning VMs to (a) specific Hypervisor Node(s) by Pools
Beside the tag approach, you can also pin a resource group to a specific hypervisor or groups of hypervisors by defining a `pin` key of type list.
**Example Config**
```
balancing:
[...]
pools: # Optional: Define affinity/anti-affinity rules per pool
dev: # Pool name: dev
type: affinity # Type: affinity (keeping VMs together)
pin: # Pin VMs to Nodes
- virt77 # Pinning to 'virt77' which is maybe an older system for dev labs
```
You can also repeat this step multiple times for different node names to create a potential group of allowed hosts where a the guest may be served on. In this case, ProxLB takes the node with the lowest used resources according to the defined balancing values from this group.
**Note:** The given node names from the tag are validated. This means, ProxLB validated if the given node name is really part of the cluster. In case of a wrongly defined or unavailable node name it continous to use the regular processes to make sure the guest keeps running.

View File

@@ -60,6 +60,14 @@ balancing:
# pressure_full: 0.20
# pressure_some: 0.20
# pressure_spikes: 1.00
pools: # Optional: Define affinity/anti-affinity rules per pool
dev: # Pool name: dev
type: affinity # Type: affinity (keeping VMs together)
de-nbg01-db: # Pool name: de-nbg01-db
type: anti-affinity # Type: anti-affinity (spreading VMs apart)
pin: # Define a pinning og guests to specific node(s)
- virt66
- virt77
service:
daemon: True

20
debian/changelog vendored
View File

@@ -1,8 +1,26 @@
proxlb (1.1.10) stable; urgency=medium
* Prevent redundant rebalancing by validating existing affinity enforcement before taking actions. (Closes: #335)
* Add safety-guard for PVE 8 users when activating conntrack-aware migrations mistakenly. (Closes: #359)
* Fix the Proxmox API connection validation which returned a false-positive logging message of timeouts. (Closes: #361)
* Refactored the whole Proxmox API connection function. (Closes: #361)
* Fix a crash during PVE resource pool enumeration by skipping members not having a 'name' property. (Closes: #368)
-- Florian Paul Azim Hoberg <gyptazy@gyptazy.com> Tue, 25 Nov 2025 09:12:04 +0001
proxlb (1.1.9.1) stable; urgency=medium
* Fix quoting in f-strings which may cause issues on PVE 8 / Debian Bookworm systems. (Closes: #352)
-- Florian Paul Azim Hoberg <gyptazy@gyptazy.com> Thu, 30 Oct 2025 17:41:02 +0001
proxlb (1.1.9) stable; urgency=medium
* Add pressure (PSI) based balancing for memory, cpu, disk (req. PVE9 or greater). (Closes: #339)
* Add (memory) threshold for nodes before running balancing. (Closes: #342)
* Add affinity/anti-affinity support by pools. (Closes: #343)
-- Florian Paul Azim Hoberg <gyptazy@gyptazy.com> Thu, 22 Oct 2025 09:04:13 +0002
-- Florian Paul Azim Hoberg <gyptazy@gyptazy.com> Thu, 30 Oct 2025 06:58:43 +0001
proxlb (1.1.8) stable; urgency=medium

2
debian/control vendored
View File

@@ -7,6 +7,6 @@ Build-Depends: debhelper-compat (= 13), dh-python, python3-all, python3-setuptoo
Package: proxlb
Architecture: all
Depends: ${python3:Depends}, ${misc:Depends}, python3-requests, python3-urllib3, python3-proxmoxer, python3-yaml
Depends: ${python3:Depends}, ${misc:Depends}, python3-requests, python3-urllib3, python3-packaging, python3-proxmoxer, python3-yaml
Description: An advanced resource scheduler and load balancer for Proxmox clusters
An advanced resource scheduler and load balancer for Proxmox clusters that also supports maintenance mode and affinity/anti-affinity rules.

View File

@@ -55,7 +55,7 @@ ProxLB itself requires minimal system resources to operate. However, for managin
## Where To Run?
ProxLB can run on pretty anthing and only requires you to have a network connectivity to any of the Proxmox host's API (usually on tcp/8006).
ProxLB is lightweight and flexible where it runs on nearly any environment and only needs access to your Proxmox hosts API endpoint (commonly TCP port 8006).
Therefore, you can simply run ProxLB on:
* Bare-metal Systems

View File

@@ -80,8 +80,8 @@ ProxLB provides an advanced mechanism to define affinity and anti-affinity rules
ProxLB implements affinity and anti-affinity rules through a tag-based system within the Proxmox web interface. Each guest (virtual machine or container) can be assigned specific tags, which then dictate its placement behavior. This method maintains a streamlined and secure approach to managing VM relationships while preserving Proxmoxs inherent permission model.
#### Affinity Rules
<img align="left" src="https://cdn.gyptazy.com/images/proxlb-affinity-rules.jpg"/> Affinity rules are used to group certain VMs together, ensuring that they run on the same host whenever possible. This can be beneficial for workloads requiring low-latency communication, such as clustered databases or application servers that frequently exchange data.
#### Affinity Rules by Tags
<img align="left" src="https://cdn.gyptazy.com/img/proxlb-affinity-rules.jpg"/> Affinity rules are used to group certain VMs together, ensuring that they run on the same host whenever possible. This can be beneficial for workloads requiring low-latency communication, such as clustered databases or application servers that frequently exchange data.
To define an affinity rule which keeps all guests assigned to this tag together on a node, users assign a tag with the prefix `plb_affinity_$TAG`:
@@ -92,8 +92,20 @@ plb_affinity_talos
As a result, ProxLB will attempt to place all VMs with the `plb_affinity_web` tag on the same host (see also the attached screenshot with the same node).
#### Anti-Affinity Rules
<img align="left" src="https://cdn.gyptazy.com/images/proxlb-anti-affinity-rules.jpg"/> Conversely, anti-affinity rules ensure that designated VMs do not run on the same physical host. This is particularly useful for high-availability setups, where redundancy is crucial. Ensuring that critical services are distributed across multiple hosts reduces the risk of a single point of failure.
#### Affinity Rules by Pools
Antoher approach is by using pools in Proxmox. This way, it can easily also combined with other resources like backup jobs. However, in this approach you need to modify the ProxLB config file to your needs. Within the `balancing` section you can create a dict of pools, including the pool name and the affinity type. Please see the example for further details:
**Example Config**
```
balancing:
[...]
pools: # Optional: Define affinity/anti-affinity rules per pool
dev: # Pool name: dev
type: affinity # Type: affinity (keeping VMs together)
```
#### Anti-Affinity Rules by Tags
<img align="left" src="https://cdn.gyptazy.com/img/proxlb-anti-affinity-rules.jpg"/> Conversely, anti-affinity rules ensure that designated VMs do not run on the same physical host. This is particularly useful for high-availability setups, where redundancy is crucial. Ensuring that critical services are distributed across multiple hosts reduces the risk of a single point of failure.
To define an anti-affinity rule that ensures to not move systems within this group to the same node, users assign a tag with the prefix:
@@ -106,6 +118,18 @@ As a result, ProxLB will try to place the VMs with the `plb_anti_affinity_ntp` t
**Note:** While this ensures that ProxLB tries distribute these VMs across different physical hosts within the Proxmox cluster this may not always work. If you have more guests attached to the group than nodes in the cluster, we still need to run them anywhere. If this case occurs, the next one with the most free resources will be selected.
#### Anti-Affinity Rules by Pools
Antoher approach is by using pools in Proxmox. This way, it can easily also combined with other resources like backup jobs. However, in this approach you need to modify the ProxLB config file to your needs. Within the `balancing` section you can create a dict of pools, including the pool name and the affinity type. Please see the example for further details:
**Example Config**
```
balancing:
[...]
pools: # Optional: Define affinity/anti-affinity rules per pool
de-nbg01-db: # Pool name: de-nbg01-db
type: anti-affinity # Type: anti-affinity (spreading VMs apart)
````
### Affinity / Anti-Affinity Enforcing
When a cluster is already balanced and does not require further adjustments, enabling the enforce_affinity parameter ensures that affinity and anti-affinity rules are still respected. This parameter prioritizes the placement of guest objects according to these rules, even if it leads to slight resource imbalances or increased migration overhead. Regularly reviewing and updating these rules, along with monitoring cluster performance, helps maintain optimal performance and reliability. By carefully managing these aspects, you can create a cluster environment that meets your specific needs and maintains a good balance of resources.
@@ -117,7 +141,7 @@ balancing:
*Note: This may have impacts to the cluster. Depending on the created group matrix, the result may also be an unbalanced cluster.*
### Ignore VMs / CTs
<img align="left" src="https://cdn.gyptazy.com/images/proxlb-ignore-vm-movement.jpg"/> Guests, such as VMs or CTs, can also be completely ignored. This means, they won't be affected by any migration (even when (anti-)affinity rules are enforced). To ensure a proper resource evaluation, these guests are still collected and evaluated but simply skipped for balancing actions. Another thing is the implementation. While ProxLB might have a very restricted configuration file including the file permissions, this file is only read- and writeable by the Proxmox administrators. However, we might have user and groups who want to define on their own that their systems shouldn't be moved. Therefore, these users can simpy set a specific tag to the guest object - just like the (anti)affinity rules.
<img align="left" src="https://cdn.gyptazy.com/img/proxlb-ignore-vm-movement.jpg"/> Guests, such as VMs or CTs, can also be completely ignored. This means, they won't be affected by any migration (even when (anti-)affinity rules are enforced). To ensure a proper resource evaluation, these guests are still collected and evaluated but simply skipped for balancing actions. Another thing is the implementation. While ProxLB might have a very restricted configuration file including the file permissions, this file is only read- and writeable by the Proxmox administrators. However, we might have user and groups who want to define on their own that their systems shouldn't be moved. Therefore, these users can simpy set a specific tag to the guest object - just like the (anti)affinity rules.
To define a guest to be ignored from the balancing, users assign a tag with the prefix `plb_ignore_$TAG`:

View File

@@ -1,6 +1,6 @@
apiVersion: v3
apiVersion: v2
name: proxlb
description: A Helm chart for self-hosted ProxLB
type: application
version: "1.1.9"
appVersion: "v1.1.9"
version: "1.1.10"
appVersion: "v1.1.10"

View File

@@ -1,7 +1,7 @@
image:
registry: cr.gyptazy.com
repository: proxlb/proxlb
tag: v1.1.9
tag: v1.1.10
pullPolicy: IfNotPresent
imagePullSecrets: [ ]

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env bash
VERSION="1.1.9"
VERSION="1.1.9.1"
# ProxLB
sed -i "s/^__version__ = .*/__version__ = \"$VERSION\"/" "proxlb/utils/version.py"

View File

@@ -19,10 +19,12 @@ from utils.cli_parser import CliParser
from utils.config_parser import ConfigParser
from utils.proxmox_api import ProxmoxApi
from models.nodes import Nodes
from models.features import Features
from models.guests import Guests
from models.groups import Groups
from models.calculations import Calculations
from models.balancing import Balancing
from models.pools import Pools
from utils.helper import Helper
@@ -71,19 +73,24 @@ def main():
# Get all required objects from the Proxmox cluster
meta = {"meta": proxlb_config}
nodes = Nodes.get_nodes(proxmox_api, proxlb_config)
guests = Guests.get_guests(proxmox_api, nodes, meta)
pools = Pools.get_pools(proxmox_api)
guests = Guests.get_guests(proxmox_api, pools, nodes, meta, proxlb_config)
groups = Groups.get_groups(guests, nodes)
# Merge obtained objects from the Proxmox cluster for further usage
proxlb_data = {**meta, **nodes, **guests, **groups}
proxlb_data = {**meta, **nodes, **guests, **pools, **groups}
Helper.log_node_metrics(proxlb_data)
# Validate usable features by PVE versions
Features.validate_available_features(proxlb_data)
# Update the initial node resource assignments
# by the previously created groups.
Calculations.set_node_assignments(proxlb_data)
Calculations.set_node_hot(proxlb_data)
Calculations.set_guest_hot(proxlb_data)
Calculations.get_most_free_node(proxlb_data, cli_args.best_node)
Calculations.validate_affinity_map(proxlb_data)
Calculations.relocate_guests_on_maintenance_nodes(proxlb_data)
Calculations.get_balanciness(proxlb_data)
Calculations.relocate_guests(proxlb_data)

View File

@@ -128,11 +128,11 @@ class Calculations:
is_hot = (pressure_full >= threshold["pressure_full"] and pressure_some >= threshold["pressure_some"]) or (pressure_spikes >= threshold["pressure_spikes"])
if is_hot:
logger.debug(f"Set node {node["name"]} as hot based on {metric} pressure metrics.")
logger.debug(f"Set node {node['name']} as hot based on {metric} pressure metrics.")
proxlb_data["nodes"][node["name"]][f"{metric}_pressure_hot"] = True
proxlb_data["nodes"][node["name"]][f"pressure_hot"] = True
else:
logger.debug(f"Node {node["name"]} is not hot based on {metric} pressure metrics.")
logger.debug(f"Node {node['name']} is not hot based on {metric} pressure metrics.")
logger.debug("Finished: set_node_hot.")
return proxlb_data
@@ -161,11 +161,11 @@ class Calculations:
is_hot = (pressure_full >= threshold["pressure_full"] and pressure_some >= threshold["pressure_some"]) or (pressure_spikes >= threshold["pressure_spikes"])
if is_hot:
logger.debug(f"Set guest {guest["name"]} as hot based on {metric} pressure metrics.")
logger.debug(f"Set guest {guest['name']} as hot based on {metric} pressure metrics.")
proxlb_data["guests"][guest["name"]][f"{metric}_pressure_hot"] = True
proxlb_data["guests"][guest["name"]][f"pressure_hot"] = True
else:
logger.debug(f"guest {guest["name"]} is not hot based on {metric} pressure metrics.")
logger.debug(f"guest {guest['name']} is not hot based on {metric} pressure metrics.")
logger.debug("Finished: set_guest_hot.")
return proxlb_data
@@ -499,7 +499,7 @@ class Calculations:
logger.debug("Finished: val_node_relationships.")
@staticmethod
def update_node_resources(proxlb_data):
def update_node_resources(proxlb_data: Dict[str, Any]):
"""
Updates the resource allocation and usage statistics for nodes when a guest
is moved from one node to another.
@@ -568,3 +568,142 @@ class Calculations:
logger.debug(f"Set guest {guest_name} from node {node_current} to node {node_target}.")
logger.debug("Finished: update_node_resources.")
def validate_affinity_map(proxlb_data: Dict[str, Any]):
"""
Validates the affinity and anti-affinity constraints for all guests in the ProxLB data structure.
This function iterates through each guest and checks both affinity and anti-affinity rules.
If any guest violates these constraints, it sets the enforce_affinity flag to trigger rebalancing
and skips further validation for efficiency.
Args:
proxlb_data (Dict[str, Any]): A dictionary containing ProxLB configuration with the following structure:
- "guests" (list): List of guest identifiers to validate
- "meta" (dict): Metadata dictionary containing:
- "balancing" (dict): Balancing configuration with "enforce_affinity" flag
Returns:
None: Modifies proxlb_data in-place by updating the "enforce_affinity" flag in meta.balancing
Raises:
None: Function handles validation gracefully and logs outcomes
"""
logger.debug("Starting: validate_current_affinity.")
balancing_ok = True
for guest in proxlb_data["guests"]:
# We do not need to validate anymore if rebalancing is required
if balancing_ok is False:
proxlb_data["meta"]["balancing"]["enforce_affinity"] = True
logger.debug(f"Rebalancing based on affinity/anti-affinity map is required. Skipping further validation...")
break
balancing_state_affinity = Calculations.validate_current_affinity(proxlb_data, guest)
balancing_state_anti_affinity = Calculations.validate_current_anti_affinity(proxlb_data, guest)
logger.debug(f"Affinity for guest {guest} is {'valid' if balancing_state_affinity else 'NOT valid'}")
logger.debug(f"Anti-affinity for guest {guest} is {'valid' if balancing_state_anti_affinity else 'NOT valid'}")
balancing_ok = not balancing_state_affinity or not balancing_state_anti_affinity
if balancing_ok:
logger.debug(f"Rebalancing based on affinity/anti-affinity map is not required.")
proxlb_data["meta"]["balancing"]["enforce_affinity"] = False
logger.debug("Finished: validate_current_affinity.")
@staticmethod
def get_guest_node(proxlb_data: Dict[str, Any], guest_name: str) -> str:
"""
Return a currently assoicated PVE node where the guest is running on.
Args:
proxlb_data (Dict[str, Any]): A dictionary containing ProxLB configuration.
Returns:
node_name_current (str): The name of the current node where the guest runs on.
"""
return proxlb_data["guests"][guest_name]["node_current"]
@staticmethod
def validate_current_affinity(proxlb_data: Dict[str, Any], guest_name: str) -> bool:
"""
Validate that all guests in affinity groups containing the specified guest are on the same non-maintenance node.
This function checks affinity group constraints for a given guest. It ensures that:
1. All guests within an affinity group are located on the same physical node
2. The node hosting the affinity group is not in maintenance mode
Args:
proxlb_data (Dict[str, Any]): A dictionary containing the complete ProxLB state including:
- "groups": Dictionary with "affinity" key containing affinity group definitions
- "guests": Dictionary with guest information
- "nodes": Dictionary with node information including maintenance status
guest_name (str): The name of the guest to validate affinity for
Returns:
bool: True if all affinity groups containing the guest are valid (all members on same
non-maintenance node), False otherwise
"""
logger.debug("Starting: validate_current_affinity.")
for group_name, grp in proxlb_data["groups"]["affinity"].items():
if guest_name not in grp["guests"]:
continue
nodes = []
for group in grp["guests"]:
if group not in proxlb_data["guests"]:
continue
node = Calculations.get_guest_node(proxlb_data, group)
if proxlb_data["nodes"][node]["maintenance"]:
logger.debug(f"Group '{group_name}' invalid: node '{node}' in maintenance.")
return False
nodes.append(node)
if len(set(nodes)) != 1:
logger.debug(f"Group '{group_name}' invalid: guests spread across nodes {set(nodes)}.")
return False
return True
@staticmethod
def validate_current_anti_affinity(proxlb_data: Dict[str, Any], guest_name: str) -> bool:
"""
Validate that all guests in anti-affinity groups containing the specified guest are not on the same node.
This function checks anti-affinity group constraints for a given guest. It ensures that:
1. All guests within an anti-affinity group are located on the same physical node
2. The node hosting the anti-affinity group is not in maintenance mode
Args:
proxlb_data (Dict[str, Any]): A dictionary containing the complete ProxLB state including:
- "groups": Dictionary with "affinity" key containing affinity group definitions
- "guests": Dictionary with guest information
- "nodes": Dictionary with node information including maintenance status
guest_name (str): The name of the guest to validate affinity for
Returns:
bool: True if all anti-affinity groups containing the guest are valid (all members on different
non-maintenance node), False otherwise
"""
logger.debug("Starting: validate_current_anti_affinity.")
for group_name, grp in proxlb_data["groups"]["anti_affinity"].items():
if guest_name not in grp["guests"]:
continue
nodes = []
for group in grp["guests"]:
if group not in proxlb_data["guests"]:
continue
node = Calculations.get_guest_node(proxlb_data, group)
if proxlb_data["nodes"][node]["maintenance"]:
return False
nodes.append(node)
if len(nodes) != len(set(nodes)):
return False
return True

90
proxlb/models/features.py Normal file
View File

@@ -0,0 +1,90 @@
"""
ProxLB Features module for validating and adjusting feature flags
based on Proxmox VE node versions and cluster compatibility.
"""
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
from typing import List
from typing import Dict, Any
from utils.logger import SystemdLogger
from packaging import version
logger = SystemdLogger()
class Features:
"""
ProxLB Features module for validating and adjusting feature flags
based on Proxmox VE node versions and cluster compatibility.
Responsibilities:
- Validate and adjust feature flags based on Proxmox VE node versions.
Methods:
__init__():
No-op initializer.
validate_available_features(proxlb_data: dict) -> None:
Static method that inspects proxlb_data["nodes"] versions and disables
incompatible balancing features for Proxmox VE versions < 9.0.0.
This function mutates proxlb_data in place.
Notes:
- Expects proxlb_data to be a dict with "nodes" and "meta" keys.
"""
def __init__(self):
"""
Initializes the Features class.
"""
@staticmethod
def validate_available_features(proxlb_data: any) -> None:
"""
Validate and adjust feature flags in the provided proxlb_data according to Proxmox VE versions.
This function inspects the cluster node versions in proxlb_data and disables features
that are incompatible with Proxmox VE versions older than 9.0.0. Concretely, if any node
reports a 'pve_version' lower than "9.0.0":
- If meta.balancing.with_conntrack_state is truthy, it is set to False and a warning is logged.
- If meta.balancing.mode equals "psi", meta.balancing.enable is set to False and a warning is logged.
proxlb_data (dict): Cluster data structure that must contain:
- "nodes": a mapping (e.g., dict) whose values are mappings containing a 'pve_version' string.
- "meta": a mapping that may contain a "balancing" mapping with keys:
- "with_conntrack_state" (bool, optional)
- "mode" (str, optional)
- "enable" (bool, optional)
None: The function mutates proxlb_data in place to disable incompatible features.
Side effects:
- Mutates proxlb_data["meta"]["balancing"] when incompatible features are detected.
- Emits debug and warning log messages.
Notes:
- Unexpected or missing keys/types in proxlb_data may raise KeyError or TypeError.
- Version comparison uses semantic version parsing; callers should provide versions as strings.
Returns:
None
"""
logger.debug("Starting: validate_available_features.")
any_non_pve9_node = any(version.parse(n['pve_version']) < version.parse("9.0.0") for n in proxlb_data["nodes"].values())
if any_non_pve9_node:
with_conntrack_state = proxlb_data["meta"].get("balancing", {}).get("with_conntrack_state", False)
if with_conntrack_state:
logger.warning("Non Proxmox VE 9 systems detected: Deactivating migration option 'with-conntrack-state'!")
proxlb_data["meta"]["balancing"]["with_conntrack_state"] = False
psi_balancing = proxlb_data["meta"].get("balancing", {}).get("mode", None)
if psi_balancing == "psi":
logger.warning("Non Proxmox VE 9 systems detected: Deactivating balancing!")
proxlb_data["meta"]["balancing"]["enable"] = False
logger.debug("Finished: validate_available_features.")

View File

@@ -10,6 +10,7 @@ __license__ = "GPL-3.0"
from typing import Dict, Any
from utils.logger import SystemdLogger
from models.pools import Pools
from models.tags import Tags
import time
@@ -35,7 +36,7 @@ class Guests:
"""
@staticmethod
def get_guests(proxmox_api: any, nodes: Dict[str, Any], meta: Dict[str, Any]) -> Dict[str, Any]:
def get_guests(proxmox_api: any, pools: Dict[str, Any], nodes: Dict[str, Any], meta: Dict[str, Any], proxlb_config: Dict[str, Any]) -> Dict[str, Any]:
"""
Get metrics of all guests in a Proxmox cluster.
@@ -46,6 +47,8 @@ class Guests:
Args:
proxmox_api (any): The Proxmox API client instance.
nodes (Dict[str, Any]): A dictionary containing information about the nodes in the Proxmox cluster.
meta (Dict[str, Any]): A dictionary containing metadata information.
proxmox_config (Dict[str, Any]): A dictionary containing the ProxLB configuration.
Returns:
Dict[str, Any]: A dictionary containing metrics and information for all running guests.
@@ -91,10 +94,11 @@ class Guests:
guests['guests'][guest['name']]['processed'] = False
guests['guests'][guest['name']]['pressure_hot'] = False
guests['guests'][guest['name']]['tags'] = Tags.get_tags_from_guests(proxmox_api, node, guest['vmid'], 'vm')
guests['guests'][guest['name']]['affinity_groups'] = Tags.get_affinity_groups(guests['guests'][guest['name']]['tags'])
guests['guests'][guest['name']]['anti_affinity_groups'] = Tags.get_anti_affinity_groups(guests['guests'][guest['name']]['tags'])
guests['guests'][guest['name']]['pools'] = Pools.get_pools_for_guest(guest['name'], pools)
guests['guests'][guest['name']]['affinity_groups'] = Tags.get_affinity_groups(guests['guests'][guest['name']]['tags'], guests['guests'][guest['name']]['pools'], proxlb_config)
guests['guests'][guest['name']]['anti_affinity_groups'] = Tags.get_anti_affinity_groups(guests['guests'][guest['name']]['tags'], guests['guests'][guest['name']]['pools'], proxlb_config)
guests['guests'][guest['name']]['ignore'] = Tags.get_ignore(guests['guests'][guest['name']]['tags'])
guests['guests'][guest['name']]['node_relationships'] = Tags.get_node_relationships(guests['guests'][guest['name']]['tags'], nodes)
guests['guests'][guest['name']]['node_relationships'] = Tags.get_node_relationships(guests['guests'][guest['name']]['tags'], nodes, guests['guests'][guest['name']]['pools'], proxlb_config)
guests['guests'][guest['name']]['type'] = 'vm'
logger.debug(f"Resources of Guest {guest['name']} (type VM) added: {guests['guests'][guest['name']]}")
@@ -135,10 +139,11 @@ class Guests:
guests['guests'][guest['name']]['processed'] = False
guests['guests'][guest['name']]['pressure_hot'] = False
guests['guests'][guest['name']]['tags'] = Tags.get_tags_from_guests(proxmox_api, node, guest['vmid'], 'ct')
guests['guests'][guest['name']]['affinity_groups'] = Tags.get_affinity_groups(guests['guests'][guest['name']]['tags'])
guests['guests'][guest['name']]['anti_affinity_groups'] = Tags.get_anti_affinity_groups(guests['guests'][guest['name']]['tags'])
guests['guests'][guest['name']]['pools'] = Pools.get_pools_for_guest(guest['name'], pools)
guests['guests'][guest['name']]['affinity_groups'] = Tags.get_affinity_groups(guests['guests'][guest['name']]['tags'], guests['guests'][guest['name']]['pools'], proxlb_config)
guests['guests'][guest['name']]['anti_affinity_groups'] = Tags.get_anti_affinity_groups(guests['guests'][guest['name']]['tags'], guests['guests'][guest['name']]['pools'], proxlb_config)
guests['guests'][guest['name']]['ignore'] = Tags.get_ignore(guests['guests'][guest['name']]['tags'])
guests['guests'][guest['name']]['node_relationships'] = Tags.get_node_relationships(guests['guests'][guest['name']]['tags'], nodes)
guests['guests'][guest['name']]['node_relationships'] = Tags.get_node_relationships(guests['guests'][guest['name']]['tags'], nodes, guests['guests'][guest['name']]['pools'], proxlb_config)
guests['guests'][guest['name']]['type'] = 'ct'
logger.debug(f"Resources of Guest {guest['name']} (type CT) added: {guests['guests'][guest['name']]}")

View File

@@ -48,6 +48,7 @@ class Nodes:
Args:
proxmox_api (any): The Proxmox API client instance.
proxmox_config (Dict[str, Any]): A dictionary containing the ProxLB configuration.
nodes (Dict[str, Any]): A dictionary containing information about the nodes in the Proxmox cluster.
Returns:

117
proxlb/models/pools.py Normal file
View File

@@ -0,0 +1,117 @@
"""
The Pools class retrieves all present pools defined on a Proxmox cluster
including the chield objects.
"""
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
from typing import Dict, Any
from utils.logger import SystemdLogger
from models.tags import Tags
import time
logger = SystemdLogger()
class Pools:
"""
The Pools class retrieves all present pools defined on a Proxmox cluster
including the chield objects.
Methods:
__init__:
Initializes the Pools class.
get_pools(proxmox_api: any) -> Dict[str, Any]:
Retrieve pool definitions and membership from the Proxmox cluster.
Returns a dict with a top-level "pools" mapping each poolid to
{"name": <poolid>, "members": [<member_names>...]}.
This method does not collect per-member metrics or perform node filtering.
"""
def __init__(self):
"""
Initializes the Pools class with the provided ProxLB data.
"""
@staticmethod
def get_pools(proxmox_api: any) -> Dict[str, Any]:
"""
Retrieve all pools and their members from a Proxmox cluster.
Queries the Proxmox API for pool definitions and returns a dictionary
containing each pool's id/name and a list of its member VM/CT names.
This function does not perform per-member metric collection or node
filtering — it only gathers pool membership information.
Args:
proxmox_api (any): Proxmox API client instance.
Returns:
Dict[str, Any]: Dictionary with a top-level "pools" key mapping poolid
to {"name": <poolid>, "members": [<member_names>...]}.
"""
logger.debug("Starting: get_pools.")
pools = {"pools": {}}
# Pool objects: iterate over all pools in the cluster.
# We keep pool members even if their nodes are ignored so resource accounting
# for rebalancing remains correct and we avoid overprovisioning nodes.
for pool in proxmox_api.pools.get():
logger.debug(f"Got pool: {pool['poolid']}")
pools['pools'][pool['poolid']] = {}
pools['pools'][pool['poolid']]['name'] = pool['poolid']
pools['pools'][pool['poolid']]['members'] = []
# Fetch pool details and collect member names
pool_details = proxmox_api.pools(pool['poolid']).get()
for member in pool_details.get("members", []):
# We might also have objects without the key "name", e.g. storage pools
if "name" not in member:
logger.debug(f"Skipping member without name in pool: {pool['poolid']}")
continue
logger.debug(f"Got member: {member['name']} for pool: {pool['poolid']}")
pools['pools'][pool['poolid']]['members'].append(member["name"])
logger.debug("Finished: get_pools.")
return pools
@staticmethod
def get_pools_for_guest(guest_name: str, pools: Dict[str, Any]) -> Dict[str, Any]:
"""
Return the list of pool names that include the given guest.
Args:
guest_name (str): Name of the VM or CT to look up.
pools (Dict[str, Any]): Pools structure as returned by get_pools(),
expected to contain a top-level "pools" mapping each poolid to
{"name": <poolid>, "members": [<member_names>...]}.
Returns:
list[str]: Names of pools the guest is a member of (empty list if none).
"""
logger.debug("Starting: get_pools_for_guests.")
guest_pools = []
for pool in pools.items():
for pool_id, pool_data in pool[1].items():
if type(pool_data) is dict:
pool_name = pool_data.get("name", "")
pool_name_members = pool_data.get("members", [])
if guest_name in pool_name_members:
logger.debug(f"Guest: {guest_name} is member of Pool: {pool_name}.")
guest_pools.append(pool_name)
else:
logger.debug(f"Guest: {guest_name} is NOT member of Pool: {pool_name}.")
else:
logger.debug(f"Pool data for pool_id {pool_id} is not a dict: {pool_data}")
logger.debug("Finished: get_pools_for_guests.")
return guest_pools

View File

@@ -80,15 +80,18 @@ class Tags:
return tags
@staticmethod
def get_affinity_groups(tags: List[str]) -> List[str]:
def get_affinity_groups(tags: List[str], pools: List[str], proxlb_config: Dict[str, Any]) -> List[str]:
"""
Get affinity tags for a guest from the Proxmox cluster by the API.
This method retrieves all tags for a given guest and evaluates the
affinity tags which are required during the balancing calculations.
This method retrieves all tags for a given guest or based on a
membership of a pool and evaluates the affinity groups which are
required during the balancing calculations.
Args:
tags (List): A list holding all defined tags for a given guest.
pools (List): A list holding all defined pools for a given guest.
proxlb_config (Dict): A dict holding the ProxLB configuration.
Returns:
List: A list including all affinity tags for the given guest.
@@ -99,21 +102,36 @@ class Tags:
if len(tags) > 0:
for tag in tags:
if tag.startswith("plb_affinity"):
logger.debug(f"Adding affinity group for tag {tag}.")
affinity_tags.append(tag)
else:
logger.debug(f"Skipping affinity group for tag {tag}.")
if len(pools) > 0:
for pool in pools:
if pool in (proxlb_config['balancing'].get('pools') or {}):
if proxlb_config['balancing']['pools'][pool].get('type', None) == 'affinity':
logger.debug(f"Adding affinity group for pool {pool}.")
affinity_tags.append(pool)
else:
logger.debug(f"Skipping affinity group for pool {pool}.")
logger.debug("Finished: get_affinity_groups.")
return affinity_tags
@staticmethod
def get_anti_affinity_groups(tags: List[str]) -> List[str]:
def get_anti_affinity_groups(tags: List[str], pools: List[str], proxlb_config: Dict[str, Any]) -> List[str]:
"""
Get anti-affinity tags for a guest from the Proxmox cluster by the API.
This method retrieves all tags for a given guest and evaluates the
anti-affinity tags which are required during the balancing calculations.
This method retrieves all tags for a given guest or based on a
membership of a pool and evaluates the anti-affinity groups which
are required during the balancing calculations.
Args:
tags (List): A list holding all defined tags for a given guest.
pools (List): A list holding all defined pools for a given guest.
proxlb_config (Dict): A dict holding the ProxLB configuration.
Returns:
List: A list including all anti-affinity tags for the given guest..
@@ -124,7 +142,19 @@ class Tags:
if len(tags) > 0:
for tag in tags:
if tag.startswith("plb_anti_affinity"):
logger.debug(f"Adding anti-affinity group for tag {tag}.")
anti_affinity_tags.append(tag)
else:
logger.debug(f"Skipping anti-affinity group for tag {tag}.")
if len(pools) > 0:
for pool in pools:
if pool in (proxlb_config['balancing'].get('pools') or {}):
if proxlb_config['balancing']['pools'][pool].get('type', None) == 'anti-affinity':
logger.debug(f"Adding anti-affinity group for pool {pool}.")
anti_affinity_tags.append(pool)
else:
logger.debug(f"Skipping anti-affinity group for pool {pool}.")
logger.debug("Finished: get_anti_affinity_groups.")
return anti_affinity_tags
@@ -155,10 +185,10 @@ class Tags:
return ignore_tag
@staticmethod
def get_node_relationships(tags: List[str], nodes: Dict[str, Any]) -> str:
def get_node_relationships(tags: List[str], nodes: Dict[str, Any], pools: List[str], proxlb_config: Dict[str, Any]) -> str:
"""
Get a node relationship tag for a guest from the Proxmox cluster by the API to pin
a guest to a node.
a guest to a node or by defined pools from ProxLB configuration.
This method retrieves a relationship tag between a guest and a specific
hypervisor node to pin the guest to a specific node (e.g., for licensing reason).
@@ -166,24 +196,44 @@ class Tags:
Args:
tags (List): A list holding all defined tags for a given guest.
nodes (Dict): A dictionary holding all available nodes in the cluster.
pools (List): A list holding all defined pools for a given guest.
proxlb_config (Dict): A dict holding the ProxLB configuration.
Returns:
Str: The related hypervisor node name.
Str: The related hypervisor node name(s).
"""
logger.debug("Starting: get_node_relationships.")
node_relationship_tags = []
if len(tags) > 0:
logger.debug("Validating node pinning by tags.")
for tag in tags:
if tag.startswith("plb_pin"):
node_relationship_tag = tag.replace("plb_pin_", "")
# Validate if the node to pin is present in the cluster
if Helper.validate_node_presence(node_relationship_tag, nodes):
logger.info(f"Tag {node_relationship_tag} is valid! Defined node exists in the cluster.")
logger.debug(f"Tag {node_relationship_tag} is valid! Defined node exists in the cluster.")
logger.debug(f"Setting node relationship because of tag {tag} to {node_relationship_tag}.")
node_relationship_tags.append(node_relationship_tag)
else:
logger.warning(f"Tag {node_relationship_tag} is invalid! Defined node does not exist in the cluster. Not applying pinning.")
if len(pools) > 0:
logger.debug("Validating node pinning by pools.")
for pool in pools:
if pool in (proxlb_config['balancing'].get('pools') or {}):
node = proxlb_config['balancing']['pools'][pool].get('pin', None)
# Validate if the node to pin is present in the cluster
if Helper.validate_node_presence(node, nodes):
logger.debug(f"Pool pinning tag {node} is valid! Defined node exists in the cluster.")
logger.debug(f"Setting node relationship because of pool {pool} to {node}.")
node_relationship_tags.append(node)
else:
logger.warning(f"Pool pinning tag {node} is invalid! Defined node does not exist in the cluster. Not applying pinning.")
else:
logger.debug(f"Skipping pinning for pool {pool}. Pool is not defined in ProxLB configuration.")
logger.debug("Finished: get_node_relationships.")
return node_relationship_tags

View File

@@ -11,6 +11,7 @@ __license__ = "GPL-3.0"
import json
import uuid
import re
import socket
import sys
import time
import utils.version
@@ -307,3 +308,28 @@ class Helper:
logger.warning(f"Node {node} not found in cluster. Not applying pinning!")
logger.debug("Finished: validate_node_presence.")
return False
@staticmethod
def tcp_connect_test(addr_family: int, host: str, port: int, timeout: int) -> tuple[bool, int | None]:
"""
Attempt a TCP connection to the specified host and port to test the reachability.
Args:
addr_family (int): Address family for the socket (e.g., socket.AF_INET for IPv4, socket.AF_INET6 for IPv6).
host (str): The hostname or IP address to connect to.
port (int): The port number to connect to.
timeout (int): Connection timeout in seconds.
Returns:
tuple[bool, int | None]: A tuple containing:
- bool: True if the connection was successful, False otherwise.
- int | None: None if the connection was successful, otherwise the errno code indicating the reason for failure.
"""
test_socket = socket.socket(addr_family, socket.SOCK_STREAM)
test_socket.settimeout(timeout)
try:
rc = test_socket.connect_ex((host, port))
return (rc == 0, rc if rc != 0 else None)
finally:
test_socket.close()

View File

@@ -13,6 +13,7 @@ __copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
import errno
try:
import proxmoxer
PROXMOXER_PRESENT = True
@@ -175,49 +176,40 @@ class ProxmoxApi:
logger.debug("Starting: api_connect_get_hosts.")
# Pre-validate the given API endpoints
if not isinstance(proxmox_api_endpoints, list):
logger.critical(f"The proxmox_api hosts are not defined as a list type.")
logger.critical("The proxmox_api hosts are not defined as a list type.")
sys.exit(1)
if not proxmox_api_endpoints:
logger.critical(f"No proxmox_api hosts are defined.")
logger.critical("No proxmox_api hosts are defined.")
sys.exit(1)
if len(proxmox_api_endpoints) == 0:
logger.critical(f"No proxmox_api hosts are defined.")
sys.exit(1)
validated_api_hosts: list[tuple[str, int]] = []
# If we have multiple Proxmox API endpoints, we need to check each one by
# doing a connection attempt for IPv4 and IPv6. If we find a working one,
# we return that one. This allows us to define multiple endpoints in a cluster.
validated_api_hosts = []
for host in proxmox_api_endpoints:
retries = proxlb_config["proxmox_api"].get("retries", 1)
wait_time = proxlb_config["proxmox_api"].get("wait_time", 1)
# Get or set a default value for a maximum of retries when connecting to
# the Proxmox API
api_connection_retries = proxlb_config["proxmox_api"].get("retries", 1)
api_connection_wait_time = proxlb_config["proxmox_api"].get("wait_time", 1)
for api_connection_attempt in range(api_connection_retries):
validated_api_host, api_port = self.test_api_proxmox_host(host)
if validated_api_host:
validated_api_hosts.append(validated_api_host)
for attempt in range(retries):
candidate_host, candidate_port = self.test_api_proxmox_host(host)
if candidate_host:
validated_api_hosts.append((candidate_host, candidate_port))
break
else:
logger.warning(f"Attempt {api_connection_attempt + 1}/{api_connection_retries} failed for host {host}. Retrying in {api_connection_wait_time} seconds...")
time.sleep(api_connection_wait_time)
logger.warning(
f"Attempt {attempt + 1}/{retries} failed for host {host}. "
f"Retrying in {wait_time} seconds..."
)
time.sleep(wait_time)
if len(validated_api_hosts) > 0:
# Choose a random host to distribute the load across the cluster
# as a simple load balancing mechanism.
return random.choice(validated_api_hosts), api_port
if validated_api_hosts:
chosen_host, chosen_port = random.choice(validated_api_hosts)
return chosen_host, chosen_port
logger.critical("No valid Proxmox API hosts found.")
print("No valid Proxmox API hosts found.")
logger.debug("Finished: api_connect_get_hosts.")
sys.exit(1)
def test_api_proxmox_host(self, host: str) -> str:
def test_api_proxmox_host(self, host: str) -> tuple[str, int | None, None]:
"""
Tests the connectivity to a Proxmox host by resolving its IP address and
checking both IPv4 and IPv6 addresses.
@@ -237,31 +229,36 @@ class ProxmoxApi:
"""
logger.debug("Starting: test_api_proxmox_host.")
# Validate for custom ports in API hosts which might indicate
# that an external loadbalancer will be used.
# Validate for custom port configurations (e.g., by given external
# loadbalancer systems)
host, port = Helper.get_host_port_from_string(host)
if port is None:
port = 8006
# Try resolving DNS to IP and log non-resolvable ones
try:
ip = socket.getaddrinfo(host, None, socket.AF_UNSPEC)
infos = socket.getaddrinfo(host, None, socket.AF_UNSPEC)
except socket.gaierror:
logger.warning(f"Could not resolve {host}.")
return False
return (None, None)
# Validate if given object is IPv4 or IPv6
for address_type in ip:
if address_type[0] == socket.AF_INET:
logger.debug(f"{host} is type ipv4.")
if self.test_api_proxmox_host_ipv4(host, port):
return host, port
elif address_type[0] == socket.AF_INET6:
logger.debug(f"{host} is type ipv6.")
if self.test_api_proxmox_host_ipv6(host, port):
return host, port
else:
return False
# Check both families that are actually present
saw_family = set()
for family, *_rest in infos:
saw_family.add(family)
logger.debug("Finished: test_api_proxmox_host.")
if socket.AF_INET in saw_family:
logger.debug(f"{host} has IPv4.")
if self.test_api_proxmox_host_ipv4(host, port):
return (host, port)
if socket.AF_INET6 in saw_family:
logger.debug(f"{host} has IPv6.")
if self.test_api_proxmox_host_ipv6(host, port):
return (host, port)
logger.debug("Finished: test_api_proxmox_host (unreachable).")
return (None, None)
def test_api_proxmox_host_ipv4(self, host: str, port: int = 8006, timeout: int = 1) -> bool:
"""
@@ -280,18 +277,16 @@ class ProxmoxApi:
bool: True if the host is reachable on the specified port, False otherwise.
"""
logger.debug("Starting: test_api_proxmox_host_ipv4.")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
logger.warning(f"Warning: Host {host} ran into a timeout when connecting on IPv4 for tcp/{port}.")
result = sock.connect_ex((host, port))
if result == 0:
sock.close()
ok, rc = Helper.tcp_connect_test(socket.AF_INET, host, port, timeout)
if ok:
logger.debug(f"Host {host} is reachable on IPv4 for tcp/{port}.")
logger.debug("Finished: test_api_proxmox_host_ipv4.")
return True
sock.close()
logger.warning(f"Host {host} is unreachable on IPv4 for tcp/{port}.")
if rc == errno.ETIMEDOUT:
logger.warning(f"Timeout connecting to {host} on IPv4 tcp/{port}.")
else:
logger.warning(f"Host {host} is unreachable on IPv4 for tcp/{port} (errno {rc}).")
logger.debug("Finished: test_api_proxmox_host_ipv4.")
return False
@@ -313,18 +308,16 @@ class ProxmoxApi:
bool: True if the host is reachable on the specified port, False otherwise.
"""
logger.debug("Starting: test_api_proxmox_host_ipv6.")
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
sock.settimeout(timeout)
logger.warning(f"Host {host} ran into a timeout when connecting via IPv6 for tcp/{port}.")
result = sock.connect_ex((host, port))
if result == 0:
sock.close()
ok, rc = Helper.tcp_connect_test(socket.AF_INET6, host, port, timeout)
if ok:
logger.debug(f"Host {host} is reachable on IPv6 for tcp/{port}.")
logger.debug("Finished: test_api_proxmox_host_ipv6.")
return True
sock.close()
logger.warning(f"Host {host} is unreachable on IPv6 for tcp/{port}.")
if rc == errno.ETIMEDOUT:
logger.warning(f"Timeout connecting to {host} on IPv6 tcp/{port}.")
else:
logger.warning(f"Host {host} is unreachable on IPv6 for tcp/{port} (errno {rc}).")
logger.debug("Finished: test_api_proxmox_host_ipv6.")
return False

View File

@@ -3,5 +3,5 @@ __app_desc__ = "An advanced resource scheduler and load balancer for Proxmox clu
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
__version__ = "1.1.9"
__version__ = "1.1.10"
__url__ = "https://github.com/gyptazy/ProxLB"

View File

@@ -1,4 +1,5 @@
packaging
proxmoxer
requests
urllib3
PyYAML
PyYAML

View File

@@ -2,7 +2,7 @@ from setuptools import setup
setup(
name="proxlb",
version="1.1.9",
version="1.1.10",
description="An advanced resource scheduler and load balancer for Proxmox clusters.",
long_description="An advanced resource scheduler and load balancer for Proxmox clusters that also supports maintenance modes and affinity/anti-affinity rules.",
author="Florian Paul Azim Hoberg",