validate-cluster.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env 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[0]}")/..
  24. if [ -f "${KUBE_ROOT}/cluster/env.sh" ]; then
  25. source "${KUBE_ROOT}/cluster/env.sh"
  26. fi
  27. source "${KUBE_ROOT}/hack/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. if [[ "${KUBERNETES_PROVIDER:-}" == "gce" ]]; then
  45. EXPECTED_NUM_NODES="$(get-num-nodes)"
  46. echo "Validating gce cluster, MULTIZONE=${MULTIZONE:-}"
  47. # In multizone mode we need to add instances for all nodes in the region.
  48. if [[ "${MULTIZONE:-}" == "true" ]]; then
  49. EXPECTED_NUM_NODES=$(gcloud -q compute instances list --project="${PROJECT}" --format=[no-heading] \
  50. --filter="(name ~ '${NODE_INSTANCE_PREFIX}.*' OR name ~ '${WINDOWS_NODE_INSTANCE_PREFIX}.*') AND zone:($(gcloud -q compute zones list --project="${PROJECT}" --filter=region=${REGION} --format=csv[no-heading]\(name\) | tr "\n" "," | sed "s/,$//"))" | wc -l)
  51. echo "Computing number of nodes, NODE_INSTANCE_PREFIX=${NODE_INSTANCE_PREFIX}, REGION=${REGION}, EXPECTED_NUM_NODES=${EXPECTED_NUM_NODES}"
  52. fi
  53. else
  54. EXPECTED_NUM_NODES="${NUM_NODES}"
  55. fi
  56. if [[ "${REGISTER_MASTER_KUBELET:-}" == "true" ]]; then
  57. if [[ "${KUBERNETES_PROVIDER:-}" == "gce" ]]; then
  58. NUM_MASTERS=$(get-master-replicas-count)
  59. else
  60. NUM_MASTERS=1
  61. fi
  62. EXPECTED_NUM_NODES=$((EXPECTED_NUM_NODES+NUM_MASTERS))
  63. fi
  64. REQUIRED_NUM_NODES=$((EXPECTED_NUM_NODES - ALLOWED_NOTREADY_NODES))
  65. # Make several attempts to deal with slow cluster birth.
  66. return_value=0
  67. attempt=0
  68. # Set the timeout to ~25minutes (100 x 15 second) to avoid timeouts for 1000-node clusters.
  69. PAUSE_BETWEEN_ITERATIONS_SECONDS=15
  70. MAX_ATTEMPTS=100
  71. ADDITIONAL_ITERATIONS=$(((CLUSTER_READY_ADDITIONAL_TIME_SECONDS + PAUSE_BETWEEN_ITERATIONS_SECONDS - 1)/PAUSE_BETWEEN_ITERATIONS_SECONDS))
  72. while true; do
  73. # Pause between iterations of this large outer loop.
  74. if [[ ${attempt} -gt 0 ]]; then
  75. sleep 15
  76. fi
  77. attempt=$((attempt+1))
  78. # The "kubectl get nodes -o template" exports node information.
  79. #
  80. # Echo the output and gather 2 counts:
  81. # - Total number of nodes.
  82. # - Number of "ready" nodes.
  83. #
  84. # Suppress errors from kubectl output because during cluster bootstrapping
  85. # for clusters where the master node is registered, the apiserver will become
  86. # available and then get restarted as the kubelet configures the docker bridge.
  87. #
  88. # We are assigning the result of kubectl_retry get nodes operation to the res
  89. # variable in that way, to prevent stopping the whole script on an error.
  90. #
  91. # Bash command substitution $(kubectl_...) removes all trailing whitespaces
  92. # which are important for line counting.
  93. # Use trick from https://unix.stackexchange.com/a/383411 to avoid
  94. # newline truncation.
  95. node=$(kubectl_retry get nodes --no-headers; ret=$?; echo .; exit "$ret") && res="$?" || res="$?"
  96. node="${node%.}"
  97. if [ "${res}" -ne "0" ]; then
  98. if [[ "${attempt}" -gt "${last_run:-$MAX_ATTEMPTS}" ]]; then
  99. echo -e "${color_red} Failed to get nodes.${color_norm}"
  100. exit 1
  101. else
  102. continue
  103. fi
  104. fi
  105. found=$(echo -n "${node}" | wc -l)
  106. # Use grep || true so that empty result doesn't return nonzero exit code.
  107. ready=$(echo -n "${node}" | grep -c -v "NotReady" || true)
  108. if (( "${found}" == "${EXPECTED_NUM_NODES}" )) && (( "${ready}" == "${EXPECTED_NUM_NODES}")); then
  109. break
  110. elif (( "${found}" > "${EXPECTED_NUM_NODES}" )); then
  111. if [[ "${KUBE_USE_EXISTING_MASTER:-}" != "true" ]]; then
  112. echo -e "${color_red}Found ${found} nodes, but expected ${EXPECTED_NUM_NODES}. Your cluster may not behave correctly.${color_norm}"
  113. fi
  114. break
  115. elif (( "${ready}" > "${EXPECTED_NUM_NODES}")); then
  116. echo -e "${color_red}Found ${ready} ready nodes, but expected ${EXPECTED_NUM_NODES}. Your cluster may not behave correctly.${color_norm}"
  117. break
  118. else
  119. if [[ "${REQUIRED_NUM_NODES}" -le "${ready}" ]]; then
  120. echo -e "${color_green}Found ${REQUIRED_NUM_NODES} Nodes, allowing additional ${ADDITIONAL_ITERATIONS} iterations for other Nodes to join.${color_norm}"
  121. last_run="${last_run:-$((attempt + ADDITIONAL_ITERATIONS - 1))}"
  122. fi
  123. if [[ "${attempt}" -gt "${last_run:-$MAX_ATTEMPTS}" ]]; then
  124. 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}"
  125. kubectl_retry get nodes
  126. if [[ "${REQUIRED_NUM_NODES}" -gt "${ready}" ]]; then
  127. exit 1
  128. else
  129. return_value=2
  130. break
  131. fi
  132. else
  133. echo -e "${color_yellow}Waiting for ${EXPECTED_NUM_NODES} ready nodes. ${ready} ready nodes, ${found} registered. Retrying.${color_norm}"
  134. fi
  135. fi
  136. done
  137. echo "Found ${found} node(s)."
  138. kubectl_retry get nodes
  139. attempt=0
  140. while true; do
  141. # The "kubectl componentstatuses -o template" exports components health information.
  142. #
  143. # Echo the output and gather 2 counts:
  144. # - Total number of componentstatuses.
  145. # - Number of "healthy" components.
  146. cs_status=$(kubectl_retry get componentstatuses -o template --template='{{range .items}}{{with index .conditions 0}}{{.type}}:{{.status}}{{end}}{{"\n"}}{{end}}') || true
  147. componentstatuses=$(echo "${cs_status}" | grep -c 'Healthy:') || true
  148. healthy=$(echo "${cs_status}" | grep -c 'Healthy:True') || true
  149. if ((componentstatuses > healthy)) || ((componentstatuses == 0)); then
  150. if ((attempt < 5)); then
  151. echo -e "${color_yellow}Cluster not working yet.${color_norm}"
  152. attempt=$((attempt+1))
  153. sleep 30
  154. else
  155. echo -e " ${color_yellow}Validate output:${color_norm}"
  156. kubectl_retry get cs
  157. echo -e "${color_red}Validation returned one or more failed components. Cluster is probably broken.${color_norm}"
  158. exit 1
  159. fi
  160. else
  161. break
  162. fi
  163. done
  164. echo "Validate output:"
  165. kubectl_retry get cs || true
  166. if [ "${return_value}" == "0" ]; then
  167. echo -e "${color_green}Cluster validation succeeded${color_norm}"
  168. else
  169. echo -e "${color_yellow}Cluster validation encountered some problems, but cluster should be in working order${color_norm}"
  170. fi
  171. exit "${return_value}"