kubernetes1.13安装dashboard
1、下载kubernetes-dashboard.yaml
wget https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended/kubernetes-dashboard.yaml
2、修改镜像地址(mirrorgooglecontainers/kubernetes-dashboard-amd64:v1.10.1)后安装
[root@master] ~$ kubectl create -f kubernetes-dashboard.yaml
secret/kubernetes-dashboard-certs created
serviceaccount/kubernetes-dashboard created
role.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
deployment.apps/kubernetes-dashboard created
service/kubernetes-dashboard created
3、稍等一会,pod创建好后,查看服务状态
[root@master] ~$ kubectl get service --all-namespaces | grep kubernetes-dashboard
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-system kubernetes-dashboard ClusterIP 10.107.116.183 <none> 443/TCP 6m5s
4、 授予Dashboard账户集群管理权限
要让dashboard可以访问集群的各种资源,需要给dashboard账户赋予权限,这一步很关键,如果你缺少这一步的话,你打开dashboard后会报很多forbidden,如下:
configmaps is forbidden: User "system:serviceaccount:kube-system:service-controller" cannot list resource "configmaps" in API group "" in the namespace "default"
close
warning
persistentvolumeclaims is forbidden: User "system:serviceaccount:kube-system:service-controller" cannot list resource "persistentvolumeclaims" in API group "" in the namespace "default"
close
warning
....
要获得管理集群的权限,新建admin-user-admin.rbac.yaml文件,内容如下
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kube-system
---
# Create ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kube-system
执行
[root@master] ~$ kubectl create -f admin-user-admin.rbac.yaml
找到admin-user-admin的token,记下这串token,等下登录的时候会使用,这个token默认是永久的。
[root@master] ~$ kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')
Name: admin-user-token-2zc6r
Namespace: kube-system
Labels: <none>
Annotations: kubernetes.io/service-account.name: admin-user
kubernetes.io/service-account.uid: 0a358e70-18d2-11e9-a9d0-000c29245f60
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 1025 bytes
namespace: 11 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLTJ6YzZyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiIwYTM1OGU3MC0xOGQyLTExZTktYTlkMC0wMDBjMjkyNDVmNjAiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06YWRtaW4tdXNlciJ9.nXbQv2KEkuEEJgkxQYfuMtiXOsWaMm1E_34dybyPoeSuChxlzA7HlQ13mtcTSIjmA7rVMv22XN0A2dTf6bbb-31XLLAcmWwzy1cajJCXcO5zjhUYdNHZwGb2sLE4WyDcMlXIjPLGFYflnLJQ_fkU6RfnjHU0Th3tJ_YRJvcPt7eieeG2lEF6iRl48kdF0IduOWh749AzMXqxdDbW56YlazD7dzBkyHDlrpYZvC93-a-BPYXR5MpFEYSUNQWg-PILkFgwWBP0dnpbBcS80BzmuaslEhE8bSq_JZ5h_aQjM0fhN2ogPQM-6cuKXPTmLnsQQ9NN4Vjrg0YSmsfHp9OwFw
4、 访问dashboard
官方文档:https://github.com/kubernetes/dashboard/wiki/Accessing-Dashboard---1.7.X-and-above
https://github.com/kubernetes/dashboard/wiki/Creating-sample-user
参考文档:http://www.voidcn.com/article/p-ybwenzlk-bsd.html
有三种访问方式
1)kubectl proxy方式(开发测试使用,不推荐)
[root@master] ~$ kubectl proxy
Starting to serve on 127.0.0.1:8001
集群外部不能访问,只能在本地浏览器上访问,http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/
2) nodeport方式
Service 目前是 ClusterIP 类型,为了可以从集群外部访问,可以将服务修改成 NodePort 类型。
[root@master] ~$ kubectl -n kube-system edit service kubernetes-dashboard
将type: ClusterIP 修改为 type: NodePort
service/kubernetes-dashboard edited
#也可以用一条命令来执行:
kubectl patch svc kubernetes-dashboard -p '{"spec":{"type":"NodePort"}}' -n kube-system
再次查看service,发现变成nodeport类型了
[root@master] ~$ kubectl -n kube-system get service kubernetes-dashboard
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-dashboard NodePort 10.107.116.183 <none> 443:31677/TCP 48m
然后就可以通过https://<node-ip>:<nodePort>来访问。我没用这种方式,但估计这种方式应该也需要创建证书然后导入浏览器,方法在下面。
3) apiserver方式(推荐)
这种方式也可以在集群外部访问,比较方便,但是配置稍微麻烦一点。
本节参考了:
http://www.voidcn.com/article/p-ybwenzlk-bsd.html
http://www.525.life/article?id=1510739742372
1、查看集群信息
[root@master] ~$ kubectl cluster-info
Kubernetes master is running at https://192.168.255.130:6443
KubeDNS is running at https://192.168.255.130:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
然后在浏览器中访问https://192.168.255.130:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/
,输出如下
{
"kind": "Status",
"apiVersion": "v1",
"metadata": {
},
"status": "Failure",
"message": "services \"kube-dns:dns\" is forbidden: User \"system:anonymous\" cannot get services/proxy in the namespace \"kube-system\"",
"reason": "Forbidden",
"details": {
"name": "kube-dns:dns",
"kind": "services"
},
"code": 403
}
报错403,说明有权限问题,这是因为最新版的k8s默认启用了RBAC,并为未认证用户赋予了一个默认的身份:anonymous。
对于API Server来说,它是使用证书进行认证的,而我们浏览器上没有这个证书,所以我们要为浏览器配置https证书。
3、创建证书
首先需要确认kubectl命令的配置文件,默认情况下为/etc/kubernetes/admin.conf,而且已经自动创建在$HOME/.kube/config中,如果没有创建则需要手动赋值。
cat $HOME/.kube/config
1
如果确认有集群的配置,则运行以下命令来生成一个p12格式的浏览器证书
# 生成client-certificate-data
grep 'client-certificate-data' ~/.kube/config | head -n 1 | awk '{print $2}' | base64 -d >> kubecfg.crt
# 生成client-key-data
grep 'client-key-data' ~/.kube/config | head -n 1 | awk '{print $2}' | base64 -d >> kubecfg.key
# 生成p12
openssl pkcs12 -export -clcerts -inkey kubecfg.key -in kubecfg.crt -out kubecfg.p12 -name "kubernetes-client"
按要求输入密码直接回车即可,密码不要胡乱输,后面给浏览器导入的时候要用。
运行完后在当前目录会有个kubecfg.p12证书文件。
4、然后手动将证书导入chrome
正常情况下,Chrome的证书都是自动导入的。手动导入证书,只是非正常情况下才会用到,比如遇到“您打开的链接不是私密连接”,这个时候就需要手动导入证书了。
点击浏览器 菜单-设置-高级-管理证书
选择“受信任的根证书颁发机构”这一栏,然后点击导入kubecfg.p12。(注意 版本的差别:chrome71选择个人)
然后根据步骤操作完。
5、导入上面生成的p12文件后,重启浏览器chrome://restart,访问https://192.168.255.130:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/,弹出证书信息,点击确定即可。
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')
目录 返回
首页