Setup Kubernetes Local Dashboard on macOS

From Bonus Bits
Jump to: navigation, search

Purpose

This article gives the steps to set up a web-based GUI Dashboard for Kubernetes locally. Kubernetes distribution does not come with its dashboard enabled.


Prerequisites


All-in-One

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml && kubectl config set-credentials docker-desktop --token="$(kubectl -n kube-system describe secret default | grep token: | awk '{print $2}')" && nohup kubectl proxy > $HOME/.kube/dashboard.log &

Or use individual steps:

Deploy

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
Fetch Default Token
DASH_TOKEN="$(kubectl -n kube-system describe secret default | grep token: | awk '{print $2}')"
Set Credentails for docker-for-desktop Context
kubectl config set-credentials docker-for-desktop --token="${DASH_TOKEN}"

Start

kubectl proxy

OR

Run in Background
nohup kubectl proxy &
Run in Background with Log
nohup kubectl proxy > $HOME/.kube/dashboard.log &


Access Dashboard

  1. Browse to the URL
    http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/
  2. Select Kubeconfig Authentication
  3. Enter (CMD+SHIFT+G) / Browse to $HOME/.kube/config
  4. Select Login


List Running Dashboard Pod

kubectl get pod --namespace=kube-system | grep dashboard


Stop Dashboard Service

  1. Discover Process ID
    ps -elf | grep "kubectl proxy" | awk '{print $2}'
    
  2. Kill Process
    kill <pid>
    

One Liner

kill $(ps -elf | grep "kubectl proxy" | awk '{print $2}')


Example BASH Profile Script to Setup and Control

################################################################################
## Kubernetes - Dashboard
################################################################################
function kubdashsetup {
    kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml && kubectl config set-credentials docker-for-desktop --token="$(kubectl -n kube-system describe secret default | grep token: | awk '{print $2}')" && nohup kubectl proxy > $HOME/.kube/dashboard.log &
}
function kubdashsyspid {
    if ps -elf | grep 'kubectl proxy'; then
        kubdash_sys_pid=$(ps -elf | grep 'kubectl proxy' | awk '{print $2}')
    else
        kubdash_sys_pid="00000"
    fi
    # echo "DEBUG: Dashboard System PID ($kubdash_sys_pid)"
}
function kubdashfilepid() {
    if  [ -f "$HOME/.kube/dashboard.pid" ]; then
        kubdash_file_pid=$(cat $HOME/.kube/dashboard.pid)
    else
        kubdash_file_pid="00000"
    fi
    # echo "DEBUG: Dashboard File PID ($kubdash_file_pid)"
}
function kubdashstatus() {
    kubdashsyspid
    kubdashfilepid
    kubdash_status="Unknown"
    if [[ "$kubdash_sys_pid" == "00000" && "$kubdash_file_pid" == "00000" ]]; then
        kubdash_status="Stopped"
    elif [ "$kubdash_sys_pid" == "00000" ]; then
        kubdash_status="Stopped"
    else
        kubdash_status="Running"
    fi
    echo $kubdash_status
}
function kubdashstart() {
    kubdashsyspid
    kubdashfilepid
    if [ "$kubdash_status" == "Stopped" ]; then
        echo "Starting..."
        nohup kubectl proxy > $HOME/.kube/dashboard.log &
        echo $! > $HOME/.kube/dashboard.pid
        echo "Started"
    else
        echo "Already Running"
    fi
}
function kubdashstop() {
    kubdashsyspid
    kubdashfilepid
    if [[ "$kubdash_file_pid" == "00000" && "$kubdash_sys_pid" == "00000" ]]; then
        echo "Stopped"
    elif [ "$kubdash_sys_pid" == "00000" ]; then
        # If process not running but PID file exists cleanup the file
        echo "Stopped"
        rm -f $HOME/.kube/dashboard.pid
    else
        echo "Stopping..."
        if [[ "$kubdash_file_pid" == "$kubdash_sys_pid" ]]; then
            kill $kubdash_file_pid
        else
            kill $kubdash_sys_pid
        fi
        rm -f $HOME/.kube/dashboard.pid
        echo "Stopped"
    fi
}

# Use this Function to Setup and Control the Dashboard
function kubdash() {
    if [ "$1" == "stop" ]; then
        kubdashstop
    elif [ "$1" == "start" ]; then
        kubdashstart
    elif [ "$1" == "status" ]; then
        kubdashstatus
    elif [ "$1" == "setup" ]; then
        kubdashsetup
    else
        echo "Usage: kubdash [start|stop|status|setup]"
    fi
}


Related Articles


Sources