log-dump.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/bin/bash
  2. # Copyright 2016 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Call this to dump all master and node logs into the folder specified in $1
  16. # (defaults to _artifacts). Only works if the provider supports SSH.
  17. set -o errexit
  18. set -o nounset
  19. set -o pipefail
  20. KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
  21. : ${KUBE_CONFIG_FILE:="config-test.sh"}
  22. source "${KUBE_ROOT}/cluster/kube-util.sh"
  23. detect-project &> /dev/null
  24. readonly report_dir="${1:-_artifacts}"
  25. echo "Dumping master and node logs to ${report_dir}"
  26. # Copy all files /var/log/{$3}.log on node $1 into local dir $2.
  27. # $3 should be a space-separated string of files.
  28. # This function shouldn't ever trigger errexit, but doesn't block stderr.
  29. function copy-logs-from-node() {
  30. local -r node="${1}"
  31. local -r dir="${2}"
  32. local files=(${3})
  33. # Append ".log"
  34. files=("${files[@]/%/.log}")
  35. # Prepend "/var/log/"
  36. files=("${files[@]/#/\/var\/log\/}")
  37. # Replace spaces with commas, surround with braces
  38. local -r scp_files="{$(echo ${files[*]} | tr ' ' ',')}"
  39. if [[ "${KUBERNETES_PROVIDER}" == "aws" ]]; then
  40. local ip=$(get_ssh_hostname "${node}")
  41. scp -oLogLevel=quiet -oConnectTimeout=30 -oStrictHostKeyChecking=no -i "${AWS_SSH_KEY}" "${SSH_USER}@${ip}:${scp_files}" "${dir}" > /dev/null || true
  42. else
  43. gcloud compute copy-files --project "${PROJECT}" --zone "${ZONE}" "${node}:${scp_files}" "${dir}" > /dev/null || true
  44. fi
  45. }
  46. # Save logs for node $1 into directory $2. Pass in any non-common files in $3.
  47. # $3 should be a space-separated list of files.
  48. # This function shouldn't ever trigger errexit
  49. function save-logs() {
  50. local -r node_name="${1}"
  51. local -r dir="${2}"
  52. local files="${3} ${common_logfiles}"
  53. if [[ "${KUBERNETES_PROVIDER}" == "gce" || "${KUBERNETES_PROVIDER}" == "gke" ]]; then
  54. files="${files} ${gce_logfiles}"
  55. fi
  56. if [[ "${KUBERNETES_PROVIDER}" == "aws" ]]; then
  57. files="${files} ${aws_logfiles}"
  58. fi
  59. if ssh-to-node "${node_name}" "sudo systemctl status kubelet.service" &> /dev/null; then
  60. ssh-to-node "${node_name}" "sudo journalctl --output=cat -u kubelet.service" > "${dir}/kubelet.log" || true
  61. ssh-to-node "${node_name}" "sudo journalctl --output=cat -u docker.service" > "${dir}/docker.log" || true
  62. else
  63. files="${files} ${initd_logfiles} ${supervisord_logfiles}"
  64. fi
  65. copy-logs-from-node "${node_name}" "${dir}" "${files}"
  66. }
  67. readonly master_ssh_supported_providers="gce aws kubemark"
  68. readonly node_ssh_supported_providers="gce gke aws"
  69. readonly master_logfiles="kube-apiserver kube-scheduler kube-controller-manager etcd glbc cluster-autoscaler"
  70. readonly node_logfiles="kube-proxy"
  71. readonly aws_logfiles="cloud-init-output"
  72. readonly gce_logfiles="startupscript"
  73. readonly common_logfiles="kern"
  74. readonly initd_logfiles="docker"
  75. readonly supervisord_logfiles="kubelet supervisor/supervisord supervisor/kubelet-stdout supervisor/kubelet-stderr supervisor/docker-stdout supervisor/docker-stderr"
  76. # Limit the number of concurrent node connections so that we don't run out of
  77. # file descriptors for large clusters.
  78. readonly max_scp_processes=25
  79. if [[ ! "${master_ssh_supported_providers}" =~ "${KUBERNETES_PROVIDER}" ]]; then
  80. echo "Master SSH not supported for ${KUBERNETES_PROVIDER}"
  81. elif ! (detect-master &> /dev/null); then
  82. echo "Master not detected. Is the cluster up?"
  83. else
  84. readonly master_dir="${report_dir}/${MASTER_NAME}"
  85. mkdir -p "${master_dir}"
  86. save-logs "${MASTER_NAME}" "${master_dir}" "${master_logfiles}"
  87. fi
  88. detect-node-names &> /dev/null
  89. if [[ ! "${node_ssh_supported_providers}" =~ "${KUBERNETES_PROVIDER}" ]]; then
  90. echo "Node SSH not supported for ${KUBERNETES_PROVIDER}"
  91. elif [[ "${#NODE_NAMES[@]}" -eq 0 ]]; then
  92. echo "Nodes not detected. Is the cluster up?"
  93. else
  94. proc=${max_scp_processes}
  95. for node_name in "${NODE_NAMES[@]}"; do
  96. node_dir="${report_dir}/${node_name}"
  97. mkdir -p "${node_dir}"
  98. # Save logs in the background. This speeds up things when there are
  99. # many nodes.
  100. save-logs "${node_name}" "${node_dir}" "${node_logfiles}" &
  101. # We don't want to run more than ${max_scp_processes} at a time, so
  102. # wait once we hit that many nodes. This isn't ideal, since one might
  103. # take much longer than the others, but it should help.
  104. proc=$((proc - 1))
  105. if [[ proc -eq 0 ]]; then
  106. proc=${max_scp_processes}
  107. wait
  108. fi
  109. done
  110. # Wait for any remaining processes.
  111. if [[ proc -gt 0 && proc -lt ${max_scp_processes} ]]; then
  112. wait
  113. fi
  114. fi