在Ubuntu上安装Kubernetes Dashboard前,需先部署一个可用的Kubernetes集群(若未安装,可参考以下基础步骤):
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install -y docker-ce
kubelet、kubeadm、kubectl:curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl # 锁定版本避免自动升级
kubeadm初始化Master节点(假设Pod网络CIDR为10.244.0.0/16):sudo kubeadm init --pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml调整版本):kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
验证Deployment状态:kubectl get pods -n kubernetes-dashboard
# 确保所有Pod状态为"Running"
kubectl create serviceaccount dashboard-admin -n kube-system
cluster-admin角色(最高权限,生产环境建议限制为必要权限):kubectl create clusterrolebinding dashboard-admin \
--clusterrole=cluster-admin \
--serviceaccount=kube-system:dashboard-admin
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep dashboard-admin | awk '{print $1}')
输出中的token字段即为登录Dashboard所需的凭证。kubectl proxy,创建本地到集群的安全通道:kubectl proxy
代理默认监听localhost:8001。http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/http://<服务器IP>:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/若需从外部网络访问Dashboard,可将Dashboard服务类型改为NodePort:
kubectl -n kubernetes-dashboard edit service kubernetes-dashboard
type: ClusterIP改为type: NodePort,保存退出。30454):kubectl -n kubernetes-dashboard get service kubernetes-dashboard
http://<服务器IP>:<NodePort>访问(如http://192.168.1.100:30454)。