-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube.sh
executable file
·105 lines (90 loc) · 3.65 KB
/
kube.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/bash
#
# Usage: ./kube.sh
#
# This script will deploy a HyperKube kubelet Docker container.
#
# The kubelet will then spawn the apiserver, controller, scheduler, etc
# necessary to run Kubernetes. this effectively runs a Kubernetes cluster
# as a series of microservice Docker containers
#
#
export K8S_VERSION=1.5.2
export BINDIR="$HOME/bin"
ECHO='echo -e'
command="$(echo $1 | tr '[A-Z]' '[a-z]')"
# If "down" is given as the command, shut down hyperkube
if [ "$command" == "down" ]; then
# Warn user of consequences
$ECHO 'WARNING: Shutting down Kubernetes will delete all containers running under Kubernetes?'
$ECHO 'This will DELETE ALL CONTAINER DATA that is not stored on a peristent volume mount.\n'
# Confirm that user knows what they're doing.
read -p 'Are you sure you want to continue? [y/N] ' confirm_shutdown
if [ "${confirm_shutdown:0:1}" != "y" -a "${confirm_shutdown:0:1}" != "Y" ]; then
exit 1
fi
# Remove kubelet first, or else it will continue to respawn killed containers
$ECHO 'Stopping Kubelet...'
docker stop kubelet >/dev/null 2>&1
# Use at your own risk: stop and remove all k8s Docker containers
$ECHO 'Cleaning up leftover Kubernetes resources...'
docker rm -f $(docker ps -a | grep k8s | awk '{print $1}') >/dev/null 2>&1
$ECHO 'Kubernetes has been shutdown!'
exit 0
fi
# If "deploy-tools" is passed as a command, start a container to remotely deploy Labs Workbench using Ansible
# DEPRECATED: This will go away as we move toward kargo
if [ "$command" == "deploy-tools" ]; then
docker run -it --name deploy-tools -v `pwd`/deploy-tools:/root/SAVED_AND_SENSITIVE_VOLUME ndslabs/deploy-tools:latest bash
exit 0
fi
#
# By default, start Kubernetes via Hyperkube
#
# --volume=/var/lib/kubelet:/var/lib/kubelet:rw,rslave \
$ECHO 'Starting Hyperkube Kubelet...'
docker --version >/dev/null 2>&1 || ($ECHO 'Docker must be installed to run Kubernetes Hyperkube. If you prefer to use minikube, please run minikube command.' && exit 1)
(docker run \
--restart=always \
--volume=/:/rootfs:ro \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:rw \
--volume=/var/lib/kubelet:/var/lib/kubelet:rw,shared \
--volume=/var/run:/var/run:rw \
--volume=$(pwd)/manifests:/etc/kubernetes/manifests \
--net=host \
--pid=host \
--privileged=true \
--name=kubelet \
-d \
gcr.io/google_containers/hyperkube-amd64:v${K8S_VERSION} \
/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=5 \
>/dev/null 2>&1 \
|| docker start kubelet >/dev/null 2>&1)
$ECHO 'Waiting for Kubernetes API server to start on port 8080...'
#
# Download kubectl, if necessary
#
if [ ! -d "$BINDIR" ]; then
mkdir -p $BINDIR
$ECHO "Downloading kubectl binary to $BINDIR..."
curl http://storage.googleapis.com/kubernetes-release/release/v${K8S_VERSION}/bin/linux/amd64/kubectl -o ~/bin/kubectl
chmod +x ~/bin/kubectl
# TODO: Need an elegant way to add bins to PATH programmatically
export PATH="$BINDIR:$PATH"
$ECHO "Be sure to execute 'export PATH=$BINDIR:\$PATH' to add the directory contaning kubectl to your PATH."
fi
# Wait for Kubernetes to start
until $(curl --output /dev/null --silent --head --fail http://localhost:8080); do
$ECHO 'Trying again in 5 seconds...'
sleep 5s # wait for 5s before checking again
kube_output=$($BINDIR/kubectl get pods)
done
$ECHO 'Kubernetes has started!'
$ECHO 'You can access your cluster using the kubectl binary.'