1
0
mirror of https://github.com/jcwimer/docker-swarm-autoscaler synced 2026-05-21 03:49:24 +00:00

Initial commit

This commit is contained in:
2018-10-29 07:10:06 -04:00
commit cabc6749a5
7 changed files with 184 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
FROM ubuntu:xenial
RUN apt-get update -qq \
&& apt-get install -y -qq \
jq \
apt-transport-https \
ca-certificates \
curl \
software-properties-common \
&& curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - \
&& add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable" \
&& apt-get update -qq \
&& apt-get install -y -qq \
docker-ce=18.03.0~ce-0~ubuntu \
&& apt-get -qq clean \
&& apt-get autoremove -y \
&& rm -rf \
/var/lib/apt/lists/* \
/tmp/* \
/var/tmp/*
COPY auto-scale.sh /auto-scale.sh
RUN chmod a+x /auto-scale.sh
ENTRYPOINT ["/bin/bash"]
CMD ["/auto-scale.sh"]

View File

@@ -0,0 +1,33 @@
CPU_PERCENTAGE_UPPER_LIMIT=85
CPU_PERCENTAGE_LOWER_LIMIT=25
while ls > /dev/null; do
#scale up
for service in $(curl --silent "${PROMETHEUS_URL}/api/v1/query?query=sum(rate(container_cpu_usage_seconds_total%7Bcontainer_label_com_docker_swarm_task_name%3D~%27.%2B%27%7D%5B5m%5D))BY(container_label_com_docker_swarm_service_name%2Cinstance)*100>${CPU_PERCENTAGE_UPPER_LIMIT}&g0.tab=1" | jq ".data.result[].metric | .container_label_com_docker_swarm_service_name" | sort | uniq); do
service_name=$(echo $service | sed 's/\"//g')
auto_scale_label=$(docker service inspect $service_name | jq '.[].Spec.Labels["cpu.autoscale"]')
replica_maximum=$(docker service inspect $service_name | jq '.[].Spec.Labels["cpu.autoscale.maximum"]' | sed 's/\"//g')
if [[ "${auto_scale_label}" == "\"true\"" ]]; then
current_replicas=$(docker service inspect $service_name | jq ".[].Spec.Mode.Replicated | .Replicas")
new_replicas=$(expr $current_replicas + 1)
if [[ $replica_maximum -ge $new_replicas ]]; then
echo scale up $service_name to $new_replicas
docker service scale $service_name=$new_replicas
fi
fi
done
#scale down
for service in $(curl --silent "${PROMETHEUS_URL}/api/v1/query?query=sum(rate(container_cpu_usage_seconds_total%7Bcontainer_label_com_docker_swarm_task_name%3D~%27.%2B%27%7D%5B5m%5D))BY(container_label_com_docker_swarm_service_name%2Cinstance)*100<${CPU_PERCENTAGE_LOWER_LIMIT}&g0.tab=1" | jq ".data.result[].metric | .container_label_com_docker_swarm_service_name" | sort | uniq); do
service_name=$(echo $service | sed 's/\"//g')
auto_scale_label=$(docker service inspect $service_name | jq '.[].Spec.Labels["cpu.autoscale"]')
replica_minimum=$(docker service inspect $service_name | jq '.[].Spec.Labels["cpu.autoscale.minimum"]' | sed 's/\"//g')
if [[ "${auto_scale_label}" == "\"true\"" ]]; then
current_replicas=$(docker service inspect $service_name | jq ".[].Spec.Mode.Replicated | .Replicas")
new_replicas=$(expr $current_replicas - 1)
if [[ $replica_minimum -le $new_replicas ]]; then
echo scale down $service_name to $new_replicas
docker service scale $service_name=$new_replicas
fi
fi
done
done