2019-03-05 18:33:23 +01:00
|
|
|
#!/bin/bash
|
|
|
|
# @author: Daniel Keszei <keszei.daniel@gmail.com>
|
|
|
|
# @description: Kubernetes deployer
|
|
|
|
# @created: 2019-02-15
|
|
|
|
# @version: 1.0
|
|
|
|
# @origin: https://github.com/szefoka/openfaas_lab
|
|
|
|
|
|
|
|
|
|
|
|
# Variable(s)
|
|
|
|
|
|
|
|
# Script variable(s)
|
|
|
|
PID=$$
|
|
|
|
SCRIPTNAME="$(basename $0)"
|
|
|
|
WORKER_LIST="worker.list"
|
2019-04-24 13:26:06 +02:00
|
|
|
EXTERNAL=false
|
|
|
|
MASTER_IP=""
|
2019-03-05 18:33:23 +01:00
|
|
|
TOKEN=""
|
|
|
|
HASH=""
|
2019-04-24 13:26:06 +02:00
|
|
|
|
2019-03-05 18:33:23 +01:00
|
|
|
|
|
|
|
# Functions
|
|
|
|
|
|
|
|
function usage {
|
|
|
|
cat << EOF
|
2019-04-24 13:26:06 +02:00
|
|
|
Usage: $SCRIPTNAME [--external|-e] <CNI>
|
|
|
|
--external|-e : Initizalize Kubernetes on the external network
|
|
|
|
instead of on an internal one
|
|
|
|
Available <CNI> plugins:
|
2019-03-05 18:33:23 +01:00
|
|
|
* Calico
|
|
|
|
* Cilium
|
|
|
|
* Flannel
|
|
|
|
* WeaveNet
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
## Send error messages to stderr
|
|
|
|
function echo_err {
|
|
|
|
echo "Error: $@" >&2
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait_for_worker {
|
|
|
|
while [[ "$(kubectl get nodes | grep Ready | grep none | wc -l)" -lt 1 ]];
|
|
|
|
do
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait_for_podnetwork {
|
|
|
|
#podnetwork should be running on the master and at least one worker node
|
|
|
|
while [[ "$(kubectl get pods -n kube-system | grep weave-net | grep Running | wc -l)" -lt 2 ]];
|
|
|
|
do
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2019-04-24 13:26:06 +02:00
|
|
|
# Preflight checks
|
|
|
|
|
|
|
|
## Check file from parameters
|
2019-03-05 18:33:23 +01:00
|
|
|
if [ ! -f $WORKER_LIST ]; then
|
|
|
|
echo_err "Worker list file ($WORKER_LIST) not exists."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-04-24 13:26:06 +02:00
|
|
|
## Check the file contents
|
|
|
|
if [ ! -s $WORKER_LIST ]; then
|
|
|
|
echo_err "Worker list file ($WORKER_LIST) is empty."
|
2019-04-03 12:09:39 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-04-24 13:26:06 +02:00
|
|
|
## Create array from file
|
|
|
|
readarray WORKER < $WORKER_LIST
|
2019-04-03 12:09:39 +02:00
|
|
|
|
2019-03-05 18:33:23 +01:00
|
|
|
## Check for argument
|
|
|
|
if [ "$#" -lt 1 ]; then
|
|
|
|
echo_err "Missing CNI plugin name as an argument."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2019-04-24 13:26:06 +02:00
|
|
|
## Check for help parameter
|
|
|
|
for i in "$@"
|
|
|
|
do
|
|
|
|
### Make the letters of the argument lowercase
|
|
|
|
i=$(tr '[:upper:]' '[:lower:]' <<< $i)
|
|
|
|
case $i in
|
|
|
|
### Print out help message
|
|
|
|
help|h|-h|--help) usage; exit 0;;
|
|
|
|
esac
|
|
|
|
done
|
2019-03-05 18:33:23 +01:00
|
|
|
|
2019-04-24 13:26:06 +02:00
|
|
|
## Check parameters and setup variables for Kubernetes installation
|
|
|
|
for i in "$@"
|
|
|
|
do
|
|
|
|
### Make the letters of the argument lowercase
|
|
|
|
i=$(tr '[:upper:]' '[:lower:]' <<< $i)
|
|
|
|
case $i in
|
|
|
|
### Kubernetes network usage (internal|external)
|
|
|
|
-e|--external) echo "# Kubernetes will be set up for external network. #";
|
|
|
|
EXTERNAL=false;;
|
|
|
|
|
|
|
|
### Set parameters for Calico
|
|
|
|
calico) echo "[CNI] Calico selected...";
|
|
|
|
CNI="calico";
|
|
|
|
POD_NETWORK="192.168.0.0/16";;
|
|
|
|
|
|
|
|
### Set parameters for Cilium
|
|
|
|
cilium) echo "[CNI] Cilium selected...";
|
|
|
|
CNI="cilium";
|
|
|
|
POD_NETWORK="";;
|
|
|
|
|
|
|
|
### Set parameters for Flannel
|
|
|
|
flannel) echo "[CNI] Flannel selected...";
|
|
|
|
CNI="flannel";
|
|
|
|
POD_NETWORK="10.244.0.0/16";;
|
|
|
|
|
|
|
|
### Set parameters for WeaveNet...
|
|
|
|
weavenet) echo "[CNI] WeaveNet selected...";
|
|
|
|
CNI="weavenet";
|
|
|
|
POD_NETWORK="";;
|
|
|
|
|
|
|
|
### Wrong argument, print error message
|
|
|
|
*) echo_err "Unkown parameter: $i option is not valid!";
|
2019-03-05 18:33:23 +01:00
|
|
|
exit 1;;
|
2019-04-24 13:26:06 +02:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
## Get Master node IP address
|
|
|
|
if [ $EXTERNAL ]; then
|
|
|
|
MASTER_IP=$(grep -oP '(?<=src )[^ ]*' \
|
|
|
|
<(grep \
|
|
|
|
-f <(ls -l /sys/class/net | grep pci | awk '{print $9}') \
|
|
|
|
<(ip ro sh) |
|
|
|
|
grep -v $(ip ro sh | grep default | awk '{print $5}')) |
|
|
|
|
head -1)
|
|
|
|
if [ "x$MASTER_IP" == "x" ]; then
|
|
|
|
EXTERNAL=false
|
|
|
|
MASTER_IP=$(grep -oP '(?<=src )[^ ]*' <(ip ro sh | grep default))
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
MASTER_IP=$(grep -oP '(?<=src )[^ ]*' <(ip ro sh | grep default))
|
|
|
|
fi
|
|
|
|
|
|
|
|
## Setup Kubernetes
|
|
|
|
./deploy/kubernetes_install.sh master $EXTERNAL $MASTER_IP $POD_NETWORK
|
|
|
|
|
|
|
|
## Install CNI Plugin
|
|
|
|
./deploy/${CNI}_setup.sh
|
2019-03-05 18:33:23 +01:00
|
|
|
|
|
|
|
TOKEN=$(kubeadm token list | tail -n 1 | cut -d ' ' -f 1)
|
2019-04-24 13:26:06 +02:00
|
|
|
HASH=sha256:$(openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt |
|
|
|
|
openssl rsa -pubin -outform der 2>/dev/null |
|
|
|
|
openssl dgst -sha256 -hex |
|
|
|
|
sed 's/^.* //')
|
2019-03-05 18:33:23 +01:00
|
|
|
|
|
|
|
#FIXME Do I need local docker-registry?
|
2019-03-11 16:56:20 +01:00
|
|
|
#./deploy/docker_registry_setup.sh $IP:5000
|
2019-03-05 18:33:23 +01:00
|
|
|
|
|
|
|
# Join the worker nodes
|
2019-04-24 13:26:06 +02:00
|
|
|
for WORKERNAME in ${WORKER[@]}; do
|
2019-03-05 18:33:23 +01:00
|
|
|
echo "[worker:$WORKERNAME] Deploying..."
|
2019-04-24 13:26:06 +02:00
|
|
|
ssh $WORKERNAME -o "StrictHostKeyChecking no" \
|
|
|
|
"bash -s" < ./deploy/kubernetes_install.sh worker $EXTERNAL $MASTER_IP:6443 $TOKEN $HASH
|
2019-03-05 18:33:23 +01:00
|
|
|
|
|
|
|
#FIXME Do I need to wait for the worker?
|
2019-04-24 13:26:06 +02:00
|
|
|
# wait_for_worker
|
2019-03-05 18:33:23 +01:00
|
|
|
|
|
|
|
#FIXME Do I need local docker-registry?
|
2019-04-24 13:26:06 +02:00
|
|
|
# ssh $WORKERNAME -o "StrictHostKeyChecking no" "bash -s" < ./deploy/docker_registry_setup.sh $MASTER_IP:5000
|
2019-03-05 18:33:23 +01:00
|
|
|
|
|
|
|
echo "[worker:$WORKERNAME] Deployment is completed."
|
|
|
|
done
|
|
|
|
|
2019-03-12 14:44:07 +01:00
|
|
|
#Deploy Kubeless
|
2019-04-02 22:19:38 +02:00
|
|
|
./deploy/kubeless_setup.sh
|
2019-03-28 14:12:23 +01:00
|
|
|
|
|
|
|
#Deploy Metric Server
|
|
|
|
./deploy/metric_setup.sh
|