validate-cluster.sh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/bin/bash
  2. # Copyright 2014 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. # Validates that the cluster is healthy.
  16. # Error codes are:
  17. # 0 - success
  18. # 1 - fatal (cluster is unlikely to work)
  19. # 2 - non-fatal (encountered some errors, but cluster should be working correctly)
  20. set -o errexit
  21. set -o nounset
  22. set -o pipefail
  23. KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
  24. if [ -f "${KUBE_ROOT}/cluster/env.sh" ]; then
  25. source "${KUBE_ROOT}/cluster/env.sh"
  26. fi
  27. source "${KUBE_ROOT}/cluster/lib/util.sh"
  28. source "${KUBE_ROOT}/cluster/kube-util.sh"
  29. # Run kubectl and retry upon failure.
  30. function kubectl_retry() {
  31. tries=3
  32. while ! "${KUBE_ROOT}/cluster/kubectl.sh" "$@"; do
  33. tries=$((tries-1))
  34. if [[ ${tries} -le 0 ]]; then
  35. echo "('kubectl $@' failed, giving up)" >&2
  36. return 1
  37. fi
  38. echo "(kubectl failed, will retry ${tries} times)" >&2
  39. sleep 1
  40. done
  41. }
  42. ALLOWED_NOTREADY_NODES="${ALLOWED_NOTREADY_NODES:-0}"
  43. CLUSTER_READY_ADDITIONAL_TIME_SECONDS="${CLUSTER_READY_ADDITIONAL_TIME_SECONDS:-30}"
  44. EXPECTED_NUM_NODES="${NUM_NODES}"
  45. if [[ "${REGISTER_MASTER_KUBELET:-}" == "true" ]]; then
  46. EXPECTED_NUM_NODES=$((EXPECTED_NUM_NODES+1))
  47. fi
  48. REQUIRED_NUM_NODES=$((EXPECTED_NUM_NODES - ALLOWED_NOTREADY_NODES))
  49. # Make several attempts to deal with slow cluster birth.
  50. return_value=0
  51. attempt=0
  52. PAUSE_BETWEEN_ITERATIONS_SECONDS=15
  53. ADDITIONAL_ITERATIONS=$(((CLUSTER_READY_ADDITIONAL_TIME_SECONDS + PAUSE_BETWEEN_ITERATIONS_SECONDS - 1)/PAUSE_BETWEEN_ITERATIONS_SECONDS))
  54. while true; do
  55. # Pause between iterations of this large outer loop.
  56. if [[ ${attempt} -gt 0 ]]; then
  57. sleep 15
  58. fi
  59. attempt=$((attempt+1))
  60. # The "kubectl get nodes -o template" exports node information.
  61. #
  62. # Echo the output and gather 2 counts:
  63. # - Total number of nodes.
  64. # - Number of "ready" nodes.
  65. #
  66. # Suppress errors from kubectl output because during cluster bootstrapping
  67. # for clusters where the master node is registered, the apiserver will become
  68. # available and then get restarted as the kubelet configures the docker bridge.
  69. node=$(kubectl_retry get nodes) || continue
  70. found=$(($(echo "${node}" | wc -l) - 1))
  71. ready=$(($(echo "${node}" | grep -v "NotReady" | wc -l ) - 1))
  72. if (( "${found}" == "${EXPECTED_NUM_NODES}" )) && (( "${ready}" == "${EXPECTED_NUM_NODES}")); then
  73. break
  74. elif (( "${found}" > "${EXPECTED_NUM_NODES}" )); then
  75. if [[ "${KUBE_USE_EXISTING_MASTER:-}" != "true" ]]; then
  76. echo -e "${color_red}Found ${found} nodes, but expected ${EXPECTED_NUM_NODES}. Your cluster may not behave correctly.${color_norm}"
  77. fi
  78. break
  79. elif (( "${ready}" > "${EXPECTED_NUM_NODES}")); then
  80. echo -e "${color_red}Found ${ready} ready nodes, but expected ${EXPECTED_NUM_NODES}. Your cluster may not behave correctly.${color_norm}"
  81. break
  82. else
  83. if [[ "${REQUIRED_NUM_NODES}" -le "${ready}" ]]; then
  84. echo -e "${color_green}Found ${REQUIRED_NUM_NODES} Nodes, allowing additional ${ADDITIONAL_ITERATIONS} iterations for other Nodes to join.${color_norm}"
  85. last_run="${last_run:-$((attempt + ADDITIONAL_ITERATIONS - 1))}"
  86. fi
  87. # Set the timeout to ~25minutes (100 x 15 second) to avoid timeouts for 1000-node clusters.
  88. if [[ "${attempt}" -gt "${last_run:-100}" ]]; then
  89. echo -e "${color_yellow}Detected ${ready} ready nodes, found ${found} nodes out of expected ${EXPECTED_NUM_NODES}. Your cluster may not be fully functional.${color_norm}"
  90. kubectl_retry get nodes
  91. if [[ "${REQUIRED_NUM_NODES}" -gt "${ready}" ]]; then
  92. exit 1
  93. else
  94. return_value=2
  95. break
  96. fi
  97. else
  98. echo -e "${color_yellow}Waiting for ${EXPECTED_NUM_NODES} ready nodes. ${ready} ready nodes, ${found} registered. Retrying.${color_norm}"
  99. fi
  100. fi
  101. done
  102. echo "Found ${found} node(s)."
  103. kubectl_retry get nodes
  104. attempt=0
  105. while true; do
  106. # The "kubectl componentstatuses -o template" exports components health information.
  107. #
  108. # Echo the output and gather 2 counts:
  109. # - Total number of componentstatuses.
  110. # - Number of "healthy" components.
  111. cs_status=$(kubectl_retry get componentstatuses -o template --template='{{range .items}}{{with index .conditions 0}}{{.type}}:{{.status}}{{end}}{{"\n"}}{{end}}') || true
  112. componentstatuses=$(echo "${cs_status}" | grep -c 'Healthy:') || true
  113. healthy=$(echo "${cs_status}" | grep -c 'Healthy:True') || true
  114. if ((componentstatuses > healthy)); then
  115. if ((attempt < 5)); then
  116. echo -e "${color_yellow}Cluster not working yet.${color_norm}"
  117. attempt=$((attempt+1))
  118. sleep 30
  119. else
  120. echo -e " ${color_yellow}Validate output:${color_norm}"
  121. kubectl_retry get cs
  122. echo -e "${color_red}Validation returned one or more failed components. Cluster is probably broken.${color_norm}"
  123. exit 1
  124. fi
  125. else
  126. break
  127. fi
  128. done
  129. echo "Validate output:"
  130. kubectl_retry get cs
  131. if [ "${return_value}" == "0" ]; then
  132. echo -e "${color_green}Cluster validation succeeded${color_norm}"
  133. else
  134. echo -e "${color_yellow}Cluster validation encountered some problems, but cluster should be in working order${color_norm}"
  135. fi
  136. exit "${return_value}"