forked from istio/istio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump_kubernetes.sh
executable file
·340 lines (293 loc) · 9.69 KB
/
dump_kubernetes.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/bin/bash
#
# Uses kubectl to collect cluster information.
# Dumps:
# - Logs of every container of every pod of every namespace.
# - Resource configurations for ingress, endpoints, custom resource
# definitions, configmaps, secrets (names only) and "all" as defined by
# kubectl.
COREDUMP_DIR="/var/lib/istio"
# Limit total log size to 100MB by default
DEFAULT_MAX_LOG_BYTES=100000000
error() {
echo "$*" >&2
}
usage() {
error 'Collect all possible data from a Kubernetes cluster using kubectl.'
error ''
error 'Usage:'
error ' dump_kubernetes.sh [options]'
error ''
error 'Options:'
error ' -d, --output-directory directory to output files; defaults to'
error ' "istio-dump"'
error ' -z, --archive if present, archives and removes the output'
error ' directory'
error ' -q, --quiet if present, do not log'
error ' -m, --max-bytes max total bytes, 0=no limit, default='${DEFAULT_MAX_LOG_BYTES}
error ' -l, --label if set, dump logs only for pods with given labels e.g. "-l app=pilot -l istio=galley"'
error ' -n, --namespace if set, dump logs only for pods in the given namespaces e.g. "-n default -n istio-system"'
error ' --error-if-nasty-logs if present, exit with 255 if any logs'
error ' contain errors'
exit 1
}
log() {
local msg="${1}"
if [ "${QUIET}" = false ]; then
printf '%s\n' "${msg}"
fi
}
parse_args() {
local max_bytes="${DEFAULT_MAX_LOG_BYTES}"
while [ "$#" -gt 0 ]; do
case "${1}" in
-d|--output-directory)
local out_dir="${2}"
shift 2 # Shift past option and value.
;;
-z|--archive)
local should_archive=true
shift # Shift past flag.
;;
-q|--quiet)
local quiet=true
shift # Shift past flag.
;;
--error-if-nasty-logs)
local should_check_logs_for_errors=true
shift # Shift past flag.
;;
-m|--max-bytes)
max_bytes="${2}"
shift 2
;;
-l|--label)
pod_labels+="${2} "
shift 2
;;
-n|--namespace)
namespaces+="${2} "
shift 2
;;
*)
usage
;;
esac
done
readonly OUT_DIR="${out_dir:-istio-dump}"
readonly SHOULD_ARCHIVE="${should_archive:-false}"
readonly QUIET="${quiet:-false}"
readonly SHOULD_CHECK_LOGS_FOR_ERRORS="${should_check_logs_for_errors:-false}"
readonly LOG_DIR="${OUT_DIR}/logs"
readonly RESOURCES_FILE="${OUT_DIR}/resources.yaml"
readonly ISTIO_RESOURCES_FILE="${OUT_DIR}/istio-resources.yaml"
readonly MAX_LOG_BYTES="${max_bytes}"
}
check_prerequisites() {
local prerequisites=$*
for prerequisite in ${prerequisites}; do
if ! command -v "${prerequisite}" > /dev/null; then
error "\"${prerequisite}\" is required. Please install it."
return 1
fi
done
}
dump_time() {
mkdir -p "${OUT_DIR}"
date -u > "${OUT_DIR}/DUMP_TIME"
}
# mv_unless_max_exceeded src_file dest_file performs mv src_file dest_file unless the global variable stored_log_bytes
# would exceed max_log_bytes.
# If total not exceeded, file is moved and max_log_bytes updated, otherwise an error is logged.
# src_file is always deleted when calling this function.
mv_unless_max_exceeded() {
local src_file="${1}"
local dst_file="${2}"
file_size=$(wc -c "${src_file}" | awk '{print $1}')
local nsb=$(("${stored_log_bytes}" + "${file_size}"))
if (("${nsb}" > "${MAX_LOG_BYTES}")); then
log "Not storing ${log_file} because appending its ${file_size} bytes would exceed max logged bytes ${MAX_LOG_BYTES}"
rm "${src_file}"
else
dirn=$(dirname "${dst_file}")
mkdir -p "${dirn}"
mv "${src_file}" "${dst_file}"
stored_log_bytes="${nsb}"
fi
}
dump_logs_for_container() {
local namespace="${1}"
local pod="${2}"
local container="${3}"
log "Retrieving logs for ${namespace}/${pod}/${container}"
mkdir -p "${LOG_DIR}/${namespace}/${pod}"
local log_file_head="${LOG_DIR}/${namespace}/${pod}/${container}"
local temp_log_file="${LOG_DIR}/temp_log_file.log"
local log_file="${log_file_head}.log"
kubectl logs --namespace="${namespace}" "${pod}" "${container}" \
> "${temp_log_file}"
mv_unless_max_exceeded "${temp_log_file}" "${log_file}"
local filter="?(@.name == \"${container}\")"
local json_path='{.status.containerStatuses['${filter}'].restartCount}'
local restart_count
restart_count=$(kubectl get --namespace="${namespace}" \
pod "${pod}" -o=jsonpath="${json_path}")
if [ "${restart_count}" -gt 0 ]; then
log "Retrieving previous logs for ${namespace}/${pod}/${container}"
local log_previous_file
log_previous_file="${log_file_head}_previous.log"
kubectl logs --namespace="${namespace}" \
--previous "${pod}" "${container}" \
> "${temp_log_file}"
mv_unless_max_exceeded "${temp_log_file}" "${log_previous_file}"
fi
}
copy_core_dumps_if_istio_proxy() {
local namespace="${1}"
local pod="${2}"
local container="${3}"
local got_core_dump=false
if [ "istio-proxy" = "${container}" ]; then
local out_dir="${LOG_DIR}/${namespace}/${pod}"
mkdir -p "${out_dir}"
local core_dumps
core_dumps=$(kubectl exec -n "${namespace}" "${pod}" -c "${container}" -- \
find ${COREDUMP_DIR} -name 'core.*')
for f in ${core_dumps}; do
local out_file
out_file="${out_dir}/$(basename "${f}")"
kubectl exec -n "${namespace}" "${pod}" -c "${container}" -- \
cat "${f}" > "${out_file}"
log "Copied ${namespace}/${pod}/${container}:${f} to ${out_file}"
got_core_dump=true
done
fi
if [ "${got_core_dump}" = true ]; then
return 254
fi
}
# Run functions on each container. Each argument should be a function which
# takes 3 args: ${namespace} ${pod} ${container}.
# If any of the called functions returns error, tap_containers returns
# immediately with that error.
tap_containers() {
local functions=("$@")
if [ -z "${namespaces}" ]; then
namespaces=$(kubectl get \
namespaces -o=jsonpath="{.items[*].metadata.name}")
fi
for namespace in ${namespaces}; do
local pods=""
if [ -n "${pod_labels}" ]; then
for label in $pod_labels; do
pods+=$(kubectl get --namespace="${namespace}" -l"${label}" \
pods -o=jsonpath='{.items[*].metadata.name}')" "
done
else
pods=$(kubectl get --namespace="${namespace}" \
pods -o=jsonpath='{.items[*].metadata.name}')
fi
for pod in ${pods}; do
local containers
containers=$(kubectl get --namespace="${namespace}" \
pod "${pod}" -o=jsonpath='{.spec.containers[*].name}')
for container in ${containers}; do
for f in "${functions[@]}"; do
"${f}" "${namespace}" "${pod}" "${container}" || return $?
done
done
done
done
return 0
}
dump_kubernetes_resources() {
log "Retrieving kubernetes resource configurations"
mkdir -p "${OUT_DIR}"
# Only works in Kubernetes 1.8.0 and above.
kubectl get --all-namespaces --export \
all,jobs,ingresses,endpoints,customresourcedefinitions,configmaps,secrets,events \
-o yaml > "${RESOURCES_FILE}"
}
dump_istio_custom_resource_definitions() {
log "Retrieving istio resource configurations"
local istio_resources
# Trim to only first field; join by comma; remove last comma.
istio_resources=$(kubectl get customresourcedefinitions \
--no-headers 2> /dev/null \
| cut -d ' ' -f 1 \
| tr '\n' ',' \
| sed 's/,$//')
if [ -n "${istio_resources}" ]; then
kubectl get "${istio_resources}" --all-namespaces -o yaml \
> "${ISTIO_RESOURCES_FILE}"
fi
}
dump_resources() {
dump_kubernetes_resources
dump_istio_custom_resource_definitions
mkdir -p "${OUT_DIR}"
kubectl cluster-info dump > "${OUT_DIR}/cluster-info.dump.txt"
kubectl describe pods -n istio-system > "${OUT_DIR}/istio-system-pods.txt"
kubectl get events --all-namespaces -o wide > "${OUT_DIR}/events.txt"
}
dump_pilot_url(){
local pilot_pod=$1
local url=$2
local dname=$3
local outfile
outfile="${dname}/$(basename "${url}")"
log "Fetching ${url} from pilot"
kubectl -n istio-system exec -i -t "${pilot_pod}" -c istio-proxy -- \
curl "http://localhost:8080/${url}" > "${outfile}"
}
dump_pilot() {
local pilot_pod
pilot_pod=$(kubectl -n istio-system get pods -l istio=pilot \
-o jsonpath='{.items[*].metadata.name}')
if [ -n "${pilot_pod}" ]; then
local pilot_dir="${OUT_DIR}/pilot"
mkdir -p "${pilot_dir}"
dump_pilot_url "${pilot_pod}" debug/configz "${pilot_dir}"
dump_pilot_url "${pilot_pod}" debug/endpointz "${pilot_dir}"
dump_pilot_url "${pilot_pod}" debug/adsz "${pilot_dir}"
dump_pilot_url "${pilot_pod}" debug/authenticationz "${pilot_dir}"
dump_pilot_url "${pilot_pod}" metrics "${pilot_dir}"
fi
}
archive() {
local parent_dir
parent_dir=$(dirname "${OUT_DIR}")
local dir
dir=$(basename "${OUT_DIR}")
pushd "${parent_dir}" > /dev/null || exit
tar -czf "${dir}.tar.gz" "${dir}"
popd > /dev/null || exit
log "Wrote ${parent_dir}/${dir}.tar.gz"
}
check_logs_for_errors() {
log "Searching logs for errors."
grep -R --include "${LOG_DIR}/*.log" --ignore-case -e 'segmentation fault'
}
main() {
local exit_code=0
parse_args "$@"
check_prerequisites kubectl
dump_time
dump_pilot
dump_resources
tap_containers "dump_logs_for_container" "copy_core_dumps_if_istio_proxy"
exit_code=$?
if [ "${SHOULD_CHECK_LOGS_FOR_ERRORS}" = true ]; then
if ! check_logs_for_errors; then
exit_code=255
fi
fi
if [ "${SHOULD_ARCHIVE}" = true ] ; then
archive
rm -r "${OUT_DIR}"
fi
log "Wrote to ${OUT_DIR}"
return ${exit_code}
}
stored_log_bytes=0
main "$@"