Last active
August 13, 2023 10:53
-
-
Save boriel/724227bccab73f2da4ec to your computer and use it in GitHub Desktop.
Simple proxmox container scaling (Elastic cloud project)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import datetime | |
import random | |
LEN = 5 | |
PREFIX = 'ct-dm-core-job-' | |
DIGITS = ''.join(str(x) for x in range(10)) | |
LETTERS = ''.join(chr(x) for x in range(ord('a'), ord('z') + 1)) | |
ALPHA = LETTERS + DIGITS | |
def get_rnd_hostname(): | |
random.seed(datetime.datetime.now()) | |
return PREFIX + ''.join(random.choice(ALPHA) for x in range(LEN)) | |
if __name__ == '__main__': | |
print(get_rnd_hostname()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
MAX=$1 | |
MAX=$(($MAX+0)) | |
i=0 | |
while [ $i -lt $MAX ] | |
do | |
LAST=$(pct list|egrep -Ee '^4[0-9][0-9]'|sort|tail -1|sed -e 's/ .*$//') # Get last machine number 4xx | |
if [[ "$LAST" == "" ]]; then | |
echo "No containers in range 400-499 found. Nothing to do." > /dev/stderr | |
exit 1 | |
fi | |
echo Removing LXC worker $LAST | |
pct shutdown $LAST -forceStop | |
sleep 2 | |
pct destroy $LAST | |
i=$(($i+1)) | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
TEMPLATE=/var/lib/vz/dump/vzdump-lxc-400-2015_12_16-17_02_30.tar.lzo | |
STORAGE=pool0 | |
GATEWAY=10.105.234.129 | |
POOL=ceba | |
IFACE=eth0 | |
LAST=$(pct list|egrep -Ee '^4[0-9][0-9]'|sort|tail -1|sed -e 's/ .*$//') # Get last machine number 4xx | |
if [[ "$LAST" == "" ]]; then | |
LAST=399 # Minimum VMID | |
fi | |
MAX=$1 | |
MAX=$(($MAX+1)) | |
i=1 | |
NEW=$LAST | |
while [ $i -lt $MAX ] | |
do | |
NEW=$(($NEW+1)) | |
echo Deploying new LXC container VMID $NEW | |
pct restore $NEW $TEMPLATE -storage=$STORAGE -pool=$POOL | |
#pct create $NEW $TEMPLATE -storage=$STORAGE -pool=$POOL # This will regenerate SSH keys | |
pct set $NEW -hostname $(./rndhostname) | |
pct set $NEW -net0 name=$IFACE,bridge=vmbr0 | |
pct start $NEW | |
sleep 2 | |
pct exec $NEW -- /usr/local/bin/waitforiface $IFACE >/dev/null | |
sleep 2 | |
pct exec $NEW -- /bin/ping -c2 -q $GATEWAY >/dev/null | |
i=$(($i+1)) | |
done | |
exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-Use ./scale-up <number_of_containers> to scale up the cluster adding N new containers.
-Use ./scale-down <number_of_containers> to shutdown and destroy N containers.
-The rndhostname.py is a script to generate a random hostname with a prefix avoiding name-repetitions.
The waitforiface is a bash script that waits until the eth0 interface is up.