Post

Longhorn Installation On K3S

Setting Longhorn Up With Persistent Storage On Each Agent

This guide demonstrates how to configure each agent in your K3S cluster to have 500GB of persistent storage using Longhorn. The script provided automates the following steps:

  1. Checks SSH access to agents.
  2. Formats the /dev/sdb drive as ext4 and mounts it at /mnt/longhorn on each agent.
  3. Installs Longhorn on the cluster using Helm.
  4. Verifies the status of Longhorn.
  5. Provides instructions to access the Longhorn UI and configure disks.

Script: setup-longhorn.sh

Github

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/bash

# Configure agent IP addresses
agent1=192.168.100.212
agent2=192.168.100.222
agent3=192.168.100.232

# Node username
user=k3s

# Array of agents
agents=($agent1 $agent2 $agent3)

# SSH key path
ssh_key="~/.ssh/id_rsa"

# Check SSH access
check_ssh() {
    for node in "${agents[@]}"; do
        echo "Checking connection to $node..."
        ssh -i $ssh_key $user@$node "echo 'Connected to $node!'"
        if [ $? -ne 0 ]; then
            echo "Cannot connect to $node. Check your SSH keys."
            exit 1
        fi
    done
}

# Format and mount /dev/sdb on agents
setup_storage() {
    for node in "${agents[@]}"; do
        echo "Setting up storage on $node..."
        ssh -i $ssh_key $user@$node <<EOF
            if lsblk | grep -q 'sdb'; then
                sudo mkfs.ext4 -F /dev/sdb
                sudo mkdir -p /mnt/longhorn
                sudo mount /dev/sdb /mnt/longhorn
                echo '/dev/sdb /mnt/longhorn ext4 defaults 0 2' | sudo tee -a /etc/fstab
                echo "Storage successfully set up on $node."
            else
                echo "/dev/sdb not found on $node!"
            fi
EOF
    done
}

# Install Longhorn on the cluster
install_longhorn() {
    echo "Installing Longhorn on the cluster..."
    kubectl create namespace longhorn-system
    helm repo add longhorn https://charts.longhorn.io
    helm repo update
    helm install longhorn longhorn/longhorn --namespace longhorn-system
    echo "Longhorn installation complete!"
}

# Verify Longhorn status
verify_longhorn() {
    echo "Verifying Longhorn installation..."
    kubectl -n longhorn-system get pods
}

# Main function
main() {
    echo "Starting setup..."
    check_ssh
    setup_storage
    install_longhorn
    verify_longhorn
    echo "Setup completed!"
}

# Start the script
main

How to Use the Script

Make the script executable:

1
chmod +x setup-longhorn.sh

Run the script from the admin VM:

1
./setup-longhorn.sh

Accessing the Longhorn UI

Go to Rancher, then navigate to the left sidebar where you should find a Longhorn tab with a link to the Longhorn UI.

Configuring Disks in the Longhorn UI

  1. Go to Node & Disk in the Longhorn UI.
  2. Select the desired node.
  3. Click Add Disk:
    • Name: Provide a name, e.g., 450GB-SSD.
    • Mount Path: Use the location of the mounted SSD, e.g., /mnt/longhorn.
    • Storage Capacity: Specify the amount of space to allocate, e.g., 400GB.
  4. Save the configuration.

What the Script Does

Checks SSH Access:

  • Ensures connectivity to all agents using the specified SSH key.

Configures Storage:

  • Formats the /dev/sdb drive as ext4 on each agent.
  • Creates a mount point at /mnt/longhorn.
  • Updates /etc/fstab to persist the mount across reboots.

Installs Longhorn:

  • Deploys Longhorn on your K3s cluster using Helm.

Verifies Installation:

  • Displays the status of Longhorn pods to confirm successful deployment.

Expected Results

  • Each agent will have /dev/sdb mounted at /mnt/longhorn.
  • Longhorn will be installed and running on your K3s cluster.
  • Disks will be configured for use via the Longhorn UI.

Enjoy your persistent storage with Longhorn! 🚀

This post is licensed under CC BY 4.0 by the author.