Top Tags

Using Screen with MicroK8s dashboard proxy example

Using Screen with MicroK8s dashboard proxy example with basic commands

Introduction to GNU Screen

GNU Screen is a terminal multiplexer that allows you to manage multiple shell sessions from a single SSH connection. It's particularly useful for running long-running processes like MicroK8s dashboard proxies, as sessions persist even when you disconnect from the server.

Key Benefits:

  • Session Persistence: Processes continue running after SSH disconnection
  • Multi-window Management: Run multiple sessions in a single terminal
  • Session Sharing: Multiple users can attach to the same session
  • Process Recovery: Reconnect to running processes after network interruption

MicroK8s Dashboard Use Case

When exposing the MicroK8s dashboard via kubectl proxy, the process must remain active. Using Screen ensures the proxy continues running in the background, allowing you to disconnect from SSH while maintaining dashboard accessibility.

Create screen session

bash
1screen -S dashboard-proxy

This command creates a new named screen session called "dashboard-proxy". The -S flag specifies the session name, making it easier to identify and manage multiple sessions.

Common session naming conventions:

  • Use descriptive names: dashboard-proxy, k8s-monitor, log-watcher
  • Avoid spaces in session names
  • Use hyphens or underscores for multi-word names

After creating the session, you can start your MicroK8s dashboard proxy:

bash
1microk8s kubectl proxy --address='0.0.0.0' --accept-hosts='.*'

This proxy command makes the Kubernetes dashboard accessible from external networks by binding to all interfaces and accepting all host headers.

Detach from screen session

bash
1# Press Ctrl+A and then press D

Detach Sequence Breakdown:

  1. Hold Ctrl and press A (this is Screen's command prefix)
  2. Release both keys
  3. Press D to detach

The session will continue running in the background. You'll see a message like: [detached from 12345.dashboard-proxy]

Alternative detach method:

bash
1# Detach from outside the session
2screen -d dashboard-proxy

This command detaches the session programmatically, useful in scripts or when the session is attached elsewhere.

List all screen sessions

bash
1screen -ls

This displays all active screen sessions with their IDs and states (Attached/Detached). Example output:

There is a screen on: 12345.dashboard-proxy (Detached) 1 Socket in /run/screen/S-username.

Attach to screen session

bash
1screen -r dashboard-proxy

The -r flag stands for "reattach". This reconnects you to the specified screen session, allowing you to view and interact with the running process.

If the session is already attached elsewhere:

bash
1# Force reattach and detach other connections
2screen -dr dashboard-proxy

The -d flag detaches any other attached terminals first, then reattaches to your current terminal. This is useful when you have a stuck SSH connection.

Attach to session by ID:

bash
1# When you know the session ID (e.g., 12345)
2screen -r 12345

View session without attaching

bash
1# Multi-display mode - view session read-only
2screen -x dashboard-proxy

The -x flag allows multiple terminals to attach to the same session simultaneously. All users see the same output in real-time, useful for collaborative debugging or monitoring.

Delete screen session

bash
1screen -X -S dashboard-proxy quit

This command terminates the screen session completely, killing all processes running within it.

Command breakdown:

  • -X: Send a command to the screen session
  • -S dashboard-proxy: Specify the session name
  • quit: The command to terminate the session

Alternative termination methods:

bash
1# From inside the session: type 'exit' or press Ctrl+D
2exit
3
4# Kill all screen sessions at once (use with caution)
5killall screen
6
7# Kill specific session by ID
8screen -S 12345 -X quit

Warning: Terminating a screen session will also stop the MicroK8s proxy running inside it, making the dashboard inaccessible.

Advanced Screen Commands

Screen Window Management

bash
1# Create a new window within current session (Ctrl+A, then C)
2# Navigate between windows (Ctrl+A, then N for next)
3# Navigate to previous window (Ctrl+A, then P)
4# List all windows (Ctrl+A, then ")
5# Rename current window (Ctrl+A, then A)

Screen Logging

bash
1# Enable logging for current window
2# Press Ctrl+A, then H
3# This creates a screenlog.0 file in the current directory

This is invaluable for capturing output from long-running processes like kubectl proxy logs.

Screen Configuration

Create a .screenrc file in your home directory for persistent settings:

bash
1# Example ~/.screenrc configuration
2cat > ~/.screenrc << 'EOF'
3# Disable startup message
4startup_message off
5
6# Increase scrollback buffer
7defscrollback 10000
8
9# Show window list at the bottom
10hardstatus alwayslastline
11hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{= kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B} %m-%d %{W}%c %{g}]'
12
13# Enable mouse scrolling
14termcapinfo xterm* ti@:te@
15
16# Auto-detach on hangup
17autodetach on
18EOF

Complete MicroK8s Dashboard Workflow

Step-by-step deployment with Screen:

bash
1# 1. Install Screen if not present
2sudo apt update && sudo apt install screen -y
3
4# 2. Verify MicroK8s is running
5microk8s status
6
7# 3. Enable the dashboard addon
8microk8s enable dashboard
9
10# 4. Create a new screen session
11screen -S dashboard-proxy
12
13# 5. Start the proxy (this runs in the foreground)
14microk8s kubectl proxy --address='0.0.0.0' --accept-hosts='.*' --port=8001
15
16# 6. Detach from screen (Ctrl+A, then D)
17
18# 7. Verify session is running
19screen -ls
20
21# 8. Access dashboard via browser
22# http://your-server-ip:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

Getting Dashboard Token

bash
1# Create a service account token for dashboard access
2microk8s kubectl create token default -n kube-system
3
4# Or get existing token (pre-1.24)
5microk8s kubectl -n kube-system describe secret $(microk8s kubectl -n kube-system get secret | grep default-token | cut -d " " -f1)

Troubleshooting

Session not found

If screen -r dashboard-proxy returns "There is no screen to be resumed":

  • Check if the session exists: screen -ls
  • Verify the session name spelling
  • Ensure you're logged in as the correct user (screen sessions are user-specific)

Screen session appears dead

bash
1# Clean up dead sessions
2screen -wipe
3
4# List sessions again
5screen -ls

Port already in use

bash
1# Check if proxy is already running
2sudo netstat -tulpn | grep 8001
3
4# Or with ss command
5ss -tulpn | grep 8001
6
7# Kill the process using the port
8sudo kill -9 <PID>

Security Considerations

Warning: The command microk8s kubectl proxy --address='0.0.0.0' --accept-hosts='.*' exposes your Kubernetes dashboard to all network interfaces without authentication beyond the token.

Production recommendations:

  1. Use SSH tunneling instead:
    bash
    1ssh -L 8001:localhost:8001 user@server
    2microk8s kubectl proxy
  2. Implement firewall rules to restrict access
  3. Use kubectl port-forward for specific services
  4. Consider using a proper ingress controller with TLS

Additional Resources