Compare commits

...

113 Commits

Author SHA1 Message Date
Florian Paul Azim Hoberg
c56d465f90 Add DPM (Dynamic Power Management) feature for Proxmox cluster nodes
Fixes: #141
2025-05-09 07:32:46 +02:00
Florian
1e096e1aae Merge pull request #221 from gyptazy/fix/137-systemd-unit-file
fix: Adjust the systemd unit file to run after the network target on non PVE nodes
2025-04-26 08:43:33 +02:00
gyptazy
420d669236 fix: Adjust the systemd unit file to run after the network target on non PVE nodes
Fixes: #137
2025-04-26 08:42:24 +02:00
Florian
24aa6aabc6 Merge pull request #220 from gyptazy/feature/157-add-retry-proxmox-api
feature: Add a retry mechanism when connecting to the Proxmox API
2025-04-24 13:49:55 +02:00
Florian Paul Azim Hoberg
5a9a4af532 feature: Add a retry mechanism when connecting to the Proxmox API
Fixes: #157
2025-04-24 13:29:41 +02:00
Florian
50f93e5f59 Merge pull request #219 from gyptazy/feature/218-add-1-to-1-relations-guest-hypervisor
feature: Add possibility to pin guests to a specific hypervisor node.
2025-04-24 13:01:44 +02:00
Florian Paul Azim Hoberg
33784f60b4 feature: Add possibility to pin guests to a specific hypervisor node.
Fixes: #218
2025-04-24 08:54:58 +02:00
Florian
9a261aa781 Merge pull request #213 from gyptazy/prepare/release-v1.1.2
release: Prepare release v1.1.2
2025-04-19 20:14:12 +02:00
gyptazy
366d5bc264 release: Prepare release v1.1.2 2025-04-19 20:10:49 +02:00
Florian
96ffa086b1 Merge pull request #212 from gyptazy/release/1.1.1
release: Create release 1.1.1
2025-04-19 19:45:33 +02:00
gyptazy
db005c138e release: Create release 1.1.1
Fixes: #211
2025-04-19 19:43:07 +02:00
Florian
1168f545e5 Merge pull request #210 from gyptazy/docs/209-adjust-options-in-readme
docs: * Fix the rendering of the possible values of the ProxLB option…
2025-04-19 06:50:48 +02:00
gyptazy
cc663c0518 docs: * Fix the rendering of the possible values of the ProxLB options in the README file
* Mention the privilege separation part on the token generation chapter

Fixes: #209
2025-04-19 06:49:04 +02:00
Florian
40de31bc3b Merge pull request #208 from gyptazy/techdebt/fix-code-style
tecdebt: Adjust code style.
2025-04-18 17:07:01 +02:00
gyptazy
5884d76ff4 tecdebt: Adjust code style. 2025-04-18 16:52:59 +02:00
Florian
7cc59eb6fc Merge pull request #202 from glitchvern/fix/200-requery-zero-guest-cpu-used2
fix: Requery a guest if that running guest reports 0 cpu usage
2025-04-18 16:38:17 +02:00
gyptazy
24b3b35640 fix: Fix the guest type relationship in the logs when a migration job failed (by @gyptazy) [#204]
feature: Providing the API upstream error message when migration fails in debug mode (by @gyptazy) [#205]

Fixes: #204
Fixes: #205
2025-04-18 16:35:02 +02:00
Florian
f2b8829299 Merge pull request #204 from sid3windr/patch-1
Fix default configuration file path in README.md
2025-04-18 12:41:22 +02:00
Tom Laermans
4b64a041cc Fix default configuration file path in README.md
With 1.1.0, the default configuration file changed from proxlb.conf to proxlb.yaml but the README was not fully updated.
2025-04-18 11:04:51 +02:00
glitchvern
bd1157127a fix: limit to 10 requerys per a guest 2025-04-17 16:13:28 +00:00
glitchvern
be6e4bbfa0 fix: Requery a guest if that running guest reports 0 cpu usage 2025-04-16 18:42:27 +00:00
Florian
25b631099c Merge pull request #199 from gyptazy/docs/193-add-chapter-ignore-vm
docs: Add documentation about ignore guests such like VMs or CTs.
2025-04-15 19:23:27 +02:00
gyptazy
1d698c5688 docs: Add documentation about ignore guests such like VMs or CTs.
Fixes: #193
2025-04-15 19:22:10 +02:00
Florian
40f848ad7f Merge pull request #198 from glitchvern/fix/197-remove-hard-coded-memory-usage-from-lowest-usage-node
fix: Use method/mode in configuration to calculate lowest_usage_node
2025-04-15 19:08:52 +02:00
Florian
fd2725c878 Merge pull request #196 from glitchvern/fix/195-cpu-used-times-cpu-cores
fix: set cpu_used to be cpu usage times number of cpu cores
2025-04-15 18:36:25 +02:00
glitchvern
34b1d72e40 fix: Use method and mode specified in configuration to calculate lowest_usage_node 2025-04-15 16:27:08 +00:00
glitchvern
ca7db26976 fix: set cpu_used to be cpu usage times number of cpu cores 2025-04-14 21:23:05 +00:00
Florian
94552f9c9e Merge pull request #194 from crandler/main
Main
2025-04-14 12:44:50 +02:00
Sven Eulberg
32c67b9c96 fix: typos 2025-04-14 12:36:28 +02:00
Florian
89f337d8c3 Merge pull request #192 from gyptazy/tecdebt/185-improve-logging-code
tecdebt: Improve logging handler creation
2025-04-14 06:55:51 +02:00
Florian Paul Azim Hoberg (@gyptazy)
8a724400b8 tecdebt: Improve logging handler creation
Fixes: #185
2025-04-14 06:52:04 +02:00
Florian
f96f1d0f64 Merge pull request #186 from glitchvern/fix/185-logging-handler-for-no-systemd-integration
fix: logging handler for no systemd integration
2025-04-14 06:46:58 +02:00
Florian
15398712ee Merge pull request #190 from mika/mika/docs
docs: Fix minor typos
2025-04-13 11:19:18 +02:00
Florian
ddb9963062 Merge pull request #191 from gyptazy/feature/184-validate-user-permissions
Feature: Add validation for the minimum required permissions of a user in Proxmox.
2025-04-13 11:16:09 +02:00
Florian Paul Azim Hoberg (@gyptazy)
f18a9f3d4c Feature: Add validation for the minimum required permissions of a user in Proxmox.
Fixes: #184
2025-04-13 11:12:30 +02:00
Michael Prokop
1402ba9732 Minor typo fixes
s/connectoing/connecting/
s/furhter/further/
s/interating/iterating/
s/ist/is/
s/maintence/maintenance/
s/performt/performed/
s/ressources/resources/
s/sucessfully/successfully/
s/the the/the/
s/timout/timeout/
s/wether/whether/
2025-04-13 10:48:23 +02:00
Florian
af51f53221 Merge pull request #188 from glitchvern/fix/187-allow-use-of-minutes-instead-of-hours
fix: allow use of minutes instead of hours
2025-04-13 08:49:17 +02:00
glitchvern
bce2d640ef fix: allow use of minutes instead of hours 2025-04-11 23:09:00 +00:00
glitchvern
1bb1847e45 fix: logging handler for no systemd integration 2025-04-11 21:55:09 +00:00
Florian
e9543db138 Merge pull request #182 from gyptazy/change/180-switch-default-balancing-to-used-instead-assigned
change: Change the default banalcing mode to used instead of assigned.
2025-04-10 09:34:19 +02:00
gyptazy
a8e8229787 change: Change the default banalcing mode to used instead of assigned.
Fixes: #180
2025-04-10 09:33:17 +02:00
Florian
d1c91c6f2a Merge pull request #179 from gyptazy/docs/164-adjust-api-token-usage
docs: Adjust docs regarding API Token and privilege separation.
2025-04-07 16:14:40 +02:00
gyptazy
843691f8b4 docs: Adjust docs regarding API Token and priviledge separation.
Fixes: #164
2025-04-07 15:51:44 +02:00
Florian
c9f14946d1 Merge pull request #178 from gyptazy/fix/174-honor-balancing-activation-value
fix: Honor the value when balancing should not be performed and stop balancing.
2025-04-07 15:41:02 +02:00
gyptazy
77cd7b5388 fix: Honor the value when balancing should not be performed and stop balancing.
Fixes: #174
2025-04-07 15:38:32 +02:00
Florian
55502f9bed Merge pull request #177 from gyptazy/change/176-change-turn-daemon-mode-on-default
change: Change the default behaviour of the daemon mode to active.
2025-04-07 15:28:12 +02:00
gyptazy
f08b823cc4 change: Change the default behaviour of the daemon mode to active.
Fixes: #176
2025-04-07 15:25:10 +02:00
Florian
f831d4044f Merge pull request #175 from gyptazy/feature/168-add-more-flexible-schedule-timers
feature: Add a more flexible way to define schedules directly in minutes or hours
2025-04-07 15:20:22 +02:00
gyptazy
e8d8d160a7 feature: Add a more flexible way to define schedules directly in minutes or hours. [#168]
Sponsored-by: @gyptazy
Fixes: #168
2025-04-07 15:16:55 +02:00
Florian
dbbd4c0ec8 Merge pull request #172 from gyptazy/changelog/171-set-correct-python-path-docker-image
changelog: Add changelog for: Fix Python 3 path for Docker entrypoint
2025-04-02 07:24:01 +02:00
Florian
fc9a0e2858 Merge pull request #171 from crandler/main
fix: path correction for docker entrypoint
2025-04-02 07:23:48 +02:00
gyptazy
17eb43db94 changelog: Add changelog for: Fix Python 3 path for Docker entrypoint
Sponsored-by: @crandler
Fixes: #170
Fixes: #171
2025-04-02 07:20:15 +02:00
Sven Eulberg
06610e9b9d Path correction 2025-04-01 18:38:58 +02:00
Florian
889b88fd6c Merge pull request #167 from gyptazy/prep/1.1.1
release: Prepare development branch for release 1.1.1
2025-04-01 08:03:36 +02:00
gyptazy
c5ca3e13e0 release: Prepare development branch for release 1.1.1 2025-04-01 08:02:40 +02:00
Florian
c1c524f092 Merge pull request #166 from gyptazy/fix/163-ignore-vm-tag
fix: Fix tag evluation for VMs for being ignored for further balancing
2025-04-01 07:01:14 +02:00
gyptazy
7ea7defa1f fix: Fix tag evluation for VMs for being ignored for further balancing
Fixes: #163
Fixes: #165
2025-04-01 06:51:42 +02:00
Florian
6147c0085b Merge pull request #161 from gyptazy/fix/spell-docs
fix: Adjust spelling in the docs
2025-03-31 07:39:40 +02:00
gyptazy
0b70a9c767 fix: Adjust spelling in the docs 2025-03-31 07:38:04 +02:00
Florian
d6d22c4096 Merge pull request #160 from gyptazy/fix/142-mutal-exclusive-on-pass
fix: Fix mutal exclusive authentication based on secrets.
2025-03-31 06:50:26 +02:00
gyptazy
6da54c1255 fix: Fix mutal exclusive authentication based on secrets.
Fixes: #142
2025-03-31 06:46:31 +02:00
Florian
b55b4ea7a0 Merge pull request #153 from gyptazy/docs/installation
release: Prepare release 1.1.0
2025-03-31 05:15:05 +02:00
Florian
51625fe09e Merge pull request #159 from gyptazy/feature/json-output
fix: Add JSON output again
2025-03-25 09:34:10 +01:00
Florian Paul Azim Hoberg (@gyptazy)
f3b9d33c87 fix: Add JSON output again
Fixes: #158
2025-03-25 09:28:33 +01:00
Florian
8e4326f77a Merge pull request #156 from gyptazy/fix/137-fix-systemd-unit
fix: Fix the systemd unit file to start after the pveproxy daemon
2025-03-24 18:25:10 +01:00
gyptazy
3d642a7404 fix: Fix the systemd unit file to start after the pveproxy daemon
Fixes: #137
2025-03-24 18:15:11 +01:00
gyptazy
552364471d release: Create release 1.1.0
- Create release 1.1.0 content
 - Add documentation for release 1.1.0
 - Adjust changelog

Fixes: #114
Fixes: #154
Sponsored-by: credativ GmbH (https://credativ.de)
2025-03-20 20:19:34 +01:00
Florian
cf15866270 Merge pull request #151 from gyptazy/packaging/container-image
feature: Add Dockerfile to create container image
2025-03-19 14:53:04 +01:00
Florian Paul Azim Hoberg (@gyptazy)
7d4def14b1 feature: Add Dockerfile to create container image
* Also switch from Debian image to Alpine image
2025-03-19 14:48:44 +01:00
Florian
20ad9389d4 Merge pull request #150 from gyptazy/docs/adjust_docs
docs: Add docs for configuration and faq.
2025-03-18 15:09:04 +01:00
Florian Paul Azim Hoberg (@gyptazy)
d73073a187 docs: Add docs for configuration and faq. 2025-03-18 15:05:29 +01:00
Florian
b307d556e5 Merge pull request #149 from gyptazy/packaging/debian
packaging: Add Debian packaging
2025-03-18 09:30:15 +01:00
gyptazy
17c4dc445e packaging: Add Debian packaging
Fixes: #148
2025-03-18 08:40:28 +01:00
Florian
03ea29ae81 Merge pull request #147 from gyptazy/adjustment/146-rename-param-force-to-enforce-affinity
adjustment: Rename param `force` to `enforce_affinity`
2025-03-17 09:14:38 +01:00
gyptazy
e22a27652c adjustment: Rename param force to enforce_affinity
* This change should make the parameter's intention more clear
2025-03-17 09:10:34 +01:00
Florian
c3ae3e1f8c Merge pull request #145 from gyptazy/fix/142-api-user-token-mutually-exclusive
fix: Add validation for mutal exclusive user authentication
2025-03-17 07:43:00 +01:00
gyptazy
094a9b2ebb fix: Add validation for mutal exclusive user authentication
* Validate that only username/password OR API token is being used

Fixes: #142
2025-03-17 07:40:01 +01:00
Florian
d8b1c74155 Merge pull request #144 from gyptazy/fix/143-catch-non-resolvable-dns-addresses
fix: Catch non-resolvable DNS names and log them but proceed by the g…
2025-03-17 06:59:47 +01:00
gyptazy
c8fad9605c fix: Catch non-resolvable DNS names and log them but proceed by the given host list
Fixes: #143
2025-03-17 06:57:01 +01:00
Florian
e8d0c13f16 Merge pull request #140 from gyptazy/fix/adjust-branch-license-info
enhancement: Adjust license information, adjust class/func descriptions
2025-03-03 10:51:00 +01:00
gyptazy
f781e74d3a enhancement: Adjust license information, adjust class/func descriptions 2025-03-03 10:48:12 +01:00
Florian
3cbdb12741 Merge pull request #139 from gyptazy/feature/125-add-proxmox-api-token-support
feature: Add Proxmox API token authentication
2025-03-02 17:38:46 +01:00
gyptazy
a714ea8d64 feature: Add Proxmox API token authentication
Fixes: #125
2025-03-02 17:35:02 +01:00
Florian
d81d4380de Merge pull request #138 from gyptazy/release/1.1.0-alpha
refactor: Code refactor of ProxLB preparing release 1.1.0
2025-03-02 17:10:40 +01:00
gyptazy
31498da25a refactor: Code refactor of ProxLB preparing release 1.1.0
Fixes: #114
Fixes: #132
Fixes: #130
Fixes: #129
Fixes: #128
Fixes: #127
Fixes: #123
Fixes: #102
2025-03-02 17:03:49 +01:00
Florian
7f59f69eab Merge pull request #137 from thomasfinstad/fix/135-systemd-service-install-target
fix: systemd service install target
2025-02-19 08:50:29 +01:00
Thomas Finstad
200b7cd170 fix: systemd service install target 2025-02-19 08:47:03 +01:00
Florian
94df2fd1a6 Merge pull request #134 from Mrton0121/main
feat: optimize Dockerfile, create docker-compose with build
2025-01-24 10:38:15 +01:00
Mrton0121
8d8fd518fe feat: optimize Dockerfile, create docker-compose with build 2025-01-24 09:38:32 +01:00
Florian
37bb226cf0 Merge pull request #131 from gyptazy/release/1.0.6
release: Create release 1.0.6
2024-12-24 11:00:53 +01:00
gyptazy
48b8a07135 release: Create release 1.0.6 2024-12-24 10:59:40 +01:00
Florian
222beb360c Merge pull request #120 from gyptazy/fix/119-maintenance-mode-cli-and-config
Fix maintenance mode when using cli arg and config mode by using the merged list.
2024-11-09 09:36:57 +01:00
gyptazy
f9b30d0af4 Fix maintenance mode when using cli arg and config mode by using the merged list.
Sponsored-by: @CartCaved
Fixes: #119
2024-11-09 09:35:21 +01:00
Florian
28f87e2907 Merge pull request #117 from gyptazy/fix/115-ignore-schedule-bool-parsing
fix: Fix that a scheduler time definition of 1 (int) gets wrongly interpreted as a bool.
2024-11-06 08:47:10 +01:00
gyptazy
7587e1beaf fix: Fix that a scheduler time definition of 1 (int) gets wrongly interpreted as a bool.
Fixes: #115
2024-11-06 08:43:00 +01:00
Florian
5542b9bc6c Merge pull request #116 from gyptazy/release/prepare-release-1.0.5
release: Create release 1.0.5
2024-10-30 17:13:06 +01:00
gyptazy
16c5ee4d74 release: Create release 1.0.5 2024-10-30 17:04:14 +01:00
Alex Shut
21a73b71df Allow migration from local disks (#113)
Add parameter `with-local-disks=1` to allow migration from local disks.
2024-10-19 08:17:10 +02:00
Elliot Nevills
d3c055cbad fix: bug to allow log_verbosity be set to DEBUG, WARN, and WARNING (#98)
* fixed bug to allow log_verbosity be set to DEBUG, WARN, and WARNING

* removed WARN logging verbosity option

* adjusted for logging verbosity options

* add to changelog

* added fix log verbosity issue to changelog
2024-10-18 21:21:35 +02:00
Florian
24b7d2c860 Merge pull request #111 from gyptazy/fix/106-fix-maintenance-node-eval
fix: Fix offline node evaluation & maintenance compare of different type objects
2024-10-18 21:20:54 +02:00
gyptazy
6e87e2d478 fix: Fix offline node evaluation & maintenance compare of different type objects
- Fix node (and its objects) evaluation when not reachable (e.g., maintenance).
  - Fix evaluation of maintenance mode where comparing list & string resulted in a crash (by @glitchvern).
  - Set ProxLB version to 1.0.5b

Fixes: #160
Fixes: #107
Contributed-by: @glitchvern
2024-10-17 13:32:47 +02:00
Florian
2593b87d3f Merge pull request #105 from gyptazy/fix/104-adjust-docs-parallel-migration
fix(docs): Change docs to make bool usage in configs more clear.
2024-10-16 08:48:56 +02:00
gyptazy
6310262e97 fix(docs): Change docs to make bool usage in configs more clear.
Fixes: #104
2024-10-16 08:19:16 +02:00
Florian
38712e90a3 Update issue templates 2024-10-11 14:49:43 +02:00
Florian
c2b2f62462 Update issue templates 2024-10-11 14:47:22 +02:00
Florian
adde04639e Update issue templates 2024-10-11 14:44:00 +02:00
Florian
a4b1f4af24 Merge pull request #96 from gyptazy/release/1.0.4
release: Create stable release 1.0.4
2024-10-11 12:37:44 +02:00
Florian Paul Azim Hoberg
55c714a888 release: Create stable release 1.0.4
Fixes: #95
2024-10-11 12:30:10 +02:00
Florian
3cd631db20 Merge pull request #93 from gyptazy/fix/75-fix-cpu-balancing-calculations
fix: Fix CPU balancing where calculations are done in float instead of int.
2024-10-11 08:35:28 +02:00
Florian Paul Azim Hoberg
d44da076cc fix: Fix CPU balancing where calculations are done in float instead of string.
By: @glitchvern
Fixes: #75
2024-10-11 08:25:12 +02:00
Florian
95e8fc5737 Merge pull request #92 from gyptazy/feature/91-make-api-timeout-configureable
feature: Add feature to make API timeout configureable
2024-10-10 19:35:57 +02:00
Florian Paul Azim Hoberg
50a9e91633 feature: Add feature to make API timeout configureable
Fixes: #91
2024-10-10 19:33:21 +02:00
Florian
cca4c454dd Merge pull request #90 from gyptazy/feature/add-version-output
feature: Add version output as cli arg
2024-10-10 10:36:17 +02:00
101 changed files with 3970 additions and 2472 deletions

View File

@@ -0,0 +1,2 @@
fixed:
- Fix CPU balancing where calculations are done in float instead of int. (by @glitchvern) [#75]

View File

@@ -0,0 +1,2 @@
added:
- Add feature to make API timeout configureable. [#91]

View File

@@ -1 +1 @@
date: TBD
date: 2024-10-11

View File

@@ -0,0 +1,2 @@
changed:
- Change docs to make bool usage in configs more clear (by @gyptazy). [#104]

View File

@@ -0,0 +1,2 @@
fixed:
- Fix evaluation of maintenance mode where comparing list & string resulted in a crash (by @glitchvern). [#106]

View File

@@ -0,0 +1,2 @@
fixed:
- Fix node (and its objects) evaluation when not reachable, e.g., maintenance (by @gyptazy). [#107]

View File

@@ -0,0 +1,2 @@
fixed:
- Fix migration from local disks (by @greenlogles). [#113]

View File

@@ -0,0 +1,2 @@
fixed:
- Fix allowed values (add DEBUG, WARNING) for log verbosity (by @gyptazy). [#98]

View File

@@ -0,0 +1 @@
date: 2024-10-30

View File

@@ -0,0 +1,2 @@
fixed:
- Fix that a scheduler time definition of 1 (int) gets wrongly interpreted as a bool (by @gyptazy). [#115]

View File

@@ -0,0 +1,2 @@
fixed:
- Fix maintenance mode when using cli arg and config mode by using the merged list (by @CartCaved). [#119]

View File

@@ -0,0 +1 @@
date: 2024-12-24

View File

@@ -0,0 +1,11 @@
fixed:
- Refactored code base for ProxLB [#114]
- Switched to `pycodestyle` for linting [#114]
- Package building will be done within GitHub actions pipeline [#114]
- ProxLB now only returns a warning when no guests for further balancing are not present (instead of quitting) [132#]
- All nodes (according to the free resources) will be used now [#130]
- Fixed logging outputs where highest/lowest were mixed-up [#129]
- Stop balancing when movement would get worste (new force param to enfoce for affinity rules) [#128]
- Added requested documentation regarding Proxmox HA groups [#127]
- Rewrite of the whole affinity/anti-affinity rules evaluation and placement [#123]
- Fixed the `ignore` parameter for nodes where the node and guests on the node will be untouched [#102]

View File

@@ -0,0 +1,2 @@
feature:
- Add Proxmox API authentication support. [#125]

View File

@@ -0,0 +1,2 @@
fixed:
- Fix the systemd unit file to start ProxLB after pveproxy (by @robertdahlem). [#137]

View File

@@ -0,0 +1 @@
date: 2025-04-01

View File

@@ -0,0 +1,2 @@
fixed:
- Fix tag evluation for VMs for being ignored for further balancing [#163]

View File

@@ -0,0 +1,2 @@
fixed:
- Improve logging verbosity of messages that had a wrong servity [#165]

View File

@@ -0,0 +1,2 @@
feature:
- Add a more flexible way to define schedules in minutes or hours (by @gyptazy) [#168]

View File

@@ -0,0 +1,2 @@
fixed:
- Fix Python path for Docker entrypoint (by @crandler) [#170]

View File

@@ -0,0 +1,2 @@
fixed:
- Honor the value when balancing should not be performed and stop balancing [#174]

View File

@@ -0,0 +1,2 @@
changed:
- Change the default behaviour of the daemon mode to active [#176]

View File

@@ -0,0 +1,2 @@
changed:
- Change the default banalcing mode to used instead of assigned [#180]

View File

@@ -0,0 +1,2 @@
feature:
- Add validation for the minimum required permissions of a user in Proxmox [#184]

View File

@@ -0,0 +1,2 @@
fix:
- add handler to log messages with severity less than info to the screen when there is no systemd integration, for instance, inside a docker container (by @glitchvern) [#185]

View File

@@ -0,0 +1,2 @@
fixed:
- allow the use of minutes instead of hours and only accept hours or minutes in the format (by @glitchvern) [#187]

View File

@@ -0,0 +1,2 @@
fixed:
- Set cpu_used to the cpu usage, which is a percent, times the total number of cores to get a number where guest cpu_used can be added to nodes cpu_used and be meaningful (by @glitchvern) [#195]

View File

@@ -0,0 +1,2 @@
fixed:
- Remove hard coded memory usage from lowest usage node and use method and mode specified in configuration instead (by @glitchvern) [#197]

View File

@@ -0,0 +1,2 @@
fixed:
- Requery a guest if that running guest reports 0 cpu usage (by @glitchvern) [#200]

View File

@@ -0,0 +1,2 @@
fixed:
- Fix the guest type relationship in the logs when a migration job failed (by @gyptazy) [#204]

View File

@@ -0,0 +1,2 @@
added:
- Providing the API upstream error message when migration fails in debug mode (by @gyptazy) [#205]

View File

@@ -0,0 +1 @@
date: 2025-04-20

View File

@@ -0,0 +1,2 @@
fixed:
- Fix systemd unit file to run after network on non PVE nodes (by @robertdahlem) [#137]

View File

@@ -0,0 +1,2 @@
added:
- Add a configurable retry mechanism when connecting to the Proxmox API (by @gyptazy) [#157]

View File

@@ -0,0 +1,2 @@
added:
- Add 1-to-1 relationships between guest and hypervisor node to ping a guest on a node (by @gyptazy) [#218]

View File

@@ -0,0 +1 @@
date: TBD

View File

@@ -0,0 +1,2 @@
added:
- Add power management feature for cluster nodes (by @gyptazy) [#141]

View File

@@ -0,0 +1 @@
date: TBD

View File

@@ -1,3 +0,0 @@
[flake8]
per-file-ignores =
proxlb: E501,E221,E266,E231,E127,E222,E128

27
.github/ISSUE_TEMPLATE/bug_report.md vendored Normal file
View File

@@ -0,0 +1,27 @@
---
name: Bug report
about: Create a bug report
title: "`Bug`:"
labels: bug, needs-analysis
assignees: ''
---
## General
<-- Describe the bug from a high level perspective. -->
## Weighting
Score: <-- Define a scoring from 0-10 (10 highest, most urgent) -->
## Config
<-- Attach the ProxLB configuration for further analysis. Please take car to NOT publish your API credentials! -->
## Log
<-- Attach the ProxLB debug log for further analysis. Please take car to NOT publish your API credentials! -->
## Meta
Please provide some more information about your setup. This includes where you obtained ProxLB (e.g., as a `.deb` file, from the repository or container image) and also which version you're running in which mode. You can obtain the used version from you image version, your local repository information or by running `proxlb -v`.
Version: <-- DEFINE_VERSION -->
Installed from: <-- DEFINE_INSTALL_SOURCE -->
Running as: <-- Container, local on Proxmox, local on all Proxmox, dedicated -->

View File

@@ -0,0 +1,14 @@
---
name: Feature request
about: Create a new request for a missing feature
title: "`Feature`: "
labels: feature, needs-analysis
assignees: ''
---
## General
<-- Describe the feature idea from a high level perspective. -->
## Details
<-- Provide some more details about the new feature request and provide examples. -->

View File

@@ -1,26 +0,0 @@
name: Run basic pipeline on push
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8"]
steps:
- uses: actions/checkout@v3
- name: Set up Python for ProxLB
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies for ProxLB
run: |
python -m pip install --upgrade pip
pip install pytest proxmoxer flake8
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Run Python linting
run: |
python3 -m flake8 proxlb
- name: Create distro packages
run: |
cd packaging
./01_package.sh

21
.github/workflows/10-code-liniting.yml vendored Normal file
View File

@@ -0,0 +1,21 @@
name: Code linting
on: [push]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8"]
steps:
- uses: actions/checkout@v3
- name: Setup dependencies for code linting
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install additional dependencies for code linting
run: |
sudo apt-get update
sudo apt-get -y install python3-pycodestyle pycodestyle
- name: Run code linting on ProxLB Python code
run: |
pycodestyle proxlb/*

View File

@@ -0,0 +1,78 @@
name: "Build package: .deb"
on: [push]
jobs:
lint-code-proxlb:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8"]
steps:
- uses: actions/checkout@v3
- name: Setup dependencies for code linting
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install additional dependencies for code linting
run: |
sudo apt-get update
sudo apt-get -y install python3-pycodestyle pycodestyle
- name: Run code linting on ProxLB Python code
run: |
pycodestyle proxlb/* && \
echo "OK: Code linting successfully performed on ProxLB code."
build-package-debian:
needs: lint-code-proxlb
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v3
with:
ref: ${{ github.ref }}
- name: Set up Docker with Debian image
run: |
docker pull debian:latest
- name: Build DEB package in Docker container
run: |
docker run --rm -v $(pwd):/workspace -w /workspace debian:latest bash -c "
# Install dependencies
apt-get update && \
apt-get install -y python3 python3-setuptools debhelper dh-python python3-pip python3-stdeb python3-proxmoxer python3-requests python3-urllib3 devscripts python3-all && \
# Build package using stdeb / setuptools
# python3 setup.py --command-packages=stdeb.command bdist_deb && \
# Build native package
dpkg-buildpackage -us -uc && \
mkdir package && \
mv ../*.deb package/ && \
echo 'OK: Debian package successfully created.'
"
- name: Upload Debian package python3-proxlb as artifact
uses: actions/upload-artifact@v4
with:
name: debian-package
path: package/*.deb
integration-test-debian:
needs: build-package-debian
runs-on: ubuntu-latest
steps:
- name: Download Debian package artifact
uses: actions/download-artifact@v4
with:
name: debian-package
path: package/
- name: Set up Docker with Debian image
run: docker pull debian:latest
- 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.\")'
"

View File

@@ -0,0 +1,96 @@
name: "Build package: .rpm"
on: [push]
jobs:
lint-code-proxlb:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8"]
steps:
- uses: actions/checkout@v3
- name: Setup dependencies for code linting
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install additional dependencies for code linting
run: |
sudo apt-get update
sudo apt-get -y install python3-pycodestyle pycodestyle
- name: Run code linting on ProxLB Python code
run: |
pycodestyle proxlb/* && \
echo "OK: Code linting successfully performed on ProxLB code."
build-package-rpm:
needs: lint-code-proxlb
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v3
with:
ref: 'development'
- name: Set up Docker with Debian image
run: |
docker pull debian:latest
- name: Build DEB package in Docker container
run: |
docker run --rm -v $(pwd):/workspace -w /workspace debian:latest bash -c "
# Install dependencies
apt-get update && \
apt-get install -y python3 python3-setuptools rpm debhelper dh-python python3-pip python3-stdeb python3-proxmoxer python3-requests python3-urllib3 && \
# Build package
python3 setup.py --command-packages=stdeb.command bdist_rpm && \
echo 'OK: RPM package successfully created.'
"
- name: Upload RPM package python3-proxlb as artifact
uses: actions/upload-artifact@v4
with:
name: rpm-package
path: dist/*.rpm
# integration-test-rpm-rockylinux-9:
# needs: build-package-rpm
# runs-on: ubuntu-latest
# steps:
# - name: Download RPM package artifact
# uses: actions/download-artifact@v4
# with:
# name: rpm-package
# path: dist/
# - name: Set up Docker with RockyLinux 9 image
# run: docker pull rockylinux:9
# - name: Install and test RPM package in Rocky Linux Docker container
# run: |
# docker run --rm -v $(pwd)/dist:/dist -w /dist rockylinux:9 bash -c "
# # DNF does not handle wildcards well
# rpm_file=\$(ls proxlb*.noarch.rpm) && \
# dnf install -y \$rpm_file && \
# python3 -c 'import proxlb; print(\"OK: RPM package successfully installed.\")'
# "
# integration-test-rpm-rockylinux-8:
# needs: build-package-rpm
# runs-on: ubuntu-latest
# steps:
# - name: Download RPM package artifact
# uses: actions/download-artifact@v4
# with:
# name: rpm-package
# path: dist/
# - name: Set up Docker with RockyLinux 8 image
# run: docker pull rockylinux:8
# - name: Install and test RPM package in Rocky Linux Docker container
# run: |
# docker run --rm -v $(pwd)/dist:/dist -w /dist rockylinux:8 bash -c "
# # DNF does not handle wildcards well
# rpm_file=\$(ls proxlb*.noarch.rpm) && \
# dnf install -y \$rpm_file && \
# python3 -c 'import proxlb; print(\"OK: RPM package successfully installed.\")'
# "

9
.gitignore vendored
View File

@@ -1,2 +1,7 @@
packaging/changelog-fragments-creator/
dev/
__pycache__
*.pyc
.DS_Store
build/
dist/
*.egg-info/
proxlb_dev.yaml

View File

@@ -5,18 +5,98 @@ 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.1] - 2025-04-20
### Added
- Providing the API upstream error message when migration fails in debug mode (by @gyptazy) [#205]
### Changed
- Change the default behaviour of the daemon mode to active [#176]
- Change the default banalcing mode to used instead of assigned [#180]
### Fixed
- Set cpu_used to the cpu usage, which is a percent, times the total number of cores to get a number where guest cpu_used can be added to nodes cpu_used and be meaningful (by @glitchvern) [#195]
- Fix tag evluation for VMs for being ignored for further balancing [#163]
- Honor the value when balancing should not be performed and stop balancing [#174]
- allow the use of minutes instead of hours and only accept hours or minutes in the format (by @glitchvern) [#187]
- Remove hard coded memory usage from lowest usage node and use method and mode specified in configuration instead (by @glitchvern) [#197]
- Fix the guest type relationship in the logs when a migration job failed (by @gyptazy) [#204]
- Requery a guest if that running guest reports 0 cpu usage (by @glitchvern) [#200]
- Fix Python path for Docker entrypoint (by @crandler) [#170]
- Improve logging verbosity of messages that had a wrong servity [#165]
## [1.1.0] - 2025-04-01
### Fixed
- Refactored code base for ProxLB [#114]
- Switched to `pycodestyle` for linting [#114]
- Package building will be done within GitHub actions pipeline [#114]
- ProxLB now only returns a warning when no guests for further balancing are not present (instead of quitting) [132#]
- All nodes (according to the free resources) will be used now [#130]
- Fixed logging outputs where highest/lowest were mixed-up [#129]
- Stop balancing when movement would get worste (new force param to enfoce for affinity rules) [#128]
- Added requested documentation regarding Proxmox HA groups [#127]
- Rewrite of the whole affinity/anti-affinity rules evaluation and placement [#123]
- Fixed the `ignore` parameter for nodes where the node and guests on the node will be untouched [#102]
## [1.0.6] - 2024-12-24
### Fixed
- Fix maintenance mode when using cli arg and config mode by using the merged list (by @CartCaved). [#119]
- Fix that a scheduler time definition of 1 (int) gets wrongly interpreted as a bool (by @gyptazy). [#115]
## [1.0.5] - 2024-10-30
### Changed
- Change docs to make bool usage in configs more clear (by @gyptazy). [#104]
### Fixed
- Fix node (and its objects) evaluation when not reachable, e.g., maintenance (by @gyptazy). [#107]
- Fix migration from local disks (by @greenlogles). [#113]
- Fix evaluation of maintenance mode where comparing list & string resulted in a crash (by @glitchvern). [#106]
- Fix allowed values (add DEBUG, WARNING) for log verbosity (by @gyptazy). [#98]
## [1.0.4] - 2024-10-11
### Added
- Add maintenance mode to evacuate a node and move workloads for other nodes in the cluster. [#58]
- Add feature to make API timeout configureable. [#91]
- Add version output cli arg. [#89]
### Changed
- Run storage balancing only on supported shared storages. [#79]
- Run storage balancing only when needed to save time. [#79]
### Fixed
- Fix CPU balancing where calculations are done in float instead of int. (by @glitchvern) [#75]
- Fix documentation for the underlying infrastructure. [#81]
## [1.0.3] - 2024-09-12
### Added
- Add cli arg `-b` to return the next best node for next VM/CT placement. [#8]
- Add a convert function to cast all bool alike options from configparser to bools. [#53]
- Add a config parser options for future features. [#53]
- Add a config versio schema that must be supported by ProxLB. [#53]
- Add feature to allow the API hosts being provided as a comma separated list. [#60]
- Add storage balancing function. [#51]
- Add doc how to add dedicated user for authentication. (by @Dulux-Oz)
- Add cli arg `-b` to return the next best node for next VM/CT placement. [#8]
- Add storage balancing function. [#51]
### Changed
@@ -25,42 +105,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Fix documentation for the master_only parameter placed in the wrong config section. [#74]
- Fix anti-affinity rules not evaluating a new and different node. [#67]
- Fixed `master_only` function by inverting the condition.
- Improved the overall validation and error handling. [#64]
- Fix documentation for the master_only parameter placed in the wrong config section. [#74]
- Fix bug in the `proxlb.conf` in the vm_balancing section.
- Fix handling of unset `ignore_nodes` and `ignore_vms` resulted in an attribute error. [#71]
- Fix anti-affinity rules not evaluating a new and different node. [#67]
- Improved the overall validation and error handling. [#64]
## [1.0.2] - 2024-08-13
### Added
- Add option to run migration in parallel or sequentially. [#41]
- Add option to run ProxLB only on the Proxmox's master node in the cluster (reg. HA feature). [#40]
- Add option to run migrations in parallel or sequentially. [#41]
### Changed
- Fix daemon timer to use hours instead of minutes. [#45]
### Fixed
- Fix CMake packaging for Debian package to avoid overwriting the config file. [#49]
- Fix wonkey code style.
## [1.0.0] - 2024-08-01
### Added
- Add feature to prevent VMs from being relocated by defining a wildcard pattern. [#7]
- Add feature to make log verbosity configurable [#17].
- Add option_mode to rebalance by node's free resources in percent (instead of bytes). [#29]
- Add option to rebalance by assigned VM resources to avoid over provisioning. [#16]
- Add Docker/Podman support. [#10 by @daanbosch]
- Add exclude grouping feature to rebalance VMs from being located together to new nodes. [#4]
- Add feature to prevent VMs from being relocated by defining the 'plb_ignore_vm' tag. [#7]
- Add feature to prevent VMs from being relocated by defining a wildcard pattern. [#7]
- Add Docker/Podman support. [#10 by @daanbosch]
- Add option to rebalance by assigned VM resources to avoid overprovisioning. [#16]
- Add feature to make log verbosity configurable [#17].
- Add dry-run support to see what kind of rebalancing would be done. [#6]
- Add LXC/Container integration. [#27]
- Add exclude grouping feature to rebalance VMs from being located together to new nodes. [#4]
- Add include grouping feature to rebalance VMs bundled to new nodes. [#3]
- Add option_mode to rebalance by node's free resources in percent (instead of bytes). [#29]
### Changed

View File

@@ -29,7 +29,7 @@ Before submitting a pull request, ensure that your changes sucessfully perform t
1. **Install pytest if you haven't already:**
```sh
pip install fake8
pip install flake8
```
2. **Run the lintin:**
@@ -116,6 +116,6 @@ By participating in this project, you agree to abide by our [Code of Conduct](CO
## Getting Help
If you need help or have any questions, feel free to reach out by creating an issue or by joining our [discussion forum](https://github.com/gyptazy/proxlb/discussions). You can also refer to our [documentation](https://github.com/gyptazy/ProxLB/tree/main/docs) for more information about the project or join our [chat room](https://matrix.to/#/#proxlb:gyptazy.ch) in Matrix.
If you need help or have any questions, feel free to reach out by creating an issue or by joining our [discussion forum](https://github.com/gyptazy/proxlb/discussions). You can also refer to our [documentation](https://github.com/gyptazy/ProxLB/tree/main/docs) for more information about the project or join our [chat room](https://matrix.to/#/#proxlb:gyptazy.com) in Matrix.
Thank you for contributing to ProxLB! Together, we can enhance the efficiency and performance of Proxmox clusters.

View File

@@ -1,20 +1,16 @@
# Use the official Debian 12 base image
FROM debian:12
# Use the latest Alpine image
FROM alpine:latest
# Labels
LABEL maintainer="gyptazy@gyptazy.ch"
LABEL org.label-schema.schema-version="0.9"
LABEL org.label-schema.description="ProxLB - Rebalance VM workloads across nodes in a Proxmox cluster."
LABEL org.label-schema.url="https://github.com/gyptazy/ProxLB"
LABEL maintainer="gyptazy@gyptazy.com"
LABEL org.label-schema.name="ProxLB"
LABEL org.label-schema.description="ProxLB - An advanced load balancer for Proxmox clusters."
LABEL org.label-schema.vendor="gyptazy"
LABEL org.label-schema.url="https://proxlb.de"
LABEL org.label-schema.vcs-url="https://github.com/gyptazy/ProxLB"
# Set environment variables
ENV DEBIAN_FRONTEND=noninteractive
# Install python3 and python3-venv
RUN apt-get update && \
apt-get install -y python3 python3-pip python3-venv && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Install Python3
RUN apk add --no-cache python3 py3-pip
# Create a directory for the app
WORKDIR /app
@@ -22,14 +18,11 @@ WORKDIR /app
# Copy the python program from the current directory to /app
COPY proxlb /app/proxlb
# Create a virtual environment
RUN python3 -m venv venv
# Copy requirements to the container
COPY requirements.txt /app/requirements.txt
# Install dependencies in the virtual environment
RUN . venv/bin/activate && pip install -r /app/requirements.txt
RUN pip install --break-system-packages -r /app/requirements.txt
# Set the entry point to use the virtual environment's python
ENTRYPOINT ["/app/venv/bin/python3", "/app/proxlb"]
ENTRYPOINT ["/usr/bin/python3", "/app/proxlb/main.py"]

View File

@@ -671,4 +671,4 @@ into proprietary programs. If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License. But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.
<https://www.gnu.org/licenses/why-not-lgpl.html>.

674
README.md
View File

@@ -4,350 +4,118 @@
<p float="center"><img src="https://img.shields.io/github/license/gyptazy/ProxLB"/><img src="https://img.shields.io/github/contributors/gyptazy/ProxLB"/><img src="https://img.shields.io/github/last-commit/gyptazy/ProxLB/main"/><img src="https://img.shields.io/github/issues-raw/gyptazy/ProxLB"/><img src="https://img.shields.io/github/issues-pr/gyptazy/ProxLB"/></p>
## Table of Contents
- [ProxLB - (Re)Balance VM Workloads in Proxmox Clusters](#proxlb---rebalance-vm-workloads-in-proxmox-clusters)
- [Table of Contents](#table-of-contents)
- [Introduction](#introduction)
- [Video of Migration](#video-of-migration)
- [Features](#features)
- [How does it work?](#how-does-it-work)
- [Usage](#usage)
- [Dependencies](#dependencies)
- [Options](#options)
- [Notes](#notes)
- [Parameters](#parameters)
- [Balancing](#balancing)
- [General](#general)
- [By Used Memory of VMs/CTs](#by-used-memory-of-vmscts)
- [By Assigned Memory of VMs/CTs](#by-assigned-memory-of-vmscts)
- [Storage Balancing](#storage-balancing)
- [Affinity Rules / Grouping Relationships](#affinity-rules--grouping-relationships)
- [Affinity (Stay Together)](#affinity-stay-together)
- [Anti-Affinity (Keep Apart)](#anti-affinity-keep-apart)
- [Ignore VMs (Tag Style)](#ignore-vms-tag-style)
- [Systemd](#systemd)
- [Manual](#manual)
- [Proxmox GUI Integration](#proxmox-gui-integration)
- [Quick Start](#quick-start)
- [Container Quick Start (Docker/Podman)](#container-quick-start-dockerpodman)
- [Logging](#logging)
- [Motivation](#motivation)
- [References](#references)
- [Downloads](#downloads)
- [Packages](#packages)
- [Repository](#repository)
- [Stable Releases](#stable-releases)
- [Beta/Testing Releases](#betatesting-releases)
- [Container Images (Docker/Podman)](#container-images-dockerpodman)
- [Misc](#misc)
- [Bugs](#bugs)
- [Contributing](#contributing)
- [Documentation](#documentation)
- [Support](#support)
- [Author(s)](#authors)
1. [Introduction](#introduction)
2. [Features](#features)
3. [How does it work?](#how-does-it-work)
4. [Installation](#installation)
1. [Requirements / Dependencies](#requirements--dependencies)
2. [Debian Package](#debian-package)
4. [Container / Docker](#container--docker)
5. [Source](#source)
5. [Usage / Configuration](#usage--configuration)
1. [GUI Integration](#gui-integration)
2. [Proxmox HA Integration](#proxmox-ha-integration)
3. [Options](#options)
6. [Affinity & Anti-Affinity Rules](#affinity--anti-affinity-rules)
1. [Affinity Rules](#affinity-rules)
2. [Anti-Affinity Rules](#anti-affinity-rules)
3. [Ignore VMs](#ignore-vms)
4. [Pin VMs to Hypervisor Nodes](#pin-vms-to-hypervisor-nodes)
7. [Maintenance](#maintenance)
8. [Misc](#misc)
1. [Bugs](#bugs)
2. [Contributing](#contributing)
3. [Documentation](#documentation)
4. [Support](#support)
9. [Author(s)](#authors)
## Introduction
`ProxLB` (PLB) is an advanced tool designed to enhance the efficiency and performance of Proxmox clusters by optimizing the distribution of virtual machines (VMs) or Containers (CTs) across the cluster nodes by using the Proxmox API. ProxLB meticulously gathers and analyzes a comprehensive set of resource metrics from both the cluster nodes and the running VMs. These metrics include CPU usage, memory consumption, and disk utilization, specifically focusing on local disk resources.
ProxLB is an advanced load balancing solution specifically designed for Proxmox clusters, addressing the absence of a Dynamic Resource Scheduler (DRS) that is familiar to VMware users. As a third-party solution, ProxLB enhances the management and efficiency of Proxmox clusters by intelligently distributing workloads across available nodes. Workloads can be balanced by different times like the guest's memory, CPU or disk usage or their assignment to avoid overprovisioning and ensuring resources.
PLB collects resource usage data from each node in the Proxmox cluster, including CPU, (local) disk and memory utilization. Additionally, it gathers resource usage statistics from all running VMs, ensuring a granular understanding of the cluster's workload distribution.
One of the key advantages of ProxLB is that it is fully open-source and free, making it accessible for anyone to use, modify, and contribute to. This ensures transparency and fosters community-driven improvements. ProxLB supports filtering and ignoring specific nodes and guests through configuration files and API calls, providing administrators with the flexibility to tailor the load balancing behavior to their specific needs.
Intelligent rebalancing is a key feature of ProxLB where it re-balances VMs based on their memory, disk or CPU usage, ensuring that no node is overburdened while others remain underutilized. The rebalancing capabilities of PLB significantly enhance cluster performance and reliability. By ensuring that resources are evenly distributed, PLB helps prevent any single node from becoming a performance bottleneck, improving the reliability and stability of the cluster. Efficient rebalancing leads to better utilization of available resources, potentially reducing the need for additional hardware investments and lowering operational costs.
A standout feature of ProxLB is its maintenance mode. When enabled, all guest workloads are automatically moved to other nodes within the cluster, ensuring that a node can be safely updated, rebooted, or undergo hardware maintenance without disrupting the overall cluster operation. Additionally, ProxLB supports both affinity and anti-affinity rules, allowing operators to group multiple guests to run together on the same node or ensure that certain guests do not run on the same node, depending on the cluster's node count. This feature is crucial for optimizing performance and maintaining high availability.
Automated rebalancing reduces the need for manual actions, allowing operators to focus on other critical tasks, thereby increasing operational efficiency.
ProxLB can also return the best next node for guest placement, which can be integrated into CI/CD pipelines using tools like Ansible or Terraform. This capability streamlines the deployment process and ensures efficient resource utilization. Furthermore, ProxLB leverages the Proxmox API, including the entire ACL (Access Control List) system, for secure and efficient operation. Unlike some solutions, it does not require SSH access, enhancing security and simplifying configuration.
Overall, ProxLB significantly enhances resource management by intelligently distributing workloads, reducing downtime through its maintenance mode, and providing improved flexibility with affinity and anti-affinity rules. Its seamless integration with CI/CD tools and reliance on the Proxmox API make it a robust and secure solution for optimizing Proxmox cluster performance.
### Video of Migration
<img src="https://cdn.gyptazy.com/images/proxlb-rebalancing-demo.gif"/>
## Features
* Rebalance VMs/CTs in the cluster by:
* Memory
* Disk (only local storage)
* CPU
* Rebalance Storage in the cluster
* Rebalance VMs/CTs disks to other storage pools
* Rebalance by used storage
* Get best Node for new VM/CT placement in cluster
* Performing
* Periodically
* One-shot solution
* Types
* Rebalance only VMs
* Rebalance only CTs
* Rebalance all (VMs and CTs)
* Rebalance VM/CT disks (Storage)
* Filter
* Exclude nodes
* Exclude virtual machines
* Grouping
* Include groups (VMs that are rebalanced to nodes together)
* Exclude groups (VMs that must run on different nodes)
* Ignore groups (VMs that should be untouched)
* Dry-run support
* Human readable output in CLI
* JSON output for further parsing
* Migrate VM workloads away (e.g. maintenance preparation)
ProxLB's key features are by enabling automatic rebalancing of VMs and CTs across a Proxmox cluster based on memory, CPU, and local disk usage while identifying optimal nodes for automation. It supports maintenance mode, affinity rules, and seamless Proxmox API integration with ACL support, offering flexible usage as a one-time operation, a daemon, or through the Proxmox Web GUI. In addition, ProxLB also supports additional enterprise alike features like power managements for nodes (often also known as DPM) where nodes can be turned on/off on demand when workloads are higher/lower than usual. Also the automated security-patching of nodes within the cluster (known as ASPM) may help to reduce the manual work from cluster admins, where nodes will install patches, move guests across the cluster, reboot and then reblance the cluster again.
**Features**
* Re-Balancing (DRS)
* Supporting VMs & CTs
* Balancing by:
* CPU
* Memory
* Disk
* Affinity / Anti-Affinity Rules
* Affinity: Groups guests together
* Anti-Affinity: Ensuring guests run on different nodes
* Best node evaluation
* Get the best node for guest placement (e.g., CI/CD)
* Maintenance Mode
* Evacuating a sinlge or multiple nodes
* Node Power Management (DPM)
* Auto Node Security-Patch-Management (ASPM)
* Fully based on Proxmox API
* Usage
* One-Shot (one-shot)
* Periodically (daemon)
* Proxmox Web GUI Integration (optional)
* Utilizing the Proxmox User Authentications
* Supporting API tokens
* No SSH or Agents required
* Can run everywhere
## How does it work?
ProxLB is a load-balancing system designed to optimize the distribution of virtual machines (VMs) and containers (CTs) across a cluster. It works by first gathering resource usage metrics from all nodes in the cluster through the Proxmox API. This includes detailed resource metrics for each VM and CT on every node. ProxLB then evaluates the difference between the maximum and minimum resource usage of the nodes, referred to as "Balanciness." If this difference exceeds a predefined threshold (which is configurable), the system initiates the rebalancing process.
Before starting any migrations, ProxLB validates that rebalancing actions are necessary and beneficial. Depending on the selected balancing mode — such as CPU, memory, or disk — it creates a balancing matrix. This matrix sorts the VMs by their maximum used or assigned resources, identifying the VM with the highest usage. ProxLB then places this VM on the node with the most free resources in the selected balancing type. This process runs recursively until the operator-defined Balanciness is achieved. Balancing can be defined for the used or max. assigned resources of VMs/CTs.
## Usage
Running PLB is easy and it runs almost everywhere since it just depends on `Python3` and the `proxmoxer` library. Therefore, it can directly run on a Proxmox node, dedicated systems like Debian, RedHat, or even FreeBSD, as long as the API is reachable by the client running PLB.
## Installation
### Dependencies
* Python3
* proxmoxer (Python module)
### Requirements / Dependencies
* Python3.x
* proxmoxer
* requests
* urllib3
* pyyaml
### Options
The following options can be set in the `proxlb.conf` file:
| Section | Option | Example | Description |
|------|:------:|:------:|:------:|
| `proxmox` | api_host | hypervisor01.gyptazy.com | Host or IP address (or comma separated list) of the remote Proxmox API. |
| | api_user | root@pam | Username for the API. |
| | api_pass | FooBar | Password for the API. |
| | verify_ssl | 1 | Validate SSL certificates (1) or ignore (0). (default: 1) |
| `vm_balancing` | enable | 1 | Enables VM/CT balancing. |
| | method | memory | Defines the balancing method (default: memory) where you can use `memory`, `disk` or `cpu`. |
| | mode | used | Rebalance by `used` resources (efficiency) or `assigned` (avoid overprovisioning) resources. (default: used)|
| | mode_option | byte | Rebalance by node's resources in `bytes` or `percent`. (default: bytes) |
| | type | vm | Rebalance only `vm` (virtual machines), `ct` (containers) or `all` (virtual machines & containers). (default: vm)|
| | balanciness | 10 | Value of the percentage of lowest and highest resource consumption on nodes may differ before rebalancing. (default: 10) |
| | parallel_migrations | 1 | Defines if migrations should be done parallely or sequentially. (default: 1) |
| | maintenance_nodes | dummynode03,dummynode04 | Defines a comma separated list of nodes to set them into maintenance mode and move VMs/CTs to other nodes. |
| | ignore_nodes | dummynode01,dummynode02,test* | Defines a comma separated list of nodes to exclude. |
| | ignore_vms | testvm01,testvm02 | Defines a comma separated list of VMs to exclude. (`*` as suffix wildcard or tags are also supported) |
| `storage_balancing` | enable | 0 | Enables storage balancing. |
| | balanciness | 10 | Value of the percentage of lowest and highest storage consumption may differ before rebalancing. (default: 10) |
| | parallel_migrations | 1 | Defines if migrations should be done parallely or sequentially. (default: 1) |
| `update_service` | enable | 0 | Enables the automated update service (rolling updates). |
| `api` | enable | 0 | Enables the ProxLB API. |
| `service`| daemon | 1 | Run as a daemon (1) or one-shot (0). (default: 1) |
| | schedule | 24 | Hours to rebalance in hours. (default: 24) |
| | master_only | 0 | Defines is this should only be performed (1) on the cluster master node or not (0). (default: 0) |
| | log_verbosity | INFO | Defines the log level (default: CRITICAL) where you can use `INFO`, `WARN` or `CRITICAL` |
| | config_version | 3 | Defines the current config version schema for ProxLB |
An example of the configuration file looks like:
The dependencies can simply be installed with `pip` by running the following command:
```
[proxmox]
api_host: hypervisor01.gyptazy.com
api_user: root@pam
api_pass: FooBar
verify_ssl: 1
[vm_balancing]
enable: 1
method: memory
mode: used
type: vm
# Balanciness defines how much difference may be
# between the lowest & highest resource consumption
# of nodes before rebalancing will be done.
# Examples:
# Rebalancing: node01: 41% memory consumption :: node02: 52% consumption
# No rebalancing: node01: 43% memory consumption :: node02: 50% consumption
balanciness: 10
# Enable parallel migrations. If set to 0 it will wait for completed migrations
# before starting next migration.
parallel_migrations: 1
maintenance_nodes: dummynode03,dummynode04
ignore_nodes: dummynode01,dummynode02
ignore_vms: testvm01,testvm02
[storage_balancing]
enable: 0
[update_service]
enable: 0
[api]
enable: 0
[service]
# The master_only option might be useful if running ProxLB on all nodes in a cluster
# but only a single one should do the balancing. The master node is obtained from the Proxmox
# HA status.
master_only: 0
daemon: 1
config_version: 3
pip install -r requirements.txt
```
#### Notes
* If running ProxLB on more than one Proxmox node you can set `api_host` to a comma-separated list of each node's IP address or hostname. (Example: `api_host: node01.gyptazy.com,node02.gyptazy.com,node03.gyptazy.com`)
* The `verify_ssl` parameter can switch between the mode to verify trusted remote certificates. Keep in mind, that even local ones are **not** trusted by default and need to be imported to the truststore.
* Even when using only the `vm_balancing` mode, ensure to have the other sections listed in your config:
```
[storage_balancing]
enable: 0
[update_service]
enable: 0
[api]
enable: 0
```
*Note: Distribution packages, such like the provided `.deb` package will automatically resolve and install all required dependencies by using already packaged version from the distribution's repository. By using the Docker (container) image or Debian packages, you do not need to take any care of the requirements listed here.*
### Parameters
The following options and parameters are currently supported:
### Debian Package
ProxLB is a powerful and flexible load balancer designed to work across various architectures, including `amd64`, `arm64`, `rv64` and many other ones that support Python. It runs independently of the underlying hardware, making it a versatile choice for different environments. This chapter covers the step-by-step process to install ProxLB on Debian-based systems, including Debian clones like Ubuntu.
| Option | Long Option | Description | Default |
|------|:------:|------:|------:|
| -c | --config | Path to a config file. | /etc/proxlb/proxlb.conf (default) |
| -d | --dry-run | Performs a dry-run without doing any actions. | Unset |
| -j | --json | Returns a JSON of the VM movement. | Unset |
| -b | --best-node | Returns the best next node for a VM/CT placement (useful for further usage with Terraform/Ansible). | Unset |
| -m | --maintenance | Sets node(s) to maintenance mode & moves workloads away. | Unset |
| -v | --version | Returns the ProxLB version on stdout. | Unset |
### Balancing
#### General
In general, virtual machines (VMs), containers (CTs) can be rebalanced and moved around nodes or shared storage (storage balancing) in the cluster. Often, this also works without downtime without any further downtimes. However, this does **not** work with containers. LXC based containers will be shutdown, copied and started on the new node. Also to note, live migrations can work fluently without any issues but there are still several things to be considered. This is out of scope for ProxLB and applies in general to Proxmox and your cluster setup. You can find more details about this here: https://pve.proxmox.com/wiki/Migrate_to_Proxmox_VE.
#### By Used Memory of VMs/CTs
By continuously monitoring the current resource usage of VMs, ProxLB intelligently reallocates workloads to prevent any single node from becoming overloaded. This approach ensures that resources are balanced efficiently, providing consistent and optimal performance across the entire cluster at all times. To activate this balancing mode, simply activate the following option in your ProxLB configuration:
```
mode: used
```
Afterwards, restart the service (if running in daemon mode) to activate this rebalancing mode.
#### By Assigned Memory of VMs/CTs
By ensuring that resources are always available for each VM, ProxLB prevents over-provisioning and maintains a balanced load across all nodes. This guarantees that users have consistent access to the resources they need. However, if the total assigned resources exceed the combined capacity of the cluster, ProxLB will issue a warning, indicating potential over-provisioning despite its best efforts to balance the load. To activate this balancing mode, simply activate the following option in your ProxLB configuration:
```
mode: assigned
```
Afterwards, restart the service (if running in daemon mode) to activate this rebalancing mode.
#### Storage Balancing
Starting with ProxLB 1.0.3, ProxLB also supports the balancing of underlying shared storage. In this case, all attached disks (`rootfs` in a context of a CT) of a VM or CT are being fetched and evaluated. If a VM has multiple disks attached, the disks can also be distributed over different storages. As a result, only shared storage is supported. Non shared storage would require to move the whole VM including all attached disks to the parent's node local storage.
Limitations:
* Only shared storage
* Only supported for the following VM disk types:
* ide (only disks, not CD)
* nvme
* scsi
* virtio
* sata
* rootfs (Container)
*Note: Storage balancing is currently in beta and should be used carefully.*
### Affinity Rules / Grouping Relationships
#### Affinity (Stay Together)
<img align="left" src="https://cdn.gyptazy.com/images/plb-rebalancing-include-balance-group.jpg"/> Access the Proxmox Web UI by opening your web browser and navigating to your Proxmox VE web interface, then log in with your credentials. Navigate to the VM you want to tag by selecting it from the left-hand navigation panel. Click on the "Options" tab to view the VM's options, then select "Edit" or "Add" (depending on whether you are editing an existing tag or adding a new one). In the tag field, enter plb_include_ followed by your unique identifier, for example, plb_include_group1. Save the changes to apply the tag to the VM. Repeat these steps for each VM that should be included in the group.
#### Anti-Affinity (Keep Apart)
<img align="left" src="https://cdn.gyptazy.com/images/plb-rebalancing-exclude-balance-group.jpg"/> Access the Proxmox Web UI by opening your web browser and navigating to your Proxmox VE web interface, then log in with your credentials. Navigate to the VM you want to tag by selecting it from the left-hand navigation panel. Click on the "Options" tab to view the VM's options, then select "Edit" or "Add" (depending on whether you are editing an existing tag or adding a new one). In the tag field, enter plb_exclude_ followed by your unique identifier, for example, plb_exclude_critical. Save the changes to apply the tag to the VM. Repeat these steps for each VM that should be excluded from being on the same node.
#### Ignore VMs (Tag Style)
<img align="left" src="https://cdn.gyptazy.com/images/plb-rebalancing-ignore-vm.jpg"/> In Proxmox, you can ensure that certain VMs are ignored during the rebalancing process by setting a specific tag within the Proxmox Web UI, rather than solely relying on configurations in the ProxLB config file. This can be achieved by adding the tag 'plb_ignore_vm' to the VM. Once this tag is applied, the VM will be excluded from any further rebalancing operations, simplifying the management process.
### Systemd
When installing a Linux distribution (such as .deb or .rpm) file, this will be shipped with a systemd unit file. The default configuration file will be sourced from `/etc/proxlb/proxlb.conf`.
| Unit Name | Options |
|------|:------:|
| proxlb | start, stop, status, restart |
### Manual
A manual installation is possible and also supports BSD based systems. Proxmox Rebalancing Service relies on mainly two important files:
* proxlb (Python Executable)
* proxlb.conf (Config file)
The executable must be able to read the config file, if no dedicated config file is given by the `-c` argument, PLB tries to read it from `/etc/proxlb/proxlb.conf`.
### Proxmox GUI Integration
<img align="left" src="https://cdn.gyptazy.com/images/proxlb-GUI-integration.jpg"/> PLB can also be directly be used from the Proxmox Web UI by installing the optional package `pve-proxmoxlb-service-ui` package which has a dependency on the `proxlb` package. For the Web UI integration, it requires to be installed (in addition) on the nodes on the cluster. Afterwards, a new menu item is present in the HA chapter called `Rebalancing`. This chapter provides two possibilities:
* Rebalancing VM workloads
* Migrate VM workloads away from a defined node (e.g. maintenance preparation)
### Quick Start
The easiest way to get started is by using the ready-to-use packages that I provide on my CDN and to run it on a Linux Debian based system. This can also be one of the Proxmox nodes itself.
```
wget https://cdn.gyptazy.com/files/os/debian/proxlb/proxlb_1.0.3_amd64.deb
dpkg -i proxlb_1.0.3_amd64.deb
# Adjust your config
vi /etc/proxlb/proxlb.conf
systemctl restart proxlb
systemctl status proxlb
```
### Container Quick Start (Docker/Podman)
Creating a container image of ProxLB is straightforward using the provided Dockerfile. The Dockerfile simplifies the process by automating the setup and configuration required to get ProxLB running in a container. Simply follow the steps in the Dockerfile to build the image, ensuring all dependencies and configurations are correctly applied. For those looking for an even quicker setup, a ready-to-use ProxLB container image is also available, eliminating the need for manual building and allowing for immediate deployment.
#### Quick-Start
You can simply use this snippet to install the repository and to install ProxLB on your system.
```bash
git clone https://github.com/gyptazy/ProxLB.git
cd ProxLB
docker build -t proxlb .
echo "deb https://repo.gyptazy.com/stable /" > /etc/apt/sources.list.d/proxlb.list
wget -O /etc/apt/trusted.gpg.d/proxlb.asc https://repo.gyptazy.com/repository.gpg
apt-get update && apt-get -y install proxlb
cp /etc/proxlb/proxlb_example.yaml /etc/proxlb/proxlb.yaml
# Adjust the config to your needs
vi /etc/proxlb/proxlb.yaml
systemctl start proxlb
```
Afterwards simply adjust the config file to your needs:
```
vi /etc/proxlb/proxlb.conf
```
Afterwards, ProxLB is running in the background and balances your cluster by your defined balancing method (default: memory).
Finally, start the created container.
```bash
docker run -it --rm -v $(pwd)/proxlb.conf:/etc/proxlb/proxlb.conf proxlb
```
#### Details
ProxLB provides two different repositories:
* https://repo.gyptazy.com/stable (only stable release)
* https://repo.gyptazy.com/testing (bleeding edge - not recommended)
### Logging
ProxLB uses the `SystemdHandler` for logging. You can find all your logs in your systemd unit log or in the `journalctl`. In default, ProxLB only logs critical events. However, for further understanding of the balancing it might be useful to change this to `INFO` or `DEBUG` which can simply be done in the [proxlb.conf](https://github.com/gyptazy/ProxLB/blob/main/proxlb.conf#L14) file by changing the `log_verbosity` parameter.
Available logging values:
| Verbosity | Description |
|------|:------:|
| DEBUG | This option logs everything and is needed for debugging the code. |
| INFO | This option provides insides behind the scenes. What/why has been something done and with which values. |
| WARNING | This option provides only warning messages, which might be a problem in general but not for the application itself. |
| CRITICAL | This option logs all critical events that will avoid running ProxLB. |
## Motivation
As a developer managing a cluster of virtual machines for my projects, I often encountered the challenge of resource imbalance. Nodes within the cluster would become unevenly loaded, with some nodes being overburdened while others remained underutilized. This imbalance led to inefficiencies, performance bottlenecks, and increased operational costs. Frustrated by the lack of an adequate solution to address this issue, I decided to develop the ProxLB (PLB) to ensure better resource distribution across my clusters.
My primary motivation for creating PLB stemmed from my work on my BoxyBSD project, where I consistently faced the difficulty of maintaining balanced nodes while running various VM workloads but also on my personal clusters. The absence of an efficient rebalancing mechanism made it challenging to achieve optimal performance and stability. Recognizing the necessity for a tool that could gather and analyze resource metrics from both the cluster nodes and the running VMs, I embarked on developing ProxLB.
PLB meticulously collects detailed resource usage data from each node in a Proxmox cluster, including CPU load, memory usage, and local disk space utilization. It also gathers comprehensive statistics from all running VMs, providing a granular understanding of the workload distribution. With this data, PLB intelligently redistributes VMs based on memory usage, local disk usage, and CPU usage. This ensures that no single node is overburdened, storage resources are evenly distributed, and the computational load is balanced, enhancing overall cluster performance.
As an advocate of the open-source philosophy, I believe in the power of community and collaboration. By sharing solutions like PLB, I aim to contribute to the collective knowledge and tools available to developers facing similar challenges. Open source fosters innovation, transparency, and mutual support, enabling developers to build on each other's work and create better solutions together.
Developing PLB was driven by a desire to solve a real problem I faced in my projects. However, the spirit behind this effort was to provide a valuable resource to the community. By open-sourcing PLB, I hope to help other developers manage their clusters more efficiently, optimize their resource usage, and reduce operational costs. Sharing this solution aligns with the core principles of open source, where the goal is not only to solve individual problems but also to contribute to the broader ecosystem.
## References
Here you can find some overviews of references for and about the ProxLB (PLB):
| Description | Link |
|------|:------:|
| General introduction into ProxLB | https://gyptazy.com/blog/proxlb-rebalancing-vm-workloads-across-nodes-in-proxmox-clusters/ |
| Howto install and use ProxLB on Debian to rebalance vm workloads in a Proxmox cluster | https://gyptazy.com/howtos/howto-install-and-use-proxlb-to-rebalance-vm-workloads-across-nodes-in-proxmox-clusters/ |
## Downloads
ProxLB can be obtained in man different ways, depending on which use case you prefer. You can use simply copy the code from GitHub, use created packages for Debian or RedHat based systems, use a Repository to keep ProxLB always up to date or simply use a Container image for Docker/Podman.
### Packages
Ready to use packages can be found at:
* https://cdn.gyptazy.com/files/os/debian/proxlb/
* https://cdn.gyptazy.com/files/os/ubuntu/proxlb/
* https://cdn.gyptazy.com/files/os/redhat/proxlb/
### Repository
Debian based systems can also use the repository by adding the following line to their apt sources:
#### Stable Releases
```
deb https://repo.gyptazy.com/stable /
```
#### Beta/Testing Releases
```
deb https://repo.gyptazy.com/testing /
```
The Repository's GPG key can be found at: `https://repo.gyptazy.com/repository.gpg`
The repository is signed and the GPG key can be found at:
* https://repo.gyptazy.com/repository.gpg
You can also simply import it by running:
@@ -361,16 +129,285 @@ wget -O /etc/apt/trusted.gpg.d/proxlb.asc https://repo.gyptazy.com/repository.gp
*Note: The defined repositories `repo.gyptazy.com` and `repo.proxlb.de` are the same!*
### Container Images (Docker/Podman)
Container Images for Podman, Docker etc., can be found at:
#### 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/files/os/debian/proxlb/
Afterwards, you can simply install the package by running:
```bash
dpkg -i proxlb_*.deb
cp /etc/proxlb/proxlb_example.yaml /etc/proxlb/proxlb.yaml
# Adjust the config to your needs
vi /etc/proxlb/proxlb.yaml
systemctl start proxlb
```
### Container Images / Docker
Using the ProxLB container images is straight forward and only requires you to mount the config file.
```bash
# Pull the image
docker pull cr.gyptazy.com/proxlb/proxlb:latest
# Download the config
wget -O proxlb.yaml https://raw.githubusercontent.com/gyptazy/ProxLB/refs/heads/main/config/proxlb_example.yaml
# Adjust the config to your needs
vi proxlb.yaml
# Start the ProxLB container image with the ProxLB config
docker run -it --rm -v $(pwd)/proxlb.yaml:/etc/proxlb/proxlb.yaml proxlb
```
*Note: ProxLB container images are officially only available at cr.proxlb.de and cr.gyptazy.com.*
#### Overview of Images
| Version | Image |
|------|:------:|
| latest | cr.gyptazy.com/proxlb/proxlb:latest |
| v1.1.1 | cr.gyptazy.com/proxlb/proxlb:v1.1.1 |
| v1.1.0 | cr.gyptazy.com/proxlb/proxlb:v1.1.0 |
| v1.0.6 | cr.gyptazy.com/proxlb/proxlb:v1.0.6 |
| v1.0.5 | cr.gyptazy.com/proxlb/proxlb:v1.0.5 |
| v1.0.4 | cr.gyptazy.com/proxlb/proxlb:v1.0.4 |
| v1.0.3 | cr.gyptazy.com/proxlb/proxlb:v1.0.3 |
| v1.0.2 | cr.gyptazy.com/proxlb/proxlb:v1.0.2 |
| v1.0.0 | cr.gyptazy.com/proxlb/proxlb:v1.0.0 |
| v0.9.9 | cr.gyptazy.com/proxlb/proxlb:v0.9.9 |
### Source
ProxLB can also easily be used from the provided sources - for traditional systems but also as a Docker/Podman container image.
#### Traditional System
Setting up and running ProxLB from the sources is simple and requires just a few commands. Ensure Python 3 and the Python dependencies are installed on your system, then run ProxLB using the following command:
```bash
git clone https://github.com/gyptazy/ProxLB.git
cd ProxLB
```
Afterwards simply adjust the config file to your needs:
```bash
vi config/proxlb.yaml
```
Start ProxLB by Python3 on the system:
```bash
python3 proxlb/main.py -c config/proxlb.yaml
```
#### Container Image
Creating a container image of ProxLB is straightforward using the provided Dockerfile. The Dockerfile simplifies the process by automating the setup and configuration required to get ProxLB running in an Alpine container. Simply follow the steps in the Dockerfile to build the image, ensuring all dependencies and configurations are correctly applied. For those looking for an even quicker setup, a ready-to-use ProxLB container image is also available, eliminating the need for manual building and allowing for immediate deployment.
```bash
git clone https://github.com/gyptazy/ProxLB.git
cd ProxLB
docker build -t proxlb .
```
Afterwards simply adjust the config file to your needs:
```bash
vi config/proxlb.yaml
```
Finally, start the created container.
```bash
docker run -it --rm -v $(pwd)/proxlb.yaml:/etc/proxlb/proxlb.yaml proxlb
```
## Usage / Configuration
Running ProxLB is straightforward and versatile, as it only requires `Python3` and the `proxmoxer` library. This means ProxLB can be executed directly on a Proxmox node or on dedicated systems such as Debian, RedHat, or even FreeBSD, provided that the Proxmox API is accessible from the client running ProxLB. ProxLB can also run inside a Container - Docker or LXC - and is simply up to you.
### GUI Integration
<img align="left" src="https://cdn.gyptazy.com/images/proxlb-GUI-integration.jpg"/> ProxLB can also be accessed through the Proxmox Web UI by installing the optional `pve-proxmoxlb-service-ui` package, which depends on the proxlb package. For full Web UI integration, this package must be installed on all nodes within the cluster. Once installed, a new menu item - `Rebalancing`, appears in the cluster level under the HA section. Once installed, it offers two key functionalities:
* Rebalancing VM workloads
* Migrate VM workloads away from a defined node (e.g. maintenance preparation)
**Note:** This package is currently discontinued and will be readded at a later time. See also: [#44: How to install pve-proxmoxlb-service-ui package](https://github.com/gyptazy/ProxLB/issues/44).
### Proxmox HA Integration
Proxmox HA (High Availability) groups are designed to ensure that virtual machines (VMs) remain running within a Proxmox cluster. HA groups define specific rules for where VMs should be started or migrated in case of node failures, ensuring minimal downtime and automatic recovery.
However, when used in conjunction with ProxLB, the built-in load balancer for Proxmox, conflicts can arise. ProxLB operates with its own logic for workload distribution, taking into account affinity and anti-affinity rules. While it effectively balances guest workloads, it may re-shift and redistribute VMs in a way that does not align with HA group constraints, potentially leading to unsuitable placements.
Due to these conflicts, it is currently not recommended to use both HA groups and ProxLB simultaneously. The interaction between the two mechanisms can lead to unexpected behavior, where VMs might not adhere to HA group rules after being moved by ProxLB.
A solution to improve compatibility between HA groups and ProxLB is under evaluation, aiming to ensure that both features can work together without disrupting VM placement strategies.
See also: [#65: Host groups: Honour HA groups](https://github.com/gyptazy/ProxLB/issues/65).
### Options
The following options can be set in the configuration file `proxlb.yaml`:
| Section | Option | Sub Option | Example | Type | Description |
|---------|:------:|:----------:|:-------:|:----:|:-----------:|
| `proxmox_api` | | | | | |
| | hosts | | ['virt01.example.com', '10.10.10.10', 'fe01::bad:code::cafe'] | `List` | List of Proxmox nodes. Can be IPv4, IPv6 or mixed. |
| | user | | root@pam | `Str` | Username for the API. |
| | pass | | FooBar | `Str` | Password for the API. (Recommended: Use API token authorization!) |
| | token_id | | proxlb | `Str` | Token ID of the user for the API. |
| | token_secret | | 430e308f-1337-1337-beef-1337beefcafe | `Str` | Secret of the token ID for the API. |
| | ssl_verification | | True | `Bool` | Validate SSL certificates (1) or ignore (0). [values: `1` (default), `0`] |
| | timeout | | 10 | `Int` | Timeout for the Proxmox API in sec. |
| | retries | | 1 | `Int` | How often a connection attempt to the defined API host should be performed. |
| | wait_time | | 1 | `Int` | How many seconds should be waited before performing another connection attempt to the API host. |
| `proxmox_cluster` | | | | | |
| | maintenance_nodes | | ['virt66.example.com'] | `List` | A list of Proxmox nodes that are defined to be in a maintenance. |
| | ignore_nodes | | [] | `List` | A list of Proxmox nodes that are defined to be ignored. |
| | overprovisioning | | False | `Bool` | Avoids balancing when nodes would become overprovisioned. |
| `balancing` | | | | | |
| | enable | | True | `Bool` | Enables the guest balancing.|
| | enforce_affinity | | True | `Bool` | Enforcing affinity/anti-affinity rules but balancing might become worse. |
| | parallel | | False | `Bool` | If guests should be moved in parallel or sequentially.|
| | live | | True | `Bool` | If guests should be moved live or shutdown.|
| | with_local_disks | | True | `Bool` | If balancing of guests should include local disks.|
| | balance_types | | ['vm', 'ct'] | `List` | Defined the types of guests that should be honored. [values: `vm`, `ct`]|
| | max_job_validation | | 1800 | `Int` | How long a job validation may take in seconds. (default: 1800) |
| | balanciness | | 10 | `Int` | The maximum delta of resource usage between node with highest and lowest usage. |
| | 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`] |
| `dpm` | | | | | |
| | enable | | True | `Bool` | Enables the Dynamic Power Management functions.|
| | method | | memory | `Str` | The balancing method that should be used. [values: `memory` (default), `cpu`, `disk`]|
| | mode | | static | `Str` | The balancing mode that should be used. [values: `static` (default), `auto`] |
| | cluster_min_free_resources | | 60 | `Int` | Representing the minimum required free resouzrces in percent within the cluster. [values: `60`% (default)] |
| | cluster_min_nodes | | 3 | `Int` | The minimum of required nodes that should remain in a cluster. [values: `3` (default)] |
| `service` | | | | | |
| | daemon | | True | `Bool` | If daemon mode should be activated. |
| | `schedule` | | | `Dict` | Schedule config block for rebalancing. |
| | | interval | 12 | `Int` | How often rebalancing should occur in daemon mode.|
| | | format | hours | `Str` | Sets the time format. [values: `hours` (default), `minutes`]|
| | log_level | | INFO | `Str` | Defines the default log level that should be logged. [values: `INFO` (default), `WARNING`, `CRITICAL`, `DEBUG`] |
An example of the configuration file looks like:
```
proxmox_api:
hosts: ['virt01.example.com', '10.10.10.10', 'fe01::bad:code::cafe']
user: root@pam
pass: crazyPassw0rd!
# API Token method
# token_id: proxlb
# token_secret: 430e308f-1337-1337-beef-1337beefcafe
ssl_verification: True
timeout: 10
# API Connection retries
# retries: 1
# wait_time: 1
proxmox_cluster:
maintenance_nodes: ['virt66.example.com']
ignore_nodes: []
overprovisioning: True
balancing:
enable: True
enforce_affinity: False
parallel: False
live: True
with_local_disks: True
balance_types: ['vm', 'ct']
max_job_validation: 1800
balanciness: 5
method: memory
mode: used
dpm:
# DPM requires you to define the WOL (Wake-on-Lan)
# MAC address for each node in Proxmox.
enable: True
method: memory
mode: static
cluster_min_free_resources: 60
cluster_min_nodes: 1
service:
daemon: True
schedule:
interval: 12
format: hours
log_level: INFO
```
### Parameters
The following options and parameters are currently supported:
| Option | Long Option | Description | Default |
|------|:------:|------:|------:|
| -c | --config | Path to a config file. | /etc/proxlb/proxlb.yaml (default) |
| -d | --dry-run | Performs a dry-run without doing any actions. | False |
| -j | --json | Returns a JSON of the VM movement. | False |
| -b | --best-node | Returns the best next node for a VM/CT placement (useful for further usage with Terraform/Ansible). | False |
| -v | --version | Returns the ProxLB version on stdout. | False |
## Affinity & Anti-Affinity Rules
ProxLB provides an advanced mechanism to define affinity and anti-affinity rules, enabling precise control over virtual machine (VM) placement. These rules help manage resource distribution, improve high availability configurations, and optimize performance within a Proxmox Virtual Environment (PVE) cluster. By leveraging Proxmoxs integrated access management, ProxLB ensures that users can only define and manage rules for guests they have permission to access.
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.
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/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.
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:
#### Example for Screenshot
```
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).
**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
<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.
To define a guest to be ignored from the balancing, users assign a tag with the prefix `plb_ignore_$TAG`:
#### Example for Screenshot
```
plb_ignore_dev
```
As a result, ProxLB will not migrate this guest with the `plb_ignore_dev` tag to any other node.
**Note:** Ignored guests are really ignored. Even by enforcing affinity rules this guest will be ignored.
### Pin VMs to Specific Hypervisor Nodes
<img align="left" src="https://cdn.gyptazy.com/images/proxlb-tag-node-pinning.jpg"/> Guests, such as VMs or CTs, can also be pinned to specific 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.
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
```
plb_pin_node03
```
As a result, ProxLB will pin the guest `dev-vm01` to the node `virt03`.
**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.
## Maintenance
<img src="https://cdn.gyptazy.com/images/proxlb-rebalancing-demo.gif"/>
The `maintenance_nodes` option allows operators to designate one or more Proxmox nodes for maintenance mode. When a node is set to maintenance, no new guest workloads will be assigned to it, and all existing workloads will be migrated to other available nodes within the cluster. This process ensures that (anti)-affinity rules and resource availability are respected, preventing disruptions while maintaining optimal performance across the infrastructure.
### Adding / Removing Nodes from Maintenance
Within the section `proxmox_cluster` you can define the key `maintenance_nodes` as a list object. Simply add/remove one or more nodes with their equal name in the cluster and restart the daemon.
```
proxmox_cluster:
maintenance_nodes: ['virt66.example.com']
```
Afterwards, all guest objects will be moved to other nodes in the cluster by ensuring the best balancing.
## Misc
### Bugs
Bugs can be reported via the GitHub issue tracker [here](https://github.com/gyptazy/ProxLB/issues). You may also report bugs via email or deliver PRs to fix them on your own. Therefore, you might also see the contributing chapter.
@@ -382,13 +419,18 @@ Feel free to add further documentation, to adjust already existing one or to con
You can also find additional and more detailed documentation within the [docs/](https://github.com/gyptazy/ProxLB/tree/main/docs) directory.
### Support
If you need assistance or have any questions, we offer support through our dedicated [chat room](https://matrix.to/#/#proxlb:gyptazy.com) in Matrix and on Reddit. Join our community for real-time help, advice, and discussions. Connect with us in our dedicated chat room for immediate support and live interaction with other users and developers. You can also visit our [GitHub Community](https://github.com/gyptazy/ProxLB/discussions/) to post your queries, share your experiences, and get support from fellow community members and moderators. You may also just open directly an issue [here](https://github.com/gyptazy/ProxLB/issues) on GitHub. We are here to help and ensure you have the best experience possible.
If you need assistance or have any questions, we offer support through our dedicated [chat room](https://matrix.to/#/#proxlb:gyptazy.com) in Matrix or [Discord](https://discord.gg/JemGu7WbfQ). Join our community for real-time help, advice, and discussions. The Matrix and Discord room are bridged to ensure that the communication is not splitted - so simply feel free to join which fits most to you!
Connect with us in our dedicated chat room for immediate support and live interaction with other users and developers. You can also visit our [GitHub Community](https://github.com/gyptazy/ProxLB/discussions/) to post your queries, share your experiences, and get support from fellow community members and moderators. You may also just open directly an issue [here](https://github.com/gyptazy/ProxLB/issues) on GitHub.
| Support Channel | Link |
|------|:------:|
| Matrix | [#proxlb:gyptazy.com](https://matrix.to/#/#proxlb:gyptazy.com) |
| Discord | [Discord](https://discord.gg/JemGu7WbfQ) |
| GitHub Community | [GitHub Community](https://github.com/gyptazy/ProxLB/discussions/)
| GitHub | [ProxLB GitHub](https://github.com/gyptazy/ProxLB/issues) |
**Note:** Please always keep in mind that this is a one-man show project without any further help. This includes coding, testing, packaging and all the infrastructure around it to keep this project up and running.
### Author(s)
* Florian Paul Azim Hoberg @gyptazy (https://gyptazy.com)

View File

@@ -0,0 +1,43 @@
proxmox_api:
hosts: ['virt01.example.com', '10.10.10.10', 'fe01::bad:code::cafe']
user: root@pam
pass: crazyPassw0rd!
# API Token method
# token_id: proxlb
# token_secret: 430e308f-1337-1337-beef-1337beefcafe
ssl_verification: True
timeout: 10
# API Connection retries
# retries: 1
# wait_time: 1
proxmox_cluster:
maintenance_nodes: ['virt66.example.com']
ignore_nodes: []
overprovisioning: True
balancing:
enable: True
enforce_affinity: False
parallel: False
live: True
with_local_disks: True
balance_types: ['vm', 'ct']
max_job_validation: 1800
balanciness: 5
method: memory
mode: used
dpm:
enable: True
method: memory
mode: static
cluster_min_free_resources: 60
cluster_min_nodes: 1
service:
daemon: True
schedule:
interval: 12
format: hours
log_level: INFO

27
debian/changelog vendored Normal file
View File

@@ -0,0 +1,27 @@
proxlb (1.1.2~b1) stable; urgency=medium
* Auto-created 1.1.2 beta 1 release.
-- Florian Paul Azim Hoberg <gyptazy@gyptazy.com> Mon, 17 Mar 2025 18:55:02 +0000
proxlb (1.1.1) stable; urgency=medium
* Fix tag evluation for VMs for being ignored for further balancing. (Closes: #163)
* Improve logging verbosity of messages that had a wrong servity. (Closes: #165)
* Providing the API upstream error message when migration fails in debug mode (Closes: #205)
* Change the default behaviour of the daemon mode to active. (Closes: #176)
* Change the default banalcing mode to used instead of assigned. (Closes: #180)
* Set cpu_used to the cpu usage, which is a percent, times the total number of cores to get a number where guest cpu_used can be added to nodes cpu_used and be meaningful. (Closes: #195)
* Honor the value when balancing should not be performed and stop balancing. (Closes: #174)
* Allow the use of minutes instead of hours and only accept hours or minutes in the format. (Closes: #187)
* Remove hard coded memory usage from lowest usage node and use method and mode specified in configuration instead. (Closes: #197)
* Fix the guest type relationship in the logs when a migration job failed. (Closes: #204)
* Requery a guest if that running guest reports 0 cpu usage. (Closes: #200)
-- Florian Paul Azim Hoberg <gyptazy@gyptazy.com> Sat, 20 Apr 2025 20:55:02 +0000
proxlb (1.1.0) stable; urgency=medium
* Refactored code base of ProxLB. (Closes: #114)
-- Florian Paul Azim Hoberg <gyptazy@gyptazy.com> Mon, 17 Mar 2025 18:55:02 +0000

12
debian/control vendored Normal file
View File

@@ -0,0 +1,12 @@
Source: proxlb
Maintainer: Florian Paul Azim Hoberg <gyptazy@gyptazy.com>
Section: admin
Priority: optional
Standards-Version: 4.5.0
Build-Depends: debhelper-compat (= 13), dh-python, python3-all, python3-setuptools
Package: proxlb
Architecture: all
Depends: ${python3:Depends}, ${misc:Depends}, python3-requests, python3-urllib3, python3-proxmoxer, python3-yaml
Description: A DRS alike Load Balancer for Proxmox Clusters
An advanced DRS alike loadbalancer for Proxmox clusters that also supports maintenance modes and affinity/anti-affinity rules.

2
debian/install vendored Normal file
View File

@@ -0,0 +1,2 @@
proxlb /usr/lib/python3/dist-packages/
service/proxlb.service /lib/systemd/system/

16
debian/postinst vendored Executable file
View File

@@ -0,0 +1,16 @@
#!/bin/bash
set -e
#DEBHELPER#
if [ "$1" = "configure" ]; then
systemctl enable proxlb.service
systemctl restart proxlb.service || true
# Create the 'plb' user if it does not exist
if ! id "plb" &>/dev/null; then
useradd --system --home /var/lib/proxlb --create-home --shell /usr/sbin/nologin --group nogroup plb
echo "User 'plb' created."
else
echo "User 'plb' already exists, skipping creation."
fi
fi

16
debian/prerm vendored Executable file
View File

@@ -0,0 +1,16 @@
#!/bin/bash
set -e
#DEBHELPER#
if [ "$1" = "remove" ]; then
systemctl stop proxlb.service || true
systemctl disable proxlb.service || true
# Remove the 'plb' user if it exists
if id "plb" &>/dev/null; then
userdel --remove plb
echo "User 'plb' removed."
else
echo "User 'plb' does not exist, skipping removal."
fi
fi

4
debian/rules vendored Normal file
View File

@@ -0,0 +1,4 @@
#!/usr/bin/make -f
%:
dh $@ --with python3 --buildsystem=pybuild

1
debian/source/format vendored Normal file
View File

@@ -0,0 +1 @@
3.0 (native)

View File

@@ -1,32 +0,0 @@
# Installation
## Packages
The easiest way to get started is by using the ready-to-use packages that I provide on my CDN and to run it on a Linux Debian based system. This can also be one of the Proxmox nodes itself.
```
wget https://cdn.gyptazy.ch/files/amd64/debian/proxlb/proxlb_0.9.9_amd64.deb
dpkg -i proxlb_0.9.9_amd64.deb
# Adjust your config
vi /etc/proxlb/proxlb.conf
systemctl restart proxlb
systemctl status proxlb
```
## Container (Docker/Podman)
Creating a container image of ProxLB is straightforward using the provided Dockerfile. The Dockerfile simplifies the process by automating the setup and configuration required to get ProxLB running in a container. Simply follow the steps in the Dockerfile to build the image, ensuring all dependencies and configurations are correctly applied. For those looking for an even quicker setup, a ready-to-use ProxLB container image is also available, eliminating the need for manual building and allowing for immediate deployment.
```bash
git clone https://github.com/gyptazy/ProxLB.git
cd ProxLB
build -t proxlb .
```
Afterwards simply adjust the config file to your needs:
```
vi /etc/proxlb/proxlb.conf
```
Finally, start the created container.
```bash
docker run -it --rm -v $(pwd)/proxlb.conf:/etc/proxlb/proxlb.conf proxlb
```

65
docs/01_requirements.md Normal file
View File

@@ -0,0 +1,65 @@
# Table of Contents
- [Requirements](#requirements)
- [Where To Run?](#where-to-run)
## Requirements
ProxLB is a sophisticated load balancer designed to enhance the management and distribution of workloads within a Proxmox cluster. By fully utilizing the Proxmox API, ProxLB eliminates the need for additional SSH access, streamlining cluster management while maintaining robust security. This chapter outlines the general requirements necessary to deploy and operate ProxLB effectively.
### Proxmox Cluster Requirements
To use ProxLB, you must have an existing Proxmox cluster consisting of at least two nodes. While traditional load balancers often struggle to manage minimal node configurations, ProxLB is optimized to provide efficient load distribution even in a two-node environment. The more nodes present in the cluster, the better ProxLB can optimize resource usage and manage workloads.
### ProxLB Package Requirements
Next to the previously mentioned requirements, ProxLB also requires you to fit the following ones:
* Python3.x
* proxmoxer
* requests
* urllib3
* pyyaml
### Seamless API Integration
ProxLB relies exclusively on the Proxmox API for all management tasks. This eliminates the need for direct SSH access, ensuring a cleaner and more secure interaction with the cluster. The API integration allows ProxLB to:
- Monitor cluster health and node resource utilization
- Migrate virtual machines (VMs) and containers as needed
- Manage storage utilization and distribution
- Implement load balancing policies
### Authentication and Security Standards
ProxLB fully supports Proxmoxs integrated user management system, providing robust authentication and access control. Key features include:
- **Multi-Factor Authentication (MFA):** Enhances security by requiring multiple verification methods.
- **API Key Support:** ProxLB can utilize API keys for authentication instead of traditional username/password combinations, minimizing exposure to credentials.
- **Role-Based Access Control (RBAC):** Ensures administrators have fine-grained control over user permissions.
### Flexible Storage Support
ProxLB offers versatile storage management options, supporting both local and shared storage types. It efficiently balances storage workloads across the cluster using the following storage systems:
- **Local Storage:** Direct-attached storage on each node.
- **Shared Storage:** Includes options like iSCSI, NVMeOF, and NFS for centralized storage solutions.
- **Ceph:** Integrated support for Ceph distributed storage, providing high availability and fault tolerance.
### Network Infrastructure Requirements
For optimal performance, ProxLB requires a reliable and high-speed network connection between the nodes in the cluster. Ensure that the network infrastructure meets the following criteria:
- **Low Latency:** Essential for real-time load balancing and VM migration.
- **Sufficient Bandwidth:** Adequate to handle storage access, data replication, and migration traffic.
- **Redundant Network Paths:** Recommended for increased fault tolerance and uptime.
### System Resource Allocation
ProxLB itself requires minimal system resources to operate. However, for managing larger clusters or high workloads, ensure the node running ProxLB has adequate resources available:
- **CPU:** A modern multi-core processor.
- **Memory:** At least 2 GB of RAM.
- **Storage:** Minimal disk space for configuration files and logs.
## 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).
Therefore, you can simply run ProxLB on:
* Bare-metal Systems
* VMs (even inside the Proxmox cluster)
* Docker/Podman Container
* LXC Container
* On a Proxmox node

View File

@@ -1,48 +0,0 @@
# Configuration
## Balancing
### By Used Memmory of VMs
By continuously monitoring the current resource usage of VMs, ProxLB intelligently reallocates workloads to prevent any single node from becoming overloaded. This approach ensures that resources are balanced efficiently, providing consistent and optimal performance across the entire cluster at all times. To activate this balancing mode, simply activate the following option in your ProxLB configuration:
```
mode: used
```
Afterwards, restart the service (if running in daemon mode) to activate this rebalancing mode.
### By Assigned Memory of VMs
By ensuring that resources are always available for each VM, ProxLB prevents over-provisioning and maintains a balanced load across all nodes. This guarantees that users have consistent access to the resources they need. However, if the total assigned resources exceed the combined capacity of the cluster, ProxLB will issue a warning, indicating potential over-provisioning despite its best efforts to balance the load. To activate this balancing mode, simply activate the following option in your ProxLB configuration:
```
mode: assigned
```
Afterwards, restart the service (if running in daemon mode) to activate this rebalancing mode.
## Grouping
### Include (Stay Together)
<img align="left" src="https://cdn.gyptazy.ch/images/plb-rebalancing-include-balance-group.jpg"/> Access the Proxmox Web UI by opening your web browser and navigating to your Proxmox VE web interface, then log in with your credentials. Navigate to the VM you want to tag by selecting it from the left-hand navigation panel. Click on the "Options" tab to view the VM's options, then select "Edit" or "Add" (depending on whether you are editing an existing tag or adding a new one). In the tag field, enter plb_include_ followed by your unique identifier, for example, plb_include_group1. Save the changes to apply the tag to the VM. Repeat these steps for each VM that should be included in the group.
### Exclude (Stay Separate)
<img align="left" src="https://cdn.gyptazy.ch/images/plb-rebalancing-exclude-balance-group.jpg"/> Access the Proxmox Web UI by opening your web browser and navigating to your Proxmox VE web interface, then log in with your credentials. Navigate to the VM you want to tag by selecting it from the left-hand navigation panel. Click on the "Options" tab to view the VM's options, then select "Edit" or "Add" (depending on whether you are editing an existing tag or adding a new one). In the tag field, enter plb_exclude_ followed by your unique identifier, for example, plb_exclude_critical. Save the changes to apply the tag to the VM. Repeat these steps for each VM that should be excluded from being on the same node.
### Ignore VMs (tag style)
<img align="left" src="https://cdn.gyptazy.ch/images/plb-rebalancing-ignore-vm.jpg"/> In Proxmox, you can ensure that certain VMs are ignored during the rebalancing process by setting a specific tag within the Proxmox Web UI, rather than solely relying on configurations in the ProxLB config file. This can be achieved by adding the tag 'plb_ignore_vm' to the VM. Once this tag is applied, the VM will be excluded from any further rebalancing operations, simplifying the management process.
## Authentication / User Account / User / Permissions
### Authentication
ProxLB also supports different accounts in ProxLB. Therefore, you can simply create a new user and group and add the required roles permissions.
### Creating Dedicated User for Balanciung
It is recommended to not use the `root@pam` user for balancing. Therefore, creating a new user might be suitable and is very easy to create.
A new user can be created by the gui, api and cli. The required roles are stated in the next chapter, but you can also use the following lines
to create a user on the cli with the required roles to balance VMs & CTs.
```
pveum role add ProxLBAdmin --privs Datastore.Audit,Sys.Audit,VM.Audit,VM.Migrate
pveum user add proxlb_admin@pve --password <password>
pveum acl modify / --roles ProxLBAdmin --users proxlb_admin@pve
```
### Required Roles
When using ProxLB with a dedicated account, you might also keep the assigned roles low. Therefore, you need to ensure that the newly created user is at least assigned to the following roles:
* Datastore.Audit (Required for storage evaluation)
* Sys.Audit (Required to get resource metrics of the nodes)
* VM.Audit (Requited to get resource metrics of VMs/CTs)
* VM.Migrate (Required for migration of VMs/CTs)

164
docs/02_installation.md Normal file
View File

@@ -0,0 +1,164 @@
# Table of Contents
- [Installation](#installation)
- [Requirements / Dependencies](#requirements--dependencies)
- [Debian Package](#debian-package)
- [Quick-Start](#quick-start)
- [Details](#details)
- [Debian Packages (.deb files)](#debian-packages-deb-files)
- [RedHat Package](#redhat-package)
- [Container Images / Docker](#container-images--docker)
- [Overview of Images](#overview-of-images)
- [Source](#source)
- [Traditional System](#traditional-system)
- [Container Image](#container-image)
- [Upgrading](#upgrading)
- [Upgrading from < 1.1.0](#upgrading-from--110)
- [Upgrading from >= 1.1.0](#upgrading-from--110)
## Installation
### Requirements / Dependencies
* Python3.x
* proxmoxer
* requests
* urllib3
* pyyaml
The dependencies can simply be installed with `pip` by running the following command:
```
pip install -r requirements.txt
```
*Note: Distribution packages, such like the provided `.deb` package will automatically resolve and install all required dependencies by using already packaged version from the distribution's repository. By using the Docker (container) image or Debian packages, you do not need to take any care of the requirements listed here.*
### Debian Package
ProxLB is a powerful and flexible load balancer designed to work across various architectures, including `amd64`, `arm64`, `rv64` and many other ones that support Python. It runs independently of the underlying hardware, making it a versatile choice for different environments. This chapter covers the step-by-step process to install ProxLB on Debian-based systems, including Debian clones like Ubuntu.
#### Quick-Start
You can simply use this snippet to install the repository and to install ProxLB on your system.
```bash
echo "deb https://repo.gyptazy.com/stable /" > /etc/apt/sources.list.d/proxlb.list
wget -O /etc/apt/trusted.gpg.d/proxlb.asc https://repo.gyptazy.com/repository.gpg
apt-get update && apt-get -y install proxlb
cp /etc/proxlb/proxlb_example.yaml /etc/proxlb/proxlb.yaml
# Adjust the config to your needs
vi /etc/proxlb/proxlb.yaml
systemctl start proxlb
```
Afterwards, ProxLB is running in the background and balances your cluster by your defined balancing method (default: memory).
#### Details
ProxLB provides two different repositories:
* https://repo.gyptazy.com/stable (only stable release)
* https://repo.gyptazy.com/testing (bleeding edge - not recommended)
The repository is signed and the GPG key can be found at:
* https://repo.gyptazy.com/repository.gpg
You can also simply import it by running:
```
# KeyID: 17169F23F9F71A14AD49EDADDB51D3EB01824F4C
# UID: gyptazy Solutions Repository <contact@gyptazy.com>
# SHA256: 52c267e6f4ec799d40cdbdb29fa518533ac7942dab557fa4c217a76f90d6b0f3 repository.gpg
wget -O /etc/apt/trusted.gpg.d/proxlb.asc https://repo.gyptazy.com/repository.gpg
```
*Note: The defined repositories `repo.gyptazy.com` and `repo.proxlb.de` are the same!*
#### 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/files/os/debian/proxlb/
Afterwards, you can simply install the package by running:
```bash
dpkg -i proxlb_*.deb
cp /etc/proxlb/proxlb_example.yaml /etc/proxlb/proxlb.yaml
# Adjust the config to your needs
vi /etc/proxlb/proxlb.yaml
systemctl start proxlb
```
### RedHat Package
There's currently no official support for RedHat based systems. However, there's a dummy .rpm package for such systems in the pipeline which can be found here:
* https://github.com/gyptazy/ProxLB/actions/workflows/20-pipeline-build-rpm-package.yml
### Container Images / Docker
Using the ProxLB container images is straight forward and only requires you to mount the config file.
```bash
# Pull the image
docker pull cr.gyptazy.com/proxlb/proxlb:latest
# Download the config
wget -O proxlb.yaml https://raw.githubusercontent.com/gyptazy/ProxLB/refs/heads/main/config/proxlb_example.yaml
# Adjust the config to your needs
vi proxlb.yaml
# Start the ProxLB container image with the ProxLB config
docker run -it --rm -v $(pwd)/proxlb.yaml:/etc/proxlb/proxlb.yaml proxlb
```
*Note: ProxLB container images are officially only available at cr.proxlb.de and cr.gyptazy.com.*
#### Overview of Images
| Version | Image |
|------|:------:|
| latest | cr.gyptazy.com/proxlb/proxlb:latest |
| v1.1.0 | cr.gyptazy.com/proxlb/proxlb:v1.1.0 |
| v1.0.6 | cr.gyptazy.com/proxlb/proxlb:v1.0.6 |
| v1.0.5 | cr.gyptazy.com/proxlb/proxlb:v1.0.5 |
| v1.0.4 | cr.gyptazy.com/proxlb/proxlb:v1.0.4 |
| v1.0.3 | cr.gyptazy.com/proxlb/proxlb:v1.0.3 |
| v1.0.2 | cr.gyptazy.com/proxlb/proxlb:v1.0.2 |
| v1.0.0 | cr.gyptazy.com/proxlb/proxlb:v1.0.0 |
| v0.9.9 | cr.gyptazy.com/proxlb/proxlb:v0.9.9 |
### Source
ProxLB can also easily be used from the provided sources - for traditional systems but also as a Docker/Podman container image.
#### Traditional System
Setting up and running ProxLB from the sources is simple and requires just a few commands. Ensure Python 3 and the Python dependencies are installed on your system, then run ProxLB using the following command:
```bash
git clone https://github.com/gyptazy/ProxLB.git
cd ProxLB
```
Afterwards simply adjust the config file to your needs:
```bash
vi config/proxlb.yaml
```
Start ProxLB by Python3 on the system:
```bash
python3 proxlb/main.py -c config/proxlb.yaml
```
#### Container Image
Creating a container image of ProxLB is straightforward using the provided Dockerfile. The Dockerfile simplifies the process by automating the setup and configuration required to get ProxLB running in an Alpine container. Simply follow the steps in the Dockerfile to build the image, ensuring all dependencies and configurations are correctly applied. For those looking for an even quicker setup, a ready-to-use ProxLB container image is also available, eliminating the need for manual building and allowing for immediate deployment.
```bash
git clone https://github.com/gyptazy/ProxLB.git
cd ProxLB
docker build -t proxlb .
```
Afterwards simply adjust the config file to your needs:
```bash
vi config/proxlb.yaml
```
Finally, start the created container.
```bash
docker run -it --rm -v $(pwd)/proxlb.yaml:/etc/proxlb/proxlb.yaml proxlb
```
## Upgrading
### Upgrading from < 1.1.0
Upgrading ProxLB is not supported due to a fundamental redesign introduced in version 1.1.x. With this update, ProxLB transitioned from a monolithic application to a pure Python-style project, embracing a more modular and flexible architecture. This shift aimed to improve maintainability and extensibility while keeping up with modern development practices. Additionally, ProxLB moved away from traditional ini-style configuration files and adopted YAML for configuration management. This change simplifies configuration handling, reduces the need for extensive validation, and ensures better type casting, ultimately providing a more streamlined and user-friendly experience.
### Upgrading from >= 1.1.0
Uprading within the current stable versions, starting from 1.1.0, will be possible in all supported ways.

View File

@@ -1,87 +0,0 @@
## FAQ
### Could not import all dependencies
ProxLB requires the Python library `proxmoxer`. This can simply be installed by the most
system repositories. If you encounter this error message you simply need to install it.
```
# systemctl status proxlb
x proxlb.service - Proxmox Rebalancing Service
Loaded: loaded (/etc/systemd/system/proxlb.service; static)
Active: failed (Result: exit-code) since Sat 2024-07-06 10:25:16 UTC; 1s ago
Duration: 239ms
Process: 7285 ExecStart=/usr/bin/proxlb -c /etc/proxlb/proxlb.conf (code=exited, status=2)
Main PID: 7285 (code=exited, status=2)
CPU: 129ms
Jul 06 10:25:16 build01 systemd[1]: Started proxlb.service - ProxLB.
Jul 06 10:25:16 build01 proxlb[7285]: proxlb: Error: [python-imports]: Could not import all dependencies. Please install "proxmoxer".
```
Debian/Ubuntu: apt-get install python3-proxmoxer
If the package is not provided by your systems repository, you can also install it by running `pip3 install proxmoxer`.
### How does it work?
ProxLB is a load-balancing system designed to optimize the distribution of virtual machines (VMs) and containers (CTs) across a cluster. It works by first gathering resource usage metrics from all nodes in the cluster through the Proxmox API. This includes detailed resource metrics for each VM and CT on every node. ProxLB then evaluates the difference between the maximum and minimum resource usage of the nodes, referred to as "Balanciness." If this difference exceeds a predefined threshold (which is configurable), the system initiates the rebalancing process.
Before starting any migrations, ProxLB validates that rebalancing actions are necessary and beneficial. Depending on the selected balancing mode — such as CPU, memory, or disk — it creates a balancing matrix. This matrix sorts the VMs by their maximum used or assigned resources, identifying the VM with the highest usage. ProxLB then places this VM on the node with the most free resources in the selected balancing type. This process runs recursively until the operator-defined Balanciness is achieved. Balancing can be defined for the used or max. assigned resources of VMs/CTs.
### ProxLB config version is too low
ProxLB may run into an error when the used config schema version is too low. This might happen after major changes that require new config options. Please make sure, to use a supported config version in addition to your running ProxLB config.
Example Error:
```
Error: [config-version-validator]: ProxLB config version 2 is too low. Required: 3.
```
The easiest way to solve this, is by taking the minimum required config schema version from a git tag, representing the ProxLB version.
### Logging
ProxLB uses the `SystemdHandler` for logging. You can find all your logs in your systemd unit log or in the `journalctl`. In default, ProxLB only logs critical events. However, for further understanding of the balancing it might be useful to change this to `INFO` or `DEBUG` which can simply be done in the [proxlb.conf](https://github.com/gyptazy/ProxLB/blob/main/proxlb.conf#L14) file by changing the `log_verbosity` parameter.
Available logging values:
| Verbosity | Description |
|------|:------:|
| DEBUG | This option logs everything and is needed for debugging the code. |
| INFO | This option provides insides behind the scenes. What/why has been something done and with which values. |
| WARNING | This option provides only warning messages, which might be a problem in general but not for the application itself. |
| CRITICAL | This option logs all critical events that will avoid running ProxLB. |
### Motivation
As a developer managing a cluster of virtual machines for my projects, I often encountered the challenge of resource imbalance. Nodes within the cluster would become unevenly loaded, with some nodes being overburdened while others remained underutilized. This imbalance led to inefficiencies, performance bottlenecks, and increased operational costs. Frustrated by the lack of an adequate solution to address this issue, I decided to develop the ProxLB (PLB) to ensure better resource distribution across my clusters.
My primary motivation for creating PLB stemmed from my work on my BoxyBSD project, where I consistently faced the difficulty of maintaining balanced nodes while running various VM workloads but also on my personal clusters. The absence of an efficient rebalancing mechanism made it challenging to achieve optimal performance and stability. Recognizing the necessity for a tool that could gather and analyze resource metrics from both the cluster nodes and the running VMs, I embarked on developing ProxLB.
PLB meticulously collects detailed resource usage data from each node in a Proxmox cluster, including CPU load, memory usage, and local disk space utilization. It also gathers comprehensive statistics from all running VMs, providing a granular understanding of the workload distribution. With this data, PLB intelligently redistributes VMs based on memory usage, local disk usage, and CPU usage. This ensures that no single node is overburdened, storage resources are evenly distributed, and the computational load is balanced, enhancing overall cluster performance.
As an advocate of the open-source philosophy, I believe in the power of community and collaboration. By sharing solutions like PLB, I aim to contribute to the collective knowledge and tools available to developers facing similar challenges. Open source fosters innovation, transparency, and mutual support, enabling developers to build on each other's work and create better solutions together.
Developing PLB was driven by a desire to solve a real problem I faced in my projects. However, the spirit behind this effort was to provide a valuable resource to the community. By open-sourcing PLB, I hope to help other developers manage their clusters more efficiently, optimize their resource usage, and reduce operational costs. Sharing this solution aligns with the core principles of open source, where the goal is not only to solve individual problems but also to contribute to the broader ecosystem.
### Packages / Container Images
Ready to use packages can be found at:
* https://cdn.gyptazy.ch/files/amd64/debian/proxlb/
* https://cdn.gyptazy.ch/files/amd64/ubuntu/proxlb/
* https://cdn.gyptazy.ch/files/amd64/redhat/proxlb/
* https://cdn.gyptazy.ch/files/amd64/freebsd/proxlb/
Container Images for Podman, Docker etc., can be found at:
| Version | Image |
|------|:------:|
| latest | cr.gyptazy.ch/proxlb/proxlb:latest |
### Bugs
Bugs can be reported via the GitHub issue tracker [here](https://github.com/gyptazy/ProxLB/issues). You may also report bugs via email or deliver PRs to fix them on your own. Therefore, you might also see the contributing chapter.
### Contributing
Feel free to add further documentation, to adjust already existing one or to contribute with code. Please take care about the style guide and naming conventions. You can find more in our [CONTRIBUTING.md](https://github.com/gyptazy/ProxLB/blob/main/CONTRIBUTING.md) file.
### Support
If you need assistance or have any questions, we offer support through our dedicated [chat room](https://matrix.to/#/#proxlb:gyptazy.ch) in Matrix and on Reddit. Join our community for real-time help, advice, and discussions. Connect with us in our dedicated chat room for immediate support and live interaction with other users and developers. You can also visit our [Reddit community](https://www.reddit.com/r/Proxmox/comments/1e78ap3/introducing_proxlb_rebalance_your_vm_workloads/) to post your queries, share your experiences, and get support from fellow community members and moderators. You may also just open directly an issue [here](https://github.com/gyptazy/ProxLB/issues) on GitHub. We are here to help and ensure you have the best experience possible.
| Support Channel | Link |
|------|:------:|
| Matrix | [#proxlb:gyptazy.ch](https://matrix.to/#/#proxlb:gyptazy.ch) |
| Reddit | [Reddit community](https://www.reddit.com/r/Proxmox/comments/1e78ap3/introducing_proxlb_rebalance_your_vm_workloads/) |
| GitHub | [ProxLB GitHub](https://github.com/gyptazy/ProxLB/issues) |

241
docs/03_configuration.md Normal file
View File

@@ -0,0 +1,241 @@
# Table of Contents
1. [Authentication / User Accounts / Permissions](#authentication--user-accounts--permissions)
1. [Authentication](#authentication)
2. [Creating a Dedicated User](#creating-a-dedicated-user)
3. [Creating an API Token for a User](#creating-an-api-token-for-a-user)
4. [Required Permissions for a User](#required-permissions-for-a-user)
2. [Configuration](#configuration)
1. [Affinity & Anti-Affinity Rules](#affinity--anti-affinity-rules)
1. [Affinity Rules](#affinity-rules)
2. [Anti-Affinity Rules](#anti-affinity-rules)
3. [Affinity / Anti-Affinity Enforcing](#affinity--anti-affinity-enforcing)
4. [Ignore VMs](#ignore-vms)
5. [Pin VMs to Hypervisor Nodes](#pin-vms-to-hypervisor-nodes)
2. [API Loadbalancing](#api-loadbalancing)
3. [Ignore Host-Nodes or Guests](#ignore-host-nodes-or-guests)
4. [IPv6 Support](#ipv6-support)
5. [Logging / Log-Level](#logging--log-level)
6. [Parallel Migrations](#parallel-migrations)
7. [Run as a Systemd-Service](#run-as-a-systemd-service)
8. [SSL Self-Signed Certificates](#ssl-self-signed-certificates)
9. [Dynamic Power Management (DPM)](#dynamic-power-management)
## Authentication / User Accounts / Permissions
### Authentication
ProxLB supports the traditional username and password authentication method, which is familiar to many users. This method requires users to provide their credentials (username and password) to gain access to the Proxmox system. While this method is straightforward and easy to implement, it has several security limitations. Username and password combinations can be vulnerable to brute force attacks, where an attacker systematically attempts various combinations until the correct one is found. If a user's credentials are compromised through phishing, malware, or other means, the attacker can gain unauthorized access to the system. Additionally, traditional authentication does not provide granular control over permissions and access levels, potentially exposing sensitive operations to unauthorized users.
To enhance security, ProxLB supports API token authentication. API tokens are unique identifiers that are used to authenticate API requests. They offer several advantages over traditional username and password authentication. API tokens are more secure as they are typically long, random strings that are difficult to guess. They can be revoked and regenerated as needed, reducing the risk of unauthorized access. API tokens can be associated with specific user accounts that have only the required permissions, ensuring that users only have access to the resources and operations they need. Furthermore, API tokens can be used for automated scripts and applications, facilitating seamless integration with other systems and services.
When Multi-Factor Authentication (MFA) or Two-Factor Authentication (2FA) is enabled in the Proxmox cluster, the system enforces the use of API tokens for authentication. This is because traditional username and password authentication is not considered secure enough in conjunction with MFA/2FA. To ensure the highest level of security when using API tokens, follow these best practices: Use dedicated user accounts for API tokens, each with only the necessary permissions. This limits the potential impact of a compromised token. Ensure that API tokens are long, random, and unique. Avoid using easily guessable patterns or sequences. Periodically regenerate and replace API tokens to minimize the risk of long-term exposure. Store API tokens securely, using environment variables or secure vaults. Avoid hardcoding tokens in source code or configuration files. Regularly monitor and audit the usage of API tokens to detect any suspicious activity or unauthorized access.
### Creating a Dedicated User
It is advisable to avoid using the default root@pam user for balancing tasks in ProxLB. Instead, creating a dedicated user account is recommended and can be done easily. You can create a new user through the GUI, API, or CLI. While the detailed roles required for balancing are outlined in the next chapter, you can also use the following CLI commands to create a user with the necessary roles to manage Virtual Machines (VMs) and Containers (CTs):
```
pveum role add proxlb --privs Datastore.Audit,Sys.Audit,VM.Audit,VM.Migrate
pveum user add proxlb@pve --password <password>
pveum acl modify / --roles proxlb --users proxlb@pve
```
*Note: The user management can also be done on the WebUI without invoking the CLI.*
### Creating an API Token for a User
Create an API token for user proxlb@pve with token ID proxlb and deactivated privilege separation:
```
pveum user token add proxlb@pve proxlb --privsep 0
```
Afterwards, you get the token secret returned. You can now add those entries to your ProxLB config. Make sure, that you also keep the `user` parameter, next to the new token parameters.
> [!IMPORTANT]
> The parameter `pass` then needs to be **absent**! You should also take care about the privilege and authentication mechanism behind Proxmox. You might want or even might not want to use privilege separation and this is up to your personal needs and use case.
| Proxmox API | ProxLB Config | Example |
|---|---|---|
| User | [user](https://github.com/gyptazy/ProxLB/blob/main/config/proxlb_example.yaml#L3) | proxlb@pve |
| Token ID | [token_id](https://github.com/gyptazy/ProxLB/blob/main/config/proxlb_example.yaml#L6) | proxlb |
| Token Secret | [token_secret](https://github.com/gyptazy/ProxLB/blob/main/config/proxlb_example.yaml#L7) | 430e308f-1337-1337-beef-1337beefcafe |
*Note: The API token configuration can also be done on the WebUI without invoking the CLI.*
### Required Permissions for a User
To ensure that ProxLB operates effectively and securely, it is essential to assign the appropriate permissions to the user accounts responsible for managing the load balancing tasks. The following permissions are the minimum required for a user to perform essential ProxLB operations:
* `Datastore.Audit`: Grants the ability to audit and view datastore information.
* `Sys.Audit`: Allows the user to audit and view system information.
* `VM.Audit`: Enables the user to audit and view virtual machine details.
* `VM.Migrate`: Provides the permission to migrate virtual machines.
Assigning these permissions ensures that the user can access necessary information and perform critical operations related to load balancing without granting excessive privileges. This practice helps maintain a secure and efficient ProxLB environment.
## Configuration
### Affinity & Anti-Affinity Rules
ProxLB provides an advanced mechanism to define affinity and anti-affinity rules, enabling precise control over virtual machine (VM) placement. These rules help manage resource distribution, improve high availability configurations, and optimize performance within a Proxmox Virtual Environment (PVE) cluster. By leveraging Proxmoxs integrated access management, ProxLB ensures that users can only define and manage rules for guests they have permission to access.
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.
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/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.
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:
##### Example for Screenshot
```
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).
**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.
### 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.
```
balancing:
enforce_affinity: True
```
*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.
To define a guest to be ignored from the balancing, users assign a tag with the prefix `plb_ignore_$TAG`:
#### Example for Screenshot
```
plb_ignore_dev
```
As a result, ProxLB will not migrate this guest with the `plb_ignore_dev` tag to any other node.
**Note:** Ignored guests are really ignored. Even by enforcing affinity rules this guest will be ignored.
### Pin VMs to Specific Hypervisor Nodes
<img align="left" src="https://cdn.gyptazy.com/images/proxlb-tag-node-pinning.jpg"/> Guests, such as VMs or CTs, can also be pinned to specific 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.
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
```
plb_pin_node03
```
As a result, ProxLB will pin the guest `dev-vm01` to the node `virt03`.
**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.
### API Loadbalancing
ProxLB supports API loadbalancing, where one or more host objects can be defined as a list. This ensures, that you can even operator ProxLB without further changes when one or more nodes are offline or in a maintenance. When defining multiple hosts, the first reachable one will be picked.
```
proxmox_api:
hosts: ['virt01.example.com', '10.10.10.10', 'fe01::bad:code::cafe']
```
### Ignore Host-Nodes or Guests
In managing a Proxmox environment, it's often necessary to exclude certain host nodes and guests from various operations. For host nodes, this exclusion can be achieved by specifying them in the ignore_nodes parameter within the proxmox_api chapter, effectively preventing any automated processes from interacting with these nodes. Guests, on the other hand, can be ignored by assigning them a specific tag that starts with or is equal to plb_ignore, ensuring they are omitted from any automated tasks or monitoring. By implementing these configurations, administrators can fine-tune their Proxmox management to focus only on relevant nodes and guests, optimizing operational efficiency and resource allocation.
```
proxmox_cluster:
ignore_nodes: ['node01', 'node02']
```
### IPv6 Support
Yes, ProxLB fully supports IPv6.
### Logging / Log-Level
ProxLB supports systemd for seamless service management on Linux distributions. To enable this, create a proxLB.service file in /etc/systemd/system/ from `service/proxlb.service` within this repository.
On systems without systemd, such as FreeBSD and macOS, ProxLB runs with similar configurations but logs to stdout and stderr. The logging level and verbosity can be set in the `service` section of the configuration file:
```
service:
log_level: DEBUG
```
ProxLB only support the following log levels:
* INFO
* WARNING
* CRITICAL
* DEBUG
### Parallel Migrations
By default, parallel migrations are deactivated. This means, that a guest object gets migrated and the migration job is being watched until the VM or CT got moved to a new node. However, this may take a lot of time and many environments are fast enough to handle the IO load for multiple guest objects. However, there are always corner cases and this depends on your setup. Parallel migrations can be enabled by setting `parallel` to `True` within the `balancing` chapter:
```
balancing:
parallel: False
```
### Run as a Systemd-Service
The proxlb systemd unit orchestrates the ProxLB application. ProxLB can be used either as a one-shot solution or run periodically, depending on the configuration specified in the daemon chapter of its configuration file.
```
service:
daemon: False
schedule:
interval: 12
format: hours
```
In this configuration:
* `daemon`: False indicates that the ProxLB application is not running as a daemon and will execute as a one-shot solution.
* `schedule`: 12 defines the interval for the schedule, specifying how often rebalancing should be done if running as a daemon.
* `format`: Defines the given format of schedule where you can choose between `hours` or `minutes`.
### SSL Self-Signed Certificates
If you are using SSL self-signed certificates or non-valid certificated in general and do not want to deal with additional trust levels, you may also disable the SSL validation. This may mostly be helpful for dev- & test labs.
SSL certificate validation can be disabled in the `proxmox_api` section in the config file by setting:
```
proxmox_api:
ssl_verification: False
```
*Note: Disabling SSL certificate validation is not recommended.*
### Dynamic Power Management (DPM)
<img align="left" src="https://cdn.gyptazy.com/images/proxlb-proxmox-node-wakeonlan-wol-mac-dpm.jpg"/> Configuring Dynamic Power Management (DPM) in ProxLB within a Proxmox cluster involves a few critical steps to ensure proper operation. The first consideration is that any node intended for automatic shutdown and startup must support Wake-on-LAN (WOL). This is essential because DPM relies on the ability to power nodes back on remotely. For this to work, the ProxLB instance must be able to reach the target nodes MAC address directly over the network.
To make this possible, you must configure the correct MAC address for WOL within the Proxmox web interface. This is done by selecting the node, going to the “System” section, then “Options,” and finally setting the “MAC address for Wake-on-LAN.” Alternatively, this value can also be submitted using the Proxmox API. Without this MAC address in place, ProxLB will not allow the node to be shut down. This restriction is in place to prevent nodes from being turned off without a way to bring them back online, which could lead to service disruption. By ensuring that each node has a valid WOL MAC address configured, DPM can operate safely and effectively, allowing ProxLB to manage the clusters power consumption dynamically.
#### Requirements
Using the powermanagement feature within clusters comes along with several requirements:
* ProxLB needs to reach the WOL-Mac address of the node (plain network)
* WOL must be enabled of the node in general (BIOS/UEFI)
* The related WOL network interface must be defined
* The related WOL network interface MAC address must be defined in Proxmox for the node
#### Options
| Section | Option | Sub Option | Example | Type | Description |
|---------|:------:|:----------:|:-------:|:----:|:-----------:|
| `dpm` | | | | | |
| | enable | | True | `Bool` | Enables the Dynamic Power Management functions.|
| | method | | memory | `Str` | The balancing method that should be used. [values: `memory` (default), `cpu`, `disk`]|
| | mode | | static | `Str` | The balancing mode that should be used. [values: `static` (default), `auto`] |
| | cluster_min_free_resources | | 60 | `Int` | Representing the minimum required free resouzrces in percent within the cluster. [values: `60`% (default)] |
| | cluster_min_nodes | | 3 | `Int` | The minimum of required nodes that should remain in a cluster. [values: `3` (default)] |
#### DPM Modes
##### Static
Static mode in DPM lets you set a fixed number of nodes that should always stay powered on in a Proxmox cluster. This is important to keep the cluster working properly, since you need at least three nodes to maintain quorum. The system wont let you go below that limit to avoid breaking cluster functionality.
Besides the minimum number of active nodes, you can also define a baseline for how many free resources—like CPU or RAM—should always be available when the virtual machines are running. If the available resources drop below that level, ProxLB will try to power on more nodes, as long as they're available and can be started. On the other hand, if the cluster has more than enough resources, ProxLB will begin to shut down nodes again, but only until the free resource threshold is reached.
This mode gives you a more stable setup by always keeping a minimum number of nodes ready while still adjusting the rest of the cluster based on resource usage, but in a controlled and predictable way.

24
docs/99-faq.md Normal file
View File

@@ -0,0 +1,24 @@
## Table of Contents
1. [GUI Integration](#gui-integration)
- [How to install pve-proxmoxlb-service-ui package](https://github.com/gyptazy/ProxLB/issues/44)
2. [Proxmox HA Integration](#proxmox-ha-integration)
- [Host groups: Honour HA groups](https://github.com/gyptazy/ProxLB/issues/65)
### GUI Integration
<img align="left" src="https://cdn.gyptazy.com/images/proxlb-GUI-integration.jpg"/> ProxLB can also be accessed through the Proxmox Web UI by installing the optional `pve-proxmoxlb-service-ui` package, which depends on the proxlb package. For full Web UI integration, this package must be installed on all nodes within the cluster. Once installed, a new menu item - `Rebalancing`, appears in the cluster level under the HA section. Once installed, it offers two key functionalities:
* Rebalancing VM workloads
* Migrate VM workloads away from a defined node (e.g. maintenance preparation)
**Note:** This package is currently discontinued and will be readded at a later time. See also: [#44: How to install pve-proxmoxlb-service-ui package](https://github.com/gyptazy/ProxLB/issues/44).
### Proxmox HA Integration
Proxmox HA (High Availability) groups are designed to ensure that virtual machines (VMs) remain running within a Proxmox cluster. HA groups define specific rules for where VMs should be started or migrated in case of node failures, ensuring minimal downtime and automatic recovery.
However, when used in conjunction with ProxLB, the built-in load balancer for Proxmox, conflicts can arise. ProxLB operates with its own logic for workload distribution, taking into account affinity and anti-affinity rules. While it effectively balances guest workloads, it may re-shift and redistribute VMs in a way that does not align with HA group constraints, potentially leading to unsuitable placements.
Due to these conflicts, it is currently not recommended to use both HA groups and ProxLB simultaneously. The interaction between the two mechanisms can lead to unexpected behavior, where VMs might not adhere to HA group rules after being moved by ProxLB.
A solution to improve compatibility between HA groups and ProxLB is under evaluation, aiming to ensure that both features can work together without disrupting VM placement strategies.
See also: [#65: Host groups: Honour HA groups](https://github.com/gyptazy/ProxLB/issues/65).

View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
VERSION="1.1.2b"
sed -i "s/^__version__ = .*/__version__ = \"$VERSION\"/" "proxlb/utils/version.py"
sed -i "s/version=\"[0-9]*\.[0-9]*\.[0-9]*\"/version=\"$VERSION\"/" setup.py
echo "OK: Versions have been sucessfully set to $VERSION"

View File

@@ -0,0 +1,4 @@
#!/usr/bin/env bash
git clone https://github.com/gyptazy/changelog-fragments-creator.git
./changelog-fragments-creator/changelog-creator -f .changelogs/ -o CHANGELOG.md
echo "Created changelog file"

View File

@@ -1,16 +0,0 @@
#!/bin/bash
sudo apt-get install rpm cmake git make python3-yaml
git clone https://github.com/gyptazy/changelog-fragments-creator.git
./changelog-fragments-creator/changelog-creator -f ../.changelogs/ -o ../CHANGELOG.md
mkdir packages
mkdir build
cd build
cmake ..
cpack -G DEB .
cpack -G RPM .
cp *.deb ../packages
cp *.rpm ../packages
cd ..
rm -rf build
echo "Packages created. Packages can be found in directory: packages"

View File

@@ -1,4 +0,0 @@
#!/bin/bash
git clone https://github.com/gyptazy/changelog-fragments-creator.git
./changelog-fragments-creator/changelog-creator -f ../.changelogs/ -o ../CHANGELOG.md
echo "Created changelog file"

View File

@@ -1,40 +0,0 @@
cmake_minimum_required(VERSION 3.16)
project(proxmox-rebalancing-service VERSION 1.0.3)
install(PROGRAMS ../proxlb DESTINATION /bin)
install(FILES ../proxlb.conf DESTINATION /etc/proxlb)
install(FILES proxlb.service DESTINATION /etc/systemd/system)
# General
set(CPACK_PACKAGE_NAME "proxlb")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/../LICENSE")
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/../README.md")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Florian Paul Azim <gyptazy> Hoberg <gyptazy@gyptazy.ch>")
set(CPACK_PACKAGE_CONTACT "Florian Paul Azim Hoberg <gyptazy@gyptazy.ch>")
set(CPACK_PACKAGE_VENDOR "gyptazy")
# RPM packaging
set(CPACK_PACKAGE_VERSION ${CMAKE_PROJECT_VERSION})
set(CPACK_GENERATOR "RPM")
set(CPACK_RPM_PACKAGE_ARCHITECTURE "amd64")
set(CPACK_RPM_PACKAGE_SUMMARY "ProxLB - Rebalance VM workloads across nodes in Proxmox clusters.")
set(CPACK_RPM_PACKAGE_DESCRIPTION "ProxLB - Rebalance VM workloads across nodes in Proxmox clusters.")
set(CPACK_RPM_CHANGELOG_FILE "${CMAKE_CURRENT_SOURCE_DIR}/changelog_redhat")
set(CPACK_PACKAGE_RELEASE 1)
set(CPACK_RPM_PACKAGE_LICENSE "GPL 3.0")
set(CPACK_RPM_PACKAGE_REQUIRES "python >= 3.2.0")
# DEB packaging
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
set(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "amd64")
set(CPACK_DEBIAN_PACKAGE_SUMMARY "ProxLB - Rebalance VM workloads across nodes in Proxmox clusters.")
set(CPACK_DEBIAN_PACKAGE_DESCRIPTION "ProxLB - Rebalance VM workloads across nodes in Proxmox clusters.")
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_SOURCE_DIR}/changelog_debian")
set(CPACK_DEBIAN_PACKAGE_DEPENDS "python3, python3-proxmoxer")
set(CPACK_DEBIAN_PACKAGE_LICENSE "GPL 3.0")
# Install
set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_CURRENT_SOURCE_DIR}/postinst;${CMAKE_CURRENT_SOURCE_DIR}/conffiles")
set(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_SOURCE_DIR}/postinst")
include(CPack)

View File

@@ -1,14 +0,0 @@
## Build packages
Building the packages requires cmake, deb and rpm.
For building packages, simly run the following commands:
```
mkdir build
cd build
cmake ..
cpack -G RPM .
cpack -G DEB .
```
When running on Debian/Ubuntu you can directly call `01_package.sh`
to create your own packages.

View File

@@ -1,41 +0,0 @@
proxlb (1.0.3) unstable; urgency=low
* Add a convert function to cast all bool alike options from configparser to bools.
* Add a config parser options for future features.
* Add a config versio schema that must be supported by ProxLB.
* Add feature to allow the API hosts being provided as a comma separated list.
* Add storage balancing function.
* Add doc how to add dedicated user for authentication. (by @Dulux-Oz)
* Add cli arg `-b` to return the next best node for next VM/CT placement.Fix some wonkey code styles.
* Provide a more reasonable output when HA services are not active in a Proxmox cluster.
* Improve the underlying code base for future implementations.
* Fix documentation for the master_only parameter placed in the wrong config section.
* Fixed `master_only` function by inverting the condition.
* Improved the overall validation and error handling.
* Fix bug in the `proxlb.conf` in the vm_balancing section.
* Fix handling of unset `ignore_nodes` and `ignore_vms` resulted in an attribute error.
* Fix anti-affinity rules not evaluating a new and different node.
-- Florian Paul Azim Hoberg <gyptazy@gyptazy.ch> Wed, 11 Sep 2024 17:31:03 +0200
proxlb (1.0.2) unstable; urgency=low
* Add option to run migration in parallel or sequentially.
* Add option to run ProxLB only on a Proxmox cluster master (req. HA feature).
* Fix daemon timer to use hours instead of minutes.
* Fix CMake packaging for Debian package to avoid overwriting the config file.
* Fix some wonkey code styles.
-- Florian Paul Azim Hoberg <gyptazy@gyptazy.ch> Tue, 13 Aug 2024 17:28:14 +0200
proxlb (1.0.0) unstable; urgency=low
* Initial release of ProxLB.
-- Florian Paul Azim Hoberg <gyptazy@gyptazy.ch> Thu, 01 Aug 2024 17:04:12 +0200
proxlb (0.9.0) unstable; urgency=low
* Initial development release of ProxLB as a tech preview.
-- Florian Paul Azim Hoberg <gyptazy@gyptazy.ch> Sun, 07 Jul 2024 05:38:41 +0200

View File

@@ -1,28 +0,0 @@
* Wed Sep 12 2024 Florian Paul Azim Hoberg <gyptazy@gyptazy.ch>
- Add a convert function to cast all bool alike options from configparser to bools.
- Add a config parser options for future features.
- Add a config versio schema that must be supported by ProxLB.
- Add feature to allow the API hosts being provided as a comma separated list.
- Add storage balancing function.
- Add doc how to add dedicated user for authentication. (by @Dulux-Oz)
- Add cli arg `-b` to return the next best node for next VM/CT placement.Fix some wonkey code styles.
- Provide a more reasonable output when HA services are not active in a Proxmox cluster.
- Improve the underlying code base for future implementations.
- Fix documentation for the master_only parameter placed in the wrong config section.
- Fixed `master_only` function by inverting the condition.
- Improved the overall validation and error handling.
- Fix bug in the `proxlb.conf` in the vm_balancing section.
- Fix handling of unset `ignore_nodes` and `ignore_vms` resulted in an attribute error.
- Fix anti-affinity rules not evaluating a new and different node.
* Tue Aug 13 2024 Florian Paul Azim Hoberg <gyptazy@gyptazy.ch>
- Add option to run migration in parallel or sequentially.
- Add option to run ProxLB only on a Proxmox cluster master (req. HA feature).
- Fixed daemon timer to use hours instead of minutes.
- Fixed some wonkey code styles.
* Thu Aug 01 2024 Florian Paul Azim Hoberg <gyptazy@gyptazy.ch>
- Initial release of ProxLB.
* Sun Jul 07 2024 Florian Paul Azim Hoberg <gyptazy@gyptazy.ch>
- Initial development release of ProxLB as a tech preview.

View File

@@ -1 +0,0 @@
/etc/proxlb/proxlb.conf

View File

@@ -1,5 +0,0 @@
#!/bin/bash
useradd -m plb
chown plb:plb /etc/proxlb/proxlb.conf
chmod 600 /etc/proxlb/proxlb.conf
systemctl daemon-reload

View File

@@ -1,6 +0,0 @@
[Unit]
Description=ProxLB - Rebalance VM workloads
[Service]
ExecStart=/usr/bin/proxlb -c /etc/proxlb/proxlb.conf
User=plb

1565
proxlb

File diff suppressed because it is too large Load Diff

View File

@@ -1,23 +0,0 @@
[proxmox]
api_host: hypervisor01.gyptazy.ch
api_user: root@pam
api_pass: FooBar
verify_ssl: 1
[vm_balancing]
enable: 1
method: memory
mode: used
maintenance_nodes: dummynode03,dummynode04
ignore_nodes: dummynode01,dummynode02
ignore_vms: testvm01,testvm02
[storage_balancing]
enable: 0
[update_service]
enable: 0
[api]
enable: 0
[service]
daemon: 1
schedule: 24
log_verbosity: CRITICAL
config_version: 3

95
proxlb/main.py Normal file
View File

@@ -0,0 +1,95 @@
"""
ProxLB is a load balancing tool for Proxmox Virtual Environment (PVE) clusters.
It connects to the Proxmox API, retrieves information about nodes, guests, and groups,
and performs calculations to determine the optimal distribution of resources across the
cluster. The tool supports daemon mode for continuous operation and can log metrics and
perform balancing actions based on the configuration provided. It also includes a CLI
parser for handling command-line arguments and a custom logger for systemd integration.
"""
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
import logging
from utils.logger import SystemdLogger
from utils.cli_parser import CliParser
from utils.config_parser import ConfigParser
from utils.proxmox_api import ProxmoxApi
from models.dpm import DPM
from models.nodes import Nodes
from models.guests import Guests
from models.groups import Groups
from models.calculations import Calculations
from models.balancing import Balancing
from utils.helper import Helper
def main():
"""
ProxLB main function
"""
# Initialize logging handler
logger = SystemdLogger(level=logging.INFO)
# Parses arguments passed from the CLI
cli_parser = CliParser()
cli_args = cli_parser.parse_args()
Helper.get_version(cli_args.version)
# Parse ProxLB config file
config_parser = ConfigParser(cli_args.config)
proxlb_config = config_parser.get_config()
# Update log level from config and fallback to INFO if not defined
logger.set_log_level(proxlb_config.get('service', {}).get('log_level', 'INFO'))
# Connect to Proxmox API & create API object
proxmox_api = ProxmoxApi(proxlb_config)
# Overwrite password after creating the API object
proxlb_config["proxmox_api"]["pass"] = "********"
while True:
# Get all required objects from the Proxmox cluster
meta = {"meta": proxlb_config}
nodes, cluster = Nodes.get_nodes(proxmox_api, proxlb_config)
guests = Guests.get_guests(proxmox_api, nodes, meta)
groups = Groups.get_groups(guests, nodes)
# Merge obtained objects from the Proxmox cluster for further usage
proxlb_data = {**meta, **cluster, **nodes, **guests, **groups}
Helper.log_node_metrics(proxlb_data)
# Evaluate the dynamic power management for nodes in the clustet
DPM(proxlb_data)
# Update the initial node resource assignments
# by the previously created groups.
Calculations.set_node_assignments(proxlb_data)
Calculations.get_most_free_node(proxlb_data, cli_args.best_node)
Calculations.relocate_guests_on_maintenance_nodes(proxlb_data)
Calculations.get_balanciness(proxlb_data)
Calculations.relocate_guests(proxlb_data)
Helper.log_node_metrics(proxlb_data, init=False)
# Perform balancing
if not cli_args.dry_run or not proxlb_data["meta"]["balancing"].get("enable", False):
Balancing(proxmox_api, proxlb_data)
# Perform DPM
if not cli_args.dry_run:
DPM.dpm_shutdown_nodes(proxmox_api, proxlb_data)
# Validate if the JSON output should be
# printed to stdout
Helper.print_json(proxlb_data, cli_args.json)
# Validate daemon mode
Helper.get_daemon_mode(proxlb_config)
logger.debug(f"Finished: __main__")
if __name__ == "__main__":
main()

View File

201
proxlb/models/balancing.py Normal file
View File

@@ -0,0 +1,201 @@
"""
The Balancing class is responsible for processing workloads on Proxmox clusters.
It processes the previously generated data (held in proxlb_data) and moves guests
and other supported types across Proxmox clusters based on the defined values by an operator.
"""
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
import proxmoxer
import time
from utils.logger import SystemdLogger
from typing import Dict, Any
logger = SystemdLogger()
class Balancing:
"""
The balancing class is responsible for processing workloads on Proxmox clusters.
The previously generated data (hold in proxlb_data) will processed and guests and
other supported types will be moved across Proxmox clusters based on the defined
values by an operator.
Methods:
__init__(self, proxmox_api: any, proxlb_data: Dict[str, Any]):
Initializes the Balancing class with the provided ProxLB data and initiates the rebalancing
process for guests.
exec_rebalancing_vm(self, proxmox_api: any, proxlb_data: Dict[str, Any], guest_name: str) -> None:
Executes the rebalancing of a virtual machine (VM) to a new node within the cluster. Logs the migration
process and handles exceptions.
exec_rebalancing_ct(self, proxmox_api: any, proxlb_data: Dict[str, Any], guest_name: str) -> None:
Executes the rebalancing of a container (CT) to a new node within the cluster. Logs the migration
process and handles exceptions.
get_rebalancing_job_status(self, proxmox_api: any, proxlb_data: Dict[str, Any], guest_name: str, guest_current_node: str, job_id: int, retry_counter: int = 1) -> bool:
Monitors the status of a rebalancing job on a Proxmox node until it completes or a timeout
is reached. Returns True if the job completed successfully, False otherwise.
"""
def __init__(self, proxmox_api: any, proxlb_data: Dict[str, Any]):
"""
Initializes the Balancing class with the provided ProxLB data.
Args:
proxlb_data (dict): The data required for balancing VMs and CTs.
"""
for guest_name, guest_meta in proxlb_data["guests"].items():
# Check if the guest's target is not the same as the current node
if guest_meta["node_current"] != guest_meta["node_target"]:
# Check if the guest is not ignored and perform the balancing
# operation based on the guest type
if not guest_meta["ignore"]:
guest_id = guest_meta["id"]
guest_node_current = guest_meta["node_current"]
guest_node_target = guest_meta["node_target"]
# VM Balancing
if guest_meta["type"] == "vm":
self.exec_rebalancing_vm(proxmox_api, proxlb_data, guest_name)
# CT Balancing
elif guest_meta["type"] == "ct":
self.exec_rebalancing_ct(proxmox_api, proxlb_data, guest_name)
# Just in case we get a new type of guest in the future
else:
logger.critical(f"Balancing: Got unexpected guest type: {guest_meta['type']}. Cannot proceed guest: {guest_meta['name']}.")
else:
logger.debug(f"Balancing: Guest {guest_name} is ignored and will not be rebalanced.")
else:
logger.debug(f"Balancing: Guest {guest_name} is already on the target node {guest_meta['node_target']} and will not be rebalanced.")
def exec_rebalancing_vm(self, proxmox_api: any, proxlb_data: Dict[str, Any], guest_name: str) -> None:
"""
Executes the rebalancing of a virtual machine (VM) to a new node within the cluster.
This function initiates the migration of a specified VM to a target node as part of the
load balancing process. It logs the migration process and handles any exceptions that
may occur during the migration.
Args:
proxmox_api (object): The Proxmox API client instance used to interact with the Proxmox cluster.
proxlb_data (dict): A dictionary containing data related to the ProxLB load balancing configuration.
guest_name (str): The name of the guest VM to be migrated.
Raises:
proxmox_api.core.ResourceException: If an error occurs during the migration process.
Returns:
None
"""
logger.debug("Starting: exec_rebalancing_vm.")
guest_id = proxlb_data["guests"][guest_name]["id"]
guest_node_current = proxlb_data["guests"][guest_name]["node_current"]
guest_node_target = proxlb_data["guests"][guest_name]["node_target"]
if proxlb_data["meta"]["balancing"].get("live", True):
online_migration = 1
else:
online_migration = 0
if proxlb_data["meta"]["balancing"].get("with_local_disks", True):
with_local_disks = 1
else:
with_local_disks = 0
migration_options = {
'target': {guest_node_target},
'online': online_migration,
'with-local-disks': with_local_disks
}
try:
logger.debug(f"Balancing: Starting to migrate guest {guest_name} of type VM.")
job_id = proxmox_api.nodes(guest_node_current).qemu(guest_id).migrate().post(**migration_options)
self.get_rebalancing_job_status(proxmox_api, proxlb_data, guest_name, guest_node_current, job_id)
except proxmoxer.core.ResourceException as proxmox_api_error:
logger.critical(f"Balancing: Failed to migrate guest {guest_name} of type VM due to some Proxmox errors. Please check if resource is locked or similar.")
logger.debug(f"Balancing: Failed to migrate guest {guest_name} of type VM due to some Proxmox errors: {proxmox_api_error}")
logger.debug("Finished: exec_rebalancing_vm.")
def exec_rebalancing_ct(self, proxmox_api: any, proxlb_data: Dict[str, Any], guest_name: str) -> None:
"""
Executes the rebalancing of a container (CT) to a new node within the cluster.
This function initiates the migration of a specified CT to a target node as part of the
load balancing process. It logs the migration process and handles any exceptions that
may occur during the migration.
Args:
proxmox_api (object): The Proxmox API client instance used to interact with the Proxmox cluster.
proxlb_data (dict): A dictionary containing data related to the ProxLB load balancing configuration.
guest_name (str): The name of the guest CT to be migrated.
Raises:
proxmox_api.core.ResourceException: If an error occurs during the migration process.
Returns:
None
"""
logger.debug("Starting: exec_rebalancing_ct.")
guest_id = proxlb_data["guests"][guest_name]["id"]
guest_node_current = proxlb_data["guests"][guest_name]["node_current"]
guest_node_target = proxlb_data["guests"][guest_name]["node_target"]
try:
logger.debug(f"Balancing: Starting to migrate guest {guest_name} of type CT.")
job_id = proxmox_api.nodes(guest_node_current).lxc(guest_id).migrate().post(target=guest_node_target, restart=1)
self.get_rebalancing_job_status(proxmox_api, proxlb_data, guest_name, guest_node_current, job_id)
except proxmoxer.core.ResourceException as proxmox_api_error:
logger.critical(f"Balancing: Failed to migrate guest {guest_name} of type CT due to some Proxmox errors. Please check if resource is locked or similar.")
logger.debug(f"Balancing: Failed to migrate guest {guest_name} of type CT due to some Proxmox errors: {proxmox_api_error}")
logger.debug("Finished: exec_rebalancing_ct.")
def get_rebalancing_job_status(self, proxmox_api: any, proxlb_data: Dict[str, Any], guest_name: str, guest_current_node: str, job_id: int, retry_counter: int = 1) -> bool:
"""
Monitors the status of a rebalancing job on a Proxmox node until it completes or a timeout is reached.
Args:
proxmox_api (object): The Proxmox API client instance.
proxlb_data (dict): The ProxLB configuration data.
guest_name (str): The name of the guest (virtual machine) being rebalanced.
guest_current_node (str): The current node where the guest is running.
job_id (str): The ID of the rebalancing job to monitor.
retry_counter (int, optional): The current retry count. Defaults to 1.
Returns:
bool: True if the job completed successfully, False otherwise.
"""
logger.debug("Starting: get_rebalancing_job_status.")
# Parallel migrations can take a huge time and create a higher load, if not defined by an
# operator we will use a sequential mode by default
if not proxlb_data["meta"]["balancing"].get("parallel", False):
job = proxmox_api.nodes(guest_current_node).tasks(job_id).status().get()
# Watch job id until it finalizes
if job["status"] == "running":
# Do not hammer the API while
# watching the job status
time.sleep(10)
retry_counter += 1
# Run recursion until we hit the soft-limit of maximum migration time for a guest
if retry_counter < proxlb_data["meta"]["balancing"].get("max_job_validation", 1800):
logger.debug(f"Balancing: Job ID {job_id} (guest: {guest_name}) for migration is still running... (Run: {retry_counter})")
self.get_rebalancing_job_status(proxmox_api, proxlb_data, guest_name, guest_current_node, job_id, retry_counter)
else:
logger.warning(f"Balancing: Job ID {job_id} (guest: {guest_name}) for migration took too long. Please check manually.")
logger.debug("Finished: get_rebalancing_job_status.")
return False
# Validate job output for errors when finished
if job["status"] == "stopped":
if job["exitstatus"] == "OK":
logger.debug(f"Balancing: Job ID {job_id} (guest: {guest_name}) was successfully.")
logger.debug("Finished: get_rebalancing_job_status.")
return True
else:
logger.critical(f"Balancing: Job ID {job_id} (guest: {guest_name}) went into an error! Please check manually.")
logger.debug("Finished: get_rebalancing_job_status.")
return False

View File

@@ -0,0 +1,442 @@
"""
The Calculations class is responsible for handling the balancing of virtual machines (VMs)
and containers (CTs) across all available nodes in a Proxmox cluster. It provides methods
to calculate the optimal distribution of VMs and CTs based on the provided data.
"""
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
import sys
from typing import Dict, Any
from utils.logger import SystemdLogger
logger = SystemdLogger()
class Calculations:
"""
The calculation class is responsible for handling the balancing of virtual machines (VMs)
and containers (CTs) across all available nodes in a Proxmox cluster. It provides methods
to calculate the optimal distribution of VMs and CTs based on the provided data.
Methods:
__init__(proxlb_data: Dict[str, Any]):
Initializes the Calculation class with the provided ProxLB data.
set_node_assignments(proxlb_data: Dict[str, Any]) -> Dict[str, Any]:
Sets the assigned resources of the nodes based on the current assigned
guest resources by their created groups as an initial base.
get_balanciness(proxlb_data: Dict[str, Any]) -> Dict[str, Any]:
Gets the balanciness for further actions where the highest and lowest
usage or assignments of Proxmox nodes are compared.
get_most_free_node(proxlb_data: Dict[str, Any], return_node: bool = False) -> Dict[str, Any]:
Gets the name of the Proxmox node in the cluster with the most free resources based on
the user-defined method (e.g., memory) and mode (e.g., used).
relocate_guests_on_maintenance_nodes(proxlb_data: Dict[str, Any]):
Relocates guests that are currently on nodes marked for maintenance to
nodes with the most available resources.
relocate_guests(proxlb_data: Dict[str, Any]):
Relocates guests within the provided data structure to ensure affinity groups are
placed on nodes with the most free resources.
val_anti_affinity(proxlb_data: Dict[str, Any], guest_name: str):
Validates and assigns nodes to guests based on anti-affinity rules.
update_node_resources(proxlb_data):
Updates the resource allocation and usage statistics for nodes when a guest
is moved from one node to another.
"""
def __init__(self, proxlb_data: Dict[str, Any]):
"""
Initializes the Calculation class with the provided ProxLB data.
Args:
proxlb_data (Dict[str, Any]): The data required for balancing VMs and CTs.
"""
@staticmethod
def set_node_assignments(proxlb_data: Dict[str, Any]) -> Dict[str, Any]:
"""
Set the assigned resources of the nodes based on the current assigned
guest resources by their created groups as an initial base.
Args:
proxlb_data (Dict[str, Any]): The data holding all current statistics.
Returns:
Dict[str, Any]: Updated ProxLB data of nodes section with updated node assigned values.
"""
logger.debug("Starting: set_node_assignments.")
for group_name, group_meta in proxlb_data["groups"]["affinity"].items():
for guest_name in group_meta["guests"]:
guest_node_current = proxlb_data["guests"][guest_name]["node_current"]
# Update Hardware assignments
# Update assigned values for the current node
proxlb_data["nodes"][guest_node_current]["cpu_assigned"] += proxlb_data["guests"][guest_name]["cpu_total"]
proxlb_data["nodes"][guest_node_current]["memory_assigned"] += proxlb_data["guests"][guest_name]["memory_total"]
proxlb_data["nodes"][guest_node_current]["disk_assigned"] += proxlb_data["guests"][guest_name]["disk_total"]
# Update assigned percentage values for the current node
proxlb_data["nodes"][guest_node_current]["cpu_assigned_percent"] = proxlb_data["nodes"][guest_node_current]["cpu_assigned"] / proxlb_data["nodes"][guest_node_current]["cpu_total"] * 100
proxlb_data["nodes"][guest_node_current]["memory_assigned_percent"] = proxlb_data["nodes"][guest_node_current]["memory_assigned"] / proxlb_data["nodes"][guest_node_current]["memory_total"] * 100
proxlb_data["nodes"][guest_node_current]["disk_assigned_percent"] = proxlb_data["nodes"][guest_node_current]["disk_assigned"] / proxlb_data["nodes"][guest_node_current]["disk_total"] * 100
logger.debug("Finished: set_node_assignments.")
@staticmethod
def get_balanciness(proxlb_data: Dict[str, Any]) -> Dict[str, Any]:
"""
Get the blanaciness for further actions where the highest and lowest
usage or assignments of Proxmox nodes are compared. Based on the users
provided balanciness delta the balancing will be performed.
Args:
proxlb_data (Dict[str, Any]): The data holding all content of all objects.
Returns:
Dict[str, Any]: Updated meta data section of the balanciness action defined
as a bool.
"""
logger.debug("Starting: get_balanciness.")
proxlb_data["meta"]["balancing"]["balance"] = False
if len(proxlb_data["groups"]) > 0:
method = proxlb_data["meta"]["balancing"].get("method", "memory")
mode = proxlb_data["meta"]["balancing"].get("mode", "used")
balanciness = proxlb_data["meta"]["balancing"].get("balanciness", 10)
method_value = [node_meta[f"{method}_{mode}_percent"] for node_meta in proxlb_data["nodes"].values()]
method_value_highest = max(method_value)
method_value_lowest = min(method_value)
if method_value_highest - method_value_lowest > balanciness:
proxlb_data["meta"]["balancing"]["balance"] = True
logger.debug(f"Guest balancing is required. Highest value: {method_value_highest}, lowest value: {method_value_lowest} balanced by {method} and {mode}.")
else:
logger.debug(f"Guest balancing is ok. Highest value: {method_value_highest}, lowest value: {method_value_lowest} balanced by {method} and {mode}.")
else:
logger.warning("No guests for balancing found.")
logger.debug("Finished: get_balanciness.")
@staticmethod
def get_most_free_node(proxlb_data: Dict[str, Any], return_node: bool = False) -> Dict[str, Any]:
"""
Get the name of the Proxmox node in the cluster with the most free resources based on
the user defined method (e.g.: memory) and mode (e.g.: used).
Args:
proxlb_data (Dict[str, Any]): The data holding all content of all objects.
return_node (bool): The indicator to simply return the best node for further
assignments.
Returns:
Dict[str, Any]: Updated meta data section of the node with the most free resources that should
be used for the next balancing action.
"""
logger.debug("Starting: get_most_free_node.")
proxlb_data["meta"]["balancing"]["balance_next_node"] = ""
# Do not include nodes that are marked in 'maintenance'
filtered_nodes = [node for node in proxlb_data["nodes"].values() if not node["maintenance"]]
method = proxlb_data["meta"]["balancing"].get("method", "memory")
mode = proxlb_data["meta"]["balancing"].get("mode", "used")
lowest_usage_node = min(filtered_nodes, key=lambda x: x[f"{method}_{mode}_percent"])
proxlb_data["meta"]["balancing"]["balance_reason"] = 'resources'
proxlb_data["meta"]["balancing"]["balance_next_node"] = lowest_usage_node["name"]
# If executed to simply get the best node for further usage, we return
# the best node on stdout and gracefully exit here
if return_node:
print(lowest_usage_node["name"])
sys.exit(0)
logger.debug("Finished: get_most_free_node.")
@staticmethod
def relocate_guests_on_maintenance_nodes(proxlb_data: Dict[str, Any]) -> None:
"""
Relocates guests that are currently on nodes marked for maintenance to
nodes with the most available resources.
This function iterates over all guests on maintenance nodes and attempts
to relocate them to nodes with the most free resources that are not in
maintenance mode. It updates the node resources accordingly and logs
warnings if the balancing may not be perfect due to the maintenance
status of the original node.
Args:
proxlb_data (Dict[str, Any]): The data holding all content of all objects.
Returns:
None
"""
logger.debug("Starting: get_most_free_node.")
proxlb_data["meta"]["balancing"]["balance_next_guest"] = ""
for guest_name in proxlb_data["groups"]["maintenance"]:
# Update the node with the most free nodes which is
# not in a maintenance
proxlb_data["meta"]["balancing"]["balance_next_guest"] = guest_name
Calculations.get_most_free_node(proxlb_data)
Calculations.update_node_resources(proxlb_data)
logger.warning(f"Warning: Balancing may not be perfect because guest {guest_name} was located on a node which is in maintenance mode.")
logger.debug("Finished: get_most_free_node.")
@staticmethod
def relocate_guests(proxlb_data: Dict[str, Any]) -> None:
"""
Relocates guests within the provided data structure to ensure affinity groups are
placed on nodes with the most free resources.
This function iterates over each affinity group in the provided data, identifies
the node with the most free resources, and migrates all guests within the group
to that node. It updates the node resources accordingly.
Args:
proxlb_data (Dict[str, Any]): The data holding all content of all objects.
Returns:
None
"""
logger.debug("Starting: relocate_guests.")
if proxlb_data["meta"]["balancing"]["balance"] or proxlb_data["meta"]["balancing"].get("enforce_affinity", False):
if proxlb_data["meta"]["balancing"].get("balance", False):
logger.debug("Balancing of guests will be performed. Reason: balanciness")
if proxlb_data["meta"]["balancing"].get("enforce_affinity", False):
logger.debug("Balancing of guests will be performed. Reason: enforce affinity balancing")
for group_name in proxlb_data["groups"]["affinity"]:
# We get initially the node with the most free resources and then
# migrate all guests within the group to that node to ensure the
# affinity.
Calculations.get_most_free_node(proxlb_data)
for guest_name in proxlb_data["groups"]["affinity"][group_name]["guests"]:
proxlb_data["meta"]["balancing"]["balance_next_guest"] = guest_name
Calculations.val_anti_affinity(proxlb_data, guest_name)
Calculations.val_node_relationship(proxlb_data, guest_name)
Calculations.update_node_resources(proxlb_data)
logger.debug("Finished: relocate_guests.")
@staticmethod
def val_anti_affinity(proxlb_data: Dict[str, Any], guest_name: str) -> None:
"""
Validates and assigns nodes to guests based on anti-affinity rules.
This function iterates over all defined anti-affinity groups in the provided
`proxlb_data` and checks if the specified `guest_name` is included in any of
these groups. If the guest is included and has not been processed yet, it
attempts to assign an unused and non-maintenance node to the guest, ensuring
that the anti-affinity rules are respected.
Parameters:
proxlb_data (Dict[str, Any]): The data holding all content of all objects.
guest_name (str): The name of the guest to be validated and assigned a node.
Returns:
None
"""
logger.debug("Starting: val_anti_affinity.")
# Start by iterating over all defined anti-affinity groups
for group_name in proxlb_data["groups"]["anti_affinity"].keys():
# Validate if the provided guest is included in the anti-affinity group
if guest_name in proxlb_data["groups"]["anti_affinity"][group_name]['guests'] and not proxlb_data["guests"][guest_name]["processed"]:
logger.debug(f"Anti-Affinity: Guest: {guest_name} is included in anti-affinity group: {group_name}.")
# Iterate over all available nodes
for node_name in proxlb_data["nodes"].keys():
# Only select node if it was not used before and is not in a
# maintenance mode. Afterwards, add it to the list of already
# used nodes for the current anti-affinity group
if node_name not in proxlb_data["groups"]["anti_affinity"][group_name]["used_nodes"]:
if not proxlb_data["nodes"][node_name]["maintenance"]:
# If the node has not been used yet, we assign this node to the guest
proxlb_data["meta"]["balancing"]["balance_next_node"] = node_name
proxlb_data["groups"]["anti_affinity"][group_name]["used_nodes"].append(node_name)
logger.debug(f"Node: {node_name} marked as used for anti-affinity group: {group_name} with guest {guest_name}")
break
else:
logger.critical(f"Node: {node_name} already got used for anti-affinity group:: {group_name}. (Tried for guest: {guest_name})")
else:
logger.debug(f"Guest: {guest_name} is not included in anti-affinity group: {group_name}. Skipping.")
logger.debug("Finished: val_anti_affinity.")
@staticmethod
def val_node_relationship(proxlb_data: Dict[str, Any], guest_name: str) -> None:
"""
Validates and assigns guests to nodes based on defined relationships based on tags.
Parameters:
proxlb_data (Dict[str, Any]): The data holding all content of all objects.
guest_name (str): The name of the guest to be validated and assigned a node.
Returns:
None
"""
logger.debug("Starting: val_node_relationship.")
proxlb_data["guests"][guest_name]["processed"] = True
if proxlb_data["guests"][guest_name]["node_relationship"]:
logger.info(f"Guest '{guest_name}' has a specific relationship defined to node: {proxlb_data['guests'][guest_name]['node_relationship']}. Pinning to node.")
# Validate if the specified node name is really part of the cluster
if proxlb_data['guests'][guest_name]['node_relationship'] in proxlb_data["nodes"].keys():
logger.info(f"Guest '{guest_name}' has a specific relationship defined to node: {proxlb_data['guests'][guest_name]['node_relationship']} is a known hypervisor node in the cluster.")
# Pin the guest to the specified hypervisor node.
proxlb_data["meta"]["balancing"]["balance_next_node"] = proxlb_data['guests'][guest_name]['node_relationship']
else:
logger.warning(f"Guest '{guest_name}' has a specific relationship defined to node: {proxlb_data['guests'][guest_name]['node_relationship']} but this node name is not known in the cluster!")
else:
logger.info(f"Guest '{guest_name}' does not have any specific node relationships.")
logger.debug("Finished: val_node_relationship.")
@staticmethod
def update_node_resources(proxlb_data: Dict[str, Any]) -> None:
"""
Updates the resource allocation and usage statistics for nodes when a guest
is moved from one node to another.
Parameters:
proxlb_data (dict): A dictionary containing information about the nodes and
guests, including their resource allocations and usage.
The function performs the following steps:
1. Retrieves the guest name, current node, and target node from the provided data.
2. Updates the resource allocations and usage statistics for the target node by
adding the resources of the moved guest.
3. Updates the resource allocations and usage statistics for the current node by
subtracting the resources of the moved guest.
4. Logs the start and end of the resource update process, as well as the movement
of the guest from the current node to the target node.
"""
logger.debug("Starting: update_node_resources.")
guest_name = proxlb_data["meta"]["balancing"]["balance_next_guest"]
node_current = proxlb_data["guests"][guest_name]["node_current"]
node_target = proxlb_data["meta"]["balancing"]["balance_next_node"]
# Update resources for the target node by the moved guest resources
# Add assigned resources to the target node
proxlb_data["nodes"][node_target]["cpu_assigned"] += proxlb_data["guests"][guest_name]["cpu_total"]
proxlb_data["nodes"][node_target]["memory_assigned"] += proxlb_data["guests"][guest_name]["memory_total"]
proxlb_data["nodes"][node_target]["disk_assigned"] += proxlb_data["guests"][guest_name]["disk_total"]
# Update the assigned percentages of assigned resources for the target node
proxlb_data["nodes"][node_target]["cpu_assigned_percent"] = proxlb_data["nodes"][node_target]["cpu_assigned"] / proxlb_data["nodes"][node_target]["cpu_total"] * 100
proxlb_data["nodes"][node_target]["memory_assigned_percent"] = proxlb_data["nodes"][node_target]["memory_assigned"] / proxlb_data["nodes"][node_target]["memory_total"] * 100
proxlb_data["nodes"][node_target]["disk_assigned_percent"] = proxlb_data["nodes"][node_target]["disk_assigned"] / proxlb_data["nodes"][node_target]["disk_total"] * 100
# Add used resources to the target node
proxlb_data["nodes"][node_target]["cpu_used"] += proxlb_data["guests"][guest_name]["cpu_used"]
proxlb_data["nodes"][node_target]["memory_used"] += proxlb_data["guests"][guest_name]["memory_used"]
proxlb_data["nodes"][node_target]["disk_used"] += proxlb_data["guests"][guest_name]["disk_used"]
# Update the used percentages of usage resources for the target node
proxlb_data["nodes"][node_target]["cpu_used_percent"] = proxlb_data["nodes"][node_target]["cpu_used"] / proxlb_data["nodes"][node_target]["cpu_total"] * 100
proxlb_data["nodes"][node_target]["memory_used_percent"] = proxlb_data["nodes"][node_target]["memory_used"] / proxlb_data["nodes"][node_target]["memory_total"] * 100
proxlb_data["nodes"][node_target]["disk_used_percent"] = proxlb_data["nodes"][node_target]["disk_used"] / proxlb_data["nodes"][node_target]["disk_total"] * 100
# Update resources for the current node by the moved guest resources
# Add assigned resources to the target node
proxlb_data["nodes"][node_current]["cpu_assigned"] -= proxlb_data["guests"][guest_name]["cpu_total"]
proxlb_data["nodes"][node_current]["memory_assigned"] -= proxlb_data["guests"][guest_name]["memory_total"]
proxlb_data["nodes"][node_current]["disk_assigned"] -= proxlb_data["guests"][guest_name]["disk_total"]
# Update the assigned percentages of assigned resources for the target node
proxlb_data["nodes"][node_current]["cpu_assigned_percent"] = proxlb_data["nodes"][node_current]["cpu_assigned"] / proxlb_data["nodes"][node_current]["cpu_total"] * 100
proxlb_data["nodes"][node_current]["memory_assigned_percent"] = proxlb_data["nodes"][node_current]["memory_assigned"] / proxlb_data["nodes"][node_current]["memory_total"] * 100
proxlb_data["nodes"][node_current]["disk_assigned_percent"] = proxlb_data["nodes"][node_current]["disk_assigned"] / proxlb_data["nodes"][node_current]["disk_total"] * 100
# Add used resources to the target node
proxlb_data["nodes"][node_current]["cpu_used"] -= proxlb_data["guests"][guest_name]["cpu_used"]
proxlb_data["nodes"][node_current]["memory_used"] -= proxlb_data["guests"][guest_name]["memory_used"]
proxlb_data["nodes"][node_current]["disk_used"] -= proxlb_data["guests"][guest_name]["disk_used"]
# Update the used percentages of usage resources for the target node
proxlb_data["nodes"][node_current]["cpu_used_percent"] = proxlb_data["nodes"][node_current]["cpu_used"] / proxlb_data["nodes"][node_current]["cpu_total"] * 100
proxlb_data["nodes"][node_current]["memory_used_percent"] = proxlb_data["nodes"][node_current]["memory_used"] / proxlb_data["nodes"][node_current]["memory_total"] * 100
proxlb_data["nodes"][node_current]["disk_used_percent"] = proxlb_data["nodes"][node_current]["disk_used"] / proxlb_data["nodes"][node_current]["disk_total"] * 100
# Assign guest to the new target node
proxlb_data["guests"][guest_name]["node_target"] = node_target
logger.debug(f"Set guest {guest_name} from node {node_current} to node {node_target}.")
logger.debug("Finished: update_node_resources.")
@staticmethod
def update_cluster_resources(proxlb_data: Dict[str, Any], node: str, action: str) -> None:
"""
Updates the cluster resource statistics based on the specified action and node.
This method modifies the cluster-level resource data (such as CPU, memory, disk usage,
and node counts) based on the action performed ('add' or 'remove') for the specified node.
It calculates the updated statistics after adding or removing a node and logs the results.
Parameters:
proxlb_data (Dict[str, Any]): The data representing the current state of the cluster,
including node-level statistics for CPU, memory, and disk.
node (str): The identifier of the node whose resources are being added or removed from the cluster.
action (str): The action to perform, either 'add' or 'remove'. 'add' will include the node's
resources in the cluster, while 'remove' will exclude the node's resources.
Returns:
None: The function modifies the `proxlb_data` dictionary in place to update the cluster resources.
"""
logger.debug("Starting: update_cluster_resources.")
logger.debug(f"DPM: Updating cluster statistics by online node {node}. Action: {action}")
logger.debug(f"DPM: update_cluster_resources - Before {action}: {proxlb_data['cluster']['memory_free_percent']}")
if action == "add":
proxlb_data["cluster"]["node_count"] = proxlb_data["cluster"].get("node_count", 0) + 1
proxlb_data["cluster"]["cpu_total"] = proxlb_data["cluster"].get("cpu_total", 0) + proxlb_data["nodes"][node]["cpu_total"]
proxlb_data["cluster"]["cpu_used"] = proxlb_data["cluster"].get("cpu_used", 0) + proxlb_data["nodes"][node]["cpu_used"]
proxlb_data["cluster"]["cpu_free"] = proxlb_data["cluster"].get("cpu_free", 0) + proxlb_data["nodes"][node]["cpu_free"]
proxlb_data["cluster"]["cpu_free_percent"] = proxlb_data["cluster"].get("cpu_free", 0) / proxlb_data["cluster"].get("cpu_total", 0) * 100
proxlb_data["cluster"]["cpu_used_percent"] = proxlb_data["cluster"].get("cpu_used", 0) / proxlb_data["cluster"].get("cpu_total", 0) * 100
proxlb_data["cluster"]["memory_total"] = proxlb_data["cluster"].get("memory_total", 0) + proxlb_data["nodes"][node]["memory_total"]
proxlb_data["cluster"]["memory_used"] = proxlb_data["cluster"].get("memory_used", 0) + proxlb_data["nodes"][node]["memory_used"]
proxlb_data["cluster"]["memory_free"] = proxlb_data["cluster"].get("memory_free", 0) + proxlb_data["nodes"][node]["memory_free"]
proxlb_data["cluster"]["memory_free_percent"] = proxlb_data["cluster"].get("memory_free", 0) / proxlb_data["cluster"].get("memory_total", 0) * 100
proxlb_data["cluster"]["memory_used_percent"] = proxlb_data["cluster"].get("memory_used", 0) / proxlb_data["cluster"].get("memory_total", 0) * 100
proxlb_data["cluster"]["disk_total"] = proxlb_data["cluster"].get("disk_total", 0) + proxlb_data["nodes"][node]["disk_total"]
proxlb_data["cluster"]["disk_used"] = proxlb_data["cluster"].get("disk_used", 0) + proxlb_data["nodes"][node]["disk_used"]
proxlb_data["cluster"]["disk_free"] = proxlb_data["cluster"].get("disk_free", 0) + proxlb_data["nodes"][node]["disk_free"]
proxlb_data["cluster"]["disk_free_percent"] = proxlb_data["cluster"].get("disk_free", 0) / proxlb_data["cluster"].get("disk_total", 0) * 100
proxlb_data["cluster"]["disk_used_percent"] = proxlb_data["cluster"].get("disk_used", 0) / proxlb_data["cluster"].get("disk_total", 0) * 100
proxlb_data["cluster"]["node_count_available"] = proxlb_data["cluster"].get("node_count_available", 0) + 1
proxlb_data["cluster"]["node_count_overall"] = proxlb_data["cluster"].get("node_count_overall", 0) + 1
if action == "remove":
proxlb_data["cluster"]["node_count"] = proxlb_data["cluster"].get("node_count", 0) - 1
proxlb_data["cluster"]["cpu_total"] = proxlb_data["cluster"].get("cpu_total", 0) - proxlb_data["nodes"][node]["cpu_total"]
proxlb_data["cluster"]["cpu_used"] = proxlb_data["cluster"].get("cpu_used", 0) - proxlb_data["nodes"][node]["cpu_used"]
proxlb_data["cluster"]["cpu_free"] = proxlb_data["cluster"].get("cpu_free", 0) - proxlb_data["nodes"][node]["cpu_free"]
proxlb_data["cluster"]["cpu_free_percent"] = proxlb_data["cluster"].get("cpu_free", 0) / proxlb_data["cluster"].get("cpu_total", 0) * 100
proxlb_data["cluster"]["cpu_used_percent"] = proxlb_data["cluster"].get("cpu_used", 0) / proxlb_data["cluster"].get("cpu_total", 0) * 100
proxlb_data["cluster"]["memory_total"] = proxlb_data["cluster"].get("memory_total", 0) - proxlb_data["nodes"][node]["memory_total"]
proxlb_data["cluster"]["memory_used"] = proxlb_data["cluster"].get("memory_used") - proxlb_data["nodes"][node]["memory_used"]
proxlb_data["cluster"]["memory_free"] = proxlb_data["cluster"].get("memory_free") - proxlb_data["nodes"][node]["memory_free"]
proxlb_data["cluster"]["memory_free_percent"] = proxlb_data["cluster"].get("memory_free") / proxlb_data["cluster"].get("memory_total", 0) * 100
proxlb_data["cluster"]["memory_used_percent"] = proxlb_data["cluster"].get("memory_used") / proxlb_data["cluster"].get("memory_total", 0) * 100
proxlb_data["cluster"]["disk_total"] = proxlb_data["cluster"].get("disk_total", 0) - proxlb_data["nodes"][node]["disk_total"]
proxlb_data["cluster"]["disk_used"] = proxlb_data["cluster"].get("disk_used", 0) - proxlb_data["nodes"][node]["disk_used"]
proxlb_data["cluster"]["disk_free"] = proxlb_data["cluster"].get("disk_free", 0) - proxlb_data["nodes"][node]["disk_free"]
proxlb_data["cluster"]["disk_free_percent"] = proxlb_data["cluster"].get("disk_free", 0) / proxlb_data["cluster"].get("disk_total", 0) * 100
proxlb_data["cluster"]["disk_used_percent"] = proxlb_data["cluster"].get("disk_used", 0) / proxlb_data["cluster"].get("disk_total", 0) * 100
proxlb_data["cluster"]["node_count_available"] = proxlb_data["cluster"].get("node_count_available", 0) - 1
logger.debug(f"DPM: update_cluster_resources - After {action}: {proxlb_data['cluster']['memory_free_percent']}")
logger.debug("Finished: update_cluster_resources.")

255
proxlb/models/dpm.py Normal file
View File

@@ -0,0 +1,255 @@
"""
The DPM (Dynamic Power Management) class is responsible for the dynamic management
of nodes within a Proxmox cluster, optimizing resource utilization by controlling
node power states based on specified schedules and conditions.
This class provides functionality for:
- Tracking and validating schedules for dynamic power management.
- Shutting down nodes that are underutilized or not needed.
- Starting up nodes using Wake-on-LAN (WOL) based on certain conditions.
- Ensuring that nodes are properly flagged for maintenance and startup/shutdown actions.
The DPM class can operate in different modes, such as static and automatic,
to either perform predefined actions or dynamically adjust based on real-time resource usage.
"""
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
import proxmoxer
from typing import Dict, Any
from models.calculations import Calculations
from utils.logger import SystemdLogger
logger = SystemdLogger()
class DPM:
"""
The DPM (Dynamic Power Management) class is responsible for the dynamic management
of nodes within a Proxmox cluster, optimizing resource utilization by controlling
node power states based on specified schedules and conditions.
This class provides functionality for:
- Tracking and validating schedules for dynamic power management.
- Shutting down nodes that are underutilized or not needed.
- Starting up nodes using Wake-on-LAN (WOL) based on certain conditions.
- Ensuring that nodes are properly flagged for maintenance and startup/shutdown actions.
The DPM class can operate in different modes, such as static and automatic,
to either perform predefined actions or dynamically adjust based on real-time resource usage.
Attributes:
None directly defined for the class; instead, all actions are based on input data
and interactions with the Proxmox API and other helper functions.
Methods:
__init__(proxlb_data: Dict[str, Any]):
Initializes the DPM class, checking whether DPM is enabled and operating in the
appropriate mode (static or auto).
dpm_static(proxlb_data: Dict[str, Any]) -> None:
Evaluates the cluster's resource availability and performs static power management
actions by removing nodes that are not required.
dpm_shutdown_nodes(proxmox_api, proxlb_data) -> None:
Shuts down nodes flagged for DPM shutdown by using the Proxmox API, ensuring
that Wake-on-LAN (WOL) is available for proper node recovery.
dpm_startup_nodes(proxmox_api, proxlb_data) -> None:
Powers on nodes that are flagged for startup and are not in maintenance mode,
leveraging Wake-on-LAN (WOL) functionality.
dpm_validate_wol_mac(proxmox_api, node) -> None:
Validates and retrieves the Wake-on-LAN (WOL) MAC address for a given node,
ensuring that a valid address is set for powering on the node remotely.
"""
def __init__(self, proxlb_data: Dict[str, Any]):
"""
Initializes the DPM class with the provided ProxLB data.
Args:
proxlb_data (dict): The data required for balancing VMs and CTs.
"""
logger.debug("Starting: dpm class.")
if proxlb_data["meta"].get("dpm", {}).get("enable", False):
logger.debug("DPM function is enabled.")
mode = proxlb_data["meta"].get("dpm", {}).get("mode", None)
if mode == "static":
self.dpm_static(proxlb_data)
if mode == "auto":
self.dpm_auto(proxlb_data)
else:
logger.debug("DPM function is not enabled.")
logger.debug("Finished: dpm class.")
def dpm_static(self, proxlb_data: Dict[str, Any]) -> None:
"""
Evaluates and performs static Distributed Power Management (DPM) actions based on current cluster state.
This method monitors cluster resource availability and attempts to reduce the number of active nodes
when sufficient free resources are available. It ensures a minimum number of nodes remains active
and prioritizes shutting down nodes with the least utilized resources to minimize impact. Nodes selected
for shutdown are marked for maintenance and flagged for DPM shutdown.
Parameters:
proxlb_data (Dict[str, Any]): A dictionary containing metadata, cluster status, and node-level information
including resource utilization, configuration settings, and DPM thresholds.
Returns:
None: Modifies the input dictionary in-place to reflect updated cluster state and node flags.
"""
logger.debug("Starting: dpm_static.")
method = proxlb_data["meta"].get("dpm", {}).get("method", "memory")
cluster_nodes_overall = proxlb_data["cluster"]["node_count_overall"]
cluster_nodes_available = proxlb_data["cluster"]["node_count_available"]
cluster_free_resources_percent = int(proxlb_data["cluster"][f"{method}_free_percent"])
cluster_free_resources_req_min = proxlb_data["meta"].get("dpm", {}).get("cluster_min_free_resources", 0)
cluster_mind_nodes = proxlb_data["meta"].get("dpm", {}).get("cluster_min_nodes", 3)
logger.debug(f"DPM: Cluster Nodes: {cluster_nodes_overall} | Nodes available: {cluster_nodes_available} | Nodes offline: {cluster_nodes_overall - cluster_nodes_available}")
# Only proceed removing nodes if the cluster has enough resources
while cluster_free_resources_percent > cluster_free_resources_req_min:
logger.debug(f"DPM: More free resources {cluster_free_resources_percent}% available than required: {cluster_free_resources_req_min}%. DPM evaluation starting...")
# Ensure that we have at least a defined minimum of nodes left
if cluster_nodes_available > cluster_mind_nodes:
logger.debug(f"DPM: A minimum of {cluster_mind_nodes} nodes is required. {cluster_nodes_available} are available. Proceeding...")
# Get the node with the fewest used resources to keep migrations low
Calculations.get_most_free_node(proxlb_data, False)
dpm_node = proxlb_data["meta"]["balancing"]["balance_next_node"]
# Perform cluster calculation for evaluating how many nodes can safely leave
# the cluster. Further object calculations are being processed afterwards by
# the calculation class
logger.debug(f"DPM: Removing node {dpm_node} from cluster. Node will be turned off later.")
Calculations.update_cluster_resources(proxlb_data, dpm_node, "remove")
cluster_free_resources_percent = int(proxlb_data["cluster"][f"{method}_free_percent"])
logger.debug(f"DPM: Free cluster resources changed to: {int(proxlb_data['cluster'][f'{method}_free_percent'])}%.")
# Set node to maintenance and DPM shutdown
proxlb_data["nodes"][dpm_node]["maintenance"] = True
proxlb_data["nodes"][dpm_node]["dpm_shutdown"] = True
else:
logger.warning(f"DPM: A minimum of {cluster_mind_nodes} nodes is required. {cluster_nodes_available} are available. Cannot proceed!")
logger.debug(f"DPM: Not enough free resources {cluster_free_resources_percent}% available than required: {cluster_free_resources_req_min}%. DPM evaluation stopped.")
logger.debug("Finished: dpm_static.")
return proxlb_data
@staticmethod
def dpm_shutdown_nodes(proxmox_api, proxlb_data: Dict[str, Any]) -> None:
"""
Shuts down cluster nodes that are marked for maintenance and flagged for DPM shutdown.
This method iterates through the cluster nodes in the provided data and attempts to
power off any node that has both the 'maintenance' and 'dpm_shutdown' flags set.
It communicates with the Proxmox API to issue shutdown commands and logs any failures.
Parameters:
proxmox_api: An instance of the Proxmox API client used to issue node shutdown commands.
proxlb_data: A dictionary containing node status information, including flags for
maintenance and DPM shutdown readiness.
Returns:
None: Performs shutdown operations and logs outcomes; modifies no data directly.
"""
logger.debug("Starting: dpm_shutdown_nodes.")
for node, node_info in proxlb_data["nodes"].items():
if node_info["maintenance"] and node_info["dpm_shutdown"]:
logger.debug(f"DPM: Node: {node} is flagged as maintenance mode and to be powered off.")
# Ensure that the node has a valid WOL MAC defined. If not
# we would be unable to power on that system again
valid_wol_mac = DPM.dpm_validate_wol_mac(proxmox_api, node)
if valid_wol_mac:
try:
logger.debug(f"DPM: Shutting down node: {node}.")
job_id = proxmox_api.nodes(node).status.post(command="shutdown")
except proxmoxer.core.ResourceException as proxmox_api_error:
logger.critical(f"DPM: Error while powering off node {node}. Please check job-id: {job_id}")
logger.debug(f"DPM: Error while powering off node {node}. Please check job-id: {job_id}")
else:
logger.critical(f"DPM: Node {node} cannot be powered off due to missing WOL MAC. Please define a valid WOL MAC for this node.")
logger.debug("Finished: dpm_shutdown_nodes.")
@staticmethod
def dpm_startup_nodes(proxmox_api, proxlb_data: Dict[str, Any]) -> None:
"""
Starts uo cluster nodes that are marked for DPM start up.
This method iterates through the cluster nodes in the provided data and attempts to
power on any node that is not flagged as 'maintenance' but flagged as 'dpm_startup'.
It communicates with the Proxmox API to issue poweron commands and logs any failures.
Parameters:
proxmox_api: An instance of the Proxmox API client used to issue node startup commands.
proxlb_data: A dictionary containing node status information, including flags for
maintenance and DPM shutdown readiness.
Returns:
None: Performs poweron operations and logs outcomes; modifies no data directly.
"""
logger.debug("Starting: dpm_startup_nodes.")
for node, node_info in proxlb_data["nodes"].items():
if not node_info["maintenance"]:
logger.debug(f"DPM: Node: {node} is not in maintenance mode.")
if node_info["dpm_startup"]:
logger.debug(f"DPM: Node: {node} is flagged as to be started.")
try:
logger.debug(f"DPM: Powering on node: {node}.")
# Important: This requires Proxmox Operators to define the
# WOL address for each node within the Proxmox webinterface
job_id = proxmox_api.nodes().wakeonlan.post(node=node)
except proxmoxer.core.ResourceException as proxmox_api_error:
logger.critical(f"DPM: Error while powering on node {node}. Please check job-id: {job_id}")
logger.debug(f"DPM: Error while powering on node {node}. Please check job-id: {job_id}")
logger.debug("Finished: dpm_startup_nodes.")
@staticmethod
def dpm_validate_wol_mac(proxmox_api, node: Dict[str, Any]) -> str:
"""
Retrieves and validates the Wake-on-LAN (WOL) MAC address for a specified node.
This method fetches the MAC address configured for Wake-on-LAN (WOL) from the Proxmox API.
If the MAC address is found, it is logged. In case of failure to retrieve the address,
a critical log is generated indicating the absence of a WOL MAC address for the node.
Parameters:
proxmox_api: An instance of the Proxmox API client used to query node configurations.
node: The identifier (name or ID) of the node for which the WOL MAC address is to be validated.
Returns:
node_wol_mac_address: The WOL MAC address for the specified node if found, otherwise `None`.
"""
logger.debug("Starting: dpm_validate_wol_mac.")
try:
logger.debug(f"DPM: Getting WOL MAC address for node {node} from API.")
node_wol_mac_address = proxmox_api.nodes(node).config.get(property="wakeonlan")
node_wol_mac_address = node_wol_mac_address.get("wakeonlan")
logger.debug(f"DPM: Node {node} has MAC address: {node_wol_mac_address} for WOL.")
except proxmoxer.core.ResourceException as proxmox_api_error:
logger.debug(f"DPM: Failed to get WOL MAC address for node {node} from API.")
node_wol_mac_address = None
logger.critical(f"DPM: Node {node} has no MAC address defined for WOL.")
logger.debug("Finished: dpm_validate_wol_mac.")
return node_wol_mac_address

124
proxlb/models/groups.py Normal file
View File

@@ -0,0 +1,124 @@
"""
The Groups class is responsible for handling the correlations between the guests
and their groups, such as affinity and anti-affinity groups. It ensures proper balancing
by grouping guests and evaluating them for further balancing. The class provides methods
to initialize with ProxLB data and to generate groups based on guest and node data.
"""
__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 utils.helper import Helper
logger = SystemdLogger()
class Groups:
"""
The groups class is responsible for handling the correlations between the guests
and their groups like affinity and anti-affinity groups. To ensure a proper balancing
guests will ge grouped and then evaluated for further balancing.
Methods:
__init__(proxlb_data: Dict[str, Any]):
Initializes the Groups class.
get_groups(guests: Dict[str, Any], nodes: Dict[str, Any]) -> Dict[str, Any]:
Generates and returns a dictionary of affinity and anti-affinity groups
based on the provided data.
"""
def __init__(self, proxlb_data: Dict[str, Any]):
"""
Initializes the Groups class with the provided ProxLB data.
Args:
proxlb_data (Dict[str, Any]): The data required for balancing VMs and CTs.
"""
@staticmethod
def get_groups(guests: Dict[str, Any], nodes: Dict[str, Any]) -> Dict[str, Any]:
"""
Generates and returns a dictionary of affinity and anti-affinity groups based on the provided data.
Args:
guests (Dict[str, Any]): A dictionary containing the guest data.
nodes (Dict[str, Any]): A dictionary containing the nodes data.
Returns:
Dict[str, Any]: A dictionary containing the created groups that includes:
* Affinity groups (or a randon and uniq group)
* Anti-affinity groups
* A list of guests that are currently placed on a node which
is defined to be in maintenance.
"""
logger.debug("Starting: get_groups.")
groups = {'groups': {'affinity': {}, 'anti_affinity': {}, 'maintenance': []}}
for guest_name, guest_meta in guests["guests"].items():
# Create affinity grouping
# Use an affinity group if available for the guest
if len(guest_meta["affinity_groups"]) > 0:
for affinity_group in guest_meta["affinity_groups"]:
group_name = affinity_group
logger.debug(f'Affinity group {affinity_group} for {guest_name} will be used.')
else:
# Generate a random uniq group name for the guest if
# the guest does not belong to any affinity group
random_group = Helper.get_uuid_string()
group_name = random_group
logger.debug(f'Random uniq group {random_group} for {guest_name} will be used.')
if not groups["groups"]["affinity"].get(group_name, False):
# Create group template with initial guest meta information
groups["groups"]["affinity"][group_name] = {}
groups["groups"]["affinity"][group_name]["guests"] = []
groups["groups"]["affinity"][group_name]["guests"].append(guest_name)
groups["groups"]["affinity"][group_name]["counter"] = 1
# Create groups resource template by the guests resources
groups["groups"]["affinity"][group_name]["cpu_total"] = guest_meta["cpu_total"]
groups["groups"]["affinity"][group_name]["cpu_used"] = guest_meta["cpu_used"]
groups["groups"]["affinity"][group_name]["memory_total"] = guest_meta["memory_total"]
groups["groups"]["affinity"][group_name]["memory_used"] = guest_meta["cpu_used"]
groups["groups"]["affinity"][group_name]["disk_total"] = guest_meta["disk_total"]
groups["groups"]["affinity"][group_name]["disk_used"] = guest_meta["cpu_used"]
else:
# Update group templates by guest meta information
groups["groups"]["affinity"][group_name]["guests"].append(guest_name)
groups["groups"]["affinity"][group_name]["counter"] += 1
# Update group resources by guest resources
groups["groups"]["affinity"][group_name]["cpu_total"] += guest_meta["cpu_total"]
groups["groups"]["affinity"][group_name]["cpu_used"] += guest_meta["cpu_used"]
groups["groups"]["affinity"][group_name]["memory_total"] += guest_meta["memory_total"]
groups["groups"]["affinity"][group_name]["memory_used"] += guest_meta["cpu_used"]
groups["groups"]["affinity"][group_name]["disk_total"] += guest_meta["disk_total"]
groups["groups"]["affinity"][group_name]["disk_used"] += guest_meta["cpu_used"]
# Create anti-affinity grouping
if len(guest_meta["anti_affinity_groups"]) > 0:
for anti_affinity_group in guest_meta["anti_affinity_groups"]:
anti_affinity_group_name = anti_affinity_group
logger.debug(f'Anti-affinity group {anti_affinity_group_name} for {guest_name} will be used.')
if not groups["groups"]["anti_affinity"].get(anti_affinity_group_name, False):
groups["groups"]["anti_affinity"][anti_affinity_group_name] = {}
groups["groups"]["anti_affinity"][anti_affinity_group_name]["guests"] = []
groups["groups"]["anti_affinity"][anti_affinity_group_name]["guests"].append(guest_name)
groups["groups"]["anti_affinity"][anti_affinity_group_name]["counter"] = 1
groups["groups"]["anti_affinity"][anti_affinity_group_name]["used_nodes"] = []
else:
groups["groups"]["anti_affinity"][anti_affinity_group_name]["guests"].append(guest_name)
groups["groups"]["anti_affinity"][anti_affinity_group_name]["counter"] += 1
# Create grouping of guests that are currently located on nodes that are
# marked as in maintenance and must be migrated
if nodes["nodes"][guest_meta["node_current"]]["maintenance"]:
logger.debug(f'{guest_name} will be migrated to another node because the underlying node {guest_meta["node_current"]} is defined to be in maintenance.')
groups["groups"]["maintenance"].append(guest_name)
logger.debug("Finished: get_groups.")
return groups

125
proxlb/models/guests.py Normal file
View File

@@ -0,0 +1,125 @@
"""
The Guests class retrieves all running guests on the Proxmox cluster across all available nodes.
It handles both VM and CT guest types, collecting their resource metrics.
"""
__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 Guests:
"""
The Guests class retrieves all running guests on the Proxmox cluster across all available nodes.
It handles both VM and CT guest types, collecting their resource metrics.
Methods:
__init__:
Initializes the Guests class.
get_guests(proxmox_api: any, nodes: Dict[str, Any]) -> Dict[str, Any]:
Retrieves metrics for all running guests (both VMs and CTs) across all nodes in the Proxmox cluster.
It collects resource metrics such as CPU, memory, and disk usage, as well as tags and affinity/anti-affinity groups.
"""
def __init__(self):
"""
Initializes the Guests class with the provided ProxLB data.
"""
@staticmethod
def get_guests(proxmox_api: any, nodes: Dict[str, Any], meta: Dict[str, Any]) -> Dict[str, Any]:
"""
Get metrics of all guests in a Proxmox cluster.
This method retrieves metrics for all running guests (both VMs and CTs) across all nodes in the Proxmox cluster.
It iterates over each node and collects resource metrics for each running guest, including CPU, memory, and disk usage.
Additionally, it retrieves tags and affinity/anti-affinity groups for each guest.
Args:
proxmox_api (any): The Proxmox API client instance.
nodes (Dict[str, Any]): A dictionary containing information about the nodes in the Proxmox cluster.
Returns:
Dict[str, Any]: A dictionary containing metrics and information for all running guests.
"""
logger.debug("Starting: get_guests.")
guests = {"guests": {}}
# Guest objects are always only in the scope of a node.
# Therefore, we need to iterate over all nodes to get all guests.
for node in nodes['nodes'].keys():
# VM objects: Iterate over all VMs on the current node by the qemu API object.
# Unlike the nodes we need to keep them even when being ignored to create proper
# resource metrics for rebalancing to ensure that we do not overprovisiong the node.
for guest in proxmox_api.nodes(node).qemu.get():
if guest['status'] == 'running':
# If the balancing method is set to cpu, we need to wait for the guest to report
# cpu usage. This is important for the balancing process to ensure that we do not
# wait for a guest for an infinite time.
if meta["meta"]["balancing"]["method"] == "cpu":
retry_counter = 0
while guest['cpu'] == 0 and retry_counter < 10:
guest = proxmox_api.nodes(node).qemu(guest['vmid']).status.current.get()
logger.debug(f"Guest {guest['name']} (type VM) is reporting {guest['cpu']} cpu usage on retry {retry_counter}.")
retry_counter += 1
time.sleep(1)
guests['guests'][guest['name']] = {}
guests['guests'][guest['name']]['name'] = guest['name']
guests['guests'][guest['name']]['cpu_total'] = guest['cpus']
guests['guests'][guest['name']]['cpu_used'] = guest['cpu'] * guest['cpus']
guests['guests'][guest['name']]['memory_total'] = guest['maxmem']
guests['guests'][guest['name']]['memory_used'] = guest['mem']
guests['guests'][guest['name']]['disk_total'] = guest['maxdisk']
guests['guests'][guest['name']]['disk_used'] = guest['disk']
guests['guests'][guest['name']]['id'] = guest['vmid']
guests['guests'][guest['name']]['node_current'] = node
guests['guests'][guest['name']]['node_target'] = node
guests['guests'][guest['name']]['processed'] = 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']]['ignore'] = Tags.get_ignore(guests['guests'][guest['name']]['tags'])
guests['guests'][guest['name']]['node_relationship'] = Tags.get_node_relationship(guests['guests'][guest['name']]['tags'])
guests['guests'][guest['name']]['type'] = 'vm'
else:
logger.debug(f'Metric for VM {guest["name"]} ignored because VM is not running.')
# CT objects: Iterate over all VMs on the current node by the lxc API object.
# Unlike the nodes we need to keep them even when being ignored to create proper
# resource metrics for rebalancing to ensure that we do not overprovisiong the node.
for guest in proxmox_api.nodes(node).lxc.get():
if guest['status'] == 'running':
guests['guests'][guest['name']] = {}
guests['guests'][guest['name']]['name'] = guest['name']
guests['guests'][guest['name']]['cpu_total'] = guest['cpus']
guests['guests'][guest['name']]['cpu_used'] = guest['cpu']
guests['guests'][guest['name']]['memory_total'] = guest['maxmem']
guests['guests'][guest['name']]['memory_used'] = guest['mem']
guests['guests'][guest['name']]['disk_total'] = guest['maxdisk']
guests['guests'][guest['name']]['disk_used'] = guest['disk']
guests['guests'][guest['name']]['id'] = guest['vmid']
guests['guests'][guest['name']]['node_current'] = node
guests['guests'][guest['name']]['node_target'] = node
guests['guests'][guest['name']]['processed'] = 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']]['ignore'] = Tags.get_ignore(guests['guests'][guest['name']]['tags'])
guests['guests'][guest['name']]['node_relationship'] = Tags.get_node_relationship(guests['guests'][guest['name']]['tags'])
guests['guests'][guest['name']]['type'] = 'ct'
else:
logger.debug(f'Metric for CT {guest["name"]} ignored because CT is not running.')
logger.debug("Finished: get_guests.")
return guests

171
proxlb/models/nodes.py Normal file
View File

@@ -0,0 +1,171 @@
"""
The Nodes class retrieves all running nodes in a Proxmox cluster
and collects their resource metrics.
Methods:
__init__:
Initializes the Nodes class.
get_nodes(proxmox_api: any, proxlb_config: Dict[str, Any]) -> Dict[str, Any]:
Gets metrics of all nodes in a Proxmox cluster.
set_node_maintenance(proxlb_config: Dict[str, Any], node_name: str) -> Dict[str, Any]:
Sets Proxmox nodes to a maintenance mode if required.
set_node_ignore(proxlb_config: Dict[str, Any], node_name: str) -> Dict[str, Any]:
Sets Proxmox nodes to be ignored if requested.
"""
__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
logger = SystemdLogger()
class Nodes:
"""
The Nodes class retrieves all running nodes in a Proxmox cluster
and collects their resource metrics.
"""
def __init__(self):
"""
Initializes the Nodes class with the provided ProxLB data.
"""
@staticmethod
def get_nodes(proxmox_api: any, proxlb_config: Dict[str, Any]) -> Dict[str, Any]:
"""
Get metrics of all nodes in a Proxmox cluster.
This method retrieves metrics for all available nodes in the Proxmox cluster.
It iterates over each node and collects resource metrics including CPU, memory, and disk usage.
Args:
proxmox_api (any): The Proxmox API client instance.
nodes (Dict[str, Any]): A dictionary containing information about the nodes in the Proxmox cluster.
Returns:
Dict[str, Any]: A dictionary containing metrics and information for all running nodes.
"""
logger.debug("Starting: get_nodes.")
nodes = {"nodes": {}}
cluster = {"cluster": {}}
for node in proxmox_api.nodes.get():
# Ignoring a node results into ignoring all placed guests on the ignored node!
if node["status"] == "online" and not Nodes.set_node_ignore(proxlb_config, node["node"]):
nodes["nodes"][node["node"]] = {}
nodes["nodes"][node["node"]]["name"] = node["node"]
nodes["nodes"][node["node"]]["maintenance"] = False
nodes["nodes"][node["node"]]["dpm_shutdown"] = False
nodes["nodes"][node["node"]]["dpm_startup"] = False
nodes["nodes"][node["node"]]["cpu_total"] = node["maxcpu"]
nodes["nodes"][node["node"]]["cpu_assigned"] = 0
nodes["nodes"][node["node"]]["cpu_used"] = node["cpu"] * node["maxcpu"]
nodes["nodes"][node["node"]]["cpu_free"] = (node["maxcpu"]) - (node["cpu"] * node["maxcpu"])
nodes["nodes"][node["node"]]["cpu_assigned_percent"] = nodes["nodes"][node["node"]]["cpu_assigned"] / nodes["nodes"][node["node"]]["cpu_total"] * 100
nodes["nodes"][node["node"]]["cpu_free_percent"] = nodes["nodes"][node["node"]]["cpu_free"] / node["maxcpu"] * 100
nodes["nodes"][node["node"]]["cpu_used_percent"] = nodes["nodes"][node["node"]]["cpu_used"] / node["maxcpu"] * 100
nodes["nodes"][node["node"]]["memory_total"] = node["maxmem"]
nodes["nodes"][node["node"]]["memory_assigned"] = 0
nodes["nodes"][node["node"]]["memory_used"] = node["mem"]
nodes["nodes"][node["node"]]["memory_free"] = node["maxmem"] - node["mem"]
nodes["nodes"][node["node"]]["memory_assigned_percent"] = nodes["nodes"][node["node"]]["memory_assigned"] / nodes["nodes"][node["node"]]["memory_total"] * 100
nodes["nodes"][node["node"]]["memory_free_percent"] = nodes["nodes"][node["node"]]["memory_free"] / node["maxmem"] * 100
nodes["nodes"][node["node"]]["memory_used_percent"] = nodes["nodes"][node["node"]]["memory_used"] / node["maxmem"] * 100
nodes["nodes"][node["node"]]["disk_total"] = node["maxdisk"]
nodes["nodes"][node["node"]]["disk_assigned"] = 0
nodes["nodes"][node["node"]]["disk_used"] = node["disk"]
nodes["nodes"][node["node"]]["disk_free"] = node["maxdisk"] - node["disk"]
nodes["nodes"][node["node"]]["disk_assigned_percent"] = nodes["nodes"][node["node"]]["disk_assigned"] / nodes["nodes"][node["node"]]["disk_total"] * 100
nodes["nodes"][node["node"]]["disk_free_percent"] = nodes["nodes"][node["node"]]["disk_free"] / node["maxdisk"] * 100
nodes["nodes"][node["node"]]["disk_used_percent"] = nodes["nodes"][node["node"]]["disk_used"] / node["maxdisk"] * 100
# Evaluate if node should be set to maintenance mode
if Nodes.set_node_maintenance(proxlb_config, node["node"]):
nodes["nodes"][node["node"]]["maintenance"] = True
# Generate the intial cluster statistics within the same loop to avoid a further one.
logger.debug(f"Updating cluster statistics by online node {node['node']}.")
cluster["cluster"]["node_count"] = cluster["cluster"].get("node_count", 0) + 1
cluster["cluster"]["cpu_total"] = cluster["cluster"].get("cpu_total", 0) + nodes["nodes"][node["node"]]["cpu_total"]
cluster["cluster"]["cpu_used"] = cluster["cluster"].get("cpu_used", 0) + nodes["nodes"][node["node"]]["cpu_used"]
cluster["cluster"]["cpu_free"] = cluster["cluster"].get("cpu_free", 0) + nodes["nodes"][node["node"]]["cpu_free"]
cluster["cluster"]["cpu_free_percent"] = cluster["cluster"].get("cpu_free", 0) / cluster["cluster"].get("cpu_total", 0) * 100
cluster["cluster"]["cpu_used_percent"] = cluster["cluster"].get("cpu_used", 0) / cluster["cluster"].get("cpu_total", 0) * 100
cluster["cluster"]["memory_total"] = cluster["cluster"].get("memory_total", 0) + nodes["nodes"][node["node"]]["memory_total"]
cluster["cluster"]["memory_used"] = cluster["cluster"].get("memory_used", 0) + nodes["nodes"][node["node"]]["memory_used"]
cluster["cluster"]["memory_free"] = cluster["cluster"].get("memory_free", 0) + nodes["nodes"][node["node"]]["memory_free"]
cluster["cluster"]["memory_free_percent"] = cluster["cluster"].get("memory_free", 0) / cluster["cluster"].get("memory_total", 0) * 100
cluster["cluster"]["memory_used_percent"] = cluster["cluster"].get("memory_used", 0) / cluster["cluster"].get("memory_total", 0) * 100
cluster["cluster"]["disk_total"] = cluster["cluster"].get("disk_total", 0) + nodes["nodes"][node["node"]]["disk_total"]
cluster["cluster"]["disk_used"] = cluster["cluster"].get("disk_used", 0) + nodes["nodes"][node["node"]]["disk_used"]
cluster["cluster"]["disk_free"] = cluster["cluster"].get("disk_free", 0) + nodes["nodes"][node["node"]]["disk_free"]
cluster["cluster"]["disk_free_percent"] = cluster["cluster"].get("disk_free", 0) / cluster["cluster"].get("disk_total", 0) * 100
cluster["cluster"]["disk_used_percent"] = cluster["cluster"].get("disk_used", 0) / cluster["cluster"].get("disk_total", 0) * 100
cluster["cluster"]["node_count_available"] = cluster["cluster"].get("node_count_available", 0) + 1
cluster["cluster"]["node_count_overall"] = cluster["cluster"].get("node_count_overall", 0) + 1
# Update the cluster statistics by offline nodes to have the overall count of nodes in the cluster
else:
logger.debug(f"Updating cluster statistics by offline node {node['node']}.")
cluster["cluster"]["node_count_overall"] = cluster["cluster"].get("node_count_overall", 0) + 1
logger.debug("Finished: get_nodes.")
return nodes, cluster
@staticmethod
def set_node_maintenance(proxlb_config: Dict[str, Any], node_name: str) -> Dict[str, Any]:
"""
Set nodes to maintenance mode based on the provided configuration.
This method updates the nodes dictionary to mark certain nodes as being in maintenance mode
based on the configuration provided in proxlb_config.
Args:
proxlb_config (Dict[str, Any]): A dictionary containing the ProxLB configuration, including maintenance nodes.
node_name: (str): The current node name within the outer iteration.
Returns:
Bool: Returns a bool if the provided node name is present in the maintenance section of the config file.
"""
logger.debug("Starting: set_node_maintenance.")
if proxlb_config.get("proxmox_cluster", None).get("maintenance_nodes", None) is not None:
if len(proxlb_config.get("proxmox_cluster", {}).get("maintenance_nodes", [])) > 0:
if node_name in proxlb_config.get("proxmox_cluster", {}).get("maintenance_nodes", []):
logger.warning(f"Node: {node_name} has been set to maintenance mode.")
return True
logger.debug("Finished: set_node_maintenance.")
@staticmethod
def set_node_ignore(proxlb_config: Dict[str, Any], node_name: str) -> Dict[str, Any]:
"""
Set nodes to be ignored based on the provided configuration.
This method updates the nodes dictionary to mark certain nodes as being ignored
based on the configuration provided in proxlb_config.
Args:
proxlb_config (Dict[str, Any]): A dictionary containing the ProxLB configuration, including maintenance nodes.
node_name: (str): The current node name within the outer iteration.
Returns:
Bool: Returns a bool if the provided node name is present in the ignore section of the config file.
"""
logger.debug("Starting: set_node_ignore.")
if proxlb_config.get("proxmox_cluster", None).get("ignore_nodes", None) is not None:
if len(proxlb_config.get("proxmox_cluster", {}).get("ignore_nodes", [])) > 0:
if node_name in proxlb_config.get("proxmox_cluster", {}).get("ignore_nodes", []):
logger.warning(f"Node: {node_name} has been set to be ignored. Not adding node!")
return True
logger.debug("Finished: set_node_ignore.")

179
proxlb/models/tags.py Normal file
View File

@@ -0,0 +1,179 @@
"""
The Tags class retrieves and processes tags from guests of type VM or CT running
in a Proxmox cluster. It provides methods to fetch tags from the Proxmox API and
evaluate them for affinity, anti-affinity, and ignore tags, which are used during
balancing calculations.
"""
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
import time
from typing import List
from utils.logger import SystemdLogger
logger = SystemdLogger()
class Tags:
"""
The Tags class retrieves and processes tags from guests of type VM or CT running
in a Proxmox cluster. It provides methods to fetch tags from the Proxmox API and
evaluate them for affinity, anti-affinity, and ignore tags, which are used during
balancing calculations.
Methods:
__init__:
Initializes the Tags class.
get_tags_from_guests(proxmox_api: any, node: str, guest_id: int, guest_type: str) -> List[str]:
Retrieves all tags for a given guest from the Proxmox API.
get_affinity_groups(tags: List[str]) -> List[str]:
Evaluates and returns all affinity tags from the provided list of tags.
get_anti_affinity_groups(tags: List[str]) -> List[str]:
Evaluates and returns all anti-affinity tags from the provided list of tags.
get_ignore(tags: List[str]) -> bool:
Evaluates and returns a boolean indicating whether the guest should be ignored based on the provided list of tags.
"""
def __init__(self):
"""
Initializes the tags class.
"""
@staticmethod
def get_tags_from_guests(proxmox_api: any, node: str, guest_id: int, guest_type: str) -> List[str]:
"""
Get tags for a guest from the Proxmox cluster by the API.
This method retrieves all tags for a given guest from the Proxmox API which
is held in the guest_config.
Args:
proxmox_api (any): The Proxmox API client instance.
node (str): The node name where the given guest is located.
guest_id (int): The internal Proxmox ID of the guest.
guest_type (str): The type (vm or ct) of the guest.
Returns:
List: A list of all tags assoiciated with the given guest.
"""
logger.debug("Starting: get_tags_from_guests.")
time.sleep(0.1)
if guest_type == 'vm':
guest_config = proxmox_api.nodes(node).qemu(guest_id).config.get()
tags = guest_config.get("tags", [])
if guest_type == 'ct':
guest_config = proxmox_api.nodes(node).lxc(guest_id).config.get()
tags = guest_config.get("tags", [])
if isinstance(tags, str):
tags = tags.split(";")
logger.debug("Finished: get_tags_from_guests.")
return tags
@staticmethod
def get_affinity_groups(tags: List[str]) -> 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.
Args:
tags (List): A list holding all defined tags for a given guest.
Returns:
List: A list including all affinity tags for the given guest.
"""
logger.debug("Starting: get_affinity_groups.")
affinity_tags = []
if len(tags) > 0:
for tag in tags:
if tag.startswith("plb_affinity"):
affinity_tags.append(tag)
logger.debug("Finished: get_affinity_groups.")
return affinity_tags
@staticmethod
def get_anti_affinity_groups(tags: List[str]) -> 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.
Args:
tags (List): A list holding all defined tags for a given guest.
Returns:
List: A list including all anti-affinity tags for the given guest..
"""
logger.debug("Starting: get_anti_affinity_groups.")
anti_affinity_tags = []
if len(tags) > 0:
for tag in tags:
if tag.startswith("plb_anti_affinity"):
anti_affinity_tags.append(tag)
logger.debug("Finished: get_anti_affinity_groups.")
return anti_affinity_tags
@staticmethod
def get_ignore(tags: List[str]) -> bool:
"""
Validate for ignore tags of a guest from the Proxmox cluster by the API.
This method retrieves all tags for a given guest and evaluates the
ignore tag which are required during the balancing calculations.
Args:
tags (List): A list holding all defined tags for a given guest.
Returns:
Bool: Returns a bool that indicates whether to ignore a guest or not.
"""
logger.debug("Starting: get_ignore.")
ignore_tag = False
if len(tags) > 0:
for tag in tags:
if tag.startswith("plb_ignore"):
ignore_tag = True
logger.debug("Finished: get_ignore.")
return ignore_tag
@staticmethod
def get_node_relationship(tags: List[str]) -> str:
"""
Get a node relationship tag for a guest from the Proxmox cluster by the API to pin
a guest to a node.
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).
Args:
tags (List): A list holding all defined tags for a given guest.
Returns:
Str: The related hypervisor node name.
"""
logger.debug("Starting: get_node_relationship.")
node_relationship_tag = False
if len(tags) > 0:
for tag in tags:
if tag.startswith("plb_pin"):
node_relationship_tag = tag.replace("plb_pin_", "")
logger.debug("Finished: get_node_relationship.")
return node_relationship_tag

0
proxlb/utils/__init__.py Normal file
View File

View File

@@ -0,0 +1,90 @@
"""
The CliParser class handles the parsing of command-line interface (CLI) arguments.
"""
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
import argparse
import utils.version
from utils.logger import SystemdLogger
logger = SystemdLogger()
class CliParser:
"""
The CliParser class handles the parsing of command-line interface (CLI) arguments.
"""
def __init__(self):
"""
Initializes the CliParser class.
This method sets up an argument parser for the command-line interface (CLI) with various options:
- `-c` or `--config`: Specifies the path to the configuration file.
- `-d` or `--dry-run`: Performs a dry-run without executing any actions.
- `-j` or `--json`: Returns a JSON of the VM movement.
- `-b` or `--best-node`: Returns the best next node.
- `-v` or `--version`: Returns the current ProxLB version.
Logs the start and end of the initialization process.
"""
logger.debug("Starting: CliParser.")
self.parser = argparse.ArgumentParser(
description=(
f"{utils.version.__app_name__} ({utils.version.__version__}): "
f"{utils.version.__app_desc__}"
)
)
self.parser.add_argument(
"-c", "--config",
help="Path to the configuration file",
type=str,
required=False
)
self.parser.add_argument(
"-d", "--dry-run",
help="Perform a dry-run without executing any actions",
action="store_true",
required=False
)
self.parser.add_argument(
"-j", "--json",
help="Return a JSON of the VM movement",
action="store_true",
required=False
)
self.parser.add_argument(
"-b", "--best-node",
help="Returns the best next node",
action="store_true",
required=False
)
self.parser.add_argument(
"-v", "--version",
help="Returns the current ProxLB version",
action="store_true",
required=False
)
logger.debug("Finished: CliParser.")
def parse_args(self) -> argparse.Namespace:
"""
Parses and returns the parsed command-line interface (CLI) arguments.
This method uses the argparse library to parse the arguments provided
via the command line. It logs the start and end of the parsing process,
as well as the parsed arguments for debugging purposes.
Returns:
argparse.Namespace: An object containing the parsed CLI arguments.
"""
logger.debug("Starting: parse_args.")
logger.debug(self.parser.parse_args())
logger.debug("Finished: parse_args.")
return self.parser.parse_args()

View File

@@ -0,0 +1,96 @@
"""
The ConfigParser class handles the parsing of configuration file
from a given YAML file from any location.
"""
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
import os
import sys
try:
import yaml
PYYAML_PRESENT = True
except ImportError:
PYYAML_PRESENT = False
from typing import Dict, Any
from utils.logger import SystemdLogger
if not PYYAML_PRESENT:
print("Error: The required library 'pyyaml' is not installed.")
sys.exit(1)
logger = SystemdLogger()
class ConfigParser:
"""
The ConfigParser class handles the parsing of a configuration file.
Methods:
__init__(config_path: str)
test_config_path(config_path: str) -> None
Checks if the configuration file is present at the given config path.
get_config() -> Dict[str, Any]
Parses and returns the configuration data from the YAML file.
"""
def __init__(self, config_path: str):
"""
Initializes the configuration file parser and validates the config file.
"""
logger.debug("Starting: ConfigParser.")
self.config_path = self.test_config_path(config_path)
logger.debug("Finished: ConfigParser.")
def test_config_path(self, config_path: str) -> None:
"""
Checks if configuration file is present at given config path.
"""
logger.debug("Starting: test_config_path.")
# Test for config file at given location
if config_path is not None:
if os.path.exists(config_path):
logger.debug(f"The file {config_path} exists.")
else:
logger.error(f"The file {config_path} does not exist.")
sys.exit(1)
# Test for config file at default location as a fallback
if config_path is None:
default_config_path = "/etc/proxlb/proxlb.yaml"
if os.path.exists(default_config_path):
logger.debug(f"The file {default_config_path} exists.")
config_path = default_config_path
else:
print(f"The config file {default_config_path} does not exist.")
logger.critical(f"The config file {default_config_path} does not exist.")
sys.exit(1)
logger.debug("Finished: test_config_path.")
return config_path
def get_config(self) -> Dict[str, Any]:
"""
Parses and returns CLI arguments.
"""
logger.debug("Starting: get_config.")
logger.info(f"Using config path: {self.config_path}")
try:
with open(self.config_path, "r", encoding="utf-8") as config_file:
config_data = yaml.load(config_file, Loader=yaml.FullLoader)
return config_data
except yaml.YAMLError as exception_error:
print(f"Error loading YAML file: {exception_error}")
logger.critical(f"Error loading YAML file: {exception_error}")
sys.exit(1)
logger.debug("Finished: get_config.")

164
proxlb/utils/helper.py Normal file
View File

@@ -0,0 +1,164 @@
"""
The Helper class provides some basic helper functions to not mess up the code in other
classes.
"""
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
import json
import uuid
import sys
import time
import utils.version
from utils.logger import SystemdLogger
from typing import Dict, Any
logger = SystemdLogger()
class Helper:
"""
The Helper class provides some basic helper functions to not mess up the code in other
classes.
Methods:
__init__():
Initializes the general Helper class.
get_uuid_string() -> str:
Generates a random uuid and returns it as a string.
log_node_metrics(proxlb_data: Dict[str, Any], init: bool = True) -> None:
Logs the memory, CPU, and disk usage metrics of nodes in the provided proxlb_data dictionary.
get_version(print_version: bool = False) -> None:
Returns the current version of ProxLB and optionally prints it to stdout.
get_daemon_mode(proxlb_config: Dict[str, Any]) -> None:
Checks if the daemon mode is active and handles the scheduling accordingly.
"""
def __init__(self):
"""
Initializes the general Helper clas.
"""
@staticmethod
def get_uuid_string() -> str:
"""
Generates a random uuid and returns it as a string.
Args:
None
Returns:
Str: Returns a random uuid as a string.
"""
logger.debug("Starting: get_uuid_string.")
generated_uuid = uuid.uuid4()
logger.debug("Finished: get_uuid_string.")
return str(generated_uuid)
@staticmethod
def log_node_metrics(proxlb_data: Dict[str, Any], init: bool = True) -> None:
"""
Logs the memory, CPU, and disk usage metrics of nodes in the provided proxlb_data dictionary.
This method processes the usage metrics of nodes and logs them. It also updates the
'statistics' field in the 'meta' section of the proxlb_data dictionary with the
memory, CPU, and disk usage metrics before and after a certain operation.
proxlb_data (Dict[str, Any]): A dictionary containing node metrics and metadata.
init (bool): A flag indicating whether to initialize the 'before' statistics
(True) or update the 'after' statistics (False). Default is True.
"""
logger.debug("Starting: log_node_metrics.")
nodes_usage_memory = " | ".join([f"{key}: {value['memory_used_percent']:.2f}%" for key, value in proxlb_data["nodes"].items()])
nodes_usage_cpu = " | ".join([f"{key}: {value['cpu_used_percent']:.2f}%" for key, value in proxlb_data["nodes"].items()])
nodes_usage_disk = " | ".join([f"{key}: {value['disk_used_percent']:.2f}%" for key, value in proxlb_data["nodes"].items()])
if init:
proxlb_data["meta"]["statistics"] = {"before": {"memory": nodes_usage_memory, "cpu": nodes_usage_cpu, "disk": nodes_usage_disk}, "after": {"memory": "", "cpu": "", "disk": ""}}
else:
proxlb_data["meta"]["statistics"]["after"] = {"memory": nodes_usage_memory, "cpu": nodes_usage_cpu, "disk": nodes_usage_disk}
logger.debug(f"Nodes usage memory: {nodes_usage_memory}")
logger.debug(f"Nodes usage cpu: {nodes_usage_cpu}")
logger.debug(f"Nodes usage disk: {nodes_usage_disk}")
logger.debug("Finished: log_node_metrics.")
@staticmethod
def get_version(print_version: bool = False) -> None:
"""
Returns the current version of ProxLB and optionally prints it to stdout.
Parameters:
print_version (bool): If True, prints the version information to stdout and exits the program.
Returns:
None
"""
if print_version:
print(f"{utils.version.__app_name__} version: {utils.version.__version__}\n(C) 2025 by {utils.version.__author__}\n{utils.version.__url__}")
sys.exit(0)
@staticmethod
def get_daemon_mode(proxlb_config: Dict[str, Any]) -> None:
"""
Checks if the daemon mode is active and handles the scheduling accordingly.
Parameters:
proxlb_config (Dict[str, Any]): A dictionary containing the ProxLB configuration.
Returns:
None
"""
logger.debug("Starting: get_daemon_mode.")
if proxlb_config.get("service", {}).get("daemon", True):
# Validate schedule format which changed in v1.1.1
if type(proxlb_config["service"].get("schedule", None)) != dict:
logger.error("Invalid format for schedule. Please use 'hours' or 'minutes'.")
sys.exit(1)
# Convert hours to seconds
if proxlb_config["service"]["schedule"].get("format", "hours") == "hours":
sleep_seconds = proxlb_config.get("service", {}).get("schedule", {}).get("interval", 12) * 3600
# Convert minutes to seconds
elif proxlb_config["service"]["schedule"].get("format", "hours") == "minutes":
sleep_seconds = proxlb_config.get("service", {}).get("schedule", {}).get("interval", 720) * 60
else:
logger.error("Invalid format for schedule. Please use 'hours' or 'minutes'.")
sys.exit(1)
logger.info(f"Daemon mode active: Next run in: {proxlb_config.get('service', {}).get('schedule', {}).get('interval', 12)} {proxlb_config['service']['schedule'].get('format', 'hours')}.")
time.sleep(sleep_seconds)
else:
logger.debug("Successfully executed ProxLB. Daemon mode not active - stopping.")
print("Daemon mode not active - stopping.")
sys.exit(0)
logger.debug("Finished: get_daemon_mode.")
@staticmethod
def print_json(proxlb_config: Dict[str, Any], print_json: bool = False) -> None:
"""
Prints the calculated balancing matrix as a JSON output to stdout.
Parameters:
proxlb_config (Dict[str, Any]): A dictionary containing the ProxLB configuration.
Returns:
None
"""
logger.debug("Starting: print_json.")
if print_json:
# Create a filtered list by stripping the 'meta' key from the proxlb_config dictionary
# to make sure that no credentials are leaked.
filtered_data = {k: v for k, v in proxlb_config.items() if k != "meta"}
print(json.dumps(filtered_data, indent=4))
logger.debug("Finished: print_json.")

146
proxlb/utils/logger.py Normal file
View File

@@ -0,0 +1,146 @@
"""
The SystemdLogger class provides a singleton logger that integrates with systemd's journal if available.
It dynamically evaluates the environment and adjusts the logger accordingly.
"""
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
import logging
import sys
try:
from systemd.journal import JournalHandler
SYSTEMD_PRESENT = True
except ImportError:
SYSTEMD_PRESENT = False
class SystemdLogger:
"""
The SystemdLogger class provides a singleton logger that integrates with systemd's journal if available.
It dynamically evaluates the environment and adjusts the logger accordingly.
Attributes:
instance (SystemdLogger): Singleton instance of the SystemdLogger class.
Methods:
__new__(cls, name: str = "ProxLB", level: str = logging.INFO) -> 'SystemdLogger':
Creates a new instance of the SystemdLogger class or returns the existing instance.
initialize_logger(self, name: str, level: str) -> None:
Initializes the logger with the given name and log level. Adds a JournalHandler if systemd is present.
set_log_level(self, level: str) -> None:
Sets the log level for the logger and all its handlers.
debug(self, msg: str) -> str:
Logs a message with level DEBUG.
info(self, msg: str) -> str:
Logs a message with level INFO.
warning(self, msg: str) -> str:
Logs a message with level WARNING.
error(self, msg: str) -> str:
Logs a message with level ERROR.
critical(self, msg: str) -> str:
Logs a message with level CRITICAL.
"""
# Create a singleton instance variable
instance = None
def __new__(cls, name: str = "ProxLB", level: str = logging.INFO) -> 'SystemdLogger':
"""
Creating a new systemd logger class based on a given logging name
and its logging level/verbosity.
Args:
name (str): The application name that is being used for the logger.
level (str): The log level defined as a string (e.g.: INFO).
Returns:
SystemdLogger: The systemd logger object.
"""
# Check if instance already exists, otherwise create a new one
if cls.instance is None:
cls.instance = super(SystemdLogger, cls).__new__(cls)
cls.instance.initialize_logger(name, level)
return cls.instance
def initialize_logger(self, name: str, level: str) -> None:
"""
Initializing the systemd logger class based on a given logging name
and its logging level/verbosity.
Args:
name (str): The application name that is being used for the logger.
level (str): The log level defined as a string (e.g.: INFO).
"""
self.logger = logging.getLogger(name)
self.logger.setLevel(level)
# Create a logging handler depending on the
# capabilities of the underlying OS where systemd
# logging is preferred.
if SYSTEMD_PRESENT:
# Add a JournalHandler for systemd integration
handler = JournalHandler()
else:
# Add a stdout handler as a fallback
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(level)
# Set a formatter to include the logger's name and log message
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# Add handler to logger
self.logger.addHandler(handler)
def set_log_level(self, level: str) -> None:
"""
Modifies and sets the log level on the given log level.
Args:
level (str): The log level defined as a string (e.g.: INFO).
"""
self.logger.setLevel(level)
for handler in self.logger.handlers:
handler.setLevel(level)
self.logger.debug("Set to debug level")
# Handle systemd log levels
def debug(self, msg: str) -> str:
"""
Logger out for messages of type: DEBUG
"""
self.logger.debug(msg)
def info(self, msg: str) -> str:
"""
Logger out for messages of type: INFO
"""
self.logger.info(msg)
def warning(self, msg: str) -> str:
"""
Logger out for messages of type: WARNING
"""
self.logger.warning(msg)
def error(self, msg: str) -> str:
"""
Logger out for messages of type: ERROR
"""
self.logger.error(msg)
def critical(self, msg: str) -> str:
"""
Logger out for messages of type: CRITICAL
"""
self.logger.critical(msg)

425
proxlb/utils/proxmox_api.py Normal file
View File

@@ -0,0 +1,425 @@
"""
The proxmox_api class manages connections to the Proxmox API by parsing the required objects
for the authentication which can be based on username/password or API tokens.
This class provides methods to initialize the Proxmox API connection, test connectivity to
Proxmox hosts, and handle authentication using either username/password or API tokens.
It also includes functionality to distribute load across multiple Proxmox API endpoints
and manage SSL certificate validation.
"""
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
try:
import proxmoxer
PROXMOXER_PRESENT = True
except ImportError:
PROXMOXER_PRESENT = False
import random
import socket
try:
import requests
REQUESTS_PRESENT = True
except ImportError:
REQUESTS_PRESENT = False
import sys
import time
try:
import urllib3
URLLIB3_PRESENT = True
except ImportError:
URLLIB3_PRESENT = False
from typing import Dict, Any
from utils.logger import SystemdLogger
if not PROXMOXER_PRESENT:
print("Error: The required library 'proxmoxer' is not installed.")
sys.exit(1)
if not URLLIB3_PRESENT:
print("Error: The required library 'urllib3' is not installed.")
sys.exit(1)
if not REQUESTS_PRESENT:
print("Error: The required library 'requests' is not installed.")
sys.exit(1)
logger = SystemdLogger()
class ProxmoxApi:
"""
The proxmox_api class manages connections to the Proxmox API by parsing the required objects
for the authentication which can be based on username/password or API tokens.
This class provides methods to initialize the Proxmox API connection, test connectivity to
Proxmox hosts, and handle authentication using either username/password or API tokens.
It also includes functionality to distribute load across multiple Proxmox API endpoints
and manage SSL certificate validation.
Attributes:
logger (SystemdLogger): Logger instance for logging messages.
proxmox_api (proxmoxer.ProxmoxAPI): Authenticated ProxmoxAPI object.
Methods:
__init__(proxlb_config: Dict[str, Any]) -> None:
Initializes the ProxmoxApi instance with the provided configuration.
__getattr__(name):
Delegates attribute access to the proxmox_api object.
api_connect_get_hosts(proxmox_api_endpoints: list) -> str:
Determines a working Proxmox API host from a list of endpoints.
test_api_proxmox_host(host: str) -> str:
Tests connectivity to a Proxmox host by resolving its IP address.
test_api_proxmox_host_ipv4(host: str, port: int = 8006, timeout: int = 1) -> bool:
Tests reachability of a Proxmox host on its IPv4 address.
test_api_proxmox_host_ipv6(host: str, port: int = 8006, timeout: int = 1) -> bool:
Tests reachability of a Proxmox host on its IPv6 address.
api_connect(proxlb_config: Dict[str, Any]) -> proxmoxer.ProxmoxAPI:
Establishes a connection to the Proxmox API using the provided configuration.
"""
def __init__(self, proxlb_config: Dict[str, Any]) -> None:
"""
Initializes the ProxmoxApi instance with the provided configuration.
This constructor method sets up the Proxmox API connection by calling the
api_connect method with the given configuration dictionary. It logs the
initialization process for debugging purposes.
Args:
proxlb_config (Dict[str, Any]): A dictionary containing the Proxmox API configuration.
"""
logger.debug("Starting: ProxmoxApi initialization.")
self.proxmox_api = self.api_connect(proxlb_config)
self.test_api_user_permissions(self.proxmox_api)
logger.debug("Finished: ProxmoxApi initialization.")
def __getattr__(self, name):
"""
Delegate attribute access to proxmox_api to the underlying proxmoxer module.
"""
return getattr(self.proxmox_api, name)
def validate_config(self, proxlb_config: Dict[str, Any]) -> None:
"""
Validates the provided ProxLB configuration dictionary to ensure that it contains
the necessary credentials for authentication and that the credentials are not
mutually exclusive.
Args:
proxlb_config (Dict[str, Any]): A dictionary containing the ProxLB configuration.
It must include a "proxmox_api" key with a nested dictionary that contains
either "user" and "password" keys for username/password authentication or
"token_id" and "token_secret" keys for API token authentication.
Raises:
SystemExit: If both pass/token_secret and API token authentication methods are
provided, the function will log a critical error message and terminate
the program.
Logs:
Logs the start and end of the validation process. Logs a critical error if both
authentication methods are provided.
"""
logger.debug("Starting: validate_config.")
if not proxlb_config.get("proxmox_api", False):
logger.critical(f"Config error. Please check your proxmox_api chapter in your config file.")
print(f"Config error. Please check your proxmox_api chapter in your config file.")
sys.exit(1)
proxlb_credentials = proxlb_config["proxmox_api"]
present_auth_pass = "pass" in proxlb_credentials
present_auth_secret = "token_secret" in proxlb_credentials
if present_auth_pass and present_auth_secret:
logger.critical(f"Username/password and API token authentication are mutal exclusive. Please use only one!")
print(f"Username/password and API token authentication are mutal exclusive. Please use only one!")
sys.exit(1)
logger.debug("Finished: validate_config.")
def api_connect_get_hosts(self, proxlb_config, proxmox_api_endpoints: list) -> str:
"""
Perform a connectivity test to determine a working host for the Proxmox API.
This method takes a list of Proxmox API endpoints and validates their connectivity.
It returns a working host from the list. If only one endpoint is provided, it is
returned immediately. If multiple endpoints are provided, each one is tested for
connectivity. If a valid host is found, it is returned. If multiple valid hosts
are found, one is chosen at random to distribute the load across the cluster.
Args:
proxlb_config (Dict[str, Any]): A dictionary containing the ProxLB configuration.
proxmox_api_endpoints (list): A list of Proxmox API endpoints to test.
Returns:
str: A working Proxmox API host.
Raises:
SystemExit: If the provided endpoints are not a list, if the list is empty,
or if no valid hosts are found.
"""
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.")
sys.exit(1)
if not proxmox_api_endpoints:
logger.critical(f"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)
# 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:
# 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 = self.test_api_proxmox_host(host)
if validated:
validated_api_hosts.append(validated)
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)
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)
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:
"""
Tests the connectivity to a Proxmox host by resolving its IP address and
checking both IPv4 and IPv6 addresses.
This function attempts to resolve the given hostname to its IP addresses
(both IPv4 and IPv6). It then tests the connectivity to the Proxmox API
using the resolved IP addresses. If the host is reachable via either
IPv4 or IPv6, the function returns the hostname. If the host is not
reachable, the function returns False.
Args:
host (str): The hostname of the Proxmox server to test.
Returns:
str: The hostname if the Proxmox server is reachable.
bool: False if the Proxmox server is not reachable.
"""
logger.debug("Starting: test_api_proxmox_host.")
# Try resolving DNS to IP and log non-resolvable ones
try:
ip = socket.getaddrinfo(host, None, socket.AF_UNSPEC)
except socket.gaierror:
logger.warning(f"Could not resolve {host}.")
return False
# 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):
return host
elif address_type[0] == socket.AF_INET6:
logger.debug(f"{host} is type ipv6.")
if self.test_api_proxmox_host_ipv6(host):
return host
else:
return False
logger.debug("Finished: test_api_proxmox_host.")
def test_api_proxmox_host_ipv4(self, host: str, port: int = 8006, timeout: int = 1) -> bool:
"""
Test the reachability of a Proxmox host on its IPv4 management address.
This method attempts to establish a TCP connection to the specified host and port
within a given timeout period. It logs the process and results, indicating whether
the host is reachable or not.
Args:
host (str): The IPv4 address or hostname of the Proxmox host to test.
port (int, optional): The TCP port to connect to on the host. Defaults to 8006.
timeout (int, optional): The timeout duration in seconds for the connection attempt. Defaults to 1.
Returns:
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()
logger.debug(f"Host {host} is reachable on IPv4 for tcp/{port}.")
return True
sock.close()
logger.warning(f"Host {host} is unreachable on IPv4 for tcp/{port}.")
logger.debug("Finished: test_api_proxmox_host_ipv4.")
return False
def test_api_proxmox_host_ipv6(self, host: str, port: int = 8006, timeout: int = 1) -> bool:
"""
Test the reachability of a Proxmox host on its IPv6 management address.
This method attempts to establish a TCP connection to the specified host and port
within a given timeout period. It logs the process and results, indicating whether
the host is reachable or not.
Args:
host (str): The IPv6 address or hostname of the Proxmox host to test.
port (int, optional): The TCP port to connect to on the host. Defaults to 8006.
timeout (int, optional): The timeout duration in seconds for the connection attempt. Defaults to 1.
Returns:
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()
logger.debug(f"Host {host} is reachable on IPv6 for tcp/{port}.")
return True
sock.close()
logger.warning(f"Host {host} is unreachable on IPv6 for tcp/{port}.")
logger.debug("Finished: test_api_proxmox_host_ipv6.")
return False
def test_api_user_permissions(self, proxmox_api: any):
"""
Test the permissions of the current user/token used for the Proxmox API.
This method gets all assigned permissions for all API paths for the current
used user/token and validates them against the minimum required permissions.
Args:
proxmox_api (any): The Proxmox API client instance.
"""
logger.debug("Starting: test_api_user_permissions.")
permissions_required = ["Datastore.Audit", "Sys.Audit", "VM.Audit", "VM.Migrate"]
permissions_available = []
# Get the permissions for the current user/token from API
permissions = proxmox_api.access.permissions.get()
# Get all available permissions of the current user/token
for path, permission in permissions.items():
for permission in permissions[path]:
permissions_available.append(permission)
# Validate if all required permissions are included within the available permissions
for required_permission in permissions_required:
if required_permission not in permissions_available:
logger.critical(f"Permission '{required_permission}' is missing. Please adjust the permissions for your user/token. See also: https://github.com/gyptazy/ProxLB/blob/main/docs/03_configuration.md#required-permissions-for-a-user")
sys.exit(1)
logger.debug("Finished: test_api_user_permissions.")
def api_connect(self, proxlb_config: Dict[str, Any]) -> proxmoxer.ProxmoxAPI:
"""
Establishes a connection to the Proxmox API using the provided configuration.
This function retrieves the Proxmox API endpoint from the configuration, optionally disables SSL certificate
validation warnings, and attempts to authenticate and create a ProxmoxAPI object. It handles various exceptions
related to authentication, connection timeouts, SSL errors, and connection refusals, logging appropriate error
messages and exiting the program if necessary.
Args:
proxlb_config (Dict[str, Any]): A dictionary containing the Proxmox API configuration. Expected keys include:
- "proxmox_api": A dictionary with the following keys:
- "hosts" (List[str]): A list of Proxmox API host addresses.
- "user" (str): The username for Proxmox API authentication.
- "pass" (str): The password for Proxmox API authentication.
- "ssl_verification" (bool): Whether to verify SSL certificates (default is True).
- "timeout" (int): The timeout duration for API requests.
Returns:
proxmoxer.ProxmoxAPI: An authenticated ProxmoxAPI object.
Raises:
proxmoxer.backends.https.AuthenticationError: If authentication fails.
requests.exceptions.ConnectTimeout: If the connection to the Proxmox API times out.
requests.exceptions.SSLError: If SSL certificate validation fails.
requests.exceptions.ConnectionError: If the connection to the Proxmox API is refused.
"""
logger.debug("Starting: api_connect.")
# Validate config
self.validate_config(proxlb_config)
# Get a valid Proxmox API endpoint
proxmox_api_endpoint = self.api_connect_get_hosts(proxlb_config, proxlb_config.get("proxmox_api", {}).get("hosts", []))
# Disable warnings for SSL certificate validation
if not proxlb_config.get("proxmox_api").get("ssl_verification", True):
logger.warning(f"SSL certificate validation to host {proxmox_api_endpoint} is deactivated.")
urllib3.disable_warnings(category=urllib3.exceptions.InsecureRequestWarning)
requests.packages.urllib3.disable_warnings()
# Login into Proxmox API and create API object
try:
if proxlb_config.get("proxmox_api").get("token_secret", False):
proxmox_api = proxmoxer.ProxmoxAPI(
proxmox_api_endpoint,
user=proxlb_config.get("proxmox_api").get("user", True),
token_name=proxlb_config.get("proxmox_api").get("token_id", True),
token_value=proxlb_config.get("proxmox_api").get("token_secret", True),
verify_ssl=proxlb_config.get("proxmox_api").get("ssl_verification", True),
timeout=proxlb_config.get("proxmox_api").get("timeout", True))
logger.debug("Using API token authentication.")
else:
proxmox_api = proxmoxer.ProxmoxAPI(
proxmox_api_endpoint,
user=proxlb_config.get("proxmox_api").get("user", True),
password=proxlb_config.get("proxmox_api").get("pass", True),
verify_ssl=proxlb_config.get("proxmox_api").get("ssl_verification", True),
timeout=proxlb_config.get("proxmox_api").get("timeout", True))
logger.debug("Using username/password authentication.")
except proxmoxer.backends.https.AuthenticationError as proxmox_api_error:
logger.critical(f"Authentication failed. Please check the defined credentials: {proxmox_api_error}")
sys.exit(2)
except requests.exceptions.ConnectTimeout:
logger.critical(f"Connection timeout to host {proxmox_api_endpoint}")
sys.exit(2)
except requests.exceptions.SSLError as proxmox_api_error:
logger.critical(f"SSL certificate validation failed: {proxmox_api_error}")
sys.exit(2)
except requests.exceptions.ConnectionError:
logger.critical(f"Connection refused by host {proxmox_api_endpoint}")
sys.exit(2)
logger.info(f"API connection to host {proxmox_api_endpoint} succeeded.")
logger.debug("Finished: api_connect.")
return proxmox_api

7
proxlb/utils/version.py Normal file
View File

@@ -0,0 +1,7 @@
__app_name__ = "ProxLB"
__app_desc__ = "A DRS alike loadbalancer for Proxmox clusters."
__author__ = "Florian Paul Azim Hoberg <gyptazy>"
__copyright__ = "Copyright (C) 2025 Florian Paul Azim Hoberg (@gyptazy)"
__license__ = "GPL-3.0"
__version__ = "1.1.2b"
__url__ = "https://github.com/gyptazy/ProxLB"

View File

@@ -1,5 +1,4 @@
argparse
configparser
proxmoxer
requests
urllib3
urllib3
PyYAML

11
service/proxlb.service Normal file
View File

@@ -0,0 +1,11 @@
[Unit]
Description=ProxLB - A loadbalancer for Proxmox clusters
After=network-online.target pveproxy.service
Wants=network-online.target pveproxy.service
[Service]
ExecStart=python3 /usr/lib/python3/dist-packages/proxlb/main.py -c /etc/proxlb/proxlb.yaml
User=plb
[Install]
WantedBy=multi-user.target

2
setup.cfg Normal file
View File

@@ -0,0 +1,2 @@
[pycodestyle]
ignore = E501, W503

21
setup.py Normal file
View File

@@ -0,0 +1,21 @@
from setuptools import setup
setup(
name="proxlb",
version="1.1.2b",
description="A DRS alike loadbalancer for Proxmox clusters.",
long_description="An advanced DRS alike loadbalancer for Proxmox clusters that also supports maintenance modes and affinity/anti-affinity rules.",
author="Florian Paul Azim Hoberg",
author_email="gyptazy@gyptazy.com",
maintainer="Florian Paul Azim Hoberg",
maintainer_email="gyptazy@gyptazy.com",
url="https://github.com/gyptazy/ProxLB",
packages=["proxlb", "proxlb.utils", "proxlb.models"],
install_requires=[
"requests",
"urllib3",
"proxmoxer",
"pyyaml",
],
data_files=[('/etc/systemd/system', ['service/proxlb.service']), ('/etc/proxlb/', ['config/proxlb_example.yaml'])],
)

View File

@@ -1 +0,0 @@
## Unit Tests

Some files were not shown because too many files have changed in this diff Show More