Dashboard官方地址: https://github.com/kubernetes/dashboard
dashbord是作为一个pod来运行,需要serviceaccount账号来登录。
先给dashboad创建一个专用的认证信息。
先建立私钥:
1
2
3
4
5
|
[root@master ~]# cd /etc/kubernetes/pki/
[root@master pki]# (umask 077 ; openssl genrsa -out dashboard.key 2048 )
Generating RSA private key, 2048 bit long modulus
.............................................................................................................................+++
.................................+++
|
建立一个证书签署请求:
1
|
[root@master pki]# openssl req -new -key dashboard.key -out dashboard.csr -subj "/O=zhixin/CN=dashboard"
|
下面开始签署证书:
1
2
3
4
|
[root@master pki]# openssl x 509 -req -in dashboard.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out dashboard.crt -days 365
Signature ok
subject=/O=zhixin/CN=dashboard
Getting CA Private Key
|
把上面生成的私钥和证书创建成secret
1
2
|
[root@master pki]# kubectl create secret generic dashboard-cert -n kube-system --from-file=dashboard.crt=./dashboard.crt --from-file=dashboard.key=./dashboard.key
secret/dashboard-cert created
|
1
2
|
[root@master pki]# kubectl get secret -n kube-system |grep dashboard
dashboard-cert Opaque 2 5 m
|
创建一个serviceaccount,因为dashborad需要serviceaccount(pod之间登录验证的用户)验证登录。
1
2
|
[root@master pki]# kubectl create serviceaccount dashboard-admin -n kube-system
serviceaccount/dashboard-admin created
|
1
2
|
[root@master pki]# kubectl get sa -n kube-system |grep admin
dashboard-admin 1 23 s
|
下面通过clusterrolebinding把dashboard-admin加入到clusterrole里面。
1
2
|
[root@master pki]# kubectl create clusterrolebinding dashboard-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
clusterrolebinding.rbac.authorization.k 8 s.io/dashboard-cluster-admin created
|
这样serviceaccount 用户dashboard-admin就拥有了管理所有集群的权限。
1
2
|
[root@master pki]# kubectl get secret -n kube-system |grep dashboard
dashboard-admin-token-hfxg 9 kubernetes.io/service-account-token 3 7 m
|
1
2
|
[root@master pki]# kubectl describe secret dashboard-admin-token-hfxg 9 -n kube-system
token: eyJhbGciOiJSUzI 1 NiIs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fGAkgfRPpxbwB_wyyProM-aIn 6 LL 4 aekUwBqbwOLQ
|
上面的token就是serviceaccount用户dashboad-admin的认证令牌。
下面开始部署dashboard
1
|
[root@master pki]# kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
|
1
2
3
|
[root@master ~]# kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
kubernetes-dashboard -767 dc 7 d 4 d -4 mq 9 z 1 / 1 Running 2 2 h
|
1
2
3
4
|
[root@master ~]# 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 21 d
kubernetes-dashboard ClusterIP 10.104 . 8.78 < none > 443 /TCP 45 m
|
1
2
|
[root@master ~]# kubectl patch svc kubernetes-dashboard -p '{"spec":{"type":"NodePort"}}' -n kube-system
service/kubernetes-dashboard patched
|
1
2
3
4
|
[root@master ~]# 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 21 d
kubernetes-dashboard NodePort 10.104 . 8.78 < none > 443: 31647 /TCP 47 m
|
这样我们就可以在集群外部使用31647端口访问dashboard了,ip就使用node master宿主机的ip。
用浏览器打开: https://172..16.1.100:31647,并把上面得到的token粘贴到令牌里面进行登录:
注意,要用火狐浏览器打开,其他浏览器打不开的,注意注意!!!
上面认证的方法,这个用户能看到所有集群的所有东西,是个超级管理员。下面我们再设置个用户,限定它只能访问default名称空间。
1
2
|
[root@master ~]# kubectl create serviceaccount def-ns-admin -n default
serviceaccount/def-ns-admin created
|
1
2
|
[root@master ~]# kubectl create rolebinding def-ns-admin --clusterrole=admin --serviceaccount=default:def-ns-admin
rolebinding.rbac.authorization.k 8 s.io/def-ns-admin created
|
1
2
3
4
|
[root@master ~]# kubectl get secret
NAME TYPE DATA AGE
admin-token -6 jpc 5 kubernetes.io/service-account-token 3 1 d
def-ns-admin-token -646 gx kubernetes.io/service-account-token 3 2 m
|
1
2
|
[root@master ~]# kubectl describe secret def-ns-admin-token -646 gx
token: eyJhbGciOiJSUzI 1 NiIsImtpZCI 6 IiJ 9 .eyJpc 3 MiOiJrdWJlcm 5 ldGVzL 3 NlcnZpY 2 VhY 2 NvdW 50 Iiwia 3 ViZXJuZXRlcy 5 pby 9 zZXJ 2 aWNlYWNjb 3 VudC 9 uYW 1 lc 3 BhY 2 UiOiJkZWZhdWx 0 Iiwia 3 ViZXJuZXRlcy 5 pby 9 zZXJ 2 aWNlYWNjb 3 VudC 9 zZWNyZXQubmFtZSI 6 ImRlZi 1 ucy 1 hZG 1 pbi 10 b 2 tlbi 02 NDZneCIsImt 1 YmVybmV 0 ZXMuaW 8 vc 2 VydmljZWFjY 291 bnQvc 2 VydmljZS 1 hY 2 NvdW 50 Lm 5 hbWUiOiJkZWYtbnMtYWRtaW 4 iLCJrdWJlcm 5 ldGVzLmlvL 3 NlcnZpY 2 VhY 2 NvdW 50 L 3 NlcnZpY 2 UtYWNjb 3 VudC 51 aWQiOiI 4 ODZiOGI 2 NC 1 jM 2 JmLTExZTgtYmIzNS 0 wMDUwNTZhMjRlY 2 IiLCJzdWIiOiJzeXN 0 ZW 06 c 2 VydmljZWFjY 291 bnQ 6 ZGVmYXVsdDpkZWYtbnMtYWRtaW 4 ifQ.MTyQW 7 Vn_ 1 j 9 cfmtYAE 4 CepmLsaMsMfE 5 VG 6 xkx 4 LsfrsKOO 2 FAo 1 bQuUtjLtAj 52 UzC 7 I 0 dVqQKpcx 1 DPxkr 8 QIpNm 37 PLE 01 geQ 0 C 0 me 7 QiRiM 9 KrFXmDtxUSLlhPBahxg-krlaANEWDKX 69 nss 6 qKiFgip 7 KHM_uP-b 1 d 1 caSE 8 y-zdEtTHK 8 QJ 9 reMb-EHG 6 iPkFpYJ -2 guDOUhL 5559 usR 16 o 2 AWoN 8 yRdcKtnpqwBV_n 2 UE 4 m 83 kLjA 30 PtYpqraIQp 9 yTa 21 jiVlceHZpWxx-HlOEjDE 4 ekNCe_xTorJ 7 MbHVTyfqr 37 o 8 fh 8 Gsh-P 5 _tK-qaDOO 7 pSMkHA
|
把上面的token登录到web页面的令牌,登录进去后只能看default名称空间的内容。
下面我们再用Kubeconf的方法来验证登录试试。
1
|
[root@master pki]# cd /etc/kubernetes/pki
|
1
2
|
[root@master pki]# kubectl config set-cluster kubernetes --certificate-authority=./ca.crt --server= "https://172.16.1.100:6443" --embed-certs=true --kubeconfig=/root/def-ns-admin.conf
Cluster "kubernetes" set.
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@master pki]# kubectl config view --kubeconfig=/root/def-ns-admin.conf
apiVersion: v 1
clusters:
- cluster:
certificate-authority-data: REDACTED
server: https:// 172.16 . 1.100: 6443
name: kubernetes
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []
|
1
2
3
4
|
[root@master pki]# kubectl get secret
NAME TYPE DATA AGE
admin-token -6 jpc 5 kubernetes.io/service-account-token 3 1 d
def-ns-admin-token -646 gx kubernetes.io/service-account-token 3 33 m
|
1
2
|
[root@master pki]# kubectl get secret def-ns-admin-token -646 gx -o json
"token" : "ZXlKaGJHY2lPaUpTVXpJMU5pSXNJbXRwWkNJNklpSjkuZXlKcGMzTWlPaUpyZFdKbGNtNWxkR1Z6TDNObGNuWnBZMlZoWTJOdmRXNTBJaXdpYTNWaVpYSnVaWFJsY3k1cGJ5OXpaWEoyYVdObFlXTmpiM1Z1ZEM5dVlXMWxjM0JoWTJVaU9pSmtaV1poZFd4MElpd2lhM1ZpWlhKdVpYUmxjeTVwYnk5elpYSjJhV05sWVdOamIzVnVkQzl6WldOeVpYUXVibUZ0WlNJNkltUmxaaTF1Y3kxaFpHMXBiaTEwYjJ0bGJpMDJORFpuZUNJc0ltdDFZbVZ5Ym1WMFpYTXVhVzh2YzJWeWRtbGpaV0ZqWTI5MWJuUXZjMlZ5ZG1salpTMWhZMk52ZFc1MExtNWhiV1VpT2lKa1pXWXRibk10WVdSdGFXNGlMQ0pyZFdKbGNtNWxkR1Z6TG1sdkwzTmxjblpwWTJWaFkyTnZkVzUwTDNObGNuWnBZMlV0WVdOamIzVnVkQzUxYVdRaU9pSTRPRFppT0dJMk5DMWpNMkptTFRFeFpUZ3RZbUl6TlMwd01EVXdOVFpoTWpSbFkySWlMQ0p6ZFdJaU9pSnplWE4wWlcwNmMyVnlkbWxqWldGalkyOTFiblE2WkdWbVlYVnNkRHBrWldZdGJuTXRZV1J0YVc0aWZRLk1UeVFXN1ZuXzFqOWNmbXRZQUU0Q2VwbUxzYU1zTWZFNVZHNnhreDRMc2Zyc0tPTzJGQW8xYlF1VXRqTHRBajUyVXpDN0kwZFZxUUtwY3gxRFB4a3I4UUlwTm0zN1BMRTAxZ2VRMEMwbWU3UWlSaU05S3JGWG1EdHhVU0xsaFBCYWh4Zy1rcmxhQU5FV0RLWDY5bnNzNnFLaUZnaXA3S0hNX3VQLWIxZDFjYVNFOHktemRFdFRISzhRSjlyZU1iLUVIRzZpUGtGcFlKLTJndURPVWhMNTU1OXVzUjE2bzJBV29OOHlSZGNLdG5wcXdCVl9uMlVFNG04M2tMakEzMFB0WXBxcmFJUXA5eVRhMjFqaVZsY2VIWnBXeHgtSGxPRWpERTRla05DZV94VG9ySjdNYkhWVHlmcXIzN284Zmg4R3NoLVA1X3RLLXFhRE9PN3BTTWtIQQ=="
|
1
|
[root@master pki]# DEF_NS_ADMIN_TOKEN=$(kubectl get secret def-ns-admin-token-646gx -o jsonpath={.data.token}|base64 -d)
|
1
2
|
[root@master pki]# kubectl config set-credentials def-ns-admin --token=$DEF_NS_ADMIN_TOKEN --kubeconfig=/root/def-ns-admin.conf
User "def-ns-admin" set.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[root@master pki]# kubectl config view --kubeconfig=/root/def-ns-admin.conf
apiVersion: v 1
clusters:
- cluster:
certificate-authority-data: REDACTED
server: https:// 172.16 . 1.100: 6443
name: kubernetes
contexts: []
current-context: ""
kind: Config
preferences: {}
users:
- name: def-ns-admin
|
1
2
|
[root@master pki]# kubectl config set-context def-ns-admin@kubernetes --cluster=kubernetes --user=def-ns-admin --kubeconfig=/root/def-ns-admin.conf
Context "def-ns-admin@kubernetes" created.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@master pki]# kubectl config view --kubeconfig=/root/def-ns-admin.conf
apiVersion: v 1
clusters:
- cluster:
certificate-authority-data: REDACTED
server: https:// 172.16 . 1.100: 6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: def-ns-admin
name: def-ns-admin@kubernetes
current-context: ""
kind: Config
preferences: {}
users:
- name: def-ns-admin
user:
token: eyJhbGciOiJSUzI 1 NiIsImtpZCI 6 IiJ 9 .eyJpc 3 MiOiJrdWJlcm 5 ldGVzL 3 NlcnZpY 2 VhY 2 NvdW 50 Iiwia 3 ViZXJuZXRlcy 5 pby 9 zZXJ 2 aWNlYWNjb 3 VudC 9 uYW 1 lc 3 BhY 2 UiOiJkZWZhdWx 0 Iiwia 3 ViZXJuZXRlcy 5 pby 9 zZXJ 2 aWNlYWNjb 3 VudC 9 zZWNyZXQubmFtZSI 6 ImRlZi 1 ucy 1 hZG 1 pbi 10 b 2 tlbi 02 NDZneCIsImt 1 YmVybmV 0 ZXMuaW 8 vc 2 VydmljZWFjY 291 bnQvc 2 VydmljZS 1 hY 2 NvdW 50 Lm 5 hbWUiOiJkZWYtbnMtYWRtaW 4 iLCJrdWJlcm 5 ldGVzLmlvL 3 NlcnZpY 2 VhY 2 NvdW 50 L 3 NlcnZpY 2 UtYWNjb 3 VudC 51 aWQiOiI 4 ODZiOGI 2 NC 1 jM 2 JmLTExZTgtYmIzNS 0 wMDUwNTZhMjRlY 2 IiLCJzdWIiOiJzeXN 0 ZW 06 c 2 VydmljZWFjY 291 bnQ 6 ZGVmYXVsdDpkZWYtbnMtYWRtaW 4 ifQ.MTyQW 7 Vn_ 1 j 9 cfmtYAE 4 CepmLsaMsMfE 5 VG 6 xkx 4 LsfrsKOO 2 FAo 1 bQuUtjLtAj 52 UzC 7 I 0 dVqQKpcx 1 DPxkr 8 QIpNm 37 PLE 01 geQ 0 C 0 me 7 QiRiM 9 KrFXmDtxUSLlhPBahxg-krlaANEWDKX 69 nss 6 qKiFgip 7 KHM_uP-b 1 d 1 caSE 8 y-zdEtTHK 8 QJ 9 reMb-EHG 6 iPkFpYJ -2 guDOUhL 5559 usR 16 o 2 AWoN 8 yRdcKtnpqwBV_n 2 UE 4 m 83 kLjA 30 PtYpqraIQp 9 yTa 21 jiVlceHZpWxx-HlOEjDE 4 ekNCe_xTorJ 7 MbHVTyfqr 37 o 8 fh 8 Gsh-P 5 _tK-qaDOO 7 pSMkHA
|
1
2
|
[root@master pki]# kubectl config use-context def-ns-admin@kubernetes --kubeconfig=/root/def-ns-admin.conf
Switched to context "def-ns-admin@kubernetes" .
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[root@master pki]# kubectl config view --kubeconfig=/root/def-ns-admin.conf
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: REDACTED
server: https: //172.16.1.100:6443
name: kubernetes
contexts:
- context:
cluster: kubernetes
user: def-ns-admin
name: def-ns-admin@kubernetes
current-context: def-ns-admin@kubernetes
kind: Config
preferences: {}
users:
- name: def-ns-admin
user:
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6ImRlZi1ucy1hZG1pbi10b2tlbi02NDZneCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJkZWYtbnMtYWRtaW4iLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI4ODZiOGI2NC1jM2JmLTExZTgtYmIzNS0wMDUwNTZhMjRlY2IiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6ZGVmYXVsdDpkZWYtbnMtYWRtaW4ifQ.MTyQW7Vn_1j9cfmtYAE4CepmLsaMsMfE5VG6xkx4LsfrsKOO2FAo1bQuUtjLtAj52UzC7I0dVqQKpcx1DPxkr8QIpNm37PLE01geQ0C0me7QiRiM9KrFXmDtxUSLlhPBahxg-krlaANEWDKX69nss6qKiFgip7KHM_uP-b1d1caSE8y-zdEtTHK8QJ9reMb-EHG6iPkFpYJ-2guDOUhL5559usR16o2AWoN8yRdcKtnpqwBV_n2UE4m83kLjA30PtYpqraIQp9yTa21jiVlceHZpWxx-HlOEjDE4ekNCe_xTorJ7MbHVTyfqr37o8fh8Gsh-P5_tK-qaDOO7pSMkHA
|
这时候/root/def-ns-admin.conf文件就可以用在dashboard中,用它进行登录了。
总结
1、部署:
1
|
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
|
2、将service改为NodePort:
1
|
kubectl patch svc kubernetes-dashboard -p '{"spec":{"type":"NodePort"}}' -n kube-system
|
3、认证:
认证时的账户必须为ServiceAccount:作用是被dashboard pod拿来由kubernetes进行认证。
第一种:token方式认证:
a) 创建serviceaccount,根据其管理目标,使用rolebinding或者clusterrolebinding绑定至合理role或者clusterrole;
b)获取到此serviceAccount的secret,查看secret的详细信息,其中就有token,粘贴到web界面的令牌里面
第二种: kubeconfig方式认证: 把serviceaccount的token封装为kubeconfig文件。
a) 创建serviceaccount,根据其管理目标,使用rolebinding或者clusterrolebinding绑定至合理role或者clusterrole;
b)
kubect get secret | awk '/^ServiceAccountName/{print $1}'
KUBE_TOKEN=DEF_NS_ADMIN_TOKEN=$(kubectl get secret SERVICEACCOUNT_SERCRET_NAME -o jsonpath={.data.token}|base64 -d)
c) 生成kubeconfig文件
kubectl config set-cluster --kubeconfig=/PATH/TO/SOMEFILE
kubectl config set-credentials NAME --token=$KUBE_TOKEN --kubeconfig=/PATH/TO/SOMEFILE
kubctl config set-context
kubectl config use-context
kubernetes集群的管理方式
1、命令式:create,run,expose,delete,edit....
2、命令式配置文件:create -f /PATH/TO/RESOURCE_CONFIGURATION_FILE,delete -f,replace -f
3、声明式配置文件:apply -f,patch,
一般建议不要混合使用上面三种方式。建议使用apply和patch这样的命令。