|
| 1 | +# Setting Up Kubernetes Dashboard with Minikube |
| 2 | + |
| 3 | +This guide provides step-by-step instructions for setting up the Kubernetes Dashboard in your Minikube cluster with automatic startup capabilities. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Kubernetes Dashboard provides a web-based user interface for managing your cluster. This setup includes: |
| 8 | + |
| 9 | +- Dashboard deployment in Minikube cluster |
| 10 | +- Service account with appropriate permissions |
| 11 | +- External accessibility from Windows host |
| 12 | +- Persistent configuration across cluster restarts |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +- Minikube cluster installed and running |
| 17 | +- kubectl configured to work with your Minikube cluster |
| 18 | +- WSL2 environment configured |
| 19 | +- Administrative access to both WSL2 and Windows |
| 20 | + |
| 21 | +## Step 1: Deploy Kubernetes Dashboard |
| 22 | + |
| 23 | +Deploy the official Kubernetes Dashboard to your Minikube cluster: |
| 24 | + |
| 25 | +```bash |
| 26 | +# Apply the dashboard manifests |
| 27 | +kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml |
| 28 | + |
| 29 | +# Verify the deployment |
| 30 | +kubectl get pods -n kubernetes-dashboard |
| 31 | +``` |
| 32 | + |
| 33 | +## Step 2: Create Service Account and RBAC |
| 34 | + |
| 35 | +Create a service account with appropriate permissions for dashboard access: |
| 36 | + |
| 37 | +```bash |
| 38 | +# Create the service account and cluster role binding |
| 39 | +cat <<EOF | kubectl apply -f - |
| 40 | +apiVersion: v1 |
| 41 | +kind: ServiceAccount |
| 42 | +metadata: |
| 43 | + name: admin-user |
| 44 | + namespace: kubernetes-dashboard |
| 45 | +--- |
| 46 | +apiVersion: rbac.authorization.k8s.io/v1 |
| 47 | +kind: ClusterRoleBinding |
| 48 | +metadata: |
| 49 | + name: admin-user |
| 50 | +roleRef: |
| 51 | + apiGroup: rbac.authorization.k8s.io |
| 52 | + kind: ClusterRole |
| 53 | + name: cluster-admin |
| 54 | +subjects: |
| 55 | +- kind: ServiceAccount |
| 56 | + name: admin-user |
| 57 | + namespace: kubernetes-dashboard |
| 58 | +EOF |
| 59 | +``` |
| 60 | + |
| 61 | +## Step 3: Create Dashboard Access Script |
| 62 | + |
| 63 | +Create a script to start the dashboard proxy and set up port forwarding: |
| 64 | + |
| 65 | +```bash |
| 66 | +# Create the dashboard access script |
| 67 | +cat <<'EOF' > ~/kind-dashboard.sh |
| 68 | +#!/bin/bash |
| 69 | +
|
| 70 | +# Function to get Kind cluster IP |
| 71 | +get_kind_ip() { |
| 72 | + # Get the Kind cluster IP from docker network |
| 73 | + KIND_IP=$(docker network inspect kind | jq -r '.[0].IPAM.Config[0].Gateway' 2>/dev/null) |
| 74 | + if [ -z "$KIND_IP" ] || [ "$KIND_IP" = "null" ]; then |
| 75 | + # Fallback: get IP from kubectl |
| 76 | + KIND_IP=$(kubectl get nodes -o jsonpath='{.items[0].status.addresses[?(@.type=="InternalIP")].address}') |
| 77 | + fi |
| 78 | + echo $KIND_IP |
| 79 | +} |
| 80 | +
|
| 81 | +# Function to setup port forwarding |
| 82 | +setup_port_forward() { |
| 83 | + local KIND_IP=$1 |
| 84 | +
|
| 85 | + # Remove existing port forwarding if any |
| 86 | + powershell.exe -Command "netsh interface portproxy delete v4tov4 listenport=10443" 2>/dev/null |
| 87 | +
|
| 88 | + # Add new port forwarding |
| 89 | + powershell.exe -Command "netsh interface portproxy add v4tov4 listenport=10443 listenaddress=0.0.0.0 connectport=10443 connectaddress=$KIND_IP" |
| 90 | +
|
| 91 | + echo "Port forwarding configured: localhost:10443 -> $KIND_IP:10443" |
| 92 | +} |
| 93 | +
|
| 94 | +# Main execution |
| 95 | +echo "Starting Kind Dashboard setup..." |
| 96 | +
|
| 97 | +# Wait for dashboard to be ready |
| 98 | +echo "Waiting for dashboard pods to be ready..." |
| 99 | +kubectl wait --for=condition=ready pod -l k8s-app=kubernetes-dashboard -n kubernetes-dashboard --timeout=300s |
| 100 | +
|
| 101 | +# Get Kind cluster IP |
| 102 | +KIND_IP=$(get_kind_ip) |
| 103 | +echo "Kind cluster IP: $KIND_IP" |
| 104 | +
|
| 105 | +# Setup port forwarding |
| 106 | +setup_port_forward $KIND_IP |
| 107 | +
|
| 108 | +# Start kubectl proxy in background |
| 109 | +echo "Starting kubectl proxy..." |
| 110 | +kubectl proxy --port=10443 --address=0.0.0.0 --accept-hosts='.*' & |
| 111 | +PROXY_PID=$! |
| 112 | +
|
| 113 | +# Save PID for cleanup |
| 114 | +echo $PROXY_PID > ~/.kind-dashboard-pid |
| 115 | +
|
| 116 | +echo "Dashboard is now accessible at: https://localhost:10443" |
| 117 | +echo "Press Ctrl+C to stop the proxy" |
| 118 | +
|
| 119 | +# Wait for interrupt |
| 120 | +trap "echo 'Stopping dashboard proxy...'; kill $PROXY_PID; rm -f ~/.kind-dashboard-pid; exit" INT |
| 121 | +wait $PROXY_PID |
| 122 | +EOF |
| 123 | + |
| 124 | +# Make the script executable |
| 125 | +chmod +x ~/kind-dashboard.sh |
| 126 | +``` |
| 127 | + |
| 128 | +## Step 4: Create Systemd Service (Optional) |
| 129 | + |
| 130 | +If you want the dashboard to start automatically, create a systemd service: |
| 131 | + |
| 132 | +### Option 1: Use the provided script (Recommended) |
| 133 | + |
| 134 | +```bash |
| 135 | +# Make the script executable |
| 136 | +chmod +x 03-kind/create-kind-dashboard-service.sh |
| 137 | + |
| 138 | +# Run the script to create the service file |
| 139 | +./03-kind/create-kind-dashboard-service.sh |
| 140 | + |
| 141 | +# Enable and start the service |
| 142 | +sudo systemctl daemon-reload |
| 143 | +sudo systemctl enable kind-dashboard.service |
| 144 | +sudo systemctl start kind-dashboard.service |
| 145 | +``` |
| 146 | + |
| 147 | +### Option 2: Manual creation |
| 148 | + |
| 149 | +If you prefer to create the service manually, replace `$USER` and `$HOME` with actual values: |
| 150 | + |
| 151 | +```bash |
| 152 | +# Get your username and home directory |
| 153 | +CURRENT_USER=$(whoami) |
| 154 | +USER_HOME=$(eval echo ~$CURRENT_USER) |
| 155 | + |
| 156 | +# Create the service file |
| 157 | +sudo tee /etc/systemd/system/kind-dashboard.service > /dev/null <<EOF |
| 158 | +[Unit] |
| 159 | +Description=Kind Kubernetes Dashboard Service |
| 160 | +After=network.target |
| 161 | +Wants=network.target |
| 162 | +
|
| 163 | +[Service] |
| 164 | +Type=simple |
| 165 | +User=$CURRENT_USER |
| 166 | +WorkingDirectory=$USER_HOME |
| 167 | +ExecStart=$USER_HOME/kind-dashboard.sh |
| 168 | +Restart=always |
| 169 | +RestartSec=10 |
| 170 | +Environment=KUBECONFIG=$USER_HOME/.kube/config |
| 171 | +
|
| 172 | +[Install] |
| 173 | +WantedBy=multi-user.target |
| 174 | +EOF |
| 175 | + |
| 176 | +# Enable and start the service |
| 177 | +sudo systemctl daemon-reload |
| 178 | +sudo systemctl enable kind-dashboard.service |
| 179 | +sudo systemctl start kind-dashboard.service |
| 180 | +``` |
| 181 | + |
| 182 | +## Step 5: Access the Dashboard |
| 183 | + |
| 184 | +### Method 1: Using the Script (Recommended) |
| 185 | + |
| 186 | +```bash |
| 187 | +# Run the dashboard script |
| 188 | +~/kind-dashboard.sh |
| 189 | +``` |
| 190 | + |
| 191 | +### Method 2: Manual Access |
| 192 | + |
| 193 | +```bash |
| 194 | +# Start kubectl proxy |
| 195 | +kubectl proxy --port=10443 --address=0.0.0.0 --accept-hosts='.*' |
| 196 | + |
| 197 | +# In another terminal, setup port forwarding |
| 198 | +KIND_IP=$(docker network inspect kind | jq -r '.[0].IPAM.Config[0].Gateway') |
| 199 | +powershell.exe -Command "netsh interface portproxy add v4tov4 listenport=10443 listenaddress=0.0.0.0 connectport=10443 connectaddress=$KIND_IP" |
| 200 | +``` |
| 201 | + |
| 202 | +## Step 6: Generate Authentication Token |
| 203 | + |
| 204 | +Generate a token for dashboard authentication: |
| 205 | + |
| 206 | +```bash |
| 207 | +# Create a token for the admin user |
| 208 | +kubectl create token admin-user -n kubernetes-dashboard |
| 209 | +``` |
| 210 | + |
| 211 | +## Step 7: Access the Dashboard |
| 212 | + |
| 213 | +1. Open your web browser and navigate to: [https://localhost:10443](https://localhost:10443) |
| 214 | + |
| 215 | +2. You'll be redirected to: [https://localhost:10443/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/](https://localhost:10443/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/) |
| 216 | + |
| 217 | +3. Paste the generated token from Step 6 into the dashboard login page. |
| 218 | + |
| 219 | +## Verification |
| 220 | + |
| 221 | +After completing the setup, verify that: |
| 222 | + |
| 223 | +- Dashboard pods are running: `kubectl get pods -n kubernetes-dashboard` |
| 224 | +- Service account exists: `kubectl get serviceaccount admin-user -n kubernetes-dashboard` |
| 225 | +- Port forwarding is active: `netsh interface portproxy show all` |
| 226 | +- Dashboard is accessible at [https://localhost:10443](https://localhost:10443) |
| 227 | + |
| 228 | +## Troubleshooting |
| 229 | + |
| 230 | +### Dashboard Pods Not Ready |
| 231 | + |
| 232 | +```bash |
| 233 | +# Check pod status |
| 234 | +kubectl get pods -n kubernetes-dashboard |
| 235 | + |
| 236 | +# Check pod logs |
| 237 | +kubectl logs -n kubernetes-dashboard -l k8s-app=kubernetes-dashboard |
| 238 | + |
| 239 | +# Check events |
| 240 | +kubectl get events -n kubernetes-dashboard |
| 241 | +``` |
| 242 | + |
| 243 | +### Port Forwarding Issues |
| 244 | + |
| 245 | +```bash |
| 246 | +# Remove existing port forwarding |
| 247 | +powershell.exe -Command "netsh interface portproxy delete v4tov4 listenport=10443" |
| 248 | + |
| 249 | +# Check current port forwarding rules |
| 250 | +netsh interface portproxy show all |
| 251 | +``` |
| 252 | + |
| 253 | +### Token Issues |
| 254 | + |
| 255 | +```bash |
| 256 | +# Delete and recreate the service account |
| 257 | +kubectl delete serviceaccount admin-user -n kubernetes-dashboard |
| 258 | +kubectl delete clusterrolebinding admin-user |
| 259 | + |
| 260 | +# Recreate using the commands from Step 2 |
| 261 | +``` |
| 262 | + |
| 263 | +### Kind Cluster Issues |
| 264 | + |
| 265 | +```bash |
| 266 | +# Check Kind cluster status |
| 267 | +kind get clusters |
| 268 | +kind get nodes |
| 269 | + |
| 270 | +# Restart Kind cluster if needed |
| 271 | +kind delete cluster |
| 272 | +kind create cluster |
| 273 | +``` |
| 274 | + |
| 275 | +### Systemd Service Issues |
| 276 | + |
| 277 | +If you encounter issues with the systemd service: |
| 278 | + |
| 279 | +```bash |
| 280 | +# Check service status |
| 281 | +sudo systemctl status kind-dashboard.service |
| 282 | + |
| 283 | +# Check service logs |
| 284 | +sudo journalctl -u kind-dashboard.service -f |
| 285 | + |
| 286 | +# Verify the service file syntax |
| 287 | +sudo systemd-analyze verify /etc/systemd/system/kind-dashboard.service |
| 288 | + |
| 289 | +# Check if the script exists and is executable |
| 290 | +ls -la ~/kind-dashboard.sh |
| 291 | + |
| 292 | +# Recreate the service file using the script |
| 293 | +./03-kind/create-kind-dashboard-service.sh |
| 294 | +``` |
| 295 | + |
| 296 | +## Cleanup |
| 297 | + |
| 298 | +To stop the dashboard: |
| 299 | + |
| 300 | +```bash |
| 301 | +# If using the script, press Ctrl+C |
| 302 | +# If using systemd service |
| 303 | +sudo systemctl stop kind-dashboard.service |
| 304 | + |
| 305 | +# Remove port forwarding |
| 306 | +powershell.exe -Command "netsh interface portproxy delete v4tov4 listenport=10443" |
| 307 | + |
| 308 | +# Kill any remaining proxy processes |
| 309 | +pkill -f "kubectl proxy" |
| 310 | +``` |
| 311 | + |
| 312 | +## Features |
| 313 | + |
| 314 | +With this configuration, your Kubernetes Dashboard will: |
| 315 | + |
| 316 | +- ✅ Deploy automatically in your Kind cluster |
| 317 | +- ✅ Provide secure access with token-based authentication |
| 318 | +- ✅ Be accessible from your Windows host at [https://localhost:10443](https://localhost:10443) |
| 319 | +- ✅ Support automatic restart via systemd (optional) |
| 320 | +- ✅ Work seamlessly with Kind cluster lifecycle |
0 commit comments