kubernetes-dashboard部署

背景

     想要直观地管理和监控k8s集群状况,kubernets-dashboard是一个比较大众的方式。dashboard提供了一个UI界面,使我们可以在页面上查看kubernetes的集群状态以及对集群进行相关的操作,大大便利了我们管理k8s集群。

在k8s中 dashboard可以有两种访问方式:kubeconfig(HTTPS)和token(http)本篇先来介绍下Token方式的访问。

Token访问是无登录密码的,简单方便

1、从官方网站上下载dashboard的yaml编排文件,并进行相应的修改。

# 官网版https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml

2、下载完之后开始修改YAML文件,修改镜像内容如下

image: registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetes-dashboard-amd64:v1.10.1

修改文件里面的镜像为自己可用的镜像

3、修改通过NodePort方式来进行访问dashboard:

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

kind: Service

apiVersion: v1

metadata:

  labels:

    k8s-app: kubernetes-dashboard

  name: kubernetes-dashboard

  namespace: kube-system

spec:

  type: NodePort      #增加type: NodePort

  ports:

    - port: 443

      targetPort: 8443

      nodePort: 31620  #增加nodePort: 31620

  selector:

    k8s-app: kubernetes-dashboard

4、官方提供的创建dashboard的yaml文件,由于创建的用户kubernetes-dashboard绑定的角色为kubernetes-dashboard-minimal,由于该角色并没有访问和操作集群的权限,因此登陆dashboard的时候,会提示权限错误:“configmaps is forbidden: User "system:serviceaccount:kube-system:kubernetes-dashboard"。因此需修改RoleBinding的相关参数,绑定权限更高的角色:

kind: ClusterRoleBinding

apiVersion: rbac.authorization.k8s.io/v1beta1

metadata:

  name: kubernetes-dashboard

subjects:

  - kind: ServiceAccount

    name: kubernetes-dashboard

    namespace: kube-system

roleRef:

  kind: ClusterRole

  name: cluster-admin

  apiGroup: rbac.authorization.k8s.io

5、master上通过kubernetes-dashboard.yaml文件,创建dashboard:

kubectl create -f kubernetes-dashboard.yaml

6、获取dashboard token

kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep kubernetes-dashboard-token|awk '{print $1}')|grep token:|awk '{print $2}'

7、通过火狐浏览器访问实例地址和服务端口(https://10.1.245.239:31620/#!/login)如下,拷贝步骤6中获取的token输入到令牌框,点击 登录 即可访问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.

# ------------------- 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

---

---

kind: ClusterRoleBinding

apiVersion: rbac.authorization.k8s.io/v1beta1

metadata:

  name: kubernetes-dashboard

subjects:

  - kind: ServiceAccount

    name: kubernetes-dashboard

    namespace: kube-system

roleRef:

  kind: ClusterRole

  name: cluster-admin

  apiGroup: rbac.authorization.k8s.io

---

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

kind: Deployment

apiVersion: apps/v1

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: registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetes-dashboard-amd64:v1.10.1

        ports:

        - containerPort: 8443

          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://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

---

---

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

kind: Service

apiVersion: v1

metadata:

  labels:

    k8s-app: kubernetes-dashboard

  name: kubernetes-dashboard

  namespace: kube-system

spec:

  type: NodePort      #增加type: NodePort

  ports:

    - port: 443

      targetPort: 8443

      nodePort: 31620  #增加nodePort: 31620

  selector:

    k8s-app: kubernetes-dashboard


8、通过上述创建的dashboard只能通过火狐访问,无法通过chrome等浏览器访问,是由于证书过期问题,如下解决证书过期。

 a: 由于证书无效,需要重新生成自签名证书,首先需要生成证书,生成证书通过openssl生成自签名证书即可,默认证书有效期为1个月,如果需要修改证书时间,可以增加-days参数,参考如下所示:

[ips@ips81 cert]$ openssl genrsa -out dashboard.key 2048 -days 365

Generating RSA private key, 2048 bit long modulus

...........................+++

.........+++

e is 65537 (0x10001)

[ips@ips81 cert]$ openssl req -new -out dashboard.csr -key dashboard.key -subj '/CN='10.1.235.81,10.1.235.82,10.1.235.72,10.1.245.239'' -days 365

[ips@ips81 cert]$ openssl x509 -req -in dashboard.csr -signkey dashboard.key -out dashboard.crt -days 365

Signature ok

subject=/CN=10.1.235.81,10.1.235.82,10.1.235.72,10.1.245.239

Getting Private key

[ips@ips81 cert]$ openssl x509 -in dashboard.crt -text -noout

Certificate:

    Data:

        Version: 1 (0x0)

        Serial Number: 12978830105745149643 (0xb41e11376515cecb)

    Signature Algorithm: sha1WithRSAEncryption

        Issuer: CN=10.1.235.81,10.1.235.82,10.1.235.72,10.1.245.239

        Validity

            Not Before: Apr  1 08:02:30 2019 GMT

            Not After : May  1 08:02:30 2019 GMT

        Subject: CN=10.1.235.81,10.1.235.82,10.1.235.72,10.1.245.239

        Subject Public Key Info:

            Public Key Algorithm: rsaEncryption

                Public-Key: (2048 bit)

                Modulus:

                    00:9f:4b:01:3c:d6:05:5c:1d:64:5e:e0:07:eb:3b:

                    c8:b5:d5:4b:1c:ca:5a:5c:44:49:93:b5:75:4a:e5:

                    b8:56:42:25:92:69:f1:09:d3:cf:31:75:7d:41:ed:

                    ea:92:68:e7:39:53:75:e5:92:be:db:da:ff:f9:63:

                    82:1e:58:32:54:5f:e6:b4:bc:5f:33:d5:c8:c0:eb:

                    2b:30:4d:ce:b0:22:50:7b:9a:f8:0e:ca:e9:a5:f5:

                    01:cf:8d:76:35:4a:38:12:a9:bd:85:26:f7:76:01:

                    a6:9f:8c:39:94:40:b2:10:fa:b2:fd:7a:bc:ce:0c:

                    33:cf:2d:b2:07:76:1e:55:05:e7:8d:95:95:d5:c7:

                    72:44:ff:b5:39:ae:b4:8d:83:40:05:a9:db:5e:ea:

                    6c:27:03:0b:65:a0:af:44:1e:f8:17:75:76:a9:66:

                    3d:56:04:51:fd:e1:1a:2e:ac:7b:9c:3a:f3:95:49:

                    d5:95:83:76:da:df:eb:41:d9:3f:4e:1e:3d:06:24:

                    fe:31:32:88:e8:4d:95:68:db:75:14:fa:6b:e6:5b:

                    f1:91:c0:12:82:65:ad:92:0d:48:b1:4a:d7:81:a1:

                    b4:53:c5:a2:99:f2:3f:25:33:3d:f7:a5:b0:bc:21:

                    ad:0b:7f:5f:06:aa:0e:ec:1b:a4:04:70:63:2f:d7:

                    21:9f

                Exponent: 65537 (0x10001)

    Signature Algorithm: sha1WithRSAEncryption

        37:28:4b:7e:4a:54:e1:5c:15:7c:e7:c0:71:c8:2f:ae:1b:ce:

        10:67:0a:c2:53:72:67:64:b3:4c:48:6b:bf:79:a0:cd:dd:c5:

        41:5a:0b:de:ff:78:04:10:ef:c1:4b:02:fb:ab:7e:88:f5:eb:

        6a:0d:d8:50:4f:ea:ba:73:06:2b:dd:6f:8a:28:6f:9a:20:73:

        76:42:c2:1e:54:d9:bd:4e:d5:ec:a0:13:c8:49:86:25:1b:e2:

        b0:03:fe:0c:0a:72:6f:f1:0b:4e:2b:0b:b9:63:07:a9:10:29:

        f6:a7:b4:c5:fb:e4:ee:86:97:e5:78:8a:51:2c:c5:8d:a9:33:

        85:7f:35:fb:78:80:de:70:f7:3e:c0:73:dd:4e:61:ab:22:b6:

        3f:90:7b:2b:6e:dc:7f:5e:cc:c9:8e:37:7c:b4:5b:30:fb:fb:

        8f:ed:a2:2c:ca:9e:9f:10:33:81:e2:e4:54:20:29:0c:85:8c:

        44:24:ee:c5:2d:1c:ca:1e:ba:31:46:cf:2d:80:13:05:70:5d:

        5e:76:b3:38:c3:d4:1a:b9:9c:57:49:90:4f:e1:14:9d:e3:33:

        fe:67:96:df:75:5d:55:da:a5:12:89:9e:4b:21:63:4a:5f:db:

        13:fd:2f:56:8f:25:ea:10:4e:66:04:0f:5d:96:8f:dd:56:f4:

        d3:f3:f5:d3

[ips@ips81 cert]$ ls

dashboard.crt  dashboard.csr  dashboard.key  kubernetes-dashboard.yaml

[ips@ips81 cert]$ ll

total 20

-rw-r--r-- 1 ips ips 1082 Apr  1 16:02 dashboard.crt

-rw-r--r-- 1 ips ips  944 Apr  1 16:02 dashboard.csr

-rw-r--r-- 1 ips ips 1679 Apr  1 16:02 dashboard.key

-rw-r--r-- 1 ips ips 5093 Apr  1 15:53 kubernetes-dashboard.yaml

[ips@ips81 cert]$


b: 将该配置文件中创建secret的配置文件信息去掉,将以下内容 从配置文件中去掉:

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

apiVersion: v1

kind: Secret

metadata:

  labels:

    k8s-app: kubernetes-dashboard

  name: kubernetes-dashboard-certs

  namespace: kube-system

type: Opaque

---

c: 重新生成secret,创建同名称的secret,名称为: kubernetes-dashboard-certs

kubectl create secret generic kubernetes-dashboard-certs --from-file=/data/ylh/k8sdashboard/cert/dashboard.key --from-file=/data/ylh/k8sdashboard/cert/dashboard.crt -n kube-system

kubectl describe secret kubernetes-dashboard-certs -n kube-system

d: 重新apply yaml文件或者删除之前已经在k8s创建的dashboard,重新create

kubectl apply -f kubernetes-dashboard.yaml   或者

kubectl create -f kubernetes-dashboard.yaml 

e: 此时通过chrome浏览器,可以跟火狐一样访问dashboard,首先获取token

[ips@ips81 cert]$ kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep kubernetes-dashboard-token|awk '{print $1}')|grep token:|awk '{print $2}'

eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC10b2tlbi16bjh4ciIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjYwNWMwMzE2LTU0NTMtMTFlOS04ODhmLWZhMTYzZWU2YTljOCIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTprdWJlcm5ldGVzLWRhc2hib2FyZCJ9.JIsJb0lcgs7sXFyHQAZnRlxamILSiixjjjSX0J3QZOYyXCIoFTlWgVlU-IANV-zZShnEHOtOsLsniJf5VxXGCZJ-uCLfU0RhcgtsUEBLbWLw45X3o3wl6j8D9yZgKYPywzapwNxttO0wsJd5ribNn5bmcnPsqQ2HqrUyRhnDwtb3TZiUKb0LQh9vyossiE9Vhv-_TbJJbvx8Z3dJWxb6Fp6vGak7jq4EhHH1tEbSmQCvBbZpXtzdOad_V5Nfr2uHUkFb8FjhbQqf0ItSCsO7xlwRvmdgzFHvH9HyVgDqninHyZxn-VDt85pPTBRilrYFQ3Dzs33MgShmSNzVs9DUlA


f: 访问dashboard的URL链接,(https://10.1.245.239:31620/#!/login)忽略提示,选择继续前往不安全的链接,令牌处输入上一步获取到的token,就可以正常访问dashboard。

g、查看chrome以及火狐的证书发现,证书有效期时间已经修改,不再为超过有效期的证书,不再是如下的0001年



https://www.jianshu.com/p/d6c10ee788a7

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,732评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,496评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,264评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,807评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,806评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,675评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,029评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,683评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,704评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,666评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,773评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,413评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,016评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,978评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,204评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,083评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,503评论 2 343

推荐阅读更多精彩内容