start-kubemark.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/usr/bin/env bash
  2. # Copyright 2015 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. # Script that creates a Kubemark cluster for any given cloud provider.
  16. set -o errexit
  17. set -o nounset
  18. set -o pipefail
  19. TMP_ROOT="$(dirname "${BASH_SOURCE[@]}")/../.."
  20. KUBE_ROOT=$(readlink -e "${TMP_ROOT}" 2> /dev/null || perl -MCwd -e 'print Cwd::abs_path shift' "${TMP_ROOT}")
  21. source "${KUBE_ROOT}/test/kubemark/skeleton/util.sh"
  22. source "${KUBE_ROOT}/test/kubemark/cloud-provider-config.sh"
  23. source "${KUBE_ROOT}/test/kubemark/${CLOUD_PROVIDER}/util.sh"
  24. source "${KUBE_ROOT}/cluster/kubemark/${CLOUD_PROVIDER}/config-default.sh"
  25. if [[ -f "${KUBE_ROOT}/test/kubemark/${CLOUD_PROVIDER}/startup.sh" ]] ; then
  26. source "${KUBE_ROOT}/test/kubemark/${CLOUD_PROVIDER}/startup.sh"
  27. fi
  28. source "${KUBE_ROOT}/cluster/kubemark/util.sh"
  29. KUBECTL="${KUBE_ROOT}/cluster/kubectl.sh"
  30. KUBEMARK_DIRECTORY="${KUBE_ROOT}/test/kubemark"
  31. RESOURCE_DIRECTORY="${KUBEMARK_DIRECTORY}/resources"
  32. LOCAL_KUBECONFIG="${RESOURCE_DIRECTORY}/kubeconfig.kubemark"
  33. INTERNAL_KUBECONFIG="${RESOURCE_DIRECTORY}/kubeconfig-internal.kubemark"
  34. # Generate a random 6-digit alphanumeric tag for the kubemark image.
  35. # Used to uniquify image builds across different invocations of this script.
  36. KUBEMARK_IMAGE_TAG=$(head /dev/urandom | tr -dc 'a-z0-9' | fold -w 6 | head -n 1)
  37. # Create a docker image for hollow-node and upload it to the appropriate docker registry.
  38. function create-and-upload-hollow-node-image {
  39. authenticate-docker
  40. KUBEMARK_IMAGE_REGISTRY="${KUBEMARK_IMAGE_REGISTRY:-${CONTAINER_REGISTRY}/${PROJECT}}"
  41. if [[ "${KUBEMARK_BAZEL_BUILD:-}" =~ ^[yY]$ ]]; then
  42. # Build+push the image through bazel.
  43. touch WORKSPACE # Needed for bazel.
  44. build_cmd=("bazel" "run" "//cluster/images/kubemark:push" "--define" "REGISTRY=${KUBEMARK_IMAGE_REGISTRY}" "--define" "IMAGE_TAG=${KUBEMARK_IMAGE_TAG}")
  45. run-cmd-with-retries "${build_cmd[@]}"
  46. else
  47. # Build+push the image through makefile.
  48. build_cmd=("make" "${KUBEMARK_IMAGE_MAKE_TARGET}")
  49. MAKE_DIR="${KUBE_ROOT}/cluster/images/kubemark"
  50. KUBEMARK_BIN="$(kube::util::find-binary-for-platform kubemark linux/amd64)"
  51. if [[ -z "${KUBEMARK_BIN}" ]]; then
  52. echo 'Cannot find cmd/kubemark binary'
  53. exit 1
  54. fi
  55. echo "Copying kubemark binary to ${MAKE_DIR}"
  56. cp "${KUBEMARK_BIN}" "${MAKE_DIR}"
  57. CURR_DIR=$(pwd)
  58. cd "${MAKE_DIR}"
  59. REGISTRY=${KUBEMARK_IMAGE_REGISTRY} IMAGE_TAG=${KUBEMARK_IMAGE_TAG} run-cmd-with-retries "${build_cmd[@]}"
  60. rm kubemark
  61. cd "$CURR_DIR"
  62. fi
  63. echo "Created and uploaded the kubemark hollow-node image to docker registry."
  64. # Cleanup the kubemark image after the script exits.
  65. if [[ "${CLEANUP_KUBEMARK_IMAGE:-}" == "true" ]]; then
  66. trap delete-kubemark-image EXIT
  67. fi
  68. }
  69. function delete-kubemark-image {
  70. delete-image "${KUBEMARK_IMAGE_REGISTRY}/kubemark:${KUBEMARK_IMAGE_TAG}"
  71. }
  72. # Generate secret and configMap for the hollow-node pods to work, prepare
  73. # manifests of the hollow-node and heapster replication controllers from
  74. # templates, and finally create these resources through kubectl.
  75. function create-kube-hollow-node-resources {
  76. # Create kubemark namespace.
  77. "${KUBECTL}" create -f "${RESOURCE_DIRECTORY}/kubemark-ns.json"
  78. # Create configmap for configuring hollow- kubelet, proxy and npd.
  79. "${KUBECTL}" create configmap "node-configmap" --namespace="kubemark" \
  80. --from-literal=content.type="${TEST_CLUSTER_API_CONTENT_TYPE}" \
  81. --from-file=kernel.monitor="${RESOURCE_DIRECTORY}/kernel-monitor.json"
  82. # Create secret for passing kubeconfigs to kubelet, kubeproxy and npd.
  83. # It's bad that all component shares the same kubeconfig.
  84. # TODO(https://github.com/kubernetes/kubernetes/issues/79883): Migrate all components to separate credentials.
  85. "${KUBECTL}" create secret generic "kubeconfig" --type=Opaque --namespace="kubemark" \
  86. --from-file=kubelet.kubeconfig="${HOLLOWNODE_KUBECONFIG}" \
  87. --from-file=kubeproxy.kubeconfig="${HOLLOWNODE_KUBECONFIG}" \
  88. --from-file=npd.kubeconfig="${HOLLOWNODE_KUBECONFIG}" \
  89. --from-file=heapster.kubeconfig="${HOLLOWNODE_KUBECONFIG}" \
  90. --from-file=cluster_autoscaler.kubeconfig="${HOLLOWNODE_KUBECONFIG}" \
  91. --from-file=dns.kubeconfig="${HOLLOWNODE_KUBECONFIG}"
  92. # Create addon pods.
  93. # Heapster.
  94. mkdir -p "${RESOURCE_DIRECTORY}/addons"
  95. sed "s@{{MASTER_IP}}@${MASTER_IP}@g" "${RESOURCE_DIRECTORY}/heapster_template.json" > "${RESOURCE_DIRECTORY}/addons/heapster.json"
  96. metrics_mem_per_node=4
  97. metrics_mem=$((200 + metrics_mem_per_node*NUM_NODES))
  98. sed -i'' -e "s@{{METRICS_MEM}}@${metrics_mem}@g" "${RESOURCE_DIRECTORY}/addons/heapster.json"
  99. metrics_cpu_per_node_numerator=${NUM_NODES}
  100. metrics_cpu_per_node_denominator=2
  101. metrics_cpu=$((80 + metrics_cpu_per_node_numerator / metrics_cpu_per_node_denominator))
  102. sed -i'' -e "s@{{METRICS_CPU}}@${metrics_cpu}@g" "${RESOURCE_DIRECTORY}/addons/heapster.json"
  103. eventer_mem_per_node=500
  104. eventer_mem=$((200 * 1024 + eventer_mem_per_node*NUM_NODES))
  105. sed -i'' -e "s@{{EVENTER_MEM}}@${eventer_mem}@g" "${RESOURCE_DIRECTORY}/addons/heapster.json"
  106. # Cluster Autoscaler.
  107. if [[ "${ENABLE_KUBEMARK_CLUSTER_AUTOSCALER:-}" == "true" ]]; then
  108. echo "Setting up Cluster Autoscaler"
  109. KUBEMARK_AUTOSCALER_MIG_NAME="${KUBEMARK_AUTOSCALER_MIG_NAME:-${NODE_INSTANCE_PREFIX}-group}"
  110. KUBEMARK_AUTOSCALER_MIN_NODES="${KUBEMARK_AUTOSCALER_MIN_NODES:-0}"
  111. KUBEMARK_AUTOSCALER_MAX_NODES="${KUBEMARK_AUTOSCALER_MAX_NODES:-10}"
  112. NUM_NODES=${KUBEMARK_AUTOSCALER_MAX_NODES}
  113. echo "Setting maximum cluster size to ${NUM_NODES}."
  114. KUBEMARK_MIG_CONFIG="autoscaling.k8s.io/nodegroup: ${KUBEMARK_AUTOSCALER_MIG_NAME}"
  115. sed "s/{{master_ip}}/${MASTER_IP}/g" "${RESOURCE_DIRECTORY}/cluster-autoscaler_template.json" > "${RESOURCE_DIRECTORY}/addons/cluster-autoscaler.json"
  116. sed -i'' -e "s@{{kubemark_autoscaler_mig_name}}@${KUBEMARK_AUTOSCALER_MIG_NAME}@g" "${RESOURCE_DIRECTORY}/addons/cluster-autoscaler.json"
  117. sed -i'' -e "s@{{kubemark_autoscaler_min_nodes}}@${KUBEMARK_AUTOSCALER_MIN_NODES}@g" "${RESOURCE_DIRECTORY}/addons/cluster-autoscaler.json"
  118. sed -i'' -e "s@{{kubemark_autoscaler_max_nodes}}@${KUBEMARK_AUTOSCALER_MAX_NODES}@g" "${RESOURCE_DIRECTORY}/addons/cluster-autoscaler.json"
  119. fi
  120. # Kube DNS.
  121. if [[ "${ENABLE_KUBEMARK_KUBE_DNS:-}" == "true" ]]; then
  122. echo "Setting up kube-dns"
  123. sed "s@{{dns_domain}}@${KUBE_DNS_DOMAIN}@g" "${RESOURCE_DIRECTORY}/kube_dns_template.yaml" > "${RESOURCE_DIRECTORY}/addons/kube_dns.yaml"
  124. fi
  125. "${KUBECTL}" create -f "${RESOURCE_DIRECTORY}/addons" --namespace="kubemark"
  126. # Create the replication controller for hollow-nodes.
  127. # We allow to override the NUM_REPLICAS when running Cluster Autoscaler.
  128. NUM_REPLICAS=${NUM_REPLICAS:-${NUM_NODES}}
  129. sed "s@{{numreplicas}}@${NUM_REPLICAS}@g" "${RESOURCE_DIRECTORY}/hollow-node_template.yaml" > "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  130. proxy_cpu=20
  131. if [ "${NUM_NODES}" -gt 1000 ]; then
  132. proxy_cpu=50
  133. fi
  134. proxy_cpu=${KUBEMARK_HOLLOW_PROXY_MILLICPU:-$proxy_cpu}
  135. proxy_mem_per_node=${KUBEMARK_HOLLOW_PROXY_MEM_PER_NODE_KB:-50}
  136. proxy_mem=$((100 * 1024 + proxy_mem_per_node*NUM_NODES))
  137. hollow_node_labels=${HOLLOW_NODE_LABELS:-$(calculate-node-labels)}
  138. hollow_kubelet_params=$(eval "for param in ${HOLLOW_KUBELET_TEST_ARGS:-}; do echo -n \\\"\$param\\\",; done")
  139. hollow_kubelet_params=${hollow_kubelet_params%?}
  140. hollow_proxy_params=$(eval "for param in ${HOLLOW_PROXY_TEST_ARGS:-}; do echo -n \\\"\$param\\\",; done")
  141. hollow_proxy_params=${hollow_proxy_params%?}
  142. sed -i'' -e "s@{{hollow_kubelet_millicpu}}@${KUBEMARK_HOLLOW_KUBELET_MILLICPU:-40}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  143. sed -i'' -e "s@{{hollow_kubelet_mem_Ki}}@${KUBEMARK_HOLLOW_KUBELET_MEM_KB:-$((100*1024))}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  144. sed -i'' -e "s@{{hollow_proxy_millicpu}}@${proxy_cpu}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  145. sed -i'' -e "s@{{hollow_proxy_mem_Ki}}@${proxy_mem}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  146. sed -i'' -e "s@{{npd_millicpu}}@${KUBEMARK_NPD_MILLICPU:-20}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  147. sed -i'' -e "s@{{npd_mem_Ki}}@${KUBEMARK_NPD_MEM_KB:-$((20*1024))}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  148. sed -i'' -e "s@{{kubemark_image_registry}}@${KUBEMARK_IMAGE_REGISTRY}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  149. sed -i'' -e "s@{{kubemark_image_tag}}@${KUBEMARK_IMAGE_TAG}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  150. sed -i'' -e "s@{{master_ip}}@${MASTER_IP}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  151. sed -i'' -e "s@{{hollow_node_labels}}@${hollow_node_labels}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  152. sed -i'' -e "s@{{hollow_kubelet_params}}@${hollow_kubelet_params}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  153. sed -i'' -e "s@{{hollow_proxy_params}}@${hollow_proxy_params}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  154. sed -i'' -e "s@{{kubemark_mig_config}}@${KUBEMARK_MIG_CONFIG:-}@g" "${RESOURCE_DIRECTORY}/hollow-node.yaml"
  155. "${KUBECTL}" create -f "${RESOURCE_DIRECTORY}/hollow-node.yaml" --namespace="kubemark"
  156. echo "Created secrets, configMaps, replication-controllers required for hollow-nodes."
  157. }
  158. # Wait until all hollow-nodes are running or there is a timeout.
  159. function wait-for-hollow-nodes-to-run-or-timeout {
  160. echo -n "Waiting for all hollow-nodes to become Running"
  161. start=$(date +%s)
  162. nodes=$("${KUBECTL}" --kubeconfig="${LOCAL_KUBECONFIG}" get node 2> /dev/null) || true
  163. ready=$(($(echo "${nodes}" | grep -vc "NotReady") - 1))
  164. until [[ "${ready}" -ge "${NUM_REPLICAS}" ]]; do
  165. echo -n "."
  166. sleep 1
  167. now=$(date +%s)
  168. # Fail it if it already took more than 30 minutes.
  169. if [ $((now - start)) -gt 1800 ]; then
  170. echo ""
  171. # shellcheck disable=SC2154 # Color defined in sourced script
  172. echo -e "${color_red} Timeout waiting for all hollow-nodes to become Running. ${color_norm}"
  173. # Try listing nodes again - if it fails it means that API server is not responding
  174. if "${KUBECTL}" --kubeconfig="${LOCAL_KUBECONFIG}" get node &> /dev/null; then
  175. echo "Found only ${ready} ready hollow-nodes while waiting for ${NUM_NODES}."
  176. else
  177. echo "Got error while trying to list hollow-nodes. Probably API server is down."
  178. fi
  179. pods=$("${KUBECTL}" get pods -l name=hollow-node --namespace=kubemark) || true
  180. running=$(($(echo "${pods}" | grep -c "Running")))
  181. echo "${running} hollow-nodes are reported as 'Running'"
  182. not_running=$(($(echo "${pods}" | grep -vc "Running") - 1))
  183. echo "${not_running} hollow-nodes are reported as NOT 'Running'"
  184. echo "${pods}" | grep -v Running
  185. exit 1
  186. fi
  187. nodes=$("${KUBECTL}" --kubeconfig="${LOCAL_KUBECONFIG}" get node 2> /dev/null) || true
  188. ready=$(($(echo "${nodes}" | grep -vc "NotReady") - 1))
  189. done
  190. # shellcheck disable=SC2154 # Color defined in sourced script
  191. echo -e "${color_green} Done!${color_norm}"
  192. }
  193. ############################### Main Function ########################################
  194. # Setup for hollow-nodes.
  195. function start-hollow-nodes {
  196. # shellcheck disable=SC2154 # Color defined in sourced script
  197. echo -e "${color_yellow}STARTING SETUP FOR HOLLOW-NODES${color_norm}"
  198. create-and-upload-hollow-node-image
  199. create-kube-hollow-node-resources
  200. wait-for-hollow-nodes-to-run-or-timeout
  201. }
  202. detect-project &> /dev/null
  203. create-kubemark-master
  204. if [ -f "${INTERNAL_KUBECONFIG}" ]; then
  205. HOLLOWNODE_KUBECONFIG="${INTERNAL_KUBECONFIG}"
  206. else
  207. HOLLOWNODE_KUBECONFIG="${LOCAL_KUBECONFIG}"
  208. fi
  209. MASTER_IP=$(grep server "${HOLLOWNODE_KUBECONFIG}" | awk -F "/" '{print $3}')
  210. start-hollow-nodes
  211. echo ""
  212. echo "Master IP: ${MASTER_IP}"
  213. echo "Kubeconfig for kubemark master is written in ${LOCAL_KUBECONFIG}"