k3s/docs/getting-started-guides/docker-multinode/worker.sh

190 lines
5.6 KiB
Bash
Raw Normal View History

#!/bin/bash
# Copyright 2015 The Kubernetes Authors All rights reserved.
#
# 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.
# A scripts to install k8s worker node.
# Author @wizard_cxy @reouser
set -e
# Make sure docker daemon is running
if ( ! ps -ef | grep "/usr/bin/docker" | grep -v 'grep' &> /dev/null ); then
2015-07-23 03:09:05 +00:00
echo "Docker is not running on this machine!"
exit 1
fi
# Make sure k8s version env is properly set
if [ -z ${K8S_VERSION} ]; then
2015-09-07 06:16:45 +00:00
K8S_VERSION="1.0.3"
echo "K8S_VERSION is not set, using default: ${K8S_VERSION}"
else
2015-07-23 03:09:05 +00:00
echo "k8s version is set to: ${K8S_VERSION}"
fi
2015-09-07 06:16:45 +00:00
# Run as root
if [ "$(id -u)" != "0" ]; then
2015-07-23 03:09:05 +00:00
echo >&2 "Please run as root"
exit 1
fi
# Make sure master ip is properly set
if [ -z ${MASTER_IP} ]; then
2015-07-23 03:09:05 +00:00
echo "Please export MASTER_IP in your env"
exit 1
else
2015-07-23 03:09:05 +00:00
echo "k8s master is set to: ${MASTER_IP}"
fi
# Check if a command is valid
command_exists() {
2015-07-23 03:09:05 +00:00
command -v "$@" > /dev/null 2>&1
}
lsb_dist=""
# Detect the OS distro, we support ubuntu, debian, mint, centos, fedora dist
detect_lsb() {
2015-07-23 03:09:05 +00:00
case "$(uname -m)" in
*64)
;;
*)
echo "Error: We currently only support 64-bit platforms."
exit 1
;;
2015-07-23 03:09:05 +00:00
esac
if command_exists lsb_release; then
lsb_dist="$(lsb_release -si)"
fi
if [ -z ${lsb_dist} ] && [ -r /etc/lsb-release ]; then
lsb_dist="$(. /etc/lsb-release && echo "$DISTRIB_ID")"
fi
if [ -z ${lsb_dist} ] && [ -r /etc/debian_version ]; then
lsb_dist='debian'
fi
if [ -z ${lsb_dist} ] && [ -r /etc/fedora-release ]; then
lsb_dist='fedora'
fi
if [ -z ${lsb_dist} ] && [ -r /etc/os-release ]; then
lsb_dist="$(. /etc/os-release && echo "$ID")"
fi
lsb_dist="$(echo ${lsb_dist} | tr '[:upper:]' '[:lower:]')"
case "${lsb_dist}" in
amzn|centos|debian|ubuntu)
;;
*)
echo "Error: We currently only support ubuntu|debian|amzn|centos."
exit 1
;;
esac
}
# Start the bootstrap daemon
bootstrap_daemon() {
2015-07-23 03:09:05 +00:00
sudo -b docker -d -H unix:///var/run/docker-bootstrap.sock -p /var/run/docker-bootstrap.pid --iptables=false --ip-masq=false --bridge=none --graph=/var/lib/docker-bootstrap 2> /var/log/docker-bootstrap.log 1> /dev/null
2015-07-23 03:09:05 +00:00
sleep 5
}
DOCKER_CONF=""
# Start k8s components in containers
start_k8s() {
2015-07-23 03:09:05 +00:00
# Start flannel
2015-09-24 13:56:58 +00:00
flannelCID=$(sudo docker -H unix:///var/run/docker-bootstrap.sock run -d --restart=always --net=host --privileged -v /dev/net:/dev/net quay.io/coreos/flannel:0.5.3 /opt/bin/flanneld --etcd-endpoints=http://${MASTER_IP}:4001 -iface="eth0")
2015-07-23 03:09:05 +00:00
sleep 8
2015-07-23 03:09:05 +00:00
# Copy flannel env out and source it on the host
sudo docker -H unix:///var/run/docker-bootstrap.sock cp ${flannelCID}:/run/flannel/subnet.env .
source subnet.env
2015-07-23 03:09:05 +00:00
# Configure docker net settings, then restart it
case "${lsb_dist}" in
2015-09-25 07:40:07 +00:00
centos)
DOCKER_CONF="/etc/sysconfig/docker"
2015-09-25 07:40:07 +00:00
echo "OPTIONS=\"\$OPTIONS --mtu=${FLANNEL_MTU} --bip=${FLANNEL_SUBNET}\"" | sudo tee -a ${DOCKER_CONF}
if ! command_exists ifconfig; then
yum -y -q install net-tools
fi
ifconfig docker0 down
yum -y -q install bridge-utils && brctl delbr docker0 && systemctl restart docker
;;
2015-09-25 07:40:07 +00:00
amzn)
DOCKER_CONF="/etc/sysconfig/docker"
echo "OPTIONS=\"\$OPTIONS --mtu=${FLANNEL_MTU} --bip=${FLANNEL_SUBNET}\"" | sudo tee -a ${DOCKER_CONF}
ifconfig docker0 down
yum -y -q install bridge-utils && brctl delbr docker0 && service docker restart
;;
2015-09-25 07:40:07 +00:00
ubuntu|debian)
DOCKER_CONF="/etc/default/docker"
echo "DOCKER_OPTS=\"\$DOCKER_OPTS --mtu=${FLANNEL_MTU} --bip=${FLANNEL_SUBNET}\"" | sudo tee -a ${DOCKER_CONF}
ifconfig docker0 down
apt-get install bridge-utils && brctl delbr docker0 && service docker restart
;;
2015-10-08 08:39:14 +00:00
*)
echo "Unsupported operations system ${lsb_dist}"
2015-10-08 08:39:14 +00:00
exit 1
;;
esac
2015-07-23 03:09:05 +00:00
# sleep a little bit
sleep 5
# Start kubelet & proxy in container
2015-09-07 03:38:04 +00:00
docker run \
--net=host \
--pid=host \
2015-09-07 03:38:04 +00:00
--privileged \
--restart=always \
-d \
-v /sys:/sys:ro \
-v /var/run:/var/run:rw \
-v /dev:/dev \
2015-10-16 08:30:02 +00:00
-v /var/lib/docker/:/var/lib/docker:rw \
2015-09-07 03:38:04 +00:00
-v /var/lib/kubelet/:/var/lib/kubelet:rw \
gcr.io/google_containers/hyperkube:v${K8S_VERSION} \
/hyperkube kubelet --api-servers=http://${MASTER_IP}:8080 \
--v=2 --address=0.0.0.0 --enable-server \
--hostname-override=$(hostname -i) \
--cluster-dns=10.0.0.10 \
--cluster-domain=cluster.local
docker run \
-d \
--net=host \
--privileged \
--restart=always \
gcr.io/google_containers/hyperkube:v${K8S_VERSION} \
/hyperkube proxy --master=http://${MASTER_IP}:8080 \
--v=2
}
echo "Detecting your OS distro ..."
detect_lsb
echo "Starting bootstrap docker ..."
bootstrap_daemon
echo "Starting k8s ..."
start_k8s
2015-07-23 03:09:05 +00:00
echo "Worker done!"