-
Notifications
You must be signed in to change notification settings - Fork 1
/
master.sh
84 lines (62 loc) · 1.97 KB
/
master.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
#!/bin/bash
# utilities functions (logging)
log() {
echo "$(date "+%F %T.%2N")|$(hostname)|${*}"
}
debug() {
log "DEBUG|${*}"
}
info() {
log "INFO|${*}"
}
warn() {
log "WARN|${*}"
}
error() {
log "ERROR|${*}"
}
#default peering subnet for the cluster
PEER_SUBNET="192.168.66.0"
wrong_arg(){
error "Invalid parameter for $1"
exit "1"
}
while [ $# -gt 0 ]; do
case "$1" in
-s|--subnet)
PEER_SUBNET="$2"
shift 2
;;
*)
wrong_arg "$1"
break
;;
esac
done
info "Getting private ip address"
_SUBNET=$(echo $PEER_SUBNET|cut -d "." -f1-3)
PEER_ADDRESS=$(ip -4 a |grep $_SUBNET |awk -F "inet |/" '{ print $2 }')
info "Peering address for master is: "$PEER_ADDRESS
info "Initializing kubeadm"
sudo kubeadm init --apiserver-advertise-address=$PEER_ADDRESS --pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
info "Trying to ping the kube"
sudo kubectl get nodes
info "Setting the master as a worker too"
sudo kubectl taint nodes --all node-role.kubernetes.io/master-
info "setting the networking driver: Flannel"
FLANNEL_URL="https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
sudo kubectl apply -f $FLANNEL_URL
info "Setting the join command"
JOIN_TOKEN=$(kubeadm token list |tail -n -1| awk '{ print $1 }')
JOIN_HASH=$(openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //')
JOIN_COMMAND="sudo kubeadm join "$PEER_ADDRESS":6443 --token "$JOIN_TOKEN" --discovery-token-ca-cert-hash sha256:"$JOIN_HASH
info "In order to join the cluster, run this command: "
info "$JOIN_COMMAND"
echo $JOIN_COMMAND > /vagrant/join.txt
info "removing credentials from root folder and passing them to vagrant"
mkdir -p /home/vagrant/.kube
sudo cp /etc/kubernetes/admin.conf /home/vagrant/.kube/config
sudo chown -R vagrant:vagrant /home/vagrant/.kube