Skip to content

Commit 4b7ca58

Browse files
committed
Merge branch 'main' into payments-contracts-integration
2 parents cbd98e2 + 9fa0f03 commit 4b7ca58

File tree

245 files changed

+9257
-2093
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

245 files changed

+9257
-2093
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
* @Ahmad45123
22
/shared/ @Ahmad45123
3+
/webserver/ @ShimaaBetah
34
/services/jobs @Ahmad45123 @ziadsadek999 @ShimaaBetah @abd0123
45
/services/users @Pandemic1617 @MahmoudHosamGaber @Akram-Fahim
56
/services/payments @mokhallid80 @Abdulaziz-Hassan @AhmedNasserG @Abdullah204

.github/workflows/maven-publish.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,19 @@ jobs:
5757
run: |
5858
docker build ./services/contracts --tag ahmad45123/workup:service_contracts
5959
docker push ahmad45123/workup:service_contracts
60+
61+
- name: Build and push Webserver Image
62+
run: |
63+
docker build ./webserver --tag ahmad45123/workup:webserver
64+
docker push ahmad45123/workup:webserver
65+
66+
- name: Build and push Autoscaler Image
67+
run: |
68+
docker build ./autoscaler/docker-swarm-autoscaler --tag ahmad45123/workup:autoscaler
69+
docker push ahmad45123/workup:autoscaler
70+
71+
- name: Build and push Mediaserver Image
72+
run: |
73+
docker build ./mediaserver --tag ahmad45123/workup:mediaserver
74+
docker push ahmad45123/workup:mediaserver
75+

.github/workflows/maven.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,14 @@ jobs:
3232
- name: Check Formatting
3333
run: mvn -DskipTests verify
3434

35-
- name: Check Tests
35+
- name: Check Jobs Tests
3636
run: cd services/jobs && mvn test
3737

38+
- name: Check Users Tests
39+
run: cd services/users && mvn test
40+
41+
- name: Check Payments Tests
42+
run: cd services/payments && mvn test
43+
3844
- name: Submit Dependency Snapshot
3945
uses: advanced-security/maven-dependency-submission-action@v3

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ build/
3131

3232
### VS Code ###
3333
.vscode/
34+
35+
### Logs ###
36+
logs/

.idea/compiler.xml

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
build:
44
mvn -DskipTests package
5-
docker compose build
5+
docker build ./services/jobs --tag workup:service_jobs
6+
docker build ./services/payments --tag workup:service_payments
7+
docker build ./services/users --tag workup:service_users
8+
docker build ./services/contracts --tag workup:service_contracts
9+
docker build ./webserver --tag workup:webserver
10+
docker build ./mediaserver --tag workup:mediaserver
611

712
up:
8-
docker compose up --force-recreate
13+
docker stack deploy -c compose.yaml -c compose.override.yaml workup
14+
15+
portainer:
16+
docker stack deploy -c portainer-agent-stack.yml portainer
917

1018
reload:
1119
mvn -DskipTests package
12-
docker compose up --detach --build
20+
docker compose up --detach --build
21+
22+
format:
23+
mvn git-code-format:format-code -Dgcf.globPattern=**/*

autoscaler/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# docker-swarm-autoscaler
2+
3+
## Current Release: 0.1.0
4+
5+
This project is intended to bring auto service staling to Docker Swarm. This script uses prometheus paired with cadvisor metrics to determine cpu usage. It then uses a manager node to determine if a service wants to be autoscaled and uses a manager node to scale the service.
6+
7+
Currently the project only uses cpu to autoscale. If cpu usage reaches 85% the service will scale up, if it reaches 25% it will scale down.
8+
9+
## Usage
10+
1. You can deploy prometheus, cadvisor, and docker-swarm-autoscaler by running `docker stack deploy -c swarm-autoscaler-stack.yml autoscaler` from the root of this repo.
11+
* You can also utilize an already deploy prometheus and cadvisor by specifying the `PROMETHEUS_URL` in docker-swarm-autoscaler environment. `swarm-autoscaler-stack.yml` shows an example of this.
12+
* docker-swarm-autoscale needs a placement contstraint to deploy to a manager. `swarm-autoscaler-stack.yml` shows an example of this.
13+
2. For services you want to autoscale you will need a deploy label `swarm.autoscaler=true`.
14+
15+
```
16+
deploy:
17+
labels:
18+
- "swarm.autoscaler=true"
19+
```
20+
21+
This is best paired with resource constraints limits. This is also under the deploy key.
22+
23+
```
24+
deploy:
25+
resources:
26+
reservations:
27+
cpus: '0.25'
28+
memory: 512M
29+
limits:
30+
cpus: '0.50'
31+
```
32+
33+
## Configuration
34+
| Setting | Value | Description |
35+
| --- | --- | --- |
36+
| `swarm.autoscaler` | `true` | Required. This enables autoscaling for a service. Anything other than `true` will not enable it |
37+
| `swarm.autoscaler.minimum` | Integer | Optional. This is the minimum number of replicas wanted for a service. The autoscaler will not downscale below this number |
38+
| `swarm.autoscaler.maximum` | Integer | Optional. This is the maximum number of replicas wanted for a service. The autoscaler will not scale up past this number |
39+
40+
## Test
41+
You can deploy a test app with the following commands below. Helloworld is initially only 1 replica. The autoscaler will scale to the minimum 3 replicas.
42+
1. `docker stack deploy -c swarm-autoscaler-stack.yml autoscaler`
43+
2. `docker stack deploy -c helloworld.yml hello`
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM ubuntu:xenial
2+
3+
RUN apt-get update -qq \
4+
&& apt-get install -y -qq \
5+
jq \
6+
apt-transport-https \
7+
ca-certificates \
8+
curl \
9+
software-properties-common \
10+
dnsutils \
11+
&& curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - \
12+
&& add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable" \
13+
&& apt-get update -qq \
14+
&& apt-get install -y -qq \
15+
docker-ce=5:19.03.5* \
16+
&& apt-get -qq clean \
17+
&& apt-get autoremove -y \
18+
&& rm -rf \
19+
/var/lib/apt/lists/* \
20+
/tmp/* \
21+
/var/tmp/*
22+
23+
COPY auto-scale.sh /auto-scale.sh
24+
RUN chmod a+x /auto-scale.sh
25+
26+
CMD ["/auto-scale.sh"]
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/bash
2+
3+
LOOP=${LOOP:='yes'}
4+
CPU_PERCENTAGE_UPPER_LIMIT=60
5+
CPU_PERCENTAGE_LOWER_LIMIT=25
6+
PROMETHEUS_API="api/v1/query?query="
7+
PROMETHEUS_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"
8+
9+
get_high_cpu_services () {
10+
local prometheus_results="${1}"
11+
local services=""
12+
for service in $(printf "%s$prometheus_results" | jq ".data.result[] | select( all(.value[1]|tonumber; . > $CPU_PERCENTAGE_UPPER_LIMIT) ) | .metric.container_label_com_docker_swarm_service_name" | sed 's/"//g' | sort | uniq); do
13+
services="$services $service"
14+
done
15+
echo $services
16+
}
17+
18+
get_all_services () {
19+
local prometheus_results="${1}"
20+
local services=""
21+
for service in $(printf "%s$prometheus_results" | jq ".data.result[].metric.container_label_com_docker_swarm_service_name" | sed 's/"//g' | sort | uniq); do
22+
services="$services $service"
23+
done
24+
}
25+
26+
get_low_cpu_services () {
27+
local prometheus_results="${1}"
28+
local services=""
29+
for service in $(printf "%s$prometheus_results" | jq ".data.result[] | select( all(.value[1]|tonumber; . < $CPU_PERCENTAGE_LOWER_LIMIT) ) | .metric.container_label_com_docker_swarm_service_name" | sed 's/"//g' | sort | uniq); do
30+
services="$services $service"
31+
done
32+
33+
echo $services
34+
}
35+
36+
default_scale () {
37+
service_name=$1
38+
auto_scale_label=$(docker service inspect $service_name | jq '.[].Spec.Labels["swarm.autoscaler"]')
39+
replica_minimum=$(docker service inspect $service_name | jq '.[].Spec.Labels["swarm.autoscaler.minimum"]' | sed 's/\"//g')
40+
replica_maximum=$(docker service inspect $service_name | jq '.[].Spec.Labels["swarm.autoscaler.maximum"]' | sed 's/\"//g')
41+
if [[ "${auto_scale_label}" == "\"true\"" ]]; then
42+
echo Service $service has an autoscale label.
43+
current_replicas=$(docker service inspect $service_name | jq ".[].Spec.Mode.Replicated | .Replicas")
44+
if [[ $replica_minimum -gt $current_replicas ]]; then
45+
echo Service $service_name is below the minimum. Scaling to the minimum of $replica_minimum
46+
docker service scale $service_name=$replica_minimum
47+
elif [[ $current_replicas -gt $replica_maximum ]]; then
48+
echo Service $service_name is above the maximum. Scaling to the maximum of $replica_maximum
49+
docker service scale $service_name=$replica_maximum
50+
fi
51+
else
52+
echo Service $service does not have an autoscale label.
53+
fi
54+
55+
}
56+
57+
scale_down () {
58+
service_name=$1
59+
auto_scale_label=$(docker service inspect $service_name | jq '.[].Spec.Labels["swarm.autoscaler"]')
60+
replica_minimum=$(docker service inspect $service_name | jq '.[].Spec.Labels["swarm.autoscaler.minimum"]' | sed 's/\"//g')
61+
if [[ "${auto_scale_label}" == "\"true\"" ]]; then
62+
current_replicas=$(docker service inspect $service_name | jq ".[].Spec.Mode.Replicated | .Replicas")
63+
new_replicas=$(expr $current_replicas - 1)
64+
if [[ $replica_minimum -le $new_replicas ]]; then
65+
echo Scaling down the service $service_name to $new_replicas
66+
docker service scale $service_name=$new_replicas
67+
elif [[ $current_replicas -eq $replica_minimum ]]; then
68+
echo Service $service_name has the minumum number of replicas.
69+
fi
70+
fi
71+
72+
}
73+
74+
scale_up () {
75+
service_name=$1
76+
auto_scale_label=$(docker service inspect $service_name | jq '.[].Spec.Labels["swarm.autoscaler"]')
77+
replica_maximum=$(docker service inspect $service_name | jq '.[].Spec.Labels["swarm.autoscaler.maximum"]' | sed 's/\"//g')
78+
if [[ "${auto_scale_label}" == "\"true\"" ]]; then
79+
current_replicas=$(docker service inspect $service_name | jq ".[].Spec.Mode.Replicated | .Replicas")
80+
new_replicas=$(expr $current_replicas + 1)
81+
if [[ $current_replicas -eq $replica_maximum ]]; then
82+
echo Service $service already has the maximum of $replica_maximum replicas
83+
elif [[ $replica_maximum -ge $new_replicas ]]; then
84+
echo Scaling up the service $service_name to $new_replicas
85+
docker service scale $service_name=$new_replicas
86+
fi
87+
fi
88+
}
89+
90+
main () {
91+
prometheus_initial_results=$(curl --silent "${PROMETHEUS_URL}/${PROMETHEUS_API}${PROMETHEUS_QUERY}" | jq .)
92+
echo Prometheus results
93+
echo $prometheus_initial_results
94+
for service in $(get_all_services "${prometheus_initial_results}"); do
95+
default_scale $service
96+
done
97+
echo Checking for high cpu services
98+
for service in $(get_high_cpu_services "${prometheus_initial_results}"); do
99+
echo Service $service is above $CPU_PERCENTAGE_UPPER_LIMIT percent cpu usage.
100+
scale_up $service
101+
done
102+
echo Checking for low cpu services
103+
for service in $(get_low_cpu_services "${prometheus_initial_results}"); do
104+
echo Service $service is below $CPU_PERCENTAGE_LOWER_LIMIT percent cpu usage.
105+
scale_down $service
106+
done
107+
}
108+
109+
main
110+
while [[ $LOOP == 'yes' ]]; do
111+
echo Waiting 5 seconds for the next test
112+
sleep 5s
113+
main
114+
done

autoscaler/prometheus.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
global:
2+
scrape_interval: 5s
3+
evaluation_interval: 5s
4+
5+
scrape_configs:
6+
- job_name: 'prometheus'
7+
dns_sd_configs:
8+
- names:
9+
- 'tasks.prometheus'
10+
type: 'A'
11+
port: 9090
12+
13+
- job_name: 'cadvisor'
14+
dns_sd_configs:
15+
- names:
16+
- 'tasks.cadvisor'
17+
type: 'A'
18+
port: 8080

0 commit comments

Comments
 (0)