Kubernetes Dashboard搭建流程

本文详细介绍如何在Kubernetes环境中部署和使用Dashboard UI,包括版本兼容性检查、镜像下载与导入、YAML文件修改、kubectl命令执行及浏览器访问流程。同时,探讨了常见问题如token失效与私有仓库维护。
摘要由CSDN通过智能技术生成

一. 背景


    通过 二进制方式 完成了kubernetes的安装,可以正常使用kubernetes的各种功能了,但是有点不足的是,只能通过命令查看/创建/删除/修改等操作,没有直观的Web UI界面感受直观,因此搭建kubernetes dashboard来进一步的管理容器应用。

二. 操作


    1. 调研所需的dashboard配置版本
        因为kubernetes已搭建完成,而且版本比较新v1.16.1,一般普通的dashboard会导致其与Kubernetes服务不兼容。
        官网调研,kubernetesv1.16.1所需的dashboard需要 v2.0以上,如果要展示各节点的cpu/内存/磁盘空间等使用情况,则需要安装metrics
    2. 跨网下载两个镜像并导入到本地仓库中
        因为国内无法直接访问相关的docker仓库,则需要一台网外服务器做跳板支持

网外]# docker pull kubernetesui/metrics-scraper:v1.0.1
网外]# docker pull docker.io/kubernetesui/dashboard:v2.0.0-beta4
网外]# docker save -i dashboard.tar docker.io/kubernetesui/dashboard:v2.0.0-beta4
网外]# docker save -i metrics.tar kubernetesui/metrics-scraper:v1.0.1
master]# docker load <  docker.io/kubernetesui/dashboard:v2.0.0-beta4
master]# docker load < kubernetesui/metrics-scraper:v1.0.1
master]# docker tag docker.io/kubernetesui/dashboard:v2.0.0-beta4 192.168.89.132:5000/kubernetesui/dashboard:v2.0.0-beta4
master]# docker tag kubernetesui/metrics-scraper:v1.0.1 192.168.89.132:5000/kubernetesui/metrics-scraper:v1.0.1
master]# docker push  192.168.89.132:5000/kubernetesui/dashboard:v2.0.0-beta4
master]# docker push  192.168.89.132:5000/kubernetesui/metrics-scraper:v1.0.1


    3. 到官网下载相应的dashboard yaml文件,并做修改
        位置:https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta4/aio/deploy/recommended.yaml
        修改镜像位置等信息 recommended.yaml

# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: Namespace
metadata:
  name: kubernetes-dashboard

---

apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard

---

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30000
  selector:
    k8s-app: kubernetes-dashboard
  type: NodePort

---

apiVersion: v1
kind: Secret
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-certs
  namespace: kubernetes-dashboard
type: Opaque

---

apiVersion: v1
kind: Secret
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-csrf
  namespace: kubernetes-dashboard
type: Opaque
data:
  csrf: ""

---

apiVersion: v1
kind: Secret
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-key-holder
  namespace: kubernetes-dashboard
type: Opaque

---

kind: ConfigMap
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-settings
  namespace: kubernetes-dashboard

---

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
rules:
  # Allow Dashboard to get, update and delete Dashboard exclusive secrets.
  - apiGroups: [""]
    resources: ["secrets"]
    resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs", "kubernetes-dashboard-csrf"]
    verbs: ["get", "update", "delete"]
    # Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map.
  - apiGroups: [""]
    resources: ["configmaps"]
    resourceNames: ["kubernetes-dashboard-settings"]
    verbs: ["get", "update"]
    # Allow Dashboard to get metrics.
  - apiGroups: [""]
    resources: ["services"]
    resourceNames: ["heapster", "dashboard-metrics-scraper"]
    verbs: ["proxy"]
  - apiGroups: [""]
    resources: ["services/proxy"]
    resourceNames: ["heapster", "http:heapster:", "https:heapster:", "dashboard-metrics-scraper", "http:dashboard-metrics-scraper"]
    verbs: ["get"]

---

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
rules:
  # Allow Metrics Scraper to get metrics from the Metrics server
  - apiGroups: ["metrics.k8s.io"]
    resources: ["pods", "nodes"]
    verbs: ["get", "list", "watch"]

---

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: kubernetes-dashboard
subjects:
  - kind: ServiceAccount
    name: kubernetes-dashboard
    namespace: kubernetes-dashboard

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: kubernetes-dashboard
subjects:
  - kind: ServiceAccount
    name: kubernetes-dashboard
    namespace: kubernetes-dashboard

---

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: kubernetes-dashboard
  template:
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
    spec:
      containers:
        - name: kubernetes-dashboard
          image: 192.168.89.132:5000/kubernetesui/dashboard:v2.0.0-beta4
          imagePullPolicy: Always
          ports:
            - containerPort: 8443
              protocol: TCP
          args:
            - --auto-generate-certificates
            - --namespace=kubernetes-dashboard
            # Uncomment the following line to manually specify Kubernetes API server Host
            # If not specified, Dashboard will attempt to auto discover the API server and connect
            # to it. Uncomment only if the default does not work.
            # - --apiserver-host=http://my-address:port
          volumeMounts:
            - name: kubernetes-dashboard-certs
              mountPath: /certs
              # Create on-disk volume to store exec logs
            - mountPath: /tmp
              name: tmp-volume
          livenessProbe:
            httpGet:
              scheme: HTTPS
              path: /
              port: 8443
            initialDelaySeconds: 30
            timeoutSeconds: 30
      volumes:
        - name: kubernetes-dashboard-certs
          secret:
            secretName: kubernetes-dashboard-certs
        - name: tmp-volume
          emptyDir: {}
      serviceAccountName: kubernetes-dashboard
      # Comment the following tolerations if Dashboard must not be deployed on master
      tolerations:
        - key: node-role.kubernetes.io/master
          effect: NoSchedule

---

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: dashboard-metrics-scraper
  name: dashboard-metrics-scraper
  namespace: kubernetes-dashboard
spec:
  ports:
    - port: 8000
      targetPort: 8000
  selector:
    k8s-app: dashboard-metrics-scraper

---

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    k8s-app: dashboard-metrics-scraper
  name: dashboard-metrics-scraper
  namespace: kubernetes-dashboard
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: dashboard-metrics-scraper
  template:
    metadata:
      labels:
        k8s-app: dashboard-metrics-scraper
    spec:
      containers:
        - name: dashboard-metrics-scraper
          image: 192.168.89.132:5000/kubernetesui/metrics-scraper:v1.0.1
          ports:
            - containerPort: 8000
              protocol: TCP
          livenessProbe:
            httpGet:
              scheme: HTTP
              path: /
              port: 8000
            initialDelaySeconds: 30
            timeoutSeconds: 30
          volumeMounts:
          - mountPath: /tmp
            name: tmp-volume
      serviceAccountName: kubernetes-dashboard
      # Comment the following tolerations if Dashboard must not be deployed on master
      tolerations:
        - key: node-role.kubernetes.io/master
          effect: NoSchedule
      volumes:
        - name: tmp-volume
          emptyDir: {}			

    4. 执行kubectl命令
        

master]# kubectl create -f recommended.yaml


    5. 火狐浏览器访问一台node节点,一定是火狐浏览器,谷歌和ie浏览器无法认证
        

https://192.168.89.133:30000


    6. 找出token,并在页面上输入token登录
        

master]# kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep kubernetes-dashboard | awk '{print $1}')
Name:         kubernetes-dashboard-certs
Namespace:    kubernetes-dashboard
Labels:       k8s-app=kubernetes-dashboard
Annotations:  <none>

Type:  Opaque

Data
====


Name:         kubernetes-dashboard-csrf
Namespace:    kubernetes-dashboard
Labels:       k8s-app=kubernetes-dashboard
Annotations:  <none>

Type:  Opaque

Data
====
csrf:  256 bytes


Name:         kubernetes-dashboard-key-holder
Namespace:    kubernetes-dashboard
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
pub:   459 bytes
priv:  1679 bytes


Name:         kubernetes-dashboard-token-wtqxq
Namespace:    kubernetes-dashboard
Labels:       <none>
Annotations:  kubernetes.io/service-account.name: kubernetes-dashboard
              kubernetes.io/service-account.uid: 542b2b68-cd41-40fe-811e-f7a5d52e6393

Type:  kubernetes.io/service-account-token

Data
====
ca.crt:     1090 bytes
namespace:  20 bytes
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6ImsxbF9vUHFTbWxSbk1ZTWN5QmFuUEFhcDhpRXlQWFllR2RObE5EMjdTYWcifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC10b2tlbi13dHF4cSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjU0MmIyYjY4LWNkNDEtNDBmZS04MTFlLWY3YTVkNTJlNjM5MyIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlcm5ldGVzLWRhc2hib2FyZDprdWJlcm5ldGVzLWRhc2hib2FyZCJ9.FF2H_r0M8UOZnhhsluDQ3797pF8_VkPWKEVN4n8at9GrE63VpOhioTx4q-JBLC3mJliJCABybr7GFQiNvu6Q8wUPvlYfhmIdtQqhuyKMfBD9s_C7Mz_pQBnqTulxGUGE-dXFqQcAgpWZgXS9Zgaxcl3B4lDyzV50XfU5-mGpF8dMkTrJTktvVosEQgLJxhWFzeuUg5MPo27s7Y8Idh60RpASMbvBbWH2b1-CUHCPDqyxL_Wqgia0R7wn8Pu9gvNDKfekrutn7n4qnBTngvQFcBX7VWLcEIBvRUW4IJ8ouIcMljv_MwGSHwplLa0_Jcwumr0xeqo4zH7LJbH5y9wffw


            最后有token值,复制到页面登录页即可

 

三. 后续

问题1:第二天登录dashboard发现无法登录了,查看pods运行情况,发现dashboard 镜像无法下载,开始排查

1. 先删除重新创建 recommended.yaml

master]# kubectl delete -f recommended.yaml
master]# kubectl create -f recommended.yaml

结果仍然是一样的,dashboard无法运行

2. 查看以下私有仓库是否正常

master]# ss -anptu | grep :5000
没有相应的进程,原来是私有仓库问题,现重启私有仓库并开机自启。
master]# systemctl restart docker-distribution
master]# systemctl enable docker-distribution
master]# systemctl status docker-distribution
Active: active (running) since Thu 2020-05-07 09:43:56 CST; 29s ago
master]# ss -anptu | grep 5000
tcp    LISTEN     0      128    [::]:5000               [::]:*                   users:(("registry",pid=8805,fd=3))

3. 再次打开火狐浏览器,通过token登录,发现输入token后点击登录没有反应,怀疑是token失效了,重新获取token值

使用新的token值登录成功了,由此可知,一段时间之后,token值失效,需要重新获取新的token值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值