Created
March 8, 2025 15:44
-
-
Save vitobotta/68b13edfe242fbf5c229bb347d7b69f6 to your computer and use it in GitHub Desktop.
htzner-k3s dedicated server as agent
This file contains hidden or 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
touch /etc/initialized | |
HOSTNAME=$(hostname -f) | |
PUBLIC_IP=$(hostname -I | awk '{print $1}') | |
if [ "true" = "true" ]; then | |
echo "Using private network " >/var/log/hetzner-k3s.log | |
SUBNET="10.0.1.0/24" | |
MAX_ATTEMPTS=30 | |
DELAY=10 | |
UP="false" | |
for i in $(seq 1 $MAX_ATTEMPTS); do | |
NETWORK_INTERFACE=$( | |
ip -o link show | | |
grep -E 'mtu 1400|mtu 1450' | | |
awk -F': ' '{print $2}' | | |
sed 's/@.*//' | | |
grep -Ev 'cilium|br|flannel|docker|veth' | | |
xargs -I {} bash -c 'ethtool {} &>/dev/null && echo {}' | | |
head -n1 | |
) | |
if [ ! -z "$NETWORK_INTERFACE" ]; then | |
echo "Private network IP in subnet $SUBNET is up" 2>&1 | tee -a /var/log/hetzner-k3s.log | |
UP="true" | |
break | |
fi | |
echo "Waiting for private network IP in subnet $SUBNET to be available... (Attempt $i/$MAX_ATTEMPTS)" 2>&1 | tee -a /var/log/hetzner-k3s.log | |
sleep $DELAY | |
done | |
if [ "$UP" = "false" ]; then | |
echo "Timeout waiting for private network IP in subnet $SUBNET" 2>&1 | tee -a /var/log/hetzner-k3s.log | |
fi | |
PRIVATE_IP=$( | |
ip -4 -o addr show dev "$NETWORK_INTERFACE" | | |
awk '{print $4}' | | |
cut -d'/' -f1 | | |
head -n1 | |
) | |
FLANNEL_SETTINGS=" --flannel-iface=$NETWORK_INTERFACE " | |
else | |
echo "Using public network " >/var/log/hetzner-k3s.log | |
PRIVATE_IP="${PUBLIC_IP}" | |
FLANNEL_SETTINGS=" " | |
fi | |
mkdir -p /etc/rancher/k3s | |
cat >/etc/rancher/k3s/registries.yaml <<EOF | |
mirrors: | |
"*": | |
EOF | |
curl -sfL https://get.k3s.io | K3S_TOKEN="..." INSTALL_K3S_VERSION="v1.30.8+k3s1" K3S_URL=https://10.0.0.3:6443 INSTALL_K3S_EXEC="agent \ | |
--node-name=$HOSTNAME --kubelet-arg "cloud-provider=external" --kubelet-arg "resolv-conf=/etc/k8s-resolv.conf" \ | |
--node-ip=$PRIVATE_IP \ | |
--node-external-ip=$PUBLIC_IP \ | |
--node-label=instance.hetzner.cloud/provided-by=robot \ | |
--node-label=instance.hetzner.cloud/is-root-server=true \ | |
$FLANNEL_SETTINGS " sh - | |
echo true >/etc/initialized |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment