|
| 1 | +#!/usr/bin/env bash |
| 2 | +# k3s part of Kubernetes Rebar Integrated Boot (KRIB) Kubeadm Installer |
| 3 | +# Copyright RackN 2019 |
| 4 | +set -e |
| 5 | + |
| 6 | +# Get access and who we are. |
| 7 | +{{ template "setup.tmpl" .}} |
| 8 | + |
| 9 | +if [[ {{ .Param "krib/k3s" }} != true ]]; then |
| 10 | + echo "krib/k3s must be true" |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +# Fix hostname lookup |
| 15 | +echo "{{.Machine.Address}} $(hostname -s) $(hostname)" >> /etc/hosts |
| 16 | + |
| 17 | +echo "Starting krib/k3s enabled stage" |
| 18 | +drpcli machines update $RS_UUID "{\"Meta\":{\"color\":\"black\", \"icon\": \"cube\" }}" | jq .Meta |
| 19 | + |
| 20 | +{{ if .ParamExists "krib/cluster-profile" -}} |
| 21 | +CLUSTER_PROFILE={{ .Param "krib/cluster-profile" }} |
| 22 | +PROFILE_TOKEN={{ .GenerateProfileToken (.Param "krib/cluster-profile") 7200 }} |
| 23 | +{{ else -}} |
| 24 | +xiterr 1 "Missing krib/cluster-profile on the machine!" |
| 25 | +{{ end -}} |
| 26 | + |
| 27 | +{{ template "krib-lib.sh.tmpl" .}} |
| 28 | + |
| 29 | +echo "Download k3s (for now only use latest and AMD64 arch)" |
| 30 | + |
| 31 | +KRIB_REPO={{ .Param "krib/repo" }} |
| 32 | +RELEASE=`curl -w "%{url_effective}" -I -L -s -S ${KRIB_REPO}/latest -o /dev/null | sed -e 's|.*/||'` |
| 33 | +TMP_DIR=/tmp/k3s-tmp |
| 34 | +INSTALL_DIR=/usr/bin |
| 35 | +SYSTEMD_DIR="/etc/systemd/system" |
| 36 | +K3SBIN="$INSTALL_DIR/k3s" |
| 37 | + |
| 38 | +if ! which k3s ; then |
| 39 | + echo "Download k3s ${RELEASE} from ${KRIB_REPO}/download/${RELEASE}/k3s to $K3SBIN" |
| 40 | + if [[ ! -z "$KRIB_REPO" ]] ; then |
| 41 | + download -L --remote-name-all "${KRIB_REPO}/download/${RELEASE}/k3s" -o $K3SBIN |
| 42 | + else |
| 43 | + echo "missing REPO AND RELEASE" |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + if [ -f $K3SBIN ] ; then |
| 47 | + chmod +x $K3SBIN |
| 48 | + else |
| 49 | + echo "$K3SBIN not installed on system" |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | +else |
| 53 | + echo "found $K3SBIN binary, moving on..." |
| 54 | +fi |
| 55 | + |
| 56 | +echo "Configure master or nodes..." |
| 57 | + |
| 58 | +{{ if .ParamExists "krib/ip" -}} |
| 59 | +KRIB_IP={{ .Param "krib/ip" }} |
| 60 | +{{ else -}} |
| 61 | +KRIB_IP={{ .Machine.Address }} |
| 62 | +{{ end -}} |
| 63 | + |
| 64 | +# we need a random backoff to avoid races. |
| 65 | +SLEEP=$[ ( $RANDOM % 25 ) ] |
| 66 | +sleep $SLEEP |
| 67 | + |
| 68 | +MASTER_INDEX=$(find_me $KRIB_MASTERS_PARAM "Uuid" $RS_UUID) |
| 69 | +echo "My Master index is $MASTER_INDEX" |
| 70 | +echo "k3s version: $(k3s -v)" |
| 71 | + |
| 72 | +CLUSTERNAME={{ .Param "krib/cluster-name" }} |
| 73 | + |
| 74 | +if [[ $MASTER_INDEX != notme ]] ; then |
| 75 | + |
| 76 | + echo "I am master - run k3s server" |
| 77 | + |
| 78 | + EXECSTART="${K3SBIN} server --bind-address={{ .Param "krib/cluster-master-vip" }} --https-listen-port={{ .Param "krib/cluster-api-port" }}" |
| 79 | + SYSTEMD_TYPE="notify" |
| 80 | + |
| 81 | + drpcli machines update $RS_UUID "{\"Meta\":{\"color\":\"yellow\", \"icon\": \"anchor\" }}" | jq .Meta |
| 82 | + |
| 83 | +else |
| 84 | + |
| 85 | + echo "I am a node - run k3s agent" |
| 86 | + |
| 87 | + wait_for_variable $KRIB_JOIN_PARAM |
| 88 | + # we need to get the decoded version |
| 89 | + TOKEN=$(drpcli -T $PROFILE_TOKEN profiles get $CLUSTER_PROFILE param $KRIB_JOIN_PARAM --decode) |
| 90 | + |
| 91 | + echo "Running Agent: join $TOKEN" |
| 92 | + EXECSTART="${K3SBIN} agent --token $TOKEN" |
| 93 | + SYSTEMD_TYPE="exec" |
| 94 | + |
| 95 | + # Set machine icon and color for KRIB cluster building |
| 96 | + drpcli machines update $RS_UUID "{\"Meta\":{\"color\":\"yellow\", \"icon\": \"ship\" }}" | jq .Meta |
| 97 | + |
| 98 | +fi |
| 99 | + |
| 100 | +MODEPROBE=$(which modprobe) |
| 101 | + |
| 102 | +## Build Systemd environment vars |
| 103 | +tee /etc/systemd/system/k3s.service.env >/dev/null << EOF |
| 104 | +K3S_URL=https://{{ .Param "krib/cluster-master-vip" }}:{{ .Param "krib/cluster-api-port" }} |
| 105 | +EOF |
| 106 | + |
| 107 | +## Build Systemd service |
| 108 | +tee /etc/systemd/system/k3s.service >/dev/null << EOF |
| 109 | +[Unit] |
| 110 | +Description=Lightweight Kubernetes |
| 111 | +Documentation=https://k3s.io |
| 112 | +After=network-online.target |
| 113 | +
|
| 114 | +[Service] |
| 115 | +Type=${SYSTEMD_TYPE} |
| 116 | +EnvironmentFile=/etc/systemd/system/k3s.service.env |
| 117 | +ExecStartPre=${MODEPROBE} br_netfilter |
| 118 | +ExecStartPre=${MODEPROBE} overlay |
| 119 | +ExecStart=${EXECSTART} |
| 120 | +KillMode=process |
| 121 | +Delegate=yes |
| 122 | +LimitNOFILE=infinity |
| 123 | +LimitNPROC=infinity |
| 124 | +LimitCORE=infinity |
| 125 | +TasksMax=infinity |
| 126 | +TimeoutStartSec=0 |
| 127 | +Restart=always |
| 128 | +RestartSec=5s |
| 129 | +
|
| 130 | +[Install] |
| 131 | +WantedBy=multi-user.target |
| 132 | +EOF |
| 133 | + |
| 134 | +systemctl daemon-reload && systemctl start k3s |
| 135 | +sleep 15 |
| 136 | + |
| 137 | +if [[ $MASTER_INDEX != notme ]] ; then |
| 138 | + # need more delay for master |
| 139 | + sleep 15 |
| 140 | + echo "verify install" |
| 141 | + k3s kubectl get nodes |
| 142 | + echo "Recording cluster admin config ..." |
| 143 | + drpcli -T $PROFILE_TOKEN profiles add $CLUSTER_PROFILE param $KRIB_ADMIN_CONF_PARAM to - < /etc/rancher/k3s/k3s.yaml |
| 144 | + if [[ -f /var/lib/rancher/k3s/server/node-token ]] ; then |
| 145 | + # collect the cluster bootstrap token, then store it on the Param |
| 146 | + TOKEN=$(cat /var/lib/rancher/k3s/server/node-token) |
| 147 | + drpcli -T $PROFILE_TOKEN profiles add $CLUSTER_PROFILE param $KRIB_JOIN_PARAM to "$TOKEN" |
| 148 | + else |
| 149 | + echo "halting, could not find /var/lib/rancher/k3s/server/node-token" |
| 150 | + exit 1 |
| 151 | + fi |
| 152 | + drpcli machines update $RS_UUID "{\"Meta\":{\"color\":\"green\", \"icon\": \"anchor\" }}" | jq .Meta |
| 153 | +else |
| 154 | + # Set machine icon and color for KRIB cluster building |
| 155 | + drpcli machines update $RS_UUID "{\"Meta\":{\"color\":\"green\", \"icon\": \"ship\" }}" | jq .Meta |
| 156 | +fi |
| 157 | + |
| 158 | +echo "Finished successfully" |
| 159 | +exit 0 |
0 commit comments