Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
# Check for root
if [ `id -u` != 0 ] ; then
print_error "Need root access.\nRun the script as 'root'"
exit 10
fi
print_usage(){
echo "Usage"
echo "setup_k8s <command> [option]"
echo ""
echo " master install master"
echo " node <master_ip> install nodes"
echo " help print this message"
}
install_base(){
mkdir -p /etc/docker
cat << EOF > /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
echo "Updating repo list ... "
apt-get update > /dev/null
echo "Installing base packages ... "
apt-get install -y curl apt-transport-https > /dev/null
echo "Installing gpg keys ... "
echo -n "Docker: "
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
echo -n "K8S: "
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
echo "Installing repos for Docker and K8S ... "
cat <<EOF >/etc/apt/sources.list.d/docker.list
deb https://download.docker.com/linux/$(lsb_release -si | tr '[:upper:]' '[:lower:]') $(lsb_release -cs) stable
EOF
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
echo "Updating repo list ... "
apt-get update > /dev/null
echo "Installing Docker and K8S packages ... "
apt-get install -y kubelet kubeadm kubectl docker-ce=$(apt-cache madison docker-ce | grep 17.03 | head -1 | awk '{print $3}') > /dev/null
# --
# /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
# add --cgroup-driver=systemd
#
echo "Updating kubelet cgroup driver ... "
sed -i 's@Environment="KUBELET_KUBECONFIG_ARGS.*@Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --cgroup-driver=systemd"@g' /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl daemon-reload
}
if [ "$1" == "help" ] || [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
print_usage
fi
if [ "$1" == "master" ]; then
echo "Installing master ... "
install_base
echo "Pre-fetching docker images ... "
docker pull quay.io/coreos/etcd:v3.1.10
docker pull gcr.io/google_containers/etcd-amd64:3.0.17
docker pull gcr.io/google_containers/kube-apiserver-amd64:v1.8.3
docker pull gcr.io/google_containers/kube-controller-manager-amd64:v1.8.3
docker pull gcr.io/google_containers/kube-scheduler-amd64:v1.8.3
docker pull gcr.io/google_containers/kube-proxy-amd64:v1.8.3
docker pull quay.io/calico/node:v2.6.2
docker pull quay.io/calico/kube-controllers:v1.0.0
docker pull quay.io/calico/cni:v1.11.0
docker pull gcr.io/google_containers/k8s-dns-sidecar-amd64:1.14.5
docker pull gcr.io/google_containers/k8s-dns-kube-dns-amd64:1.14.5
docker pull gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.5
kubeadm init --pod-network-cidr=192.168.0.0/16 --kubernetes-version=v1.8.3 --apiserver-advertise-address "0.0.0.0" --apiserver-cert-extra-sans "master.example.com"
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl taint nodes --all node-role.kubernetes.io/master-
echo "Updateing pod network ... "
kubectl apply -f https://docs.projectcalico.org/v2.6/getting-started/kubernetes/installation/hosted/kubeadm/1.6/calico.yaml
# TODO: Change ingress to daemonset
echo "Adding ingress controller ... "
kubectl apply -f https://gist.githubusercontent.com/thilinapiy/3b35dfafaa32d505f8856fce75c2b69e/raw/74aafc084e3360d61c34e841729fbd2c36663ae6/nginx-ingress-controller.yaml
fi
if [ "$1" == "node" ]; then
if [ -n "$2" ]; then
echo "Installing node/minion ... "
install_base
echo "Pre-fetching docker images ... "
docker pull gcr.io/google_containers/pause-amd64:3.0
docker pull gcr.io/google_containers/kube-proxy-amd64:v1.8.3
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
else
print_usage
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.