Skip to content

Instantly share code, notes, and snippets.

@mbentley
Last active March 13, 2018 21:13
Show Gist options
  • Save mbentley/fb60d49338b193c7b2569fd4d558ff6e to your computer and use it in GitHub Desktop.
Save mbentley/fb60d49338b193c7b2569fd4d558ff6e to your computer and use it in GitHub Desktop.
Swarm mode - Find NanoCPUs Reserved from a cluster or a single node
#!/bin/bash
# uses some poor techniques like awk + grep so it works on 17.03 and newer where formatting isn't available
# get a list of all of the nodes; loop through them
for NODE in $(docker node ls -q)
do
# reset the per node CPU resevation counter to 0
CPURES="0"
# find each task on a given node and loop through them
for TASK in $(docker node ps -f desired-state=running "${NODE}" | awk '{print $1}' | grep -v ^ID)
do
# find out the NanoCPUs
NANOCPUS="$(docker inspect --format '{{.Spec.Resources.Reservations.NanoCPUs}}' "${TASK}" 2>/dev/null || echo 0)"
# total the NanoCPUs from this task to the rest
CPURES="$((CPURES + NANOCPUS))"
done
# check to see if bc exists; use it if it does; fall back to just reporting NanoCPUs otherwise
if hash bc 2>/dev/null; then
# output # CPUs reserved
echo -n "${NODE} CPUs reserved: "
bc -l <<< "scale=2;${CPURES} / 1000000000"
else
# output # NanoCPUs reserved
echo "${NODE} NanoCPUs reserved: ${CPURES}"
fi
done
#!/bin/bash
# Finds NanoCPUs reserved for a single node in the cluster
NODE="demo02" ;\
CPURES="0" ;\
echo "" ;\
docker node ps -f desired-state=running ${NODE} ;\
for TASK in $(docker node ps -f desired-state=running ${NODE} | awk '{print $1}' | grep -v ^ID); do
echo "${TASK}"
NANOCPUS="$(docker inspect --format '{{.Spec.Resources.Reservations.NanoCPUs}}' ${TASK} 2>/dev/null || echo 0)"
CPURES="$(echo $((CPURES + NANOCPUS)))"
echo ${NANOCPUS}
echo ""
done ;\
if hash bc 2>/dev/null; then
echo -n "CPUs: "
bc -l <<< "scale=2;${CPURES} / 1000000000"
else
echo "NanoCPUs: ${CPURES}"
echo " (Divide by 1000000000 to get CPUs)"
fi
#!/bin/bash
docker service create --name cputest --reserve-cpu .5 --limit-cpu .5 --reserve-memory 128M --limit-memory 128M alpine:latest ping 127.0.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment