-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·258 lines (216 loc) · 6.06 KB
/
setup.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env bash
set -e # Abort on error
case "${OSTYPE}" in
darwin* ) DEFAULT_DRIVER="virtualbox";;
* ) DEFAULT_DRIVER="docker";;
esac
DRIVER="${DRIVER:-${DEFAULT_DRIVER}}" # Driver to use with minikube
NAME=ft.services # Minikube profile
PREFIX=ft_services # Docker build prefix
SRCDIR=srcs # Directory which contains the deployments
RETRY=5 # Maximum number of tries to start minikube
KEYDIR=keys # Directory where keys and certs will be stored
KEYHOST=ft.services # Load balancer hostname
# Container units
DAEMONSETS=("telegraf")
UNITS=("mysql" "wordpress" "phpmyadmin" "nginx" "ftps" "influxdb" "grafana" "${DAEMONSETS[@]}")
ADDONS=("metrics-server" "dashboard")
element_in()
{
local e match="${1}"
shift
for e; do [[ "${e}" == "${match}" ]] && return 0; done
return 1
}
start_minikube()
{
# Start minikube
local n=0
until [ "${n}" -ge "${RETRY}" ]; do
minikube -p "${NAME}" start "--driver=${DRIVER}" && break
n=$((n+1))
sleep 2
done
if [ "${n}" -eq "${RETRY}" ]; then
echo "Failed to start minikube after ${RETRY} tries!"
exit 1
fi
}
stop_minikube()
{
# Stop minikube
minikube -p "${NAME}" stop
}
delete_minikube()
{
read -p "Are you sure you want to delete the cluster? [y/N] " -n1 -r ANSWER
echo
if [[ ${ANSWER} =~ ^[Yy]$ ]]; then
# Delete the minikube cluster
minikube -p "${NAME}" delete
rm -rf "${KEYDIR}"
fi
}
setup_minikube()
{
# Start minikube
start_minikube
# Enable addons
for ADDON in ${ADDONS[@]}; do
minikube -p "${NAME}" addons enable "${ADDON}"
done
# Setup MetalLB
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.3/manifests/namespace.yaml
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.3/manifests/metallb.yaml
# Setup namespaces
kubectl apply -f "${SRCDIR}/namespaces.yaml"
}
setup_networking()
{
# Split IP by '.'
OLDIFS=${IFS}
IFS=.; set -- $(minikube -p "${NAME}" ip)
# Assign free range to MetalLB
if [ "${4}" -gt 127 ]; then
LB_RANGE="${1}.${2}.${3}.1/32"
else
LB_RANGE="${1}.${2}.${3}.128/32"
fi
# Restore IFS
IFS=${OLDIFS}
CONFIGMAP=$(cat <<- EOF
address-pools:
- name: default
protocol: layer2
addresses:
- ${LB_RANGE}
EOF
)
# Update MetalLB configmap
kubectl --namespace metallb-system delete configmap config || :
kubectl --namespace metallb-system create configmap config --from-literal="config=${CONFIGMAP}"
}
start_dashboard()
{
# Show web dashboard url
minikube -p "${NAME}" dashboard --url
}
build_unit()
{
echo "Building ${1}..."
docker build -qt "${PREFIX}/${1}" "${SRCDIR}/${1}"
}
build_units()
{
# Use minikube docker-env
eval $(minikube -p "${NAME}" docker-env)
# Build docker images
for UNIT in ${UNITS[@]}; do
build_unit "${UNIT}"
done
}
build_certs()
{
ANSWER="Y"
if [ -f "${KEYDIR}/${KEYHOST}.csr" ] || [ -f "${KEYDIR}/${KEYHOST}.key" ]; then
read -p "Do you want to overwrite the existing certificates? [y/N] " -n1 -r ANSWER
echo
fi
if [[ ${ANSWER} =~ ^[Yy]$ ]]; then
# Generate TLS keys
mkdir -p "${KEYDIR}"
openssl req -x509 -nodes -days 365\
-newkey rsa:2048\
-keyout "${KEYDIR}/${KEYHOST}.key"\
-out "${KEYDIR}/${KEYHOST}.csr"\
-subj "/CN=*.${KEYHOST}/O=${KEYHOST}"
fi
# Update TLS secret
kubectl delete secret default-tls || :
kubectl create secret tls default-tls --key "${KEYDIR}/${KEYHOST}.key" --cert "${KEYDIR}/${KEYHOST}.csr"
kubectl delete secret -n monitoring default-tls || :
kubectl create secret -n monitoring tls default-tls --key "${KEYDIR}/${KEYHOST}.key" --cert "${KEYDIR}/${KEYHOST}.csr"
# Update MetalLB secret
kubectl delete secret -n metallb-system memberlist || :
kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"
# Update Grafana secret
kubectl delete secret -n monitoring grafana-secretkey || :
kubectl create secret generic -n monitoring grafana-secretkey --from-literal=secretkey="$(openssl rand -base64 20)"
}
update_unit()
{
if [[ "${1}" = "" ]]; then
echo -e "Usage: ${0} update [UNIT]
Units:"
printf ' %s\n' ${UNITS[@]}
exit 1
fi
if element_in "${1}" "${UNITS[@]}"; then
eval $(minikube -p "${NAME}" docker-env)
build_unit "${1}"
kubectl delete -f "${SRCDIR}/${1}/deployment.yaml" || :
kubectl apply -k "${SRCDIR}"
else
echo "${1} is not a valid unit!"
exit 1
fi
}
kill_unit_process()
{
if [[ "${1}" = "" || "${2}" = "" ]]; then
echo -e "Usage: ${0} kill [UNIT] [PROCESS]
Units:"
printf ' %s\n' ${UNITS[@]}
exit 1
fi
if element_in "${1}" "${DAEMONSETS[@]}"; then
CONTAINER="$(minikube -p "${NAME}" ssh -- docker ps -qf "name=^/k8s_${1}" | tr -d '\r')"
echo "Killing '${2}' running in '${CONTAINER}' on minikube node..."
minikube -p "${NAME}" ssh -- docker exec -it "${CONTAINER}" pkill "${2}"
elif element_in "${1}" "${UNITS[@]}"; then
PODSPEC=($(kubectl get pod -o custom-columns=:metadata.name,:metadata.namespace --all-namespaces | grep "${1}"))
POD="${PODSPEC[0]}"
NAMESPACE="${PODSPEC[1]}"
echo "Killing '${2}' running in '${POD}' on namespace '${NAMESPACE}'..."
kubectl exec -n "${NAMESPACE}" -it "${POD}" -- pkill "${2}"
else
echo "${1} is not a valid unit!"
exit 1
fi
echo "Done!"
}
setup()
{
setup_minikube
setup_networking
build_units
build_certs
kubectl apply -k "${SRCDIR}"
echo "To open the Kubernetes dashboard, please run 'minikube -p \"${NAME}\" dashboard' in your terminal."
}
print_help()
{
echo -e "Usage: ${0} [COMMAND]
Commands:
setup Setup and start the cluster
start Start an existing cluster and apply changes
stop Stop the running cluster
restart Restart the running cluster
update Update an unit's image
delete Delete the cluster
dashboard Show the Kubernetes dashboard
help Show this help message
kill Kill an unit's process
If no argument is provided, 'setup' will be assumed."
}
case "${1}" in
"setup" | "") setup;;
"start" ) start_minikube;;
"stop" ) stop_minikube;;
"restart" ) stop_minikube; start_minikube;;
"delete" ) delete_minikube;;
"dashboard" ) start_dashboard;;
"update" ) update_unit "${2}";;
"kill" ) kill_unit_process "${2}" "${3}";;
* ) print_help;;
esac