-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDEMO.sh
executable file
·237 lines (189 loc) · 5.64 KB
/
DEMO.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
226
227
228
229
230
231
232
233
234
235
236
# TODO:
# - create my own flask+redis application, own functionality
# - create my own Go equivalent
# - create my own Node.js equivalent
# - tag as mjbright/flask-xxx
# - push as mjbright/flask-xxx
# - create git repo with Dockerfile and k8s templates
# - add build script (this) + README, and credit
# blog post: Scaling Python Microservices with Kubernetes
# http://blog.apcelent.com/scaling-python-microservices-kubernetes.html
# git clone https://github.com/codecraf8/kubernetes-flask-redis-microservice
# note on ErrImagePull: https://stackoverflow.com/questions/36874880/kubernetes-cannot-pull-local-image
USE_MINIKUBE=1
kubectl get nodes | grep docker-for-desktop && USE_MINIKUBE=0
#cd ~/src/git/
#git clone https://github.com/codecraf8/kubernetes-flask-redis-microservice
FLASK_IMAGE=mjbright/flask-web
#FLASK_IMAGE=flask-web
SERVICE=flask-app
VERSION=v1
################################################################################
# Helper functions:
die() {
echo "$0: die - $*" >&2
for i in 0 1 2 3 4 5 6 7 8 9 10;do
CALLER_INFO=`caller $i`
[ -z "$CALLER_INFO" ] && break
echo " Line: $CALLER_INFO" >&2
done
exit 1
}
warn() {
echo "$0: warn - $*" >&2
for i in 0 1 2 3 4 5 6 7 8 9 10;do
CALLER_INFO=`caller $i`
[ -z "$CALLER_INFO" ] && break
echo " Line: $CALLER_INFO" >&2
done
}
#
# Function: press <prompt>
# Prompt user to press <return> to continue
# Exit if the user enters q or Q
#
press()
{
[ ! -z "$1" ] && echo "$*"
echo "Press <return> to continue"
read _DUMMY
[ "$_DUMMY" = "q" ] && exit 0
[ "$_DUMMY" = "Q" ] && exit 0
}
#
# function yesno <question> [<default>]
# e.g. use as:
# yesno "Edit reminder file?" && vi $reminder_file_tomerge
#
function yesno
{
resp=""
default=""
[ ! -z "$2" ] && default="$2"
while [ 1 ]; do
if [ ! -z "$default" ];then
echo -n "$1 [yYnNqQ] [$default]:"
read resp
[ -z "$resp" ] && resp="$default"
else
echo -n "$1 [yYnNqQ]:"
read resp
fi
[ \( "$resp" = "q" \) -o \( "$resp" = "Q" \) ] && exit 0
[ \( "$resp" = "y" \) -o \( "$resp" = "Y" \) ] && return 0
[ \( "$resp" = "n" \) -o \( "$resp" = "N" \) ] && return 1
done
}
# yesno "Should I do this thing?" && echo "YES - you should" || echo "NO - you shouldn't"
################################################################################
# Main functions:
CLEAN() {
kubectl delete -f flask-service.yaml
kubectl delete -f redis-service.yaml
kubectl delete -f flask-deployment.yaml
kubectl delete -f redis-deployment.yaml
kubectl get pods
}
MINIKUBE_CACHE() {
minikube cache add redis:latest
minikube cache add python:2.7
minikube cache add mjbright/k8s-demo:1
minikube cache add mjbright/k8s-demo:2
minikube cache add mjbright/k8s-demo:3
minikube cache list
}
BUILD_ALL() {
cp -a versions/app.py.v1 app.py; BUILD v1
cp -a versions/app.py.v2 app.py; BUILD v2
cp -a versions/app.py.v3 app.py; BUILD v3
}
BUILD() {
local VERSION=$1; shift
docker build -t $FLASK_IMAGE:$VERSION .
docker images |grep $FLASK_IMAGE
}
APPLY() {
kubectl apply -f flask-deployment.yaml
kubectl apply -f redis-deployment.yaml
kubectl apply -f flask-service.yaml
kubectl apply -f redis-service.yaml
kubectl get pods
}
ACCESS() {
kubectl get service/$SERVICE || die "No such service as <$SERVICE>"
#kubectl get service $SERVICE --output='jsonpath={.spec.ports[0].nodePort}'
PORT=$(kubectl get service $SERVICE --output='jsonpath={.spec.ports[0].nodePort}')
[ -z "$PORT" ] && die "Failed to get nodePort for service $SERVICE"
IP=$(kubectl describe nodes | awk '/InternalIP:/ { print $2; }')
#if [ -f /cygdrive/c ]; then
# IP=$(kubectl describe nodes | awk '/InternalIP:/ { print $2; }')
#else
# IP=$(ip a | awk '!/127.0.0.1/ && / inet / { FS="/"; $0=$2; print $1; exit(0); }')
#fi
#IP="$(minikube ip)"
#[ -z "$IP" ] && die "Failed to get IP for minikube"
[ -z "$IP" ] && die "Failed to get IP for node"
URL="$IP:$PORT"
#CMD="curl 'http://${URL}/'"
CMD="wget -O - 'http://${URL}/'"
press "About to run: <$CMD>"
eval $CMD
## URL="$IP:$PORT/testobj"
## curl -H "Content-Type: application/json" -X PUT -d '{"hello":999}' $URL
## curl $URL
}
DO_ALL() {
CLEAN
[ $USE_MINIKUBE -ne 0 ] && MINIKUBE_CACHE
BUILD_ALL
#BUILD $VERSION
APPLY
ACCESS
}
DO_MOST() {
CLEAN
[ $USE_MINIKUBE -ne 0 ] && MINIKUBE_CACHE
#BUILD_ALL
#BUILD $VERSION
APPLY
ACCESS
}
PREPA_DEMO() {
minikube delete
#minikube start --memory 4096
minikube start --memory 3072
BUILD_ALL
}
################################################################################
# Args:
ACTION=ALL
#ACTION=MOST
while [ ! -z "$1" ]; do
case $1 in
-minikube) USE_MINIKUBE=1; eval $(minikube docker-env);;
-x) set -x;;
+x) set +x;;
-pre*) ACTION=PREPA_DEMO;;
-c) ACTION=CLEAN;;
-ba) ACTION=BUILD_ALL;;
-b) ACTION=BUILD;;
-v[0-9]) VERSION=${1#-};;
-curl) ACTION=ACCESS;;
*) die "Unknown arg <$1>";;
esac
shift
done
################################################################################
# Main:
case $ACTION in
PREPA_DEMO) PREPA_DEMO;;
ACCESS) ACCESS;;
CLEAN) CLEAN;;
BUILD_ALL) BUILD_ALL;;
BUILD)
cp -a versions/app.py.$VERSION app.py;
BUILD $VERSION;;
ALL) DO_ALL;;
MOST) DO_MOST;;
*) die "Unknown action <$ACTION>";;
esac