get-kube-local.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. # This script will download latest version of kubectl command line tool and will
  16. # bring up a local Kubernetes cluster with a single node.
  17. set -o errexit
  18. set -o nounset
  19. set -o pipefail
  20. KUBE_HOST=${KUBE_HOST:-localhost}
  21. KUBELET_KUBECONFIG=${KUBELET_KUBECONFIG:-"/var/run/kubernetes/kubelet.kubeconfig"}
  22. declare -r RED="\\033[0;31m"
  23. declare -r GREEN="\\033[0;32m"
  24. declare -r YELLOW="\\033[0;33m"
  25. function echo_green {
  26. echo -e "${GREEN}$1"; tput sgr0
  27. }
  28. function echo_red {
  29. echo -e "${RED}$1"; tput sgr0
  30. }
  31. function echo_yellow {
  32. echo -e "${YELLOW}$1"; tput sgr0
  33. }
  34. function run {
  35. # For a moment we need to change bash options to capture message if a command fails.
  36. set +o errexit
  37. output=$($1 2>&1)
  38. exit_code=$?
  39. set -o errexit
  40. if [ $exit_code -eq 0 ]; then
  41. echo_green "SUCCESS"
  42. else
  43. echo_red "FAILED"
  44. echo "${output}" >&2
  45. exit 1
  46. fi
  47. }
  48. # Creates a kubeconfig file for the kubelet.
  49. # Args: the IP address of the API server (e.g. "http://localhost:8080"), destination file path
  50. function create-kubelet-kubeconfig() {
  51. #local api_addr="${1}"
  52. local destination dest_dir
  53. destination="${2}"
  54. if [[ -z "${destination}" ]]; then
  55. echo "Must provide destination path to create Kubelet kubeconfig file!"
  56. exit 1
  57. fi
  58. echo "Creating Kubelet kubeconfig file"
  59. dest_dir="$(dirname "${destination}")"
  60. mkdir -p "${dest_dir}" &>/dev/null || sudo mkdir -p "${dest_dir}"
  61. sudo=$(test -w "${dest_dir}" || echo "sudo -E")
  62. cat <<EOF | ${sudo} tee "${destination}" > /dev/null
  63. apiVersion: v1
  64. kind: Config
  65. clusters:
  66. - cluster:
  67. server: http://localhost:8080
  68. name: local
  69. contexts:
  70. - context:
  71. cluster: local
  72. name: local
  73. current-context: local
  74. EOF
  75. }
  76. function create_cluster {
  77. echo "Creating a local cluster:"
  78. echo -e -n "\\tStarting kubelet..."
  79. create-kubelet-kubeconfig "http://localhost:8080" "${KUBELET_KUBECONFIG}"
  80. run "docker run \
  81. --volume=/:/rootfs:ro \
  82. --volume=/sys:/sys:ro \
  83. --volume=/var/lib/docker/:/var/lib/docker:rw \
  84. --volume=/var/lib/kubelet/:/var/lib/kubelet:rw \
  85. --volume=/usr/libexec/kubernetes/kubelet-plugins/volume/exec:/usr/libexec/kubernetes/kubelet-plugins/volume/exec:rw \
  86. --volume=/var/run:/var/run:rw \
  87. --volume=/run/xtables.lock:/run/xtables.lock:rw \
  88. --net=host \
  89. --pid=host \
  90. --privileged=true \
  91. -d \
  92. k8s.gcr.io/hyperkube-${arch}:${release} \
  93. /hyperkube kubelet \
  94. --containerized \
  95. --hostname-override=127.0.0.1 \
  96. --address=0.0.0.0 \
  97. --kubeconfig=${KUBELET_KUBECONFIG} \
  98. --pod-manifest-path=/etc/kubernetes/manifests \
  99. --cluster-dns=10.0.0.10 \
  100. --cluster-domain=cluster.local \
  101. --v=2"
  102. echo -e -n "\tWaiting for master components to start..."
  103. while true; do
  104. local running_count
  105. running_count=$(kubectl "-s=http://${KUBE_HOST}:8080" get pods --no-headers --namespace=kube-system 2>/dev/null | grep -c "Running")
  106. # We expect to have 3 running pods - etcd, master and kube-proxy.
  107. if [ "$running_count" -ge 3 ]; then
  108. break
  109. fi
  110. echo -n "."
  111. sleep 1
  112. done
  113. echo_green "SUCCESS"
  114. echo_green "Cluster created!"
  115. echo ""
  116. kubectl -s "http://${KUBE_HOST}:8080" clusterinfo
  117. }
  118. function get_latest_version_number {
  119. local -r latest_url="https://storage.googleapis.com/kubernetes-release/release/stable.txt"
  120. if [[ $(which wget) ]]; then
  121. wget -qO- ${latest_url}
  122. elif [[ $(which curl) ]]; then
  123. curl -Ss ${latest_url}
  124. else
  125. echo_red "Couldn't find curl or wget. Bailing out."
  126. exit 4
  127. fi
  128. }
  129. latest_release=$(get_latest_version_number)
  130. release=${KUBE_VERSION:-${latest_release}}
  131. uname=$(uname)
  132. if [[ "${uname}" == "Darwin" ]]; then
  133. platform="darwin"
  134. elif [[ "${uname}" == "Linux" ]]; then
  135. platform="linux"
  136. else
  137. echo_red "Unknown, unsupported platform: (${uname})."
  138. echo_red "Supported platforms: Linux, Darwin."
  139. echo_red "Bailing out."
  140. exit 2
  141. fi
  142. machine=$(uname -m)
  143. if [[ "${machine}" == "x86_64" ]]; then
  144. arch="amd64"
  145. elif [[ "${machine}" == "i686" ]]; then
  146. arch="386"
  147. elif [[ "${machine}" == "arm*" ]]; then
  148. arch="arm"
  149. elif [[ "${machine}" == "s390x*" ]]; then
  150. arch="s390x"
  151. else
  152. echo_red "Unknown, unsupported architecture (${machine})."
  153. echo_red "Supported architectures x86_64, i686, arm, s390x."
  154. echo_red "Bailing out."
  155. exit 3
  156. fi
  157. kubectl_url="https://storage.googleapis.com/kubernetes-release/release/${release}/bin/${platform}/${arch}/kubectl"
  158. if [[ ! -f ./kubectl ]]; then
  159. echo -n "Downloading kubectl binary..."
  160. if [[ $(which wget) ]]; then
  161. run "wget ${kubectl_url}"
  162. elif [[ $(which curl) ]]; then
  163. run "curl -OL ${kubectl_url}"
  164. else
  165. echo_red "Couldn't find curl or wget. Bailing out."
  166. exit 1
  167. fi
  168. chmod a+x kubectl
  169. echo ""
  170. else
  171. # TODO: We should detect version of kubectl binary if it too old
  172. # download newer version.
  173. echo "Detected existing kubectl binary. Skipping download."
  174. fi
  175. create_cluster
  176. echo ""
  177. echo ""
  178. echo "To list the nodes in your cluster run"
  179. echo_yellow "\\tkubectl -s=http://${KUBE_HOST}:8080 get nodes"
  180. echo ""
  181. echo "To run your first pod run"
  182. echo_yellow "\\tkubectl -s http://${KUBE_HOST}:8080 run nginx --image=nginx --port=80"