From Android to Kubernetes: Give Your OnePlus 6 New Life as a Kubernetes Node

2024-11-29

That old Android sitting in your drawer could be your next edge computing experiment. While most retired phones end up forgotten or recycled, we're going to transform this capable device into a lightweight Kubernetes node using PostmarketOS and K3s. Whether you're looking to tinker with edge computing or simply want to breathe new life into old hardware, this guide will walk you through the entire process.

Why Turn a Phone into a Kubernetes Node?

Before we dive into the technical details, let's talk about why this project makes sense:

  • Phones are power-efficient devices designed for 24/7 operation
  • The OnePlus 6's Snapdragon 845 processor and 6GB+ RAM are more than capable of running lightweight containers
  • Built-in battery provides natural UPS functionality
  • Integrated Wi-Fi and cellular capabilities offer flexible networking options
  • You're recycling hardware you already own

back

Requirements

You'll be completely replacing your phone's operating system, so there's no going back to Android without a fresh install. Here's what you'll need:

  • A OnePlus 6 (codename: enchilada) with OEM unlock capability (The same process applies to other phones but research will be required)
  • USB cable and a Linux machine for flashing
  • Basic command line knowledge and patience
  • Latest possible Android version installed (for bootloader compatibility)
  • Backup of any important data on the phone
  • Coffee (Optional but really helps)

Preparing Your Workstation

First, let's set up your Linux machine with the necessary tools. Different distributions have slightly different package names, but we'll cover the major ones:

# For Arch/Manjaro users
yay -S android-sdk-platform-tools android-udev

# For Ubuntu/Debian users
sudo apt install android-tools-adb android-tools-fastboot android-sdk udev

Pro tip: Avoid using generic android-tools packages, as they might be outdated. The SDK platform tools are regularly updated and provide better device compatibility.

The Point of No Return: Unlocking the Bootloader

This is where things get serious. Unlocking the bootloader will wipe all data on your device, but it's necessary for installing PostmarketOS. Here's how to do it safely:

  1. Enable Developer Options on your OnePlus 6:

    • Navigate to Settings → About Phone
    • Tap Build Number 7 times until you see the developer mode activated
    • Go back to Settings → Developer Options
    • Enable both "OEM unlocking" and "USB debugging"
  2. Enter fastboot mode by powering off the device and holding Volume Up + Power until you see "START"
  3. Run the unlock command:
sudo fastboot devices  # Verify connection
sudo fastboot oem unlock

If you don't see your device listed, try unplugging and replugging the USB cable. Some USB ports and cables work better than others for fastboot operations.

Unchained

Installing PostmarketOS: Your Gateway to Linux

PostmarketOS is a real Linux distribution designed for mobile devices. It's based on Alpine Linux, making it lightweight and perfect for our Kubernetes adventure. Let's get it installed:

# Install pmbootstrap
pip install --upgrade pip
pip install pmbootstrap

# Initialize the build environment
pmbootstrap init 

During initialization, you'll face several choices. Here's what I recommend:

  • Vendor: oneplus
  • Device: enchilada
  • Username: pick something memorable
  • User interface: console (we're building a server, not a phone)
  • Additional packages: networkmanager-cli,networkmanager-tui,wpa_supplicant

The flashing process is straightforward but critical:

# Generate the images
pmbootstrap install
sudo fastboot erase dtbo
pmbootstrap export
sudo fastboot flash userdata /tmp/postmarketOS-export/oneplus-enchilada.img
sudo fastboot flash boot /tmp/postmarketOS-export/boot.img
sudo fastboot reboot

Making It Battle-Ready

Now that you have Linux running on our phone, it's time to transform it into a proper Kubernetes node. This involves several key steps:

1. Network Configuration

First boot into your new system:

ssh your_username@172.16.42.1

Set up networking properly:

sudo apk add networkmanager wpa_supplicant vim htop
sudo rc-update add networkmanager
sudo rc-update add wpa_supplicant

# Create a new connection (replace SSID with your network name)
sudo nmcli connection add \
    con-name "static-wifi" \
    type wifi \
    ifname wlan0 \
    ssid "YOUR_WIFI_SSID"

# Configure static IP (adjust according to your network)
# After connection set it up in your router too! (This is highly specific and subjective to your router model/brand and so on)
sudo nmcli connection modify static-wifi \
    ipv4.method manual \
    ipv4.addresses $Your_Desired_IP/24 \
    ipv4.gateway $Your_Router_Gateway_IP \
    ipv4.dns "8.8.8.8,8.8.4.4" \
    wifi-sec.key-mgmt wpa-psk \
    wifi-sec.psk "YOUR_WIFI_PASSWORD"

# Enable the connection
sudo nmcli connection up static-wifi

# Verify configuration
ip addr show
nmcli connection show static-wifi

Static IP Issues

  • If IP conflicts occur: sudo nmcli connection modify static-wifi ipv4.addresses NEW_IP/24
  • Check DNS resolution: nslookup google.com
  • View routing table: ip route show
  • Verify gateway connectivity: ping -c 4 $Your_Router_Gateway_IP

2. Installing the Container Infrastructure

Docker will be our container runtime and K3s our lightweight Kubernetes distribution:

# System update
sudo apk update
sudo apk upgrade

# Docker setup
sudo apk add docker
sudo addgroup your_username docker
sudo rc-update add docker boot
sudo service docker start

# K3s installation
curl -sfL https://get.k3s.io | sh -s - --docker

3. Security Considerations

Running a Kubernetes node on a phone requires careful security configuration, and honestly setting up the firewall is the best way to get assurance that it won't be playing against you. Here's a robust firewall setup:

# Create firewall rules file
sudo vim /etc/nftables.rules

Add these carefully considered rules:

#!/usr/sbin/nft -f

flush ruleset

table inet filter {
    chain input {
        type filter hook input priority filter; policy drop;
        
        iifname "lo" accept
        ct state { established, related } accept
        
        tcp dport 6443 accept  # K3s API
        tcp dport 80 accept    # HTTP
        tcp dport 443 accept   # HTTPS
        tcp dport 22 accept    # SSH
        
        iifname "wlan*" accept
        iifname "usb*" accept
    }

    chain forward {
        type filter hook forward priority filter; policy accept;
        iifname "wlan*" accept
        iifname "usb*" accept
    }

    chain output {
        type filter hook output priority filter; policy accept;
    }
}

Enable and persist the firewall:

sudo chmod +x /etc/nftables.rules
sudo rc-update add nftables default
sudo nft -f /etc/nftables.rules
sudo rc-service nftables save

Verifying Your New Kubernetes Node

Let's make sure everything is working as expected:

# Check node status
sudo kubectl get nodes

# View cluster info
sudo kubectl cluster-info

# Monitor system resources
htop

# Take your kubeconfig with you to your Linux machine
# It resides /etc/rancher/k3s/k3s.yaml and you need sudo to copy it around
# Copy it to your home directory and then take it with you with scp
# Then you can merge it with your other kubeconfigs, export KUBECONFIG=/path or specify it in your kubectl commands, do as you prefer ;)

Common Issues and Solutions

Network Connectivity Problems

If you're having network issues:

  • Check connection status: nmcli connection show
  • Try cycling the connection: sudo nmcli connection down <name> && sudo nmcli connection up <name>
  • Verify IP configuration: ip addr show

K3s Troubleshooting

When K3s isn't behaving:

  • Check the logs: sudo journalctl -u k3s
  • Restart if needed: sudo rc-service k3s restart

Accomplished

Taking It Further

Now that you have a working Kubernetes node, consider these next steps:

  1. Set up persistent storage using the phone's internal storage
  2. Configure and setup Traefik and Cert manager for Ingress with TLS
  3. Configure monitoring to keep an eye on your node's health
  4. Deploy sample workloads to test performance
  5. Join additional nodes (maybe more old phones?) to create a cluster
  6. Implement proper backup solutions
  7. Passthrough video encoding/decoding capabilities, cameras, microphones or speakers to containers (?)

Real-World Applications

Your phone-turned-Kubernetes-node can serve several practical purposes:

  • Edge computing testbed
  • Local development environment
  • Home automation controller (Home Assistant)
  • Learning platform for container orchestration

Final Thoughts

Converting a OnePlus 6 into a Kubernetes node might seem like overkill, but it's an excellent way to learn about both mobile Linux and container orchestration. The process teaches valuable lessons about bootloaders, Linux systems, networking, and container infrastructure, all while giving new purpose to old hardware.

Remember: While this setup is great for learning and testing, carefully consider security implications before using it in any production capacity. Always keep your system updated and regularly monitor for any suspicious activity.

Additional Resources