create Kubernetes dash board port forwarding - unix1998/technical_notes GitHub Wiki
Yes, in addition to using kubectl proxy
, we can also use port-forwarding to access the Kubernetes Dashboard. This method is often preferred for more direct and controlled access to the Dashboard. Here’s how to set it up:
-
Deploy the Kubernetes Dashboard (if not already deployed):
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
-
Create a Service Account (if not already created):
kubectl create serviceaccount dashboard-admin-sa kubectl create clusterrolebinding dashboard-admin-sa --clusterrole=cluster-admin --serviceaccount=default:dashboard-admin-sa
-
Obtain the Bearer Token (if not already obtained):
kubectl get secret $(kubectl get serviceaccount dashboard-admin-sa -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}" > dashboard-token.txt cat dashboard-token.txt
-
Port-Forward the Kubernetes Dashboard Service: Find the name of the Kubernetes Dashboard pod:
kubectl get pods -n kubernetes-dashboard
Use the pod name from the previous command to set up port forwarding. Replace
kubernetes-dashboard-<pod-name>
with the actual pod name:kubectl port-forward -n kubernetes-dashboard service/kubernetes-dashboard 8443:443
This command forwards your local port 8443 to the Dashboard service on port 443.
-
Access the Dashboard: Open your web browser and go to:
https://localhost:8443/
-
Login to the Dashboard: Use the token you obtained in step 3 to log in.
-
Deploy the Dashboard: Ensure the Kubernetes Dashboard is deployed in your cluster.
-
Service Account and Role Binding: Create a service account with the necessary permissions.
-
Get the Bearer Token: Obtain the token for authentication.
-
Port-Forwarding: Forward a local port to the Dashboard service port. This allows you to access the Dashboard from your local machine via
https://localhost:8443
. -
Access the Dashboard: Open the forwarded URL in your web browser.
-
Login: Use the token to authenticate and access the Dashboard.
-
Security: As with using
kubectl proxy
, ensure you handle the token securely and limit access to the Dashboard. -
HTTPS: The Dashboard uses HTTPS by default. When accessing it via
https://localhost:8443
, your browser may warn you about the self-signed certificate. You can usually proceed by accepting the risk.
By following these steps, you can set up port forwarding to access the Kubernetes Dashboard, providing a more straightforward and secure method for accessing the UI.