Some checks failed
Test / test (push) Has been cancelled
Co-authored-by: Cursor <cursoragent@cursor.com>
242 lines
6.4 KiB
Bash
Executable File
242 lines
6.4 KiB
Bash
Executable File
#!/bin/bash
|
|
# K3s Installation Script for Proxmox VMs
|
|
# Installs lightweight Kubernetes (K3s) for Azure Arc integration
|
|
|
|
set -e
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m'
|
|
|
|
# K3s configuration
|
|
K3S_VERSION="${K3S_VERSION:-latest}"
|
|
INSTALL_MODE="${INSTALL_MODE:-local}" # 'local' or 'remote'
|
|
REMOTE_IP="${REMOTE_IP:-}"
|
|
REMOTE_USER="${REMOTE_USER:-root}"
|
|
SSH_KEY="${SSH_KEY:-}"
|
|
|
|
log_info() {
|
|
echo -e "${GREEN}[INFO]${NC} $1"
|
|
}
|
|
|
|
log_warn() {
|
|
echo -e "${YELLOW}[WARN]${NC} $1"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
check_root() {
|
|
if [ "$INSTALL_MODE" = "local" ] && [ "$EUID" -ne 0 ]; then
|
|
log_error "Please run as root for local installation"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
install_k3s_local() {
|
|
log_info "Installing K3s locally..."
|
|
|
|
# Check if already installed
|
|
if command -v k3s &> /dev/null; then
|
|
log_warn "K3s already installed"
|
|
k3s --version
|
|
return
|
|
fi
|
|
|
|
# Install K3s
|
|
log_info "Downloading and installing K3s..."
|
|
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION="$K3S_VERSION" sh -
|
|
|
|
# Verify installation
|
|
if command -v k3s &> /dev/null; then
|
|
log_info "K3s installed successfully"
|
|
k3s --version
|
|
|
|
# Start and enable service
|
|
systemctl enable k3s
|
|
systemctl start k3s
|
|
|
|
# Wait for service to be ready
|
|
log_info "Waiting for K3s to be ready..."
|
|
sleep 10
|
|
|
|
# Verify service status
|
|
if systemctl is-active --quiet k3s; then
|
|
log_info "K3s service is running"
|
|
else
|
|
log_error "K3s service failed to start"
|
|
systemctl status k3s
|
|
exit 1
|
|
fi
|
|
else
|
|
log_error "K3s installation failed"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
install_k3s_remote() {
|
|
log_info "Installing K3s on remote host: $REMOTE_IP"
|
|
|
|
if [ -z "$REMOTE_IP" ]; then
|
|
log_error "REMOTE_IP must be set for remote installation"
|
|
exit 1
|
|
fi
|
|
|
|
# Check connectivity
|
|
if ! ping -c 1 -W 2 "$REMOTE_IP" &> /dev/null; then
|
|
log_error "Cannot reach remote host: $REMOTE_IP"
|
|
exit 1
|
|
fi
|
|
|
|
# Create installation script
|
|
cat > /tmp/install_k3s_remote.sh <<EOF
|
|
#!/bin/bash
|
|
set -e
|
|
|
|
# Check if already installed
|
|
if command -v k3s &> /dev/null; then
|
|
echo "K3s already installed"
|
|
k3s --version
|
|
exit 0
|
|
fi
|
|
|
|
# Install K3s
|
|
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION="$K3S_VERSION" sh -
|
|
|
|
# Verify installation
|
|
if command -v k3s &> /dev/null; then
|
|
echo "K3s installed successfully"
|
|
k3s --version
|
|
|
|
# Start and enable service
|
|
sudo systemctl enable k3s
|
|
sudo systemctl start k3s
|
|
|
|
# Wait for service to be ready
|
|
sleep 10
|
|
|
|
# Verify service status
|
|
if sudo systemctl is-active --quiet k3s; then
|
|
echo "K3s service is running"
|
|
sudo systemctl status k3s --no-pager
|
|
else
|
|
echo "K3s service failed to start"
|
|
sudo systemctl status k3s --no-pager
|
|
exit 1
|
|
fi
|
|
else
|
|
echo "K3s installation failed"
|
|
exit 1
|
|
fi
|
|
EOF
|
|
|
|
# Copy and execute on remote host
|
|
if [ -n "$SSH_KEY" ]; then
|
|
scp -i "$SSH_KEY" -o StrictHostKeyChecking=no /tmp/install_k3s_remote.sh "$REMOTE_USER@$REMOTE_IP:/tmp/"
|
|
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_IP" "chmod +x /tmp/install_k3s_remote.sh && /tmp/install_k3s_remote.sh"
|
|
else
|
|
scp -o StrictHostKeyChecking=no /tmp/install_k3s_remote.sh "$REMOTE_USER@$REMOTE_IP:/tmp/"
|
|
ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_IP" "chmod +x /tmp/install_k3s_remote.sh && /tmp/install_k3s_remote.sh"
|
|
fi
|
|
|
|
log_info "K3s installed on remote host"
|
|
}
|
|
|
|
configure_kubectl() {
|
|
log_info "Configuring kubectl..."
|
|
|
|
if [ "$INSTALL_MODE" = "local" ]; then
|
|
# Copy kubeconfig for local access
|
|
mkdir -p ~/.kube
|
|
cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
|
|
sed -i 's/127.0.0.1/localhost/g' ~/.kube/config
|
|
|
|
export KUBECONFIG=~/.kube/config
|
|
else
|
|
# Get kubeconfig from remote
|
|
mkdir -p ~/.kube
|
|
|
|
if [ -n "$SSH_KEY" ]; then
|
|
ssh -i "$SSH_KEY" -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_IP" \
|
|
"sudo cat /etc/rancher/k3s/k3s.yaml" > ~/.kube/config
|
|
else
|
|
ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_IP" \
|
|
"sudo cat /etc/rancher/k3s/k3s.yaml" > ~/.kube/config
|
|
fi
|
|
|
|
# Update server URL
|
|
sed -i "s/127.0.0.1/$REMOTE_IP/g" ~/.kube/config
|
|
export KUBECONFIG=~/.kube/config
|
|
fi
|
|
|
|
# Verify kubectl access
|
|
if command -v kubectl &> /dev/null; then
|
|
log_info "Testing kubectl connection..."
|
|
kubectl cluster-info
|
|
kubectl get nodes
|
|
else
|
|
log_warn "kubectl not found. Install it to manage the cluster:"
|
|
log_info " curl -LO https://dl.k8s.io/release/\$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
|
|
fi
|
|
}
|
|
|
|
verify_installation() {
|
|
log_info "Verifying K3s installation..."
|
|
|
|
if [ "$INSTALL_MODE" = "local" ]; then
|
|
if systemctl is-active --quiet k3s; then
|
|
log_info "K3s service is running"
|
|
k3s kubectl get nodes
|
|
k3s kubectl get pods --all-namespaces
|
|
else
|
|
log_error "K3s service is not running"
|
|
exit 1
|
|
fi
|
|
else
|
|
if [ -n "$SSH_KEY" ]; then
|
|
SSH_CMD="ssh -i $SSH_KEY -o StrictHostKeyChecking=no $REMOTE_USER@$REMOTE_IP"
|
|
else
|
|
SSH_CMD="ssh -o StrictHostKeyChecking=no $REMOTE_USER@$REMOTE_IP"
|
|
fi
|
|
|
|
if $SSH_CMD "sudo systemctl is-active --quiet k3s"; then
|
|
log_info "K3s service is running on remote host"
|
|
kubectl get nodes
|
|
kubectl get pods --all-namespaces
|
|
else
|
|
log_error "K3s service is not running on remote host"
|
|
exit 1
|
|
fi
|
|
fi
|
|
}
|
|
|
|
main() {
|
|
log_info "Starting K3s installation (mode: $INSTALL_MODE)..."
|
|
check_root
|
|
|
|
case "$INSTALL_MODE" in
|
|
local)
|
|
install_k3s_local
|
|
configure_kubectl
|
|
;;
|
|
remote)
|
|
install_k3s_remote
|
|
configure_kubectl
|
|
;;
|
|
*)
|
|
log_error "INSTALL_MODE must be 'local' or 'remote'"
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
verify_installation
|
|
log_info "K3s installation completed successfully!"
|
|
log_info "Kubeconfig location: ~/.kube/config"
|
|
}
|
|
|
|
main "$@"
|
|
|