Skip to content

Instantly share code, notes, and snippets.

@0xBigBoss
Last active June 13, 2023 18:50
Show Gist options
  • Save 0xBigBoss/08679c786a8ca7695713f92d268461ce to your computer and use it in GitHub Desktop.
Save 0xBigBoss/08679c786a8ca7695713f92d268461ce to your computer and use it in GitHub Desktop.
Installs and opens the Kubernetes dashboard using the recommended Quickstart settings.
#!/bin/bash
# Function to check if Kubernetes Dashboard is installed
function is_dashboard_installed {
kubectl get services --namespace=kubernetes-dashboard | grep kubernetes-dashboard >/dev/null 2>&1
return $?
}
# Function to check if Service Account and ClusterRoleBinding are installed
function is_user_installed {
kubectl get serviceaccounts admin-user -n kubernetes-dashboard >/dev/null 2>&1 &&
kubectl get clusterrolebinding admin-user >/dev/null 2>&1
return $?
}
# Function to install Kubernetes Dashboard
function install_dashboard {
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
}
# Function to create a Service Account and ClusterRoleBinding
function create_user {
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard
EOF
}
# Function to start proxy and open Kubernetes Dashboard
function start_proxy {
echo "πŸš€ Starting kubectl proxy..."
kubectl proxy >/dev/null 2>&1 &
PROXY_PID=$!
echo "πŸŽ‰ Proxy started with PID: $PROXY_PID. Dashboard is available at http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/."
}
# Function to print the token for login
function print_token {
token=$(kubectl -n kubernetes-dashboard create token admin-user)
# if pbcopy installed, use it
if [ -x "$(command -v pbcopy)" ]; then
echo $token | pbcopy >/dev/null 2>&1
echo "πŸ“‹ Token copied to clipboard."
fi
echo "πŸ” Login with:
$token"
}
# Check if Kubernetes Dashboard is installed
if is_dashboard_installed; then
echo "πŸ‘ Kubernetes Dashboard is already installed."
else
echo "βš™οΈ Kubernetes Dashboard is not installed. Installing..."
install_dashboard
echo "βœ… Kubernetes Dashboard installed."
fi
# Check if Service Account and ClusterRoleBinding are installed
if is_user_installed; then
echo "πŸ‘ Service Account and ClusterRoleBinding are already installed."
else
echo "βš™οΈ Service Account and ClusterRoleBinding are not installed. Creating..."
create_user
echo "βœ… Service Account and ClusterRoleBinding created."
fi
# Print the token for login
print_token
# Start proxy and open Kubernetes Dashboard
start_proxy
# Kill the kubectl proxy process when the script exits
trap "kill $PROXY_PID" EXIT
# Open the proxy if user presses enter
read -p "Press enter to open Kubernetes Dashboard..." -n 1 -r
echo "🌐 Opening Kubernetes Dashboard..."
open http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
# Wait for the proxy process to exit
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment