get-kube-local.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/bin/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. declare -r RED="\033[0;31m"
  22. declare -r GREEN="\033[0;32m"
  23. declare -r YELLOW="\033[0;33m"
  24. function echo_green {
  25. echo -e "${GREEN}$1"; tput sgr0
  26. }
  27. function echo_red {
  28. echo -e "${RED}$1"; tput sgr0
  29. }
  30. function echo_yellow {
  31. echo -e "${YELLOW}$1"; tput sgr0
  32. }
  33. function run {
  34. # For a moment we need to change bash options to capture message if a command fails.
  35. set +o errexit
  36. output=$($1 2>&1)
  37. exit_code=$?
  38. set -o errexit
  39. if [ $exit_code -eq 0 ]; then
  40. echo_green "SUCCESS"
  41. else
  42. echo_red "FAILED"
  43. echo $output >&2
  44. exit 1
  45. fi
  46. }
  47. function create_cluster {
  48. echo "Creating a local cluster:"
  49. echo -e -n "\tStarting kubelet..."
  50. run "docker run \
  51. --volume=/:/rootfs:ro \
  52. --volume=/sys:/sys:ro \
  53. --volume=/var/lib/docker/:/var/lib/docker:rw \
  54. --volume=/var/lib/kubelet/:/var/lib/kubelet:rw \
  55. --volume=/var/run:/var/run:rw \
  56. --net=host \
  57. --pid=host \
  58. --privileged=true \
  59. -d \
  60. gcr.io/google_containers/hyperkube-${arch}:${release} \
  61. /hyperkube kubelet \
  62. --containerized \
  63. --hostname-override="127.0.0.1" \
  64. --address="0.0.0.0" \
  65. --api-servers=http://localhost:8080 \
  66. --config=/etc/kubernetes/manifests \
  67. --allow-privileged=true \
  68. --cluster-dns=10.0.0.10 \
  69. --cluster-domain=cluster.local \
  70. --v=2"
  71. echo -e -n "\tWaiting for master components to start..."
  72. while true; do
  73. local running_count=$(kubectl -s=http://${KUBE_HOST}:8080 get pods --no-headers 2>/dev/null | grep "Running" | wc -l)
  74. # We expect to have 3 running pods - etcd, master and kube-proxy.
  75. if [ "$running_count" -ge 3 ]; then
  76. break
  77. fi
  78. echo -n "."
  79. sleep 1
  80. done
  81. echo_green "SUCCESS"
  82. echo_green "Cluster created!"
  83. echo ""
  84. kubectl -s http://${KUBE_HOST}:8080 clusterinfo
  85. }
  86. function get_latest_version_number {
  87. local -r latest_url="https://storage.googleapis.com/kubernetes-release/release/stable.txt"
  88. if [[ $(which wget) ]]; then
  89. wget -qO- ${latest_url}
  90. elif [[ $(which curl) ]]; then
  91. curl -Ss ${latest_url}
  92. else
  93. echo_red "Couldn't find curl or wget. Bailing out."
  94. exit 4
  95. fi
  96. }
  97. latest_release=$(get_latest_version_number)
  98. release=${KUBE_VERSION:-${latest_release}}
  99. uname=$(uname)
  100. if [[ "${uname}" == "Darwin" ]]; then
  101. platform="darwin"
  102. elif [[ "${uname}" == "Linux" ]]; then
  103. platform="linux"
  104. else
  105. echo_red "Unknown, unsupported platform: (${uname})."
  106. echo_red "Supported platforms: Linux, Darwin."
  107. echo_red "Bailing out."
  108. exit 2
  109. fi
  110. machine=$(uname -m)
  111. if [[ "${machine}" == "x86_64" ]]; then
  112. arch="amd64"
  113. elif [[ "${machine}" == "i686" ]]; then
  114. arch="386"
  115. elif [[ "${machine}" == "arm*" ]]; then
  116. arch="arm"
  117. elif [[ "${machine}" == "s390x*" ]]; then
  118. arch="s390x"
  119. else
  120. echo_red "Unknown, unsupported architecture (${machine})."
  121. echo_red "Supported architectures x86_64, i686, arm, s390x."
  122. echo_red "Bailing out."
  123. exit 3
  124. fi
  125. kubectl_url="https://storage.googleapis.com/kubernetes-release/release/${release}/bin/${platform}/${arch}/kubectl"
  126. if [[ $(ls . | grep ^kubectl$ | wc -l) -lt 1 ]]; then
  127. echo -n "Downloading kubectl binary..."
  128. if [[ $(which wget) ]]; then
  129. run "wget ${kubectl_url}"
  130. elif [[ $(which curl) ]]; then
  131. run "curl -OL ${kubectl_url}"
  132. else
  133. echo_red "Couldn't find curl or wget. Bailing out."
  134. exit 1
  135. fi
  136. chmod a+x kubectl
  137. echo ""
  138. else
  139. # TODO: We should detect version of kubectl binary if it too old
  140. # download newer version.
  141. echo "Detected existing kubectl binary. Skipping download."
  142. fi
  143. create_cluster
  144. echo ""
  145. echo ""
  146. echo "To list the nodes in your cluster run"
  147. echo_yellow "\tkubectl -s=http://${KUBE_HOST}:8080 get nodes"
  148. echo ""
  149. echo "To run your first pod run"
  150. echo_yellow "\tkubectl -s http://${KUBE_HOST}:8080 run nginx --image=nginx --port=80"