-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmemory-based-autoscaling.sh
225 lines (199 loc) · 7.64 KB
/
memory-based-autoscaling.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
heapmem=0
podmem=0
TODAY=`date +%F`
KUBECTL=/usr/local/bin/kubectl
SCRIPT_HOME=/var/log/kube-deploy
if [ ! -d $SCRIPT_HOME ]; then
mkdir -p $SCRIPT_HOME
fi
#LOG_FILE=$SCRIPT_HOME/kube-$TODAY.log
#touch $LOG_FILE
RED='\033[01;31m'
YELLOW='\033[0;33m'
NONE='\033[00m'
print_help(){
echo -e "${YELLOW}Use the following Command:"
echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo -e "${RED}./<script-name> --action <action-name> --deployment <deployment-name> --scaleup <scaleupthreshold> --scaledown <scaledownthreshold>"
echo -e "${YELLOW}+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
printf "Choose one of the available actions below:\n"
printf " get-heapmemory\n get-podmemory\n deploy-heap-autoscaling\n deploy-pod-autoscaling\n"
echo -e "You can get the list of existing deployments using command: kubectl get deployments${NONE}"
}
ARG="$#"
if [[ $ARG -eq 0 ]]; then
print_help
exit
fi
while test -n "$1"; do
case "$1" in
--action)
ACTION=$2
shift
;;
--deployment)
DEPLOYMENT=$2
shift
;;
--scaleup)
SCALEUPTHRESHOLD=$2
shift
;;
--scaledown)
SCALEDOWNTHRESHOLD=$2
shift
;;
*)
print_help
exit
;;
esac
shift
done
LOG_FILE=$SCRIPT_HOME/kube-$DEPLOYMENT-$TODAY.log
touch $LOG_FILE
REPLICAS=`$KUBECTL get deployment -l name=$DEPLOYMENT | awk '{print $3}' | grep -v "CURRENT"`
MINREPLICAS=`$KUBECTL get hpa $DEPLOYMENT | awk '{print $5}' | grep -v "MINPODS"`
MAXREPLICAS=`$KUBECTL get hpa $DEPLOYMENT | awk '{print $6}' | grep -v "MAXPODS"`
#########################################
#defining function to calculate heap memory
calculate_heap(){
echo "===========================" >> $LOG_FILE
pods=`$KUBECTL get pod -l name=$DEPLOYMENT | awk '{print $1}' | grep -v NAME`
for i in $pods
do
echo "Pod: "$i >> $LOG_FILE
PID=`$KUBECTL exec -it $i -- ps -ef | grep -v grep | grep java | awk '{print $2}'` >> $LOG_FILE
TOTALHEAP=`$KUBECTL exec -t $i -- ps -ef | grep java | grep -v grep | awk -F'Xmx' '{print $2}' | awk '{print $1}' | grep -o '[0-9]\+[a-z]'`
if [[ $TOTALHEAP =~ .*g.* ]]; then
TOTALHEAPINGB=${TOTALHEAP//[!0-9]/}
TOTALHEAPINMB=$((TOTALHEAPINGB * 1024))
echo "Total Heap Capacity Allocated: "$TOTALHEAPINMB"MB" >> $LOG_FILE
elif [[ $TOTALHEAP =~ .*m.* ]]; then
TOTALHEAPINMB=${TOTALHEAP//[!0-9]/}
echo "Total Heap Capacity Allocated: "$TOTALHEAPINMB"MB" >> $LOG_FILE
fi
USEDHEAP=`$KUBECTL exec -it $i -- jstat -gc $PID | tail -n 1 | awk '{ print ($3 + $4 + $6 + $8 + $10) / 1024 }'`
echo "Used Heap Memory: "$USEDHEAP"MB" >> $LOG_FILE
UTILIZEDHEAP=$(awk "BEGIN { pc=100*${USEDHEAP}/${TOTALHEAPINMB}; i=int(pc); print (pc-i<0.5)?i:i+1 }")
echo "Heap memory Percent: "$UTILIZEDHEAP"%" >> $LOG_FILE
heapmem=$((heapmem+UTILIZEDHEAP))
echo "===========================" >> $LOG_FILE
done
AVGHEAPMEM=$(( $heapmem/$REPLICAS ))
echo "Average Heap Memory: "$AVGHEAPMEM >> $LOG_FILE
}
#########################################
#defining function to autoscale based on heap memory
heapmemory_autoscale(){
if [ $AVGHEAPMEM -gt $SCALEUPTHRESHOLD ] && [ $REPLICAS -lt $MAXREPLICAS ]
then
echo "Memory is greater than the threshold" >> $LOG_FILE
count=$((REPLICAS+1))
echo "Updated No. of Replicas will be: "$count >> $LOG_FILE
scale=`$KUBECTL scale --replicas=$count deployment/$DEPLOYMENT`
echo "Deployment Scaled Up" >> $LOG_FILE
elif [ $AVGHEAPMEM -lt $SCALEDOWNTHRESHOLD ] && [ $REPLICAS -gt $MINREPLICAS ]
then
echo "Memory is less than threshold" >> $LOG_FILE
count=$((REPLICAS-1))
echo "Updated No. of Replicas will be: "$count >> $LOG_FILE
scale=`$KUBECTL scale --replicas=$count deployment/$DEPLOYMENT`
echo "Deployment Scaled Down" >> $LOG_FILE
else
echo "Heap Memory is not crossing the threshold. No Scaling Done." >> $LOG_FILE
fi
}
##########################################
#defining function to calculate pod memory
calculate_podmemory(){
pods=`$KUBECTL top pod -l name=$DEPLOYMENT | awk '{print $3}' | grep -o '[0-9]\+'`
TOTALMEM=`$KUBECTL describe pod -l name=$DEPLOYMENT | grep -A 2 "Limits:" | grep memory | grep -o '[0-9]\+[A-Z]' | head -1`
if [[ $TOTALMEM =~ .*G.* ]]; then
TOTALMEMINGB=${TOTALMEM//[!0-9]/}
TOTALMEMINMB=$((TOTALMEMINGB * 1024))
echo "Total Pod Memory Allocated: "$TOTALMEMINMB"MB" >> $LOG_FILE
echo "===========================" >> $LOG_FILE
elif [[ $TOTALMEM =~ .*M.* ]]; then
TOTALMEMINMB=${TOTALMEM//[!0-9]/}
echo "Total Pod Memory Allocated: "$TOTALMEMINMB"MB" >> $LOG_FILE
echo "===========================" >> $LOG_FILE
fi
for i in $pods
do
podmem=$((podmem+i))
echo "Used Pod Memory: "$podmem >> $LOG_FILE
UTILIZEDPODMEM=$(awk "BEGIN { pc=100*${podmem}/${TOTALMEMINMB}; i=int(pc); print (pc-i<0.5)?i:i+1 }")
echo "Pod memory Percent: "$UTILIZEDPODMEM"%" >> $LOG_FILE
echo "===========================" >> $LOG_FILE
done
AVGPODMEM=$(( $UTILIZEDPODMEM/$REPLICAS ))
echo "Average Pod Memory: "$AVGPODMEM >> $LOG_FILE
}
##########################################
#defining function to autoscale based on pod memory
podmemory_autoscale(){
if [ $AVGPODMEM -gt $SCALEUPTHRESHOLD ] && [ $REPLICAS -lt $MAXREPLICAS ]
then
echo "Memory is greater than threshold" >> $LOG_FILE
count=$((REPLICAS+1))
echo "Updated No. of Replicas will be: "$count >> $LOG_FILE
scale=`$KUBECTL scale --replicas=$count deployment/$DEPLOYMENT`
echo "Deployment Scaled Up" >> $LOG_FILE
elif [ $AVGPODMEM -lt $SCALEDOWNTHRESHOLD ] && [ $REPLICAS -gt $MINREPLICAS ]
then
echo "Memory is less than threshold" >> $LOG_FILE
count=$((REPLICAS-1))
echo "Updated No. of Replicas will be: "$count >> $LOG_FILE
scale=`$KUBECTL scale --replicas=$count deployment/$DEPLOYMENT`
echo "Deployment Scaled Down" >> $LOG_FILE
else
echo "Memory is not crossing the threshold. No Scaling done." >> $LOG_FILE
fi
}
##########################################
#Calling Functions
if [[ $REPLICAS ]]; then
if [ "$ACTION" = "deploy-heap-autoscaling" ];then
if [ $ARG -ne 8 ]
then
echo "Incorrect No. of Arguments Provided"
print_help
exit 1
fi
calculate_heap
heapmemory_autoscale
elif [ "$ACTION" = "get-heapmemory" ];then
if [ $ARG -ne 4 ]
then
echo "Incorrect No. of Arguments Provided"
print_help
exit 1
fi
calculate_heap
elif [ "$ACTION" = "get-podmemory" ];then
if [ $ARG -ne 4 ]
then
echo "Incorrect No. of Arguments Provided"
print_help
exit 1
fi
calculate_podmemory
elif [ "$ACTION" = "deploy-pod-autoscaling" ];then
if [ $ARG -ne 8 ]
then
echo "Incorrect No. of Arguments Provided"
print_help
exit 1
fi
calculate_podmemory
podmemory_autoscale
else
echo "Unknown Action"
print_help
fi
else
echo "No Deployment exists with name: "$DEPLOYMENT
print_help
fi