k8s Dashboard 配置(tocken登录实现)

1、获取dashboard image镜像

docker pull registry.cn-hangzhou.aliyuncs.com/rsqlh/kubernetes-dashboard:v1.10.1
docker tag registry.cn-hangzhou.aliyuncs.com/rsqlh/kubernetes-dashboard:v1.10.1 k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1
docker rmi registry.cn-hangzhou.aliyuncs.com/rsqlh/kubernetes-dashboard:v1.10.1

2、创建yaml文件

vim kubernetes-dashboard.yaml

set paste

# 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

---
# ------------------- 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/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: k8s.gcr.io/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:
  ports:
    - port: 443
      targetPort: 8443
  selector:
    k8s-app: kubernetes-dashboard

3、Kubernetes Dashboard 的部署非常简单,只需要运行

kubectl apply -f kubernetes-dashboard.yaml

4、修改对外访问接口 ——可以自己进入yaml文件中添加也可以用以下方式随机生成

kubectl patch svc kubernetes-dashboard -p '{"spec":{"type":"NodePort"}}' -n kube-system  #以打补丁方式修改dasboard的访问方式,添加随机的接口

5、查看对应的端口号

kubectl -n kube-system get service kubernetes-dashboard

kubectl get svc -n kube-system

NAME                       TYPE                                  DATA   AGE
def-ns-admin-token-sl8kd   kubernetes.io/service-account-token   3      27m
default-token-768l6        kubernetes.io/service-account-token   3      3h44m

详情

kubectl -n kube-system describe service kubernetes-dashboard

然后就可以通过 http://nodeIP:32729 来访问了。火狐浏览器
参考:

cd /etc/kuernetes/pki/
ls
1、给dashboard创建一个专用的证书
(umask 077; openssl genrsa -out dashboard.key 2048)
2、建立一个证书专属请求
openssl req -new -key dashboard.key -out dashboard.csr -subj "/O=magedu/CN=dashboard"
ls
3、给证书签证
openssl x509 -req -in dashboard.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out dashboard.crt -days 365
4、需要把生成的私钥证建成一个secret
kubectl create secret generic dashboard-cert -n kube-system --from-file=dashboard.crt=./dashboard.crt --from-file=dashboard.key=./dashboard.key
5、kubectl get secret -n kube-system

**tocken认证:**
 kubectl create serviceaccount dashboard-admin -n kube-system
 kubectl get sa -n kube-system
 1、把serviceaccount绑定在cluster集群角色上 有权限看到集群的所有信息--集群管理员
kubectl create clusterrolebinding dashboard-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
2、获取对应的 serviceaccount 的secret信息 找dashboard-admin
kubectl get secret -n kube-system
kubectl describe secret dashboard-admin-token-xkck5 -n kube-system
#dashboard-admin-token-xkck5为kubectl get secret -n kube-system查到的dashboard的secret
3、启动dashboard
kubectl apply -f kubernetes-dashboard.yaml
kubectl patch svc kubernetes-dashboard -p '{"spec":{"type":"NodePort"}}' -n kube-system


k8s Dashboard 配置(tocken登录实现)_第1张图片
k8s Dashboard 配置(tocken登录实现)_第2张图片
提示:认证时的账号必须为ServiceAccount:被dashboard pod拿来由kubernetes进行认证:
tocken:
(1)创建ServiceAccount,根据其管理目标,使用rolebinding或clusterrolebinding绑定至合理role或clusterrole;
(2)获取到此ServiceAccount的secret,查看secret的详细信息,其中就有tocken

6、查看 def-ns-admin 的 token

kubectl get secret
NAME                       TYPE                                  DATA   AGE
def-ns-admin-token-sl8kd   kubernetes.io/service-account-token   3      27m
default-token-768l6        kubernetes.io/service-account-token   3      3h44m

#获取 token 值,用来在填写网页登录认证信息

kubectl describe secret def-ns-admin-token-8vzj5
Name:         def-ns-admin-token-sl8kd
Namespace:    default
Labels:       
Annotations:  kubernetes.io/service-account.name: def-ns-admin
              kubernetes.io/service-account.uid: ddb6afef-881c-11e9-ba05-00163e00611e

Type:  kubernetes.io/service-account-token

Data
====
namespace:  7 bytes
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6ImRlZi1ucy1hZG1pbi10b2tlbi1zbDhrZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJkZWYtbnMtYWRtaW4iLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJkZGI2YWZlZi04ODFjLTExZTktYmEwNS0wMDE2M2UwMDYxMWUiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6ZGVmYXVsdDpkZWYtbnMtYWRtaW4ifQ.h_nPlFuTZTFizS_faXOQAEtdPPoDT_0wX6-ENdZp7kdawIfuKthmZ6poEzW7VggBd_D43H0MCHKXs8q12Ut17_NDeW3RM9eK2bfVW7hczpFjAL1mJOd_3_KZwwPKLdWfYgD42490J3ihRzRuKg3XGmrIUdgCLeIN_FvbGnb3TeYsXV5RXgduIMZscALiDtlrxM8Bz1pOTdUwj1kEyg-LhiaahTzxXawy2bHK38yNUJF7tid_V2YKW5SiYsY3oGDtMEyzl6yJa3lC43vm7vvFv5HmhyLh2zLjpyNMREb7R6zIM-fP_nRtzF_4asT116Ar9USn8404AsD5ZazMRkNl-g
ca.crt:     1025 bytes

方式二:令牌+tocken登录
如果报错

正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|151.101.228.133|:443... 失败:拒绝连接。

解决办法:

yum install ca-certificates

你可能感兴趣的:(IT,Kubernetes)