Skip to content

Instantly share code, notes, and snippets.

@YiuTerran
Last active February 23, 2023 01:00
Show Gist options
  • Save YiuTerran/f229c686c1abd39f8105bcf587a50af7 to your computer and use it in GitHub Desktop.
Save YiuTerran/f229c686c1abd39f8105bcf587a50af7 to your computer and use it in GitHub Desktop.
ubuntu_install_k3s.sh
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "$0 is not running as root. Try using sudo."
exit 2
fi
set -e
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OFFLINE=0
MAX_POD_CNT=0
DATA_PATH=""
CLUSTER_MODE=0
AGENT_MODE=0
SERVER_TOKEN=""
SERVER_IP=""
# NOTE: k3s version must match rancher version!!!
K3S_VERSION="v1.23.16+k3s1"
export DEBIAN_FRONTEND=noninteractive
if [ -f "$DIR/do_install.sh" ] && [ -f "$DIR/k3s" ] && [ -f "$DIR/k3s-airgap-images-amd64.tar.gz" ]; then
OFFLINE=1
fi
help() {
echo "install script for k3s, only works for Ubuntu"
echo "Usage:"
echo " k3s_install.sh [-c] [-a] [-s server_url] [-t server_token] [-p max_pod_cnt] [-d data_path]"
echo " -c: install server as cluster mode, use embedded etcd instead of sqlite. Use it for multi servers, but not one server and multi agents. "
echo " -a: install k3s as agent. Use it with -t <server token> and -s <server url>"
echo " -t: token, for server or agent join cluster"
echo " -s: server ip"
echo " -p: max pod count, default is 110"
echo " -d: data path, default is /var/lib/rancher/k3s"
echo " -o: force online mode"
exit 0
}
while getopts 's:t:p:d:caoh' OPT; do
case $OPT in
c) CLUSTER_MODE=1 ;;
a) AGENT_MODE=1 ;;
t) SERVER_TOKEN="$OPTARG" ;;
s) SERVER_IP="$OPTARG" ;;
p) MAX_POD_CNT="$OPTARG" ;;
d) DATA_PATH="$OPTARG" ;;
o) OFFLINE=0 ;;
h) help ;;
?) help ;;
esac
done
if [ $OFFLINE -eq 1 ]; then
echo "Detect offline files, enable offline mode. You can still use -o to disable offline mode."
chmod a+x "$DIR/k3s"
K3S_VERSION=$("$DIR"/k3s --version | head -1 | awk '{print $3}')
fi
echo "To install k3s version is $K3S_VERSION"
# Check parameters
if [ $AGENT_MODE -eq 1 ] && { [ -z "$SERVER_IP" ] || [ -z "$SERVER_TOKEN" ]; }; then
echo "miss server_ip or server_token for agent mode"
exit 1
fi
if [ -n "$SERVER_IP" ] && [ -n "$SERVER_TOKEN" ] && [ $AGENT_MODE -eq 0 ]; then
echo "Cluster mode enabled"
CLUSTER_MODE=1
fi
# upgrade kernel
if [ $OFFLINE -eq 0 ]; then
apt update && apt upgrade -y
apt install -y curl
fi
# Check if DHCP is being used
# Retrieve the network interface name
iface=$(ip route | awk '/default/ { print $5 }' | head -1)
if [[ $(ip -o -4 addr show dev "$iface") == *"dynamic"* ]]; then
# Retrieve the current IP address and gateway settings from DHCP
ip=$(ip addr show "$iface" | awk '/inet / {print $2}' | cut -f1 -d'/')
gateway=$(ip route | awk '/default/ {print $3}' | head -1)
echo "$iface IP address set to static: $ip, gateway:$gateway"
sudo netplan generate
sudo bash -c "cat > /etc/netplan/01-netcfg.yaml" <<EOF
network:
version: 2
renderer: networkd
ethernets:
$iface:
dhcp4: no
addresses: [$ip/24]
gateway4: $gateway
nameservers:
addresses: [114.114.114.114,180.76.76.76]
EOF
sudo netplan apply
else
# If not using DHCP, do nothing
echo "IP address is already static, nothing to change..."
fi
# install if k3s not exist or version not match
if [ ! -f "/usr/local/bin/k3s" ] || [[ $(k3s --version | head -1 | awk '{print $3}') != "$K3S_VERSION" ]]; then
fresh_install=1
if [ -f "/usr/local/bin/k3s" ]; then
fresh_install=0
fi
opts="K3S_KUBECONFIG_MODE=\"644\""
cmd=""
server_url="https://$SERVER_IP:6443"
if [ $AGENT_MODE -eq 1 ]; then
echo "install k3s as agent, server url is $server_url"
opts="$opts K3S_URL=$server_url K3S_TOKEN=$SERVER_TOKEN"
else
if [ $CLUSTER_MODE -eq 1 ]; then
if [ -z "$SERVER_TOKEN" ] || [ -z "$SERVER_IP" ]; then
cmd="server --cluster-init"
else
opts="$opts K3S_TOKEN=$SERVER_TOKEN"
cmd="server --server $server_url"
fi
fi
fi
# firewall settings
if [[ $(ufw status) == *"inactive"* ]]; then
echo "firewall disabled, skip setting..."
else
echo "firewall enabled, set ufw..."
sudo ufw allow 2379/tcp
sudo ufw allow 2380/tcp
sudo ufw allow 6443/tcp
sudo ufw allow 8472/udp
sudo ufw allow 10250/tcp
sudo ufw allow 51820/udp
sudo ufw allow 51821/udp
fi
# set to `--kube-proxy-arg=proxy-mode=ipvs` if you want ipvs mode
exec_opts=""
if [ "$MAX_POD_CNT" -gt 0 ]; then
exec_opts="$exec_opts --kubelet-arg=max-pods=$MAX_POD_CNT"
fi
if [ -n "$DATA_PATH" ]; then
exec_opts="$exec_opts --data-dir=$DATA_PATH"
fi
if [ -n "$exec_opts" ]; then
exec_opts=$(echo "$exec_opts" | xargs)
opts="$opts INSTALL_K3S_EXEC='$exec_opts'"
fi
if [ $OFFLINE -eq 0 ]; then
echo "install k3s online..."
opts="$opts INSTALL_K3S_MIRROR=cn INSTALL_K3S_VERSION=$K3S_VERSION"
cmd="curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | $opts sh -s - $cmd"
else
echo "install k3s offline..."
gunzip -c "$DIR"/k3s-airgap-images-amd64.tar.gz >"$DIR"/k3s-airgap-images-amd64.tar
mkdir -p /var/lib/rancher/k3s/agent/images/
mv "$DIR"/k3s-airgap-images-amd64.tar /var/lib/rancher/k3s/agent/images/
cp k3s /usr/local/bin
chmod +x /usr/local/bin/k3s
chmod +x "$DIR"/do_install.sh
opts="$opts INSTALL_K3S_SKIP_DOWNLOAD=true"
cmd="$opts $DIR/do_install.sh $cmd"
fi
echo "$cmd"
eval "$cmd"
if [ -z "$SERVER_TOKEN" ]; then
echo ""
echo "server token is:"
cat /var/lib/rancher/k3s/server/token
echo "server ip is:"
ip addr show "$iface" | awk '/inet / {print $2}' | cut -f1 -d'/'
fi
if [ $fresh_install -eq 0 ]; then
echo "restart k3s for upgrade..."
systemctl restart k3s
fi
echo ""
else
echo "skip k3s install..."
fi
if [ $AGENT_MODE -eq 0 ]; then
echo "restart every 9 month for certificate rotate in server node"
grep 'systemctl restart k3s' /etc/crontab || echo '0 2 1 */9 * systemctl restart k3s >/dev/null 2>&1' >>/etc/crontab
echo ""
echo "=========================NOTE================================"
echo "Please register k3s to rancher manually."
echo "Then run 'kubectl get pods -A' to watch pod status"
echo "After all the pods getting ready, you should see k3s active in rancher"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment