Dashboard
这里坑是真的多
预先准备
1. 创建dashboard私钥
# (umask 077; openssl genrsa -out dashboard.key 2048)
2. 生成证书请求,如果需要通过域名访问dashboard,CN那块写对应的域名(未测试)
# openssl req -new -key dashboard.key -out dashboard.csr -subj "/O=LotusChing/CN=dashboard" # PS: CN=k8s.domain.com
3. 使用CA证书签发dashboard证书
# openssl x509 -req -in dashboard.csr -CA ca.crt -CAkey ca.key -out dashboard.crt -CAcreateserial -days 365
4. 创建Secret导入私钥及证书
# kubectl create secret generic dashboard-cert -n kube-system --from-file=./dashboard.crt --from-file=./dashboard.key
5. 创建dashboard集群管理员,能够管理所有名称空间资源
# kubectl create sa dashboard-admin -n kube-system
# kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
获取编辑dashboard资源配置清单
1. 获取资源配置清单
# wget https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
2. 编辑资源配置清单
首先要修改的就是Deployment中的args定义,将自动生成证书设置为false,并指明自定义使用的私钥和证书
...
args:
- --auto-generate-certificates=false
- --authentication-mode=token,basic
- --tls-cert-file=dashboard.crt
- --tls-key-file=dashboard.key
...
然后修改Deployment中volumes、volumeMounts的定义,配置使用刚才创建好的Secret资源dashboard-cert
...
volumeMounts:
- name: dashboard-cert
mountPath: /certs
...
...
volumes:
- name: dashboard-cert
secret:
secretName: dashboard-cert
- name: tmp-volume
emptyDir: {}
serviceAccountName: kubernetes-dashboard
...
应用创建Dashboard相关资源
应用资源清单创建Dashboard相关资源,由于默认Service类型是ClusterIP,如果需要集群外部访问的话,最简单的方法就是修改为NodePort
# kubectl apply -f kubernetes-dashboard.yaml
# kubectl patch svc -n kube-system kubernetes-dashboard -p '{"spec": {"type": "NodePort"}}'
访问 Kubernetes Dashboard
获取NodePort随即端口
# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP 18d
kubernetes-dashboard NodePort 10.97.48.132 <none> 443:31847/TCP 26m
打开浏览器访问对应端口,Node外部IP + NodePort随机端口,选择忽略证书安全,继续访问
不出意外能够看到三种认证方式,选择令牌认证,获取上面创建dashboard集群管理员帐号dashboard-admin
的token
# kubectl get secret -n kube-system |grep dashboard-admi
# kubectl describe secret -n kube-system dashboard-admin-token-t2kc5
复制token贴进输入框即可。
补充说明
名称空间级别管理员
创建 ServiceAccount
# kubectl create sa def-ns-admin
# kubectl create rolebinding def-ns-admin --clusterrole=admin --serviceaccount=default:def-ns-admin
获取Token登录Dashboard
# kubectl get secret | grep def
# kubectl describe secret def-ns-admin-token-kxmfk
使用kubeconfig登录验证(待完成)
# KUBE_TOKEN=$(kubectl get secret SERVCIEACCOUNT_SERRET_NAME -o jsonpath={.data.token} |
base64 -d)
生成kubeconfig文件
# kubectl config set-cluster --kubeconfig=/PATH/TO/SOMEFILE
# kubectl config set-credentials NAME --token=$KUBE_TOKEN --kubeconfig=/PATH/TO/SOMEFILE
# kubectl config set-context --cluster=kubernetes --user=USER --kubeconfig=/PATH/TO/SOMEFILE
# kubectl config use-context USER@kubernetes --kubeconfig=/PATH/TO/SOMEFILE