云计算
K8S使用dashboard管理集群
今年3月,在公司的内部k8s研修会上,开发同事表示使用dashboard,可以满足查看pod日志、执行exec指令、查看pod的执行状态等日常开发需求,但对于basic认证的权限控制
以前在1.5.2版中引入过dashboard服务。 1.9.1版的脱机部署还介绍了dashboard服务的RBAC配置和使用。 为此,本文在前文的基础上完善Heapster集成和利用token控制用户权限。
dashboard的特点主要包括:
1、可以直观查看rc、deployment、pod、services等k8s组件的运行情况和日志信息。
2、结合heapster和influxdb,可以在dashboard的监控图上看到pod的cpu和内存消耗情况。
Heapster简介
1、Heapster是一种容器群集监控和性能分析工具,支持Kubernetes和CoreOS操作系统。
2、K8S集群的HPA功能的实现依赖于这些metric数据,HPA将Heapster作为资源metrics API获取metric。
3、Kubernetes有cAdvisor监控(在1.9版中,cAdvisor已经与kubelet集成)。
在所有kubernetes上
Node运行cAdvisor以收集本地和容器监视数据( cpu、memory、文件系统、网络和更新)。 Heapster是采集者,Heapster采集节点的cAdvisor数据,汇总各节点的cAdvisor数据,按kubernetes的资源类型汇集资源,如Pod、Namespace、等也可以将数据导入到第三方工具,如InfluxDB。
Influxdb数据库简介
2、Influxdb数据库相关知识介绍,参考文献: https://www.Jian Shu.com/p/d 2935 e 99006 e
2、如果Heapster收集的metric数据没有持续化需求,则可以不重新配置Influxdb数据库
3、本文的Influxdb数据库存储采用emptydir方式实现,在实际使用过程中,应该可以选择的Influxdb数据库位于k8s集群之外,或者使用其他存储方案。
4、根据需要,也可以集成一个grafana进行web展示。 有关Grafana配置,请参阅文章编号https://blog.51cto.com/ylw 6006/2084403
一.获取相关镜像
需要科学地通过互联网获取dashboard相关的镜像文件,仓库可以放在本地仓库统一管理
# cat/etc/systemd/system/docker.service.d/http-proxy.conf [服务]
环境=& amp; #039; http _ proxy=http://192.168.115.2:1080 & amp; #039; # systemctldaemon-reload # systemctlrestartdocker # docker docker pull k8s.gcr.io/heap ster-influx d B- amd64:v 1.3.3
二.纪要准备
1、配置k8s-dashborad-sa.yaml文件、secrct和serviceaccount
# cat k8s-dash borad-sa.YAML---------- dashboardsecret------ dashboardsecret ----------------------------------- -
labels :
k8s-app:kubernetes-dashboard
name:kubernetes-dashboard-certs
namespace---- kube---- system type---- opaque-------- opaque---------- dashboardserververve
labels :
k8s-app:kubernetes-dashboard
name:kubernetes-dashboard
namespace:kube-system
2、配置k8s-dashborad-rbac.yaml文件、Role和Role Binding
# cat k8s---- dash borad---- RBAC.YAML---------- dashboardrolerolebinding---- dashboardrolerolebinding
name:kubernetes-dashboard-minimal
namespace:kube-systemrules :
#allowdashboardtocreate&; #039; kubernetes-dashboard-key-holder & amp; #039; secret.-APIgroups:[&; #039; &; #039; ]
资源: [ & amp; #039; 安全&自动; #039; ]
VERBS:[&; #039; 创建和映射; #039; ]#allowdashboardtocreate&; #039; kubernetes-dashboard-settings & amp; #039; configmap.-APIgroups:[&; #039; &; #039; ]
资源: [ & amp; #039; configmaps&; #039; ]
verbs:[&; #039; 创建和映射; #039; ]#allowdashboardtoget,updateanddeletedashboardexclusivesecrets.- API groups:[ & amp; #039; &; #039; ]
资源: [ & amp; #039; 安全&自动; #039; ]
资源名称:“& amp; #039; kubernetes-dashboard-key-holder & amp; #039;&; #039; kubernetes-dashboard-certs&; #039; ]&; #039; ”
verbs:[&; #039; get&; #039;&; #039; 更新& amp; #039;&; #039; 删除& amp; #039; ] # allowdashboardtogetandupdate & amp; #039; kubernetes-dashboard-settings & amp; #039; configmap.config
资源: [ & amp; #039; configmaps&; #039; ]
资源名称: [ & amp; #039; kubernetes-dashboard-settings & amp; #039; ]
verbs:[&; #039; get&; #039;&; #039; 更新& amp; #039; ] # allowdashboardtogetmetricsfromheapster.- API groups:[ & amp; #039; &; #039; ]
资源: [ & amp; #039; 服务& amp; #039; ]
资源名称: [ & amp; #039; 头开始& amp; #039; ]
verbs:[&; #039; 代理& amp; #039; ]
-apiGroups:[&; #039; &; #039; ]
资源:“& amp; #039; 服务/代理& amp; #039; ”
资源名称: [ & amp; #039; 头开始& amp; #039;&; #039; http:heapster:&; #039;&; #039; 头开始: & amp; #039; ]
verbs :“获取”
--- API version:RBAC.authorization.k8s.io/v1 kind:rolebindingmetadata :
name:kubernetes-dashboard-minimal
namespace:kube-systemroleRef :
apigroup:RBAC.authorization.k8s.io
kind:Role
name:kubernetes-dashboard-minimal subjects:- kind:service account
name:kubernetes-dashboard
namespace:kube-system
3、k8s-dashborad-deployment.yaml配置文件,定义制作pod的模板和拷贝数
# cat k8s---- dash borad---- deployment.YAML------------ dashboard deployment-----dashboard deployment
labels :
k8s-app:kubernetes-dashboard
name:kubernetes-dashboard
namespace:kube-systemspec :
replicas:1
revisionHistoryLimit:10
选择器:
匹配标签:
k8s-app:kubernetes-dashboard
template :
元数据:
labels :
k8s-app:kubernetes-dashboard
spec :
containers :
-name:kubernetes-dashboard
image:k8s.gcr.io/kubernetes-dashboard-amd64:v1.8. 3
端口:
-容器端口: 8443
protocol:TCP
args :
--- auto-generate-certificates # uncommentthefollowinglinetomanuallyspecifykubernetesapiserverhost
#Ifnotspecified,dashboardwillattempttoautodiscovertheapiserverandconnect
# toit.uncommentonlyifthedefaultdoesnotwork。
----apiserver-host=http://my-address:port
volumeMounts :
- name:kubernetes-dashboard-certs
mount path:/certs # create on-diskvolumetostoreexeclogs
-mountPath:/tmp
name:tmp-volume
livenessProbe :
httpGet :
scheme:HTTPS
path:/
端口: 8443
initialDelaySeconds:30
timeoutSeconds:30
volumes :
- name:kubernetes-dashboard-certs
secret :
secret name:kubernetes-dashboard-certs
-name:tmp-volume
emptyDir:{}
service account name:kubernetes-dashboard # commentthefollowingtolerationsifdashboardmustnotbedeployedonmaster
tolerations :
- key:node-role.kubernetes.io/master
effect:NoSchedule
4、k8s-dashborad-service.yaml配置文件,定义service
# cat k8s---- dash borad---- service.YAML-------dashboardservice-----dashboardservice
labels :
k8s-app:kubernetes-dashboard
name:kubernetes-dashboard
namespace:kube-systemspec :
端口:
-端口: 443
targetPort:8443
节点端口: 8490
type:NodePort
选择器:
k8s-app:kubernetes-dashboard
三.根据配置文件创建dashboard
# kubectlcreate-f.# kubectlgetpod,deployment,svc-nkube-system
四.配置使用basic认证方式
缺省情况下,仅支持kubeconfig和令牌验证
#echo&; #039; admin,admin,1&; #039;/etc/kubernetes/basic _ auth _ file # grep & amp; #039; 自动& amp; #039; /usr/lib/systemd/system/kube
- -授权模式=node,RBAC\\\\&; quot;
- runtime-config=RBAC.authorization.k8s.io/v1 alpha1\\& amp; quot;
- enable-bootstrap-token-auth=true\\ & amp; quot;
- token-auth-file=/etc/kubernetes/token.CSV\\ & amp; quot;
- basic-auth-file=/etc/kubernetes/basic _ auth _ file\& amp; quot; # grep‘basic’k8s-dash borad-deployment
----authentic ation-mode=basic # systemctldaemon-reload # systemctlrestartkube-apiserver # kubectlapply-fk8s-dash
绑定管理用户和群集管理角色
# curl-- insecure https://vm1:6443-basic-uadmin:admin # kubectlcreateclusterrolebinding\\ & amp; quot; 登录密码
- cluster role=cluster-admin-- user=admin # curl-- insecure https://vm1:6443-basic-uadmin:admin
五.访问测试
六.集成heapster和influxdb
如果未配置heapster和influxdb,则无法获得pod的metric信息,但以前版本K8S的HPA特性所依赖的metric数据源碰巧是heapster和influxdb。
1 .准备yaml个人资料
# catheapster-sa.yamlapiversion:v1 kind:serviceaccountmetadata :
name:heapster
namespace:kube-system
# catheapster-RBAC.YAML kind:clusterrolebindingapiversion:RBAC.authorization.k8s.io/v1 beta1metadata :
name:heapsterroleRef :
apigroup:RBAC.authorization.k8s.io
kind:ClusterRole
name:system:heapstersubjects:- kind:service account
name:heapster
namespace:kube-system
# catheapster-deployment.yamlapiversion:extensions/v1测试1
kind :部署
元数据:
name:heapsternamespace:kube-system
spec :
replicas:1
template :
元数据:
labels :
任务:监视
k8s-app:heapster
spec :
serviceAccountName:heapster
containers :
-name:heapster
image:k8s.gcr.io/heap ster-amd64:v1.4. 2
imagePullPolicy:IfNotPresent
command :
-/heapster
--- source=kubernetes:https://kubernetes.default
--- sink=influx db:http://monitoring-influx db.kube-system.SVC:8086
# catheapster-service.yamlapiversion:v1 kind:service metadata :
labels :
任务:监视
ubernetes.io/cluster-service:& amp; #039; 真& amp; #039;
kubernetes.io/name:Heapster
name:heapster
namespace:kube-systemspec :
端口:
-端口: 80
目标端口: 8082
selector:k8s-app:heapster
# catinfluxd B- deployment.yamlapiversion:extensions/v1 beta1kind:deployment metadata :
name:monitoring-influxdb
namespace:kube-systemspec :
replicas:1
template :
元数据:
labels :
任务:监视
k8s-app:influxdb
spec :
containers :
-name:influxdb
image:k8s.gcr.io/heap ster-influx d B- amd64:v1.3. 3
volumeMounts :
-mountPath:/data
name:influxdb-storage
volumes :
-name:influxdb-storage
emptyDir:{}
# catinfluxd B- service.yamlapiversion:v1 kind:service metadata :
labels :
任务:监视
ubernetes.io/cluster-service:& amp; #039; 真& amp; #039;
kern etes.io/name:monitoring-influx db
name:monitoring-influxdb
namespace:kube-systemspec :
端口:
-端口: 8086
目标端口: 8086
选择器:
k8s-app:influxdb
获取接受帮助中心支持的metrics
# kubectlrun-I-- tty curl-- namespace=kube-system\\ & amp; quot; --image=registry.59ie du.com/web wurst/curl -
# curl 3358 heap ster/API/v1/model/metrics # curl 3358 heap ster/API/v1/model/debug/all keys
#kubectlgetnode#kubectltopnode
如果heapster和influxdb pod正常运行,dashboard将显示CPU和内存的监视数据。
七.设置用户权限
1、删除apiserver中与basic认证相关的配置后,重新启动apiserver
basic-auth-file=/etc/kubernetes/basic _ auth _ file
# systemctldaemon-reload # systemctlrestartkube-apiserver
2、删除clusterrolebinding
# kubectldeleteclusterrolebindinglogin-on-dashboard-with-cluster-admin
3、修改k8s-dashborad-deployment.yaml文件
- -删除身份验证模式=基本参数
4、创建常规用户并向所有namespace下的资源授予get、watch和list权限。
在此,通过clusterrole和culsterrolebinding向所有namespace相关资源授予get、watch和list权限。 在生产环境中,建议创建role和rolebinding以指定特定的namespace相关资源权限,每个资源权限的授予规则都遵循最低权限原则。
# cat RBAC-yang.YAML kind:clusterroleapiversion:RBAC.authorization.k8s.io/v1 metadata :
name:role-yang rules:- API groups:[ & amp; #039; &; #039; ]
资源: [ & amp; #039; *&; #039; ]
verbs:[&; #039; get&; #039;&; #039; watch&; #039;&; #039; list&; #039; ]
-apiGroups:[&; #039; storage.k8s.io&; #039; ]
资源: [ & amp; #039; *&; #039; ]
verbs:[&; #039; get&; #039;&; #039; watch&; #039;&; #039; list&; #039; ]
-APIgroups:[&; #039; RBAC.authorization.k8s.io&; #039; ]
资源: [ & amp; #039; *&; #039; ]
verbs:[&; #039; get&; #039;&; #039; watch&; #039;&; #039; list&; #039; ]
-apiGroups:[&; #039; batch&; #039; ]
资源: [ & amp; #039; *&; #039; ]
verbs:[&; #039; get&; #039;&; #039; watch&; #039;&; #039; list&; #039; ]
-apiGroups:[&; #039; apps&; #039; ]
资源: [ & amp; #039; *&; #039; ]
verbs:[&; #039; get&; #039;&; #039; watch&; #039;&; #039; list&; #039; ]
-apiGroups:[&; #039; 扩展& amp; #039; ]
资源: [ & amp; #039; *&; #039; ]
verbs:[&; #039; get&; #039;&; #039; watch&; #039;&; #039; list&; #039; ]
--- kind:clusterrolebindingapiversion:RBAC.authorization.k8s.io/v1 metadata :
name:role-bind-yang subjects:- kind:service account
name:yang
namespace:kube-systemroleRef :
kind:ClusterRole
名称: role-yang
apigroup:RBAC.authorization.k8s.io
# kubectlcreatesayang-nku be-system # kubectlcreate-fr BAC-yang.YAML # kube CTL-nku be-systemdescribesecret $ ( kube
5、测试普通用户权限
6、创建超级用户管理员
# kubectlcreatesaadmin-nku be-system # cat RBAC-admin.yamlapiversion:RBAC.authorization.k8s.io/v1 beta 1
kind:ClusterRoleBinding
元数据:
name:admin
roleRef :
apigroup:RBAC.authorization.k8s.io
kind:ClusterRole
name:cluster-admin
subjects :
- kind :服务帐户
name:admin
namespace:kube-system # kubectlcreate-fr BAC-admin.YAML # kube CTL-nku be-systemdescribesecret $ (
使用管理员用户token登录后,继承cluster-admin的权限
参考:
3359 github.com/kubernetes/dashboard/wiki/creating-sample-user
33559 github.com/kubernetes/dashboard/wiki/access-control
3359 github.com/kubernetes/heap ster/blob/master/docs/model.MD
详情请访问云服务器、域名注册、虚拟主机的问题,请访问西部数码代理商官方网站: www.chenqinet.cn