ubuntu

如何在 Ubuntu 上使用 Kubernetes Dashboard

小樊
55
2025-09-25 10:31:36
栏目: 智能运维

Prerequisites: Ensure Kubernetes Cluster is Installed
Before using Kubernetes Dashboard, you need a running Kubernetes cluster on Ubuntu. If you haven’t set one up, use kubeadm to initialize a cluster (replace 10.244.0.0/16 with your desired pod network CIDR):

sudo kubeadm init --pod-network-cidr=10.244.0.0/16

Follow the output to configure kubectl (copy /etc/kubernetes/admin.conf to ~/.kube/config) and install a network plugin (e.g., Flannel):

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

Verify nodes are ready:

kubectl get nodes

Ensure the cluster status is Ready before proceeding.

Step 1: Deploy Kubernetes Dashboard
Use the official YAML manifest to deploy the Dashboard. For newer versions (≥2.7.0), run:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml

This creates resources in the kubernetes-dashboard namespace (check with kubectl get pods -n kubernetes-dashboard). Wait for all pods to be Running (may take a few minutes).

Step 2: Configure Access Permissions
To access the Dashboard, create a ServiceAccount with cluster-admin privileges:

# Create a ServiceAccount
kubectl create serviceaccount dashboard-admin -n kubernetes-dashboard

# Bind it to the cluster-admin role
kubectl create clusterrolebinding dashboard-admin \
  --clusterrole=cluster-admin \
  --serviceaccount=kubernetes-dashboard:dashboard-admin

This grants the account full control over the cluster.

Step 3: Obtain Access Token
Generate a token for authenticating with the Dashboard. For long-term use (e.g., 1 year), run:

kubectl create token dashboard-admin -n kubernetes-dashboard --duration=87600h > dashboard-token.txt

View the token:

cat dashboard-token.txt

Copy the token—you’ll need it to log in.

Step 4: Access the Dashboard
Start the Kubernetes proxy to securely expose the Dashboard locally:

kubectl proxy

Open your browser and navigate to:

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

Click Token on the login page and paste the token from Step 3. You’ll gain access to the Dashboard.

Optional: Configure External/Long-Term Access

Basic Usage of Kubernetes Dashboard
Once logged in, you can:

The interface is intuitive—use the left sidebar to navigate between resource types.

0
看了该问题的人还看了