Last active
July 9, 2024 01:40
-
-
Save qrkourier/15c0453e54534ca609baeabe6cc2fe76 to your computer and use it in GitHub Desktop.
install single-node, single-stack IPv6 k3s with a random, private IPv6 address on the loopback interface for host-local communication
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
_gen_ula(){ | |
# Generate a 40-bit random global ID | |
local random_id | |
random_id=$(od -An -N5 -tx1 /dev/urandom | xxd -p | tr -d '\n') | |
# Construct the ULA address prefix with the generated global ID | |
local ula_prefix="fd${random_id:0:2}:${random_id:2:4}:${random_id:6:4}" | |
echo "${ula_prefix}::1" | |
} | |
_cluster_cidr(){ | |
echo "$(_gen_ula)/56" | |
} | |
_service_cidr(){ | |
echo "$(_gen_ula)/112" | |
} | |
# if executed | |
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then | |
set -euxo pipefail | |
ULA="$(_gen_ula)" | |
BIND_SUBNET="${ULA}/64" | |
BIND_ADDRESS="${ULA}/128" | |
if ! ip -o -6 route get "${BIND_ADDRESS}" | grep -qE '\sdev\slo\s' | |
then | |
sudo ip -6 addr add "${BIND_SUBNET}" dev lo | |
fi | |
# Check if there is a default IPv6 route | |
if ! ip -6 route show default | grep -q '^default' | |
then | |
EGRESS_INTERFACE="$(ip -o -4 route show default | awk '{print $5}')" | |
ip -6 route add default dev "${EGRESS_INTERFACE}" | |
fi | |
curl -sfL https://get.k3s.io \ | |
| sudo INSTALL_K3S_EXEC="server" bash -s - \ | |
--node-ip "${ULA}" \ | |
--cluster-cidr "$(_cluster_cidr)" \ | |
--service-cidr "$(_service_cidr)" \ | |
--flannel-ipv6-masq | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment