一、前置准备:确保已安装Kubernetes集群
在Ubuntu上安装Kubernetes Dashboard前,需先部署一个可用的Kubernetes集群(若未安装,可参考以下基础步骤):
- 安装Docker
更新系统包并添加Docker官方源,安装Docker CE: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 - 安装Kubernetes组件
添加Kubernetes软件源,安装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 # 锁定版本避免自动升级 - 初始化Kubernetes集群
使用kubeadm初始化Master节点(假设Pod网络CIDR为10.244.0.0/16):sudo kubeadm init --pod-network-cidr=10.244.0.0/16 - 配置kubectl
将集群配置复制到当前用户目录:mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config - 安装网络插件
以Flannel为例,部署容器网络:kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
二、安装Kubernetes Dashboard
- 部署Dashboard
使用官方推荐的YAML文件部署Dashboard(最新版本可通过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"
三、配置访问权限
- 创建ServiceAccount
创建专用于Dashboard访问的ServiceAccount:kubectl create serviceaccount dashboard-admin -n kube-system - 绑定ClusterRole权限
为ServiceAccount绑定cluster-admin角色(最高权限,生产环境建议限制为必要权限):kubectl create clusterrolebinding dashboard-admin \ --clusterrole=cluster-admin \ --serviceaccount=kube-system:dashboard-admin
四、获取访问Token
- 生成Token
通过以下命令获取ServiceAccount对应的Token:kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep dashboard-admin | awk '{print $1}')输出中的
token字段即为登录Dashboard所需的凭证。
五、访问Dashboard
- 启动本地代理
在Ubuntu服务器上启动kubectl proxy,创建本地到集群的安全通道:kubectl proxy代理默认监听
localhost:8001。 - 通过浏览器访问
打开浏览器,输入以下URL(替换为服务器IP,若需外部访问需配置Ingress或NodePort):- 本地访问:
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/ - 外部访问(需提前配置端口转发或Ingress):
http://<服务器IP>:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
- 本地访问:
- 登录Dashboard
在登录页面选择“Token”选项,粘贴之前获取的Token,即可登录Dashboard。
可选:配置外部访问(NodePort方式)
若需从外部网络访问Dashboard,可将Dashboard服务类型改为NodePort:
- 编辑服务配置:
kubectl -n kubernetes-dashboard edit service kubernetes-dashboard - 修改服务类型:
将type: ClusterIP改为type: NodePort,保存退出。 - 获取访问端口:
查看服务详情,获取分配的NodePort(如30454):kubectl -n kubernetes-dashboard get service kubernetes-dashboard - 访问Dashboard:
通过http://<服务器IP>:<NodePort>访问(如http://192.168.1.100:30454)。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/1435850.html