十六、部署Dashboard

1、部署Dashboard

wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml

2、将Service改为NodePort

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

3.制作kubeconfig

必须使用k8s集群本身的ca证书,不然认证不通过
[root@k8s-master ~]# cd /etc/kubernetes/pki/
[root@k8s-master pki]# (umask 077;openssl genrsa -out dashboard.key 2048)
[root@k8s-master pki]# openssl  req -new -key dashboard.key -out dashboard.csr -subj "/O=qushuaibo/CN=dashboard"
如果要使用域名访问CN需要改为域名
[root@k8s-master pki]# openssl  x509 -req -in  dashboard.csr -CA ca.crt  -CAkey ca.key  -CAcreateserial -out dashboard.crt  -days 365
[root@k8s-master pki]# kubectl create secret generic  dashboard-cert  -n kube-system --from-file=dashboard.crt=./dashboard.crt  --from-file=dashboard.key=./dashboard.key 
如果我们浏览器登陆dashboard的ui页面的话config里面的用户账号必须是ServiceAccount账号,相当于是pod认证到api-server,所以必须提供ServiceAccount
认证的2种方式
①token认证
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin  (命名空间:账号)
这里设置的是集群管理员,如果不需要集群管理员的话那么可以根据上面绑定admin来实现单命名空间管理
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
或者
kubectl describe secrets -n kube-system  dashboard-admin-token-fhgph也可以
设置命名空间管理员
[root@k8s-master ~]# kubectl create serviceaccount  default-admin -n default
[root@k8s-master ~]# kubectl create rolebinding  default-admin  --clusterrole=admin --serviceaccount=default:default-admin
②kubeconfig认证(相当于把token封装为一个config文件)
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin 

[root@k8s-master rbac]# kubectl config set-cluster  default.conf --kubeconfig=/root/test.conf --embed-certs=true --certificate-authority=/etc/kubernetes/pki/ca.crt   --server="https://192.168.74.230:6443"
找出你secret的名字并查看token,然后用bash64解码
[root@k8s-master ~]# KUBE_TOKEN=$(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
获得解码的token
[root@k8s-master ~]# KUBE_JIEMA=$(kubectl get secret -n kube-system  $KUBE_TOKEN -o jsonpath={.data.token} | base64 -d )
创建users  qushuaibo
[root@k8s-master ~]#kubectl config set-credentials qushuaibo --token=$KUBE_JIEMA --kubeconfig=/root/test.conf  
添加users和集群的关系
[root@k8s-master ~]# kubectl config  set-context  [email protected] --cluster=default.conf  --user=qushuaibo   --kubeconfig=/root/test.conf 
添加默认登陆
[root@k8s-master ~]# kubectl config use-context  [email protected] --kubeconfig=/root/test.conf 
然后就可以用创建的test.conf 去访问Dashboard,当然这里创建的是admin的想针对命名空间管理员以及其他的需要另外设置

猜你喜欢

转载自blog.csdn.net/qq_26489043/article/details/112461987