Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a script to run local docker based cluster #18425

Merged
merged 1 commit into from Dec 10, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
168 changes: 168 additions & 0 deletions cluster/get-kube-local.sh
@@ -0,0 +1,168 @@
#!/bin/bash

# Copyright 2014 The Kubernetes Authors All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: 2015 (at least for a few more weeks)

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This script will download latest version of kubectl command line tool and will
# bring up a local Kubernetes cluster with a single node.
#
# Usage:
# wget -q -O - https://get.k8s.io/local | bash
# or
# curl -sS https://get.k8s.io/local | bash
#
# On Mac OS X you'll have to additionally pass env variable:
# wget -q -O - https://get.k8s.io/local | KUBE_HOST=<docker_machine_ip> bash -c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the docker machine ip something we can auto-discover on mac so that users don't have to do it themselves?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be hard, because it depends which docker machine you are using (they have names). However it's possible I'm missing something as I haven't actually used it too many times :)

set -o errexit
set -o nounset
set -o pipefail

KUBE_HOST=${KUBE_HOST:-localhost}

RED="\033[0;31m"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want these to be constants, you can put declare -r at the beginning of each line.

GREEN="\033[0;32m"
ORANGE="\033[0;33m"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some people would call this color yellow...


function echo_green {
echo -e "${GREEN}$1"; tput sgr0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the tput sgr0 reset the color? The way I've seen this done before (by @mbforbes) is to set a var like COLOR_NORM="\033[0;m" and then do echo ${GREEN}$1${COLOR_NORM}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's equivalent, however if you prefer I can change that if you prefer it.

}

function echo_red {
echo -e "${RED}$1"; tput sgr0
}

function echo_orange {
echo -e "${ORANGE}$1"; tput sgr0
}

function run {
output=`$1 2>&1 || true`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer $() instead of `` for executing subshells.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my own education - what's the difference in bash?

if [ $? -eq 0 ]; then
echo_green "SUCCESS"
else
echo_red "FAILED"
echo $output >&2
exit 1
fi
}

function create_cluster {
echo "Creating a local cluster:"
echo -e -n "\tStarting kubelet..."
run "docker run \
--volume=/:/rootfs:ro \
--volume=/sys:/sys:ro \
--volume=/dev:/dev \
--volume=/var/lib/docker/:/var/lib/docker:rw \
--volume=/var/lib/kubelet/:/var/lib/kubelet:rw \
--volume=/var/run:/var/run:rw \
--net=host \
--pid=host \
--privileged=true \
-d \
gcr.io/google_containers/hyperkube-${arch}:${release} \
/hyperkube kubelet \
--containerized \
--hostname-override="127.0.0.1" \
--address="0.0.0.0" \
--api-servers=http://localhost:8080 \
--config=/etc/kubernetes/manifests \
--allow-privileged=true \
--v=2"

echo -e -n "\tWaiting for master components to start..."
while true; do
local running_count=`kubectl -s=http://${KUBE_HOST}:8080 get pods --no-headers 2>/dev/null | grep "Running" | wc -l`
# We expect to have 3 running pods - etcd, master and kube-proxy.
if [ "$running_count" -ge 3 ]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might also want to break after some large amount of time (10 minutes?) and say that there was an error.

break
fi
echo -n "."
sleep 1
done
echo_green "SUCCESS"
echo_green "Cluster created!"
echo ""
kubectl -s http://${KUBE_HOST}:8080 clusterinfo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your recent PR replaced the -s ... with instructions for setting up a kubeconfig entry. Why not do that here (there should be reusable functions available to create one).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not obvious to me what should be the cluster/context name we should use to avoid overwriting something that already existed.

}

function get_latest_version_number {
local -r latest_url="https://storage.googleapis.com/kubernetes-release/release/stable.txt"
if [[ $(which wget) ]]; then
wget -qO- ${latest_url}
elif [[ $(which curl) ]]; then
curl -Ss ${latest_url}
else
echo_red "Couldn't find curl or wget. Bailing out."
exit 4
fi
}

release=$(get_latest_version_number)

uname=$(uname)
if [[ "${uname}" == "Darwin" ]]; then
platform="darwin"
elif [[ "${uname}" == "Linux" ]]; then
platform="linux"
else
echo_red "Unknown, unsupported platform: (${uname})."
echo_red "Supported platforms: Linux, Darwin."
echo_red "Bailing out."
exit 2
fi

machine=$(uname -m)
if [[ "${machine}" == "x86_64" ]]; then
arch="amd64"
elif [[ "${machine}" == "i686" ]]; then
arch="386"
elif [[ "${machine}" == "arm*" ]]; then
arch="arm"
elif [[ "${machine}" == "s390x*" ]]; then
arch="s390x"
else
echo_red "Unknown, unsupported architecture (${machine})."
echo_red "Supported architectures x86_64, i686, arm, s390x."
echo_red "Bailing out."
exit 3
fi

kubectl_url=https://storage.googleapis.com/kubernetes-release/release/${release}/bin/${platform}/${arch}/kubectl
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quote the value since it contains vars that will be expanded.


if [[ `ls . | grep ^kubectl$ | wc -l` -lt 1 ]]; then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use $() for the subshell invocations.

echo -n "Downloading kubectl binary..."
if [[ $(which wget) ]]; then
run "wget ${kubectl_url}"
elif [[ $(which curl) ]]; then
run "curl -L ${kubectl_url}"
else
echo_red "Couldn't find curl or wget. Bailing out."
exit 1
fi
chmod a+x kubectl
echo ""
else
echo "Detected existing kubectl binary. Skipping download."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a TODO to make sure it's somewhat recent. If we find a 0.1.0 kubectl binary it probably isn't going to be very useful....

fi

create_cluster

echo ""
echo ""
echo "To list the nodes in your cluster run"
echo_orange "\tkubectl -s=http://${KUBE_HOST}:8080 get nodes"
echo ""
echo "To run your first pod run"
echo_orange "\tkubectl -s http://${KUBE_HOST}:8080 run nginx --image=nginx --port=80"