configure.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. # Due to the GCE custom metadata size limit, we split the entire script into two
  16. # files configure.sh and configure-helper.sh. The functionality of downloading
  17. # kubernetes configuration, manifests, docker images, and binary files are
  18. # put in configure.sh, which is uploaded via GCE custom metadata.
  19. set -o errexit
  20. set -o nounset
  21. set -o pipefail
  22. function set-broken-motd {
  23. cat > /etc/motd <<EOF
  24. Broken (or in progress) Kubernetes node setup! Check the cluster initialization status
  25. using the following commands.
  26. Master instance:
  27. - sudo systemctl status kube-master-installation
  28. - sudo systemctl status kube-master-configuration
  29. Node instance:
  30. - sudo systemctl status kube-node-installation
  31. - sudo systemctl status kube-node-configuration
  32. EOF
  33. }
  34. function download-kube-env {
  35. # Fetch kube-env from GCE metadata server.
  36. local -r tmp_kube_env="/tmp/kube-env.yaml"
  37. curl --fail --retry 5 --retry-delay 3 --silent --show-error \
  38. -H "X-Google-Metadata-Request: True" \
  39. -o "${tmp_kube_env}" \
  40. http://metadata.google.internal/computeMetadata/v1/instance/attributes/kube-env
  41. # Convert the yaml format file into a shell-style file.
  42. eval $(python -c '''
  43. import pipes,sys,yaml
  44. for k,v in yaml.load(sys.stdin).iteritems():
  45. print("readonly {var}={value}".format(var = k, value = pipes.quote(str(v))))
  46. ''' < "${tmp_kube_env}" > "${KUBE_HOME}/kube-env")
  47. rm -f "${tmp_kube_env}"
  48. }
  49. function validate-hash {
  50. local -r file="$1"
  51. local -r expected="$2"
  52. actual=$(sha1sum ${file} | awk '{ print $1 }') || true
  53. if [[ "${actual}" != "${expected}" ]]; then
  54. echo "== ${file} corrupted, sha1 ${actual} doesn't match expected ${expected} =="
  55. return 1
  56. fi
  57. }
  58. # Retry a download until we get it. Takes a hash and a set of URLs.
  59. #
  60. # $1 is the sha1 of the URL. Can be "" if the sha1 is unknown.
  61. # $2+ are the URLs to download.
  62. function download-or-bust {
  63. local -r hash="$1"
  64. shift 1
  65. local -r urls=( $* )
  66. while true; do
  67. for url in "${urls[@]}"; do
  68. local file="${url##*/}"
  69. rm -f "${file}"
  70. if ! curl -f --ipv4 -Lo "${file}" --connect-timeout 20 --max-time 300 --retry 6 --retry-delay 10 "${url}"; then
  71. echo "== Failed to download ${url}. Retrying. =="
  72. elif [[ -n "${hash}" ]] && ! validate-hash "${file}" "${hash}"; then
  73. echo "== Hash validation of ${url} failed. Retrying. =="
  74. else
  75. if [[ -n "${hash}" ]]; then
  76. echo "== Downloaded ${url} (SHA1 = ${hash}) =="
  77. else
  78. echo "== Downloaded ${url} =="
  79. fi
  80. return
  81. fi
  82. done
  83. done
  84. }
  85. function split-commas {
  86. echo $1 | tr "," "\n"
  87. }
  88. # Downloads kubernetes binaries and kube-system manifest tarball, unpacks them,
  89. # and places them into suitable directories. Files are placed in /home/kubernetes.
  90. function install-kube-binary-config {
  91. cd "${KUBE_HOME}"
  92. local -r server_binary_tar_urls=( $(split-commas "${SERVER_BINARY_TAR_URL}") )
  93. local -r server_binary_tar="${server_binary_tar_urls[0]##*/}"
  94. if [[ -n "${SERVER_BINARY_TAR_HASH:-}" ]]; then
  95. local -r server_binary_tar_hash="${SERVER_BINARY_TAR_HASH}"
  96. else
  97. echo "Downloading binary release sha1 (not found in env)"
  98. download-or-bust "" "${server_binary_tar_urls[@]/.tar.gz/.tar.gz.sha1}"
  99. local -r server_binary_tar_hash=$(cat "${server_binary_tar}.sha1")
  100. fi
  101. echo "Downloading binary release tar"
  102. download-or-bust "${server_binary_tar_hash}" "${server_binary_tar_urls[@]}"
  103. tar xzf "${KUBE_HOME}/${server_binary_tar}" -C "${KUBE_HOME}" --overwrite
  104. # Copy docker_tag and image files to ${KUBE_HOME}/kube-docker-files.
  105. src_dir="${KUBE_HOME}/kubernetes/server/bin"
  106. dst_dir="${KUBE_HOME}/kube-docker-files"
  107. mkdir -p "${dst_dir}"
  108. cp "${src_dir}/"*.docker_tag "${dst_dir}"
  109. if [[ "${KUBERNETES_MASTER:-}" == "false" ]]; then
  110. cp "${src_dir}/kube-proxy.tar" "${dst_dir}"
  111. else
  112. cp "${src_dir}/kube-apiserver.tar" "${dst_dir}"
  113. cp "${src_dir}/kube-controller-manager.tar" "${dst_dir}"
  114. cp "${src_dir}/kube-scheduler.tar" "${dst_dir}"
  115. cp -r "${KUBE_HOME}/kubernetes/addons" "${dst_dir}"
  116. fi
  117. local -r kube_bin="${KUBE_HOME}/bin"
  118. mv "${src_dir}/kubelet" "${kube_bin}"
  119. mv "${src_dir}/kubectl" "${kube_bin}"
  120. if [[ "${NETWORK_PROVIDER:-}" == "kubenet" ]] || \
  121. [[ "${NETWORK_PROVIDER:-}" == "cni" ]]; then
  122. #TODO(andyzheng0831): We should make the cni version number as a k8s env variable.
  123. local -r cni_tar="cni-9d5e6e60e79491207834ae8439e80c943db65a69.tar.gz"
  124. download-or-bust "" "https://storage.googleapis.com/kubernetes-release/network-plugins/${cni_tar}"
  125. local -r cni_dir="${KUBE_HOME}/cni"
  126. mkdir -p "${cni_dir}"
  127. tar xzf "${KUBE_HOME}/${cni_tar}" -C "${cni_dir}" --overwrite
  128. mv "${cni_dir}/bin"/* "${kube_bin}"
  129. rmdir "${cni_dir}/bin"
  130. rm -f "${KUBE_HOME}/${cni_tar}"
  131. fi
  132. mv "${KUBE_HOME}/kubernetes/LICENSES" "${KUBE_HOME}"
  133. mv "${KUBE_HOME}/kubernetes/kubernetes-src.tar.gz" "${KUBE_HOME}"
  134. # Put kube-system pods manifests in ${KUBE_HOME}/kube-manifests/.
  135. dst_dir="${KUBE_HOME}/kube-manifests"
  136. mkdir -p "${dst_dir}"
  137. local -r manifests_tar_urls=( $(split-commas "${KUBE_MANIFESTS_TAR_URL}") )
  138. local -r manifests_tar="${manifests_tar_urls[0]##*/}"
  139. if [ -n "${KUBE_MANIFESTS_TAR_HASH:-}" ]; then
  140. local -r manifests_tar_hash="${KUBE_MANIFESTS_TAR_HASH}"
  141. else
  142. echo "Downloading k8s manifests sha1 (not found in env)"
  143. download-or-bust "" "${manifests_tar_urls[@]/.tar.gz/.tar.gz.sha1}"
  144. local -r manifests_tar_hash=$(cat "${manifests_tar}.sha1")
  145. fi
  146. echo "Downloading k8s manifests tar"
  147. download-or-bust "${manifests_tar_hash}" "${manifests_tar_urls[@]}"
  148. tar xzf "${KUBE_HOME}/${manifests_tar}" -C "${dst_dir}" --overwrite
  149. local -r kube_addon_registry="${KUBE_ADDON_REGISTRY:-gcr.io/google_containers}"
  150. if [[ "${kube_addon_registry}" != "gcr.io/google_containers" ]]; then
  151. find "${dst_dir}" -name \*.yaml -or -name \*.yaml.in | \
  152. xargs sed -ri "s@(image:\s.*)gcr.io/google_containers@\1${kube_addon_registry}@"
  153. find "${dst_dir}" -name \*.manifest -or -name \*.json | \
  154. xargs sed -ri "s@(image\":\s+\")gcr.io/google_containers@\1${kube_addon_registry}@"
  155. fi
  156. cp "${dst_dir}/kubernetes/gci-trusty/gci-configure-helper.sh" "${KUBE_HOME}/bin/configure-helper.sh"
  157. cp "${dst_dir}/kubernetes/gci-trusty/health-monitor.sh" "${KUBE_HOME}/bin/health-monitor.sh"
  158. chmod -R 755 "${kube_bin}"
  159. # Clean up.
  160. rm -rf "${KUBE_HOME}/kubernetes"
  161. rm -f "${KUBE_HOME}/${server_binary_tar}"
  162. rm -f "${KUBE_HOME}/${server_binary_tar}.sha1"
  163. rm -f "${KUBE_HOME}/${manifests_tar}"
  164. rm -f "${KUBE_HOME}/${manifests_tar}.sha1"
  165. }
  166. ######### Main Function ##########
  167. echo "Start to install kubernetes files"
  168. set-broken-motd
  169. KUBE_HOME="/home/kubernetes"
  170. download-kube-env
  171. source "${KUBE_HOME}/kube-env"
  172. install-kube-binary-config
  173. echo "Done for installing kubernetes files"