ubuntu

如何在Ubuntu上安装Kubernetes Dashboard

小樊
52
2025-10-18 17:41:47
栏目: 智能运维

一、前置准备:确保已安装Kubernetes集群

在Ubuntu上安装Kubernetes Dashboard前,需先部署一个可用的Kubernetes集群(若未安装,可参考以下基础步骤):

  1. 安装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
    
  2. 安装Kubernetes组件
    添加Kubernetes软件源,安装kubeletkubeadmkubectl
    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  # 锁定版本避免自动升级
    
  3. 初始化Kubernetes集群
    使用kubeadm初始化Master节点(假设Pod网络CIDR为10.244.0.0/16):
    sudo kubeadm init --pod-network-cidr=10.244.0.0/16
    
  4. 配置kubectl
    将集群配置复制到当前用户目录:
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
  5. 安装网络插件
    以Flannel为例,部署容器网络:
    kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
    

二、安装Kubernetes Dashboard

  1. 部署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"
    

三、配置访问权限

  1. 创建ServiceAccount
    创建专用于Dashboard访问的ServiceAccount:
    kubectl create serviceaccount dashboard-admin -n kube-system
    
  2. 绑定ClusterRole权限
    为ServiceAccount绑定cluster-admin角色(最高权限,生产环境建议限制为必要权限):
    kubectl create clusterrolebinding dashboard-admin \
      --clusterrole=cluster-admin \
      --serviceaccount=kube-system:dashboard-admin
    

四、获取访问Token

  1. 生成Token
    通过以下命令获取ServiceAccount对应的Token:
    kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep dashboard-admin | awk '{print $1}')
    
    输出中的token字段即为登录Dashboard所需的凭证。

五、访问Dashboard

  1. 启动本地代理
    在Ubuntu服务器上启动kubectl proxy,创建本地到集群的安全通道:
    kubectl proxy
    
    代理默认监听localhost:8001
  2. 通过浏览器访问
    打开浏览器,输入以下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/
  3. 登录Dashboard
    在登录页面选择“Token”选项,粘贴之前获取的Token,即可登录Dashboard。

可选:配置外部访问(NodePort方式)

若需从外部网络访问Dashboard,可将Dashboard服务类型改为NodePort

  1. 编辑服务配置
    kubectl -n kubernetes-dashboard edit service kubernetes-dashboard
    
  2. 修改服务类型
    type: ClusterIP改为type: NodePort,保存退出。
  3. 获取访问端口
    查看服务详情,获取分配的NodePort(如30454):
    kubectl -n kubernetes-dashboard get service kubernetes-dashboard
    
  4. 访问Dashboard
    通过http://<服务器IP>:<NodePort>访问(如http://192.168.1.100:30454)。

0
看了该问题的人还看了