Kubernetes dashboard安装

初学K8S,自己搭了个K8S集群,具体怎么搭的都忘记了,没啥困难的,主要是得科学上网从k8s.gcr.io上pull image,这篇文章不讨论怎么搭K8S集群,主要描述我怎么搭建K8S的dashboard。

既然是K8S的dashboard,那必然也是基于容器的。下面的链接可以自己先查查兼容性,确认需要安装哪个版本的dashboard。

https://github.com/kubernetes/dashboard/releases

首先下载dashboard的yaml文件:

wget https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended/kubernetes-dashboard.yaml

下载到本地后,先别急着部署容器,使用文本编辑器查找image,看看dashboard需要pull那些image:

containers:
- name: kubernetes-dashboard
image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1

上述可以看出,需要k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1这个image,需要科学上网pull下来。

如果k8s集群的各个节点不方便科学上网,例如我现在做测试的环境,那可以曲线救国:

1、找一台能访问k8s.gcr.io的服务器,docker pull k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1。

2、将image push到docker hub上:

$ docker login 
$ docker tag k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1 your_docker_account/kubernetes-dashboard-amd64:v1.10.1
$ docker push your_docker_account/kubernetes-dashboard-amd64:v1.10.1

3、在本地的k8s集群的每个节点上从docker hub上pull dashboard image。

$ docker login
$ docker pull your_docker_account/kubernetes-dashboard-amd64:v1.10.1
$ docker tag your_docker_account/kubernetes-dashboard-amd64:v1.10.1 k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1
$ docker rmi your_docker_account/kubernetes-dashboard-amd64:v1.10.1

下载完镜像,先别急着部署容器,还得修改一下dashboard的yaml文件。K8S集群基于RBAC方式访问,我们需要对账号做授权,这里我直接修改了kubernetes-dashboard-minimal账号的权限(正确的方法应该是新建一个账号,然后赋予相关权限)。修改kubernetes-dashboard.yaml文件,修改 kubernetes-dashboard-minimal的属性改为如下内容:

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

这里再附录一下如何新建账号并赋予cluster admin的权限:

apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kube-system


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

接下来就可以部署dashboard容器了:

$ kubectl create -f kubernetes-dashboard.yaml
$ kubectl get svc -n kube-system

使用浏览器访问dashboard。K8S dashboard提供了四种方位的方式:Proxy、NodePort、API Server和Ingress,详细介绍可参考如下链接:

https://github.com/kubernetes/dashboard/wiki/Accessing-Dashboard—1.7.X-and-above

这里主要讨论Proxy和NodePort:

1、Proxy方式访问。运行如下命令开启本地代理:

$ kubectl proxy
Starting to serve on 127.0.0.1:8001

开启浏览器,输入一下URL访问dashboard:

http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/

2、NodePort方式访问。编辑kubernetes-dashboard服务,修改service为NodePort方式:

$ kubectl -n kube-system edit service kubernetes-dashboard

修改type为NodePort:

apiVersion: v1
kind: Service
metadata:
creationTimestamp: 2019-03-05T09:21:22Z
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-system
resourceVersion: "12362875"
selfLink: /api/v1/namespaces/kube-system/services/kubernetes-dashboard
uid: 0b11a598-3f28-11e9-8c7c-00155d0c2307
spec:
clusterIP: 10.101.237.197
externalTrafficPolicy: Cluster
ports:
- nodePort: 39033
port: 443
protocol: TCP
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}

检查host端dashboard service expose的端口:

$ kubectl get svc -n kube-system 
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 53/UDP,53/TCP 126d
kubernetes-dashboard NodePort 10.101.237.197 443:39033/TCP 21h
tiller-deploy ClusterIP 10.111.108.186 44134/TCP 58d

在浏览器中输入地址: https://<master-ip>:39033 访问dashboard。

接下来我们在浏览器中能看到登录界面:

有两种方式登录,一个是Kubeconfig文件,一个是令牌,这里我们使用令牌登录。记得我们最开始修改 kubernetes-dashboard.yaml 文件,为kubernetes-dashboard用户赋予集群管理的权限,现在我们需要拿到 kubernetes-dashboard 的token:

$ kubectl -n kube-system get secrets |grep kubernetes-dashboard
kubernetes-dashboard-certs Opaque 0 21h
kubernetes-dashboard-csrf Opaque 1 21h
kubernetes-dashboard-key-holder Opaque 2 22h
kubernetes-dashboard-token-t5ksp kubernetes.io/service-account-token 3 21h
$ kubectl -n kube-system describe secret kubernetes-dashboard-token-t5ks

复制token到浏览器中,点击登录,这样就登录到K8S的dashboard啦!


此条目发表在docker分类目录。将固定链接加入收藏夹。

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注