Skip to content

Instantly share code, notes, and snippets.

@aric49
Last active January 12, 2017 19:32
Show Gist options
  • Select an option

  • Save aric49/4d424929346e7fbb3ddbd87c7a49ba67 to your computer and use it in GitHub Desktop.

Select an option

Save aric49/4d424929346e7fbb3ddbd87c7a49ba67 to your computer and use it in GitHub Desktop.
Localhost Kube Join Script
#!/bin/bash
#Title: Localhost-kubejoin.sh
#Description: Run this script to join newly deployed nodes to kubernetes cluster by passing in token and master node IP address
#Version: 1.7
#Last Updated: 1/12/2017
#Example on how to execute:
#sudo ./localhost-kubejoin.sh 87swiuuoii8vnni 192.168.1.1
#Sleep script for 2 mins
sleep 120
#Kill script if not run as root
if [[ $EUID -ne 0 ]]
then
echo "Script must be executed as root or with sudo privileges"
exit 100
fi
#Kill script if no CMD args passed in
if [[ $@ ]]
then
true
else
"Must pass in token and kube master URL parameters in the following format: localhost-kubejoin.sh token IPofMaster"
exit 1
fi
#Setting $HOME
if [[ -z "$HOME" ]]
then
echo "Setting Home variable"
export HOME=/root
else
echo "Home set"
fi
echo "================================="
echo "Creating Helm and Ceph Directories"
echo "/var/lib/openstack-helm/ceph/osd"
echo "/var/lib/openstack-helm/ceph/osd"
echo "================================="
mkdir -p /var/lib/openstack-helm/ceph/osd
mkdir -p /var/lib/openstack-helm/ceph/ceph
mkdir -p /var/lib/openstack-helm/ceph/mon
mkdir -p /var/lib/nova/instances
echo "================================="
echo "Configuring NTP and Other Packages"
echo "================================="
apt-get install -y ntp ntpdate lldpd
#Stop NTP if it's running
systemctl stop ntp
#NTP Update
ntpdate -s 0.us.pool.ntp.org
#Restart NTP
systemctl restart ntp
#Enable NTP on boot
systemctl enable ntp
systemctl start ntp
systemctl enable lldpd
systemctl start lldpd
echo "================================="
echo "Configuring /etc/hosts"
echo "================================="
#Gather host data to make SUDO work
LOCAL_HOSTNAME=$(hostname)
DEFAULT_IFACE=$(route | awk '/default/ { print $8 }')
DEFAULT_IFACE_IP=$(ifconfig $DEFAULT_IFACE | awk '/inet addr/{ print substr($2,6)}')
#Look in /etc/hosts for hostname entry:
egrep "^$DEFAULT_IFACE_IP" /etc/hosts
if [[ $? -eq 1 ]]
then
echo "Writing default IP($DEFAULT_IFACE_IP) and defualt hostname($LOCAL_HOSTNAME) to /etc/hosts if not already present...."
echo "$DEFAULT_IFACE_IP $LOCAL_HOSTNAME" >> /etc/hosts
else
true
fi
#Add repository, update and upgrade the server
echo "================================="
echo "Installing K8s and Docker Packages"
echo "================================="
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF > /etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y docker.io
sleep 10
systemctl start docker
systemctl enable docker
apt-get install -y kubelet kubeadm kubectl kubernetes-cni
#Join Kube to cluster
echo "================================="
echo "Joining to Kube Cluster"
echo "================================="
kubeadm join --token=$1 $2
if [[ $? -ne 0 ]]
then
#Delete and re-join node to cluster if join fails, kubectl commands are required to be run from the home directory.
mkdir -p /root/.kube
mkdir -p /home/ubuntu/.kube
wget -P /root/.kube/ http://10.103.88.148/config
wget -P /home/ubuntu/.kube/ http://10.103.88.148/config
kubectl delete node ${LOCAL_HOSTNAME,,}
systemctl stop kubelet
mv /etc/kubernetes/kubelet.conf /etc/kubernetes/kubelet.conf.old
rm -rf /var/lib/kubelet.old
mv /var/lib/kubelet /var/lib/kubelet.old
mkdir -p /var/lib/kubelet
kubeadm join --token=$1 $2
sleep 120
kubectl --kubeconfig /root/.kube/config get nodes
kubectl --kubeconfig /root/.kube/config cluster-info
#Label nodes
kubectl --kubeconfig /root/.kube/config label node ${LOCAL_HOSTNAME,,} openstack-compute-node=enabled
kubectl --kubeconfig /root/.kube/config label node ${LOCAL_HOSTNAME,,} openvswitch=enabled
else
#If join is successful, download config and use kubectl to get cluster details
mkdir -p /root/.kube
mkdir -p /home/ubuntu/.kube
wget -P /root/.kube/ http://10.103.88.148/config
wget -P /home/ubuntu/.kube/ http://10.103.88.148/config
sleep 120
kubectl --kubeconfig /root/.kube/config get nodes
kubectl --kubeconfig /root/.kube/config cluster-info
#Label nodes
kubectl --kubeconfig /root/.kube/config label node ${LOCAL_HOSTNAME,,} openstack-compute-node=enabled
kubectl --kubeconfig /root/.kube/config label node ${LOCAL_HOSTNAME,,} openvswitch=enabled
fi
#Enable kubelet
systemctl enable kubelet
systemctl start kubelet
echo "================================="
echo "Configuring SkyDNS"
echo "================================="
#This is a clunky method on how to grab the skyDNS IP from the kubelet itself.. the kubectl command in the playbooks only works on the master. Open to ideas!
#SKY_DNS_SERVER=$(grep "cluster-dns" /etc/systemd/system/kubelet.service.d/*.conf | awk -F"=" '{ print $4}' | awk -F" " '{ print $1 }')
#Method using kube config:
SKY_DNS_SERVER=$(kubectl --kubeconfig /root/.kube/config get --namespace=kube-system svc kube-dns -o jsonpath={.spec.clusterIP})
#Check if SkyDNS is configured in /etc/resolv.conf
grep "$SKY_DNS_SERVER" /etc/resolv.conf
if [[ $? -eq 0 ]]
then
true
else
#Backup old /etc/resolv
mv /etc/resolv.conf /etc/resolv.conf.old
#Add new contents to resolv.conf
cat <<EOF > /etc/resolv.conf
# Created by localhost-kubejoin script
search default.svc.cluster.local svc.cluster.local cluster.local
nameserver $SKY_DNS_SERVER
nameserver 8.8.8.8
options ndots:5
# These options enable hostname resolution to work when the kube-dns service
# is unavalible without an absolutely atrocious performance impact.
options timeout:1
options attempts:1
EOF
#Restart Kubelet
systemctl restart kubelet
fi
@alanmeadows

Copy link
Copy Markdown

Aric, lets make sure the host has the default gateway fix and we can redeploy it without having to "re-do" it using your mark broken method. Right now, its connecting to the cluster using a 10.200 ip (maas) vs 10.7.183 (use kubectl node delete mt01db08 to reset things for the next try)

I think you also want some of the other kubelab deploy items (not all obviously as most are for the master):

including:

  • creating directories /var/lib/openstack-helm/... (kube-prep)
  • the NTP stuff (note how it installs ntpd, turns off ntpd, runs ntpdate for really out of whack times, and starts ntpd) to avoid cert errors
  • ceph-common install
  • fix /etc/resolv.conf with skydns nameservers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment