Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

(DON'T MERGE BEFORE v0.1.x ⚠️) feat: move to k3s from k3d - colony issues purposal #9

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 62 additions & 41 deletions laptop/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,61 @@ update_apt() {
apt-get update
}

install_k3d() {
local k3d_Version=$1

wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | TAG="$k3d_version" bash
}

start_k3d() {
echo "Creating the cluster..."
k3d cluster create --network host --no-lb --k3s-arg "--disable=traefik,servicelb" --k3s-arg "--kube-apiserver-arg=feature-gates=MixedProtocolLBService=true" --host-pid-mode

echo "Waiting for the cluster to be fully operational..."
sleep 10

echo "Configuring kubeconfig..."
mkdir -p ~/.kube/

k3d kubeconfig get -a > ~/.kube/config || echo "Failed to get kubeconfig"

echo "Checking nodes..."
until kubectl wait --for=condition=Ready nodes --all --timeout=600s; do
echo "Waiting for nodes to be ready..."
sleep 5
done
start_k3s() {
local k3s_version=$1
echo "Creating the cluster..."

docker run -d --privileged --name ctrlplane-laptop \
-e K3S_KUBECONFIG_OUTPUT=/output/kubeconfig.yaml \
-e K3S_KUBECONFIG_MODE=666 \
-v "$(pwd):/output" \
-v k3s-server:/var/lib/rancher/k3s \
--tmpfs=/run --tmpfs=/var/run \
--network=host \
rancher/k3s:"$k3s_version" server \
--disable=traefik,servicelb \
--tls-san=ctrlplane-laptop \
--node-label="colony.konstruct.io/node-type=laptop"

echo "Waiting for the cluster to be fully operational..."
sleep 30

echo "Configuring kubeconfig..."
mkdir -p ~/.kube/

cp ./kubeconfig.yaml ~/.kube/config || echo "Failed to get kubeconfig"
sed -i 's/127.0.0.1/localhost/g' ~/.kube/config
jairoFernandez marked this conversation as resolved.
Show resolved Hide resolved
export KUBECONFIG=~/.kube/config
cat ~/.kube/config
jairoFernandez marked this conversation as resolved.
Show resolved Hide resolved

if [ -f ./kubeconfig.yaml ]; then
cp ./kubeconfig.yaml ~/.kube/config
else
echo "Failed to get kubeconfig: kubeconfig.yaml not found"
exit 1
fi

echo "Checking nodes..."
until kubectl wait --for=condition=Ready nodes --all --timeout=600s; do
echo "Waiting for nodes to be ready..."
sleep 5
done

echo "All nodes are ready."
echo "Generating join token and storing it in a secret..."
# docker exec -i ctrlplane-laptop k3s token create --print-join-command > token.txt (incompatible)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I consider to maintain this comment, just for informational purposes

docker exec -i ctrlplane-laptop cat /var/lib/rancher/k3s/server/node-token > token.txt
kubectl create secret -n kube-system generic k3s-join-token --from-file=token.txt
}

kubectl_for_vagrant_user() {
runuser -l vagrant -c "mkdir -p ~/.kube/"
runuser -l vagrant -c "k3d kubeconfig get -a > ~/.kube/config"
echo "**********************************"
runuser -l vagrant -c "mkdir -p /home/vagrant/.kube"
cp ./kubeconfig.yaml /home/vagrant/.kube/config
chown vagrant:vagrant /home/vagrant/.kube/config

chmod 600 /home/vagrant/.kube/config
echo 'export KUBECONFIG="/home/vagrant/.kube/config"' >> /home/vagrant/.bashrc
echo "**********************************"
}

helm_install_tink_stack() {
Expand Down Expand Up @@ -104,13 +127,13 @@ helm_install_tink_stack() {

configure_dnsmasq() {
cat <<-EOF >/etc/dnsmasq.conf
dhcp-range=10.0.10.100,10.0.10.200,255.255.255.0,12h
#dhcp-option=option:router,172.31.0.1
dhcp-option=option:router,10.0.10.1
dhcp-option=option:dns-server,1.1.1.1
dhcp-authoritative
interface=eth1
port=0
dhcp-range=10.0.10.100,10.0.10.200,255.255.255.0,12h
#dhcp-option=option:router,172.31.0.1
dhcp-option=option:router,10.0.10.1
dhcp-option=option:dns-server,1.1.1.1
dhcp-authoritative
interface=eth1
port=0
EOF
systemctl restart dnsmasq
}
Expand All @@ -120,24 +143,22 @@ apply_manifests() {
local namespace=$2

kubectl apply -n "$namespace" -f "$manifests_dir"/ubuntu-download.yaml
kubectl apply -n "$namespace" -f "$manifests_dir"/talos-download.yaml
}

run_helm() {
local manifests_dir=$1
local loadbalancer_ip=$2
local helm_chart_version=$3
local loadbalancer_interface=$4
local k3d_version=$5
local k3s_version=$5
local namespace="tink-system"

install_k3d "$k3d_version"
start_k3d
start_k3s "$k3s_version"
install_helm
kubectl get all --all-namespaces
kubectl_for_vagrant_user
helm_install_tink_stack "$namespace" "$helm_chart_version" "$loadbalancer_interface" "$loadbalancer_ip"
apply_manifests "$manifests_dir" "$namespace"
apply_manifests "$manifests_dir" "$namespace"
}

main() {
Expand All @@ -147,7 +168,7 @@ main() {
local helm_chart_version="0.4.4"
local loadbalancer_interface="eth1"
local kubectl_version="1.28.3"
local k3d_version="v5.6.0"
local k3s_version="v1.30.2-k3s1"

update_apt
install_docker
Expand All @@ -156,7 +177,7 @@ main() {
# Needed after iPXE increased the default TCP window size to 2MB.
sudo ethtool -K eth1 tx off sg off tso off
install_kubectl "$kubectl_version"
run_helm "$manifests_dir" "$loadbalancer_ip" "$helm_chart_version" "$loadbalancer_interface" "$k3d_version"
run_helm "$manifests_dir" "$loadbalancer_ip" "$helm_chart_version" "$loadbalancer_interface" "$k3s_version"
}

if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
Expand All @@ -165,4 +186,4 @@ if [[ ${BASH_SOURCE[0]} == "$0" ]]; then
main "$@"
echo loadbalancer_ip="$1"
echo "all done!"
fi
fi