Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.

Commit 799f6ce

Browse files
authored
Merge pull request #311 from digitalrebar/k3s
Enable K3s Install for KRIB
2 parents 2bdba9c + aaf7b8d commit 799f6ce

File tree

11 files changed

+278
-13
lines changed

11 files changed

+278
-13
lines changed

krib/._RequiredFeatures.meta

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sane-exit-codes, job-exit-states, fsm-runner, workflows, default-workflow, http-range-header, roles, tenants, sprig
1+
sane-exit-codes, job-exit-states, fsm-runner, workflows, default-workflow, http-range-header, roles, tenants, sprig, log-has-head-method
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
Name: "containerd/version"
3+
Description: "Version of containerd to use in cluster"
4+
Documentation: |
5+
Allows operators to determine the version of containerd to install
6+
7+
String should NOT include v as as a prefix
8+
Used to download from https://storage.googleapis.com/cri-containerd-release/cri-containerd-${VERSION}.linux-amd64.tar.gz path
9+
Schema:
10+
type: "string"
11+
default: "1.2.7"
12+
Meta:
13+
color: "blue"
14+
icon: "cubes"
15+
title: "Community Content Copyright RackN 2019"
16+
render: "semver"

krib/params/krib-k3s.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
Name: "krib/k3s"
3+
Description: "Use k3s for KRIB (instead of k8s)"
4+
Documentation: |
5+
Informs tasks to use k3s instead of k8s
6+
No need to include etcd stages when k3s is true
7+
Schema:
8+
type: "boolean"
9+
default: false
10+
Meta:
11+
color: "blue"
12+
icon: "cube"
13+
title: "Community Content"

krib/stages/k3s-config.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
Name: "k3s-config"
3+
Description: "KRIB configure a K3s cluster master and nodes"
4+
Documentation: |
5+
Designed to substitute for Kubernetes with K3s
6+
Installs k3s using the KRIB process and params
7+
with the goal of being able to use the same downstream stages
8+
RunnerWait: true
9+
Params:
10+
"krib/k3s": true
11+
"krib/container-runtime": "containerd"
12+
"krib/repo": "https://github.com/rancher/k3s/releases"
13+
Tasks:
14+
- "containerd-install"
15+
- "krib-get-masters"
16+
- "k3s-config"
17+
Meta:
18+
icon: "ship"
19+
color: "yellow"
20+
title: "Community Content"
21+
k3s: "true"
22+
copyright: "RackN 2019"

krib/tasks/k3s-config.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
Description: "A task to configure k3s"
3+
Name: "k3s-config"
4+
Documentation: |
5+
Sets Param: krib/cluster-join, krib/cluster-admin-conf
6+
Configure K3s using built-in commands
7+
This uses the Digital Rebar Cluster pattern so krib/cluster-profile must be set
8+
9+
Server is setup to also be an agent - all machines have workload
10+
11+
WARNING: Must NOT set etcd/cluster-profile when install k3s1
12+
RequiredParams:
13+
- krib/cluster-profile
14+
Templates:
15+
- ID: "k3s-config.sh.tmpl"
16+
Name: "Config K3s"
17+
Path: ""
18+
Meta:
19+
icon: "ship"
20+
color: "blue"
21+
title: "Community Content"
22+
feature-flags: "sane-exit-codes"
23+
k3s: "true"
24+
copyright: "RackN 2019"

krib/templates/containerd-install.sh.tmpl

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
# Kubernetes Rebar Integrated Boot (KRIB) Docker Install
33
set -e
44

5+
echo "starting containerd v{{.Param "containerd/version"}} install"
56
# Get access and who we are.
67
{{template "setup.tmpl" .}}
78
[[ $RS_UUID ]] && export RS_UUID="{{.Machine.UUID}}"
89

9-
ETCD_CONTROLLER_IP={{.Param "etcd/controller-ip"}}
10-
1110
{{if .ParamExists "kubectl/working-dir" -}}
1211
# Only do this if it exists.
1312
# if it isn't setup, don't use it.
@@ -19,14 +18,26 @@ fi
1918

2019
# Allow for a local repository for installation files
2120
{{if .ParamExists "krib/package-repository" -}}
22-
KRIB_REPO={{.Param "krib/package-repository"}}
21+
echo "local repo download containerd v{{.Param "containerd/version"}} binaries in {{.Param "krib/package-repository"}}"
22+
curl -L {{.Param "krib/package-repository"}}/cri-containerd-{{.Param "containerd/version"}}.linux-amd64.tar.gz | tar xvz -C /
23+
{{else}}
24+
BINARIES="cri-containerd-{{.Param "containerd/version"}}.linux-amd64.tar.gz"
25+
if [[ ! -f $BINARIES ]] ; then
26+
echo "download containerd v{{.Param "containerd/version"}} to $BINARIES"
27+
curl -L https://storage.googleapis.com/cri-containerd-release/$BINARIES -o $BINARIES
28+
else
29+
echo "$BINARIES already downloaded"
30+
fi
31+
CHECK=$(sha256sum $BINARIES)
32+
SHA256=$(curl -L https://storage.googleapis.com/cri-containerd-release/$BINARIES.sha256)
33+
if [[ "$CHECK" == "$SHA256 $BINARIES" ]] ; then
34+
echo "verified checksum for $BINARIES!"
35+
tar --no-overwrite-dir -C / -xzf $BINARIES
36+
else
37+
echo "checksum does not match! computed $CHECK vs download $SHA256"
38+
exit 1
39+
fi
2340
{{end -}}
24-
25-
if [[ ! -z "$KRIB_REPO" ]] ; then
26-
curl -L ${KRIB_REPO}/cri-containerd-{{.Param "containerd/version"}}.linux-amd64.tar.gz | tar xvz -C /
27-
else
28-
curl -L https://storage.googleapis.com/cri-containerd-release/cri-containerd-{{.Param "containerd/version"}}.linux-amd64.tar.gz | tar xvz -C /
29-
fi
3041
# Configure containerd
3142

3243
{{if .ParamExists "containerd/config" -}}
@@ -42,5 +53,6 @@ echo "Skipping custom etc/containerd/config.toml: No containerd/config defined"
4253
# start containerd
4354
systemctl start containerd
4455

45-
echo "Containerd installed successfully"
56+
ctr --version
57+
echo "Containerd v{{.Param "containerd/version"}} installed successfully"
4658
exit 0

krib/templates/k3s-config.sh.tmpl

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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

krib/templates/krib-config.sh.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ if [[ $MASTER_INDEX != notme ]] ; then
187187
esac
188188

189189
else
190+
echo "waiting for $KRIB_MASTER_CERTS_PARAM"
190191
wait_for_variable $KRIB_MASTER_CERTS_PARAM
191192

192193
cd /
@@ -275,6 +276,7 @@ do
275276
done
276277

277278
# Wait for KRIB config to be updated from bootstrap node
279+
echo "waiting for $KRIB_ADMIN_CONF_PARAM"
278280
wait_for_variable $KRIB_ADMIN_CONF_PARAM > label.conf
279281

280282
# Set labels for nodes / runs on all nodes

krib/templates/krib-get-masters.sh.tmpl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ KRIB_IP={{ .Machine.Address }}
3939
KRIB_MASTER_COUNT={{.Param "krib/cluster-master-count"}}
4040
echo "Creating $KRIB_MASTER_COUNT k8s masters"
4141

42-
if [[ $MASTER_ON_ETCDS == true ]] ; then
42+
if [[ {{ .Param "krib/k3s" }} == true ]] ; then
43+
echo "K3s, add $KRIB_MASTERS_PARAM to $CLUSTER_PROFILE"
44+
drpcli -T "$PROFILE_TOKEN" profiles add "$CLUSTER_PROFILE" param "$KRIB_MASTERS_PARAM" to "[]" || true
45+
echo "K3s, skipping etcd config"
46+
elif [[ $MASTER_ON_ETCDS == true ]] ; then
4347
if (( $KRIB_MASTER_COUNT >= $ETCD_COUNT )) ; then
4448
# If we have the same or more masters than etcd, start with the etcd list.
4549
ETCD_MASTERS=$(get_param $ETCD_SERVERS_PARAM)

krib/workflows/k3s-cluster.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Description: "KRIB built K3s cluster"
2+
Errors: []
3+
Meta:
4+
color: "yellow"
5+
icon: "ship"
6+
title: "k3s cluster"
7+
k3s: "true"
8+
copyright: "RackN 2019"
9+
Name: "k3s-cluster"
10+
ReadOnly: false
11+
Stages:
12+
- "k3s-config"
13+
- "krib-live-wait"

0 commit comments

Comments
 (0)