上篇文章中,已经完成了基于kubeadm安装的kubernetes集群,本文将基于上述的集群环境,搭建dashboard组件。

安装环境及版本
kubernetes版本及基础组件版本可参考前一篇安装集群环境的文章

dashboard组件:kubernetes-dashboard-v1.10.1

需准备的镜像:

k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1           
k8s.gcr.io/heapster-amd64:v1.5.4                        
k8s.gcr.io/heapster-influxdb-amd64:v1.5.2               
k8s.gcr.io/heapster-grafana-amd64:v5.0.4

 

#!/bin/bash
DASHDOARD_VERSION=v1.10.1
HEAPSTER_VERSION=v1.5.4
GRAFANA_VERSION=v5.0.4
INFLUXDB_VERSION=v1.5.2
username=registry.cn-hangzhou.aliyuncs.com/google_containers
images=(
        kubernetes-dashboard-amd64:${DASHDOARD_VERSION}
        heapster-grafana-amd64:${GRAFANA_VERSION}
        heapster-amd64:${HEAPSTER_VERSION}
        heapster-influxdb-amd64:${INFLUXDB_VERSION}
        )
for image in ${images[@]}
do
docker pull ${username}/${image}
docker tag ${username}/${image} k8s.gcr.io/${image}
docker rmi ${username}/${image}
done

 

主节点与个node节点均需要以上镜像:

k8s.gcr.io/kubernetes-dashboard-amd64 v1.10.1    f9aed6605b81 3 months ago 122MB
k8s.gcr.io/heapster-amd64 v1.5.4                 72d68eecf40c 8 months ago 75.3MB
k8s.gcr.io/heapster-influxdb-amd64 v1.5.2        eb180058aee0 8 months ago 16.5MB
k8s.gcr.io/heapster-grafana-amd64 v5.0.4         25e1da333f76 8 months ago 171MB


   在主节点上准备部署所需的yaml文件:

dashboard项目官方地址:https://github.com/kubernetes/dashboard/releases

这是作者自己修改过的yaml文件: https://github.com/chopperchan/k8s-dashboard.git

包含有:

kubernetes-dashboard.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.

# Configuration to deploy release version of the Dashboard UI compatible with
# Kubernetes 1.8.
#
# Example usage: kubectl create -f <this_file>

# ------------------- Dashboard Secret ------------------- #

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

---
# ------------------- Dashboard Service Account ------------------- #

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

---
# ------------------- Dashboard Role & Role Binding ------------------- #

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: kubernetes-dashboard-minimal
  namespace: kube-system
rules:
  # Allow Dashboard to create 'kubernetes-dashboard-key-holder' secret.
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["create"]
  # Allow Dashboard to create 'kubernetes-dashboard-settings' config map.
- apiGroups: [""]
  resources: ["configmaps"]
  verbs: ["create"]
  # Allow Dashboard to get, update and delete Dashboard exclusive secrets.
- apiGroups: [""]
  resources: ["secrets"]
  resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs"]
  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 from heapster.
- apiGroups: [""]
  resources: ["services"]
  resourceNames: ["heapster"]
  verbs: ["proxy"]
- apiGroups: [""]
  resources: ["services/proxy"]
  resourceNames: ["heapster", "http:heapster:", "https:heapster:"]
  verbs: ["get"]

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

---
# ------------------- Dashboard Deployment ------------------- #

kind: Deployment
apiVersion: apps/v1beta2
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: kubernetes-dashboard
  template:
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
    spec:
      containers:
      - name: kubernetes-dashboard
        image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1
        ports:
        - containerPort: 9090
          protocol: TCP
        args:
          #- --auto-generate-certificates
          # 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://10.0.1.168:8080
        volumeMounts:
        - name: kubernetes-dashboard-certs
          mountPath: /certs
          # Create on-disk volume to store exec logs
        - mountPath: /tmp
          name: tmp-volume
        livenessProbe:
          httpGet:
            scheme: HTTP
            path: /
            port: 9090
          initialDelaySeconds: 30
          timeoutSeconds: 30
      volumes:
      - name: kubernetes-dashboard-certs
        secret:
          secretName: kubernetes-dashboard-certs
      - name: tmp-volume
        emptyDir: {}
      serviceAccountName: kubernetes-dashboard-admin
      # Comment the following tolerations if Dashboard must not be deployed on master
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule

---
# ------------------- Dashboard Service ------------------- #

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  ports:
    - port: 9090
      targetPort: 9090
  selector:
    k8s-app: kubernetes-dashboard

# ------------------------------------------------------------
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-external
  namespace: kube-system
spec:
  ports:
    - port: 9090
      targetPort: 9090
      nodePort: 31666
  type: NodePort
  selector:
    k8s-app: kubernetes-dashboard

 


kubernetes-dashboard-admin.rbac.yaml

---
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-admin
  namespace: kube-system
  
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: kubernetes-dashboard-admin
  labels:
    k8s-app: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: kubernetes-dashboard-admin
  namespace: kube-system

 


heapster.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: heapster
  namespace: kube-system
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: heapster
  namespace: kube-system
spec:
  replicas: 1
  template:
    metadata:
      labels:
        task: monitoring
        k8s-app: heapster
    spec:
      serviceAccountName: heapster
      containers:
      - name: heapster
        image: k8s.gcr.io/heapster-amd64:v1.5.4
        imagePullPolicy: IfNotPresent
        command:
        - /heapster
        - --source=kubernetes:https://kubernetes.default
        - --sink=influxdb:http://monitoring-influxdb.kube-system.svc:8086
---
apiVersion: v1
kind: Service
metadata:
  labels:
    task: monitoring
    # For use as a Cluster add-on (https://github.com/kubernetes/kubernetes/tree/master/cluster/addons)
    # If you are NOT using this as an addon, you should comment out this line.
    kubernetes.io/cluster-service: 'true'
    kubernetes.io/name: Heapster
  name: heapster
  namespace: kube-system
spec:
  ports:
  - port: 80
    targetPort: 8082
  selector:
    k8s-app: heapster

 


heapster-rbac.yaml

kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: heapster
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:heapster
subjects:
- kind: ServiceAccount
  name: heapster
  namespace: kube-system

 


官方的文件中只包含了kubernetes-dashboard.yaml,且kubernetes在1.6版本以后启用了RBAC访问控制策略,直接使用官方版会出现访问权限不足的报错

 此问题可使用kubectl或Kubernetes API进行配置。使用RBAC可以直接授权给用户,让用户拥有授权管理的权限,这样就不再需要直接触碰Master Node。

相关修改为:

kubernetes-dashboard.yaml 中修改 serviceAccountName,外部访问dashboard的方式(本文为token访问)

kubernetes-dashboard-admin(默认为kubernetes-dashboard)

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-external
  namespace: kube-system
spec:
  ports:
    - port: 9090
      targetPort: 9090
      nodePort: 31666
  type: NodePort
  selector:
    k8s-app: kubernetes-dashboard

 


在kubernetes-dashboard-admin.rbac.yaml中则是执行了创建kubernetes-dashboard-admin用户并赋予所有权限的命令

PS:yaml中涉及的image版本请根据自己实际情况调整

 

安装dashboard及相关组件

ll
#以下内容其中grafana和influxdb是作者自己增加的,读者可不用部署
-rw-r--r-- 1 root root 2314 4月   3 10:02 grafana.yaml
-rw-r--r-- 1 root root  264 11月 23 15:19 heapster-rbac.yaml
-rw-r--r-- 1 root root 1100 4月   3 09:52 heapster.yaml
-rw-r--r-- 1 root root  998 4月   3 10:02 influxdb.yaml
-rw-r--r-- 1 root root  504 4月   3 10:13 kubernetes-dashboard-admin.rbac.yaml
-rw-r--r-- 1 root root 5132 4月   3 12:40 kubernetes-dashboard.yaml
 
#执行安装
kubectl apply -n kube-system -f .
 
#执行完成后查看pod是否正常运行
kubectl get pod -n kube-system |grep kubernetes-dashboard
 
kubernetes-dashboard-6cfdc589c7-krfmk   1/1     Running   0          164m
 
#查看服务暴露的端口号
kubectl get service  -n kube-system |grep kubernetes-dashboard
kubernetes-dashboard-external   NodePort    10.98.178.105    <none>        9090:31666/TCP           168m

 


Running状态说明运行正常,直接可通过masterip:端口号访问ui界面。

读者可根据需要修改nodePort参数来修改访问端口。

可能会遇到的问题
访问时出现权限不足

 

解决方法:

执行本文中的kubernetes-dashboard-admin.rbac.yaml,或读者自行创建serviceAccount,并修改kubernetes-dashboard.yml中serviceAccount参数为你创建的用户名称

 

转载于:https://www.cnblogs.com/ling-yu-amen/p/10955094.html

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐