Skip to content

Instantly share code, notes, and snippets.

@Vertiwell
Created November 12, 2021 03:51
Show Gist options
  • Save Vertiwell/ff4cdaec3fa1053217f1c047a6a6d554 to your computer and use it in GitHub Desktop.
Save Vertiwell/ff4cdaec3fa1053217f1c047a6a6d554 to your computer and use it in GitHub Desktop.
k0s_deployment.sh
#!/bin/bash
### Deploying k0s for Debian/Ubuntu based OS
## Baseline Guide: https://github.com/k0sproject/k0sctl#installation
# Type of Deployment: Self - Baremetal
### Minimum Requirements ###
## This deployment requires at least 4 nodes, one to be the controller and three to be workers, this allows apps deployed to works to have a quorum.
## Each node must have passwordless SSH, using a root user (but not root itself) add the following on each node:
# printf "username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
#
### Installation Steps ###
## Provide list of host IP addresses for cluster nodes:
NODEIPS=( 192.168.1.188 192.168.1.160 192.168.1.164 192.168.1.152 )
## Provide a root user (not root) to ssh to other nodes:
## If you don't yet have a user on each node, create one with this command:
# adduser username && usermod -aG sudo username
echo "Provide the root user (not root) to ssh to other nodes:"
read varuser
## Create a private key ID for the root user
ssh-keygen -q -t rsa -N '' <<< $'\ny' >/dev/null 2>&1 && \
# Delete nodes file, in case this is run multiple times
rm nodes >/dev/null 2>&1; \
## Run loop to copy SSH keys to other hosts and push the node IP's to a file for k0s installation
for NODEIPS in "${NODEIPS[@]}" ; do
ssh-copy-id $varuser@$NODEIPS; \
echo $varuser@$NODEIPS >> nodes
done
#
## The following base packages are required for this installation:
# Golang, programming language
wget https://golang.org/dl/go1.17.2.linux-amd64.tar.gz && \
rm -rf /usr/local/go && tar -C /usr/local -xzf go1.17.2.linux-amd64.tar.gz && \
export PATH=$PATH:/usr/local/go/bin && \
export PATH=$PATH:/root/go/bin/ && \
# k0sctl, k0s installation tool
go install github.com/k0sproject/k0sctl@latest && \
# kubectl, engage with the k0s cluster
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" && \
install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl && \
#
## Create the configuration file for k0s install, more options here: https://github.com/k0sproject/k0sctl#configuration-file-spec-fields
k0sctl init $(while IFS= read -r line; do printf '%s ' "$line"; done < nodes) > k0sctl.yaml && \
## Install k0s based on the configuration file
k0sctl apply --config k0sctl.yaml && mkdir ~/.kube >/dev/null 2>&1; k0sctl kubeconfig > ~/.kube/config && chmod go-r ~/.kube/config && rm k0s* && rm nodes && \
# Test
kubectl get nodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment