如何创建和管理k8制作k8s的redis集群镜像

Kubernetes之利用prometheus监控K8S集群
时间: 17:25:35
&&&& 阅读:145
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&prometheus它是一个主动拉取的数据库,在K8S中应该展示图形的grafana数据实例化要保存下来,使用分布式文件系统加动态PV,但是在本测试环境中使用本地磁盘,安装采集数据的agent使用DaemonSet来部署,DaemonSet的特性就是在每个node上部署一个服务进程,这一切都是自动的部署。
& & & &此处只讲如何用prometheus来监控K8S集群,关于prometheus的知识参考官方文档。
& &部署前提: 准备好所需要的文件
Prometheus/prometheus#:/data/Prometheus/prometheus#&ls&-l&
drwxr-xr-x&2&root&root&4096&Jan&15&02:53&grafana
drwxr-xr-x&2&root&root&4096&Jan&15&03:11&kube-state-metrics
-rw-r--r--&1&root&root&&&60&Jan&14&06:48&namespace.yaml
drwxr-xr-x&2&root&root&4096&Jan&15&03:22&node-directory-size-metrics
drwxr-xr-x&2&root&root&4096&Jan&15&03:02&node-exporter
drwxr-xr-x&2&root&root&4096&Jan&15&02:55&prometheus
drwxr-xr-x&2&root&root&4096&Jan&15&02:37&rbac
$&ls&grafana/
grafana-configmap.yaml&&grafana-core-deployment.yaml&&grafana-import-dashboards-job.yaml&&grafana-pvc-claim.yaml&&grafana-pvc-volume.yaml&&grafana-service.yaml
$&ls&prometheus/
configmap.yaml&&deployment.yaml&&prometheus-rules.yaml&&service.yaml
& &grafana和&prometheus 都是部署文件,node-exporter、kube-state-metrics、node-directory-size-metrics这三个是采集器,相当于prometheus的agent
& &文件准备好了,现在开始一步一步来部署:
&1,,创建所需Namespace&&
& & & 因为prometheus 部署的所有的deploy、pod、svc都是在monitoring完成的,所以需要事先创建之。
&$&cat&namespace.yaml&
&apiVersion:&v1
&kind:&Namespace
&metadata:
&&name:&monitoring
&$&kubectl&create&-f&namespace.yaml&
&namespace&"monitoring"&created
&2,创建grafana的pv、 pvc
grafana#&cat&grafana-pvc-volume.yaml&
kind:&PersistentVolume
apiVersion:&v1
&&name:&grafana-pv-volume
&&&&type:&local
&&storageClassName:&grafana-pv-volume
&&capacity:
&&&&storage:&10Gi
&&accessModes:
&&&&-&ReadWriteOnce
&&persistentVolumeReclaimPolicy:&Recycle
&&hostPath:
&&&&path:&"/data/volume/grafana"
grafana#&cat&grafana-pvc-claim.yaml&
kind:&PersistentVolumeClaim
apiVersion:&v1
&&name:&grafana-pvc-volume
&&namespace:&"monitoring"
&&accessModes:
&&&&-&ReadWriteOnce
&&resources:
&&&&requests:
&&&&&&storage:&5Gi
&&storageClassName:&grafana-pv-volume
$&kubectl&create&-f&grafana/grafana-pvc-volume.yaml&-f&grafana/grafana-pvc-claim.yaml&
persistentvolume&"grafana-pv-volume"&created
persistentvolumeclaim&"grafana-pvc-volume"&created
$&kubectl&get&pvc&-n&monitoring
NAME&&&&&&&&&&STATUS&&&&&&&&&&&VOLUME&&&&&&&CAPACITY&&&ACCESS&MODES&&&STORAGECLASS&&&&&AGE
grafana-pvc-volume&&&Bound&&&&&grafana-pv-volume&&&10Gi&&&&&&&RWO&&&&&grafana-pv-volume&&&52s
状态bound已绑定到了&grafana-pv-volume
3,创建grafana应用,这些应用都是第三方的,都会有自已的配置,通过configmap来定义
grafana#&ls
grafana-configmap.yaml&&grafana-core-deployment.yaml&&grafana-import-dashboards-job.yaml&&grafana-pvc-claim.yaml&&grafana-pvc-volume.yaml&&grafana-service.yaml
grafana#&kubectl&create&-f&./&&&&#grafana目录下所有文件都创建
configmap&"grafana-import-dashboards"&created
deployment&"grafana-core"&created
job&"grafana-import-dashboards"&created
service&"grafana"&created&
grafana#&kubectl&get&deployment,pod&-n&monitoring&
NAME&&&&&&&&&&&&&&&&&&DESIRED&&&CURRENT&&&UP-TO-DATE&&&AVAILABLE&&&AGE
deploy/grafana-core&&&1&&&&&&&&&1&&&&&&&&&1&&&&&&&&&&&&0&&&&&&&&&&&1m
NAME&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&READY&&&&&STATUS&&&&&&&&&&&&&&RESTARTS&&&AGE
po/grafana-core-9c7flx&&&0/1&&&&&&&ContainerCreating&&&0&&&&&&&&&&1m
运行po/grafana-core&容器时会下载镜像:&grafana/grafana:4.2.0
& grafana创建的应用 简单的自已描述了下:
&&&&&&grafana-pv-volume=/data/volume/grafana&=10G&&&&
&&&&&&grafana-pvc-volume=5G---&grafana-pv-volume
&&&&&&---configmap=grafana-import-dashboards&&&&&
&&&&&&Job=grafana-import-dashboards
&&&&&&&&&&&&&&&&&&
&&&&&&Deployment=grafana-core&&&&&replicas:&1&&containers=grafana-core&&&mount:&&grafana-pvc-volume:/var
&&&&&&service=grafana&&&&&port:&3000&&=&nodePort:&30161&&&&&(3000是grafana服务的默认端口)
4, 现在grafana的核心应用已部署好了,现在来部署prometheus的RBAC
prometheus/rbac#&ls
grant_serviceAccount.sh&&prometheus_rbac.yaml
#先创建RBAC文件:
prometheus/rbac#&kubectl&create&-f&prometheus_rbac.yaml&
clusterrolebinding&"prometheus-k8s"&created
clusterrolebinding&"kube-state-metrics"&created
clusterrole&"kube-state-metrics"&created
serviceaccount&"kube-state-metrics"&created
clusterrolebinding&"prometheus"&created
clusterrole&"prometheus"&created
serviceaccount&"prometheus-k8s"&created
prometheus/rbac#
5,创建prometheus的deloyment,service
prometheus/prometheus#&ls
configmap.yaml&&deployment.yaml&&prometheus-rules.yaml&&service.yaml
prometheus/prometheus#&
在configmap.yaml中要注意的是在1.7以后,获取cadvsion监控pod等的信息时,用的是kubelet的4194端口,
注意以下这段:这是采集cadvision信息,必须是通过kubelet的4194端口,所以Kubelet必须监听着,4194部署了cadvsion来获取pod中容器信息
prometheus/prometheus#cat&configmap.yaml
&#&https://github.com/prometheus/prometheus/blob/master/documentation/examples/prometheus-kubernetes.yml#L37
&&&&&&-&job_name:&‘kubernetes-nodes‘
&&&&&&&&tls_config:
&&&&&&&&&&ca_file:&/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
&&&&&&&&bearer_token_file:&/var/run/secrets/kubernetes.io/serviceaccount/token
&&&&&&&&kubernetes_sd_configs:
&&&&&&&&&&-&role:&node
&&&&&&&&relabel_configs:
&&&&&&&&&&-&source_labels:&[__address__]
&&&&&&&&&&&&regex:&‘(.*):10250‘
&&&&&&&&&&&&replacement:&‘${1}:10255‘
&&&&&&&&&&&&target_label:&__address__
&&&&&&-&job_name:&‘kubernetes-cadvisor‘
&&&&&&&&scheme:&https
&&&&&&&&tls_config:
&&&&&&&&&&ca_file:&/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
&&&&&&&&bearer_token_file:&/var/run/secrets/kubernetes.io/serviceaccount/token
&&&&&&&&kubernetes_sd_configs:
&&&&&&&&&&-&role:&node
&&&&&&&&relabel_configs:
&&&&&&&&-&action:&labelmap
&&&&&&&&&&regex:&__meta_kubernetes_node_label_(.+)
&&&&&&&&-&target_label:&__address__
&&&&&&&&&&replacement:&kubernetes.default.svc.cluster.local:443
&&&&&&&&-&source_labels:&[__meta_kubernetes_node_name]
&&&&&&&&&&regex:&(.+)
&&&&&&&&&&target_label:&__metrics_path__
&&&&&&&&&&replacement:&/api/v1/nodes/${1}:4194/proxy/metrics
&&&&&&#&https://github.com/prometheus/prometheus/blob/master/documentation/examples/prometheus-kubernetes.yml#L79
& prometheus-rules.yaml 这是它的发现规则文件
& deployment.yaml& &service.yaml 这两个是部署的文件,& deployment部署中资源限制建议放大一点
& 现在部署prometheus目录下所有文件:
prometheus/prometheus#&kubectl&create&-f&./
configmap&"prometheus-core"&created
deployment&"prometheus-core"&created
configmap&"prometheus-rules"&created
service&"prometheus"&created
prometheus/prometheus#&
prometheus/prometheus#&kubectl&get&deployment,pod&-n&monitoring&
NAME&&&&&&&&&&&&&&&&&&&&&DESIRED&&&CURRENT&&&UP-TO-DATE&&&AVAILABLE&&&AGE
deploy/grafana-core&&&&&&1&&&&&&&&&1&&&&&&&&&1&&&&&&&&&&&&1&&&&&&&&&&&16m
deploy/prometheus-core&&&1&&&&&&&&&1&&&&&&&&&1&&&&&&&&&&&&1&&&&&&&&&&&1m
NAME&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&READY&&&&&STATUS&&&&RESTARTS&&&AGE
po/grafana-core-9c7f66868-wm68j&&&&&&&1/1&&&&&&&Running&&&0&&&&&&&&&&16m
po/prometheus-core-6dcnc7j&&&1/1&&&&&&&Running&&&0&&&&&&&&&&1m
prometheus应用的部署,简单描述下创建的内容:
&&&&Deployment=&prometheus-core&&&replicas:&1&&&&containers=prometheus&&&image:&prom/prometheus:v1.7.0&&&&containerPort:&9090(webui)
&&&&Service&&&&name:&prometheus&&&NodePort--&port:&9090&-webui
6,prometheus部署完了现在来部署它的agent,也就是采集器:
Prometheus/prometheus#&ls&node-directory-size-metrics/
daemonset.yaml
Prometheus/prometheus#&ls&kube-state-metrics/
deployment.yaml&&service.yaml
Prometheus/prometheus#&ls&node-exporter/
exporter-daemonset.yaml&&exporter-service.yaml
Prometheus/prometheus#&
#其中两个用的是daemonset
Prometheus/prometheus#&kubectl&create&-f&node-exporter/&-f&kube-state-metrics/&-f&node-directory-size-metrics/
daemonset&"prometheus-node-exporter"&created
service&"prometheus-node-exporter"&created
deployment&"kube-state-metrics"&created
service&"kube-state-metrics"&created
daemonset&"node-directory-size-metrics"&created
Prometheus/prometheus#&
Prometheus/prometheus#&kubectl&get&deploy,pod,svc&-n&monitoring&
NAME&&&&&&&&&&&&&&&&&&&&&&&&DESIRED&&&CURRENT&&&UP-TO-DATE&&&AVAILABLE&&&AGE
deploy/grafana-core&&&&&&&&&1&&&&&&&&&1&&&&&&&&&1&&&&&&&&&&&&1&&&&&&&&&&&26m
deploy/kube-state-metrics&&&2&&&&&&&&&2&&&&&&&&&2&&&&&&&&&&&&2&&&&&&&&&&&1m
deploy/prometheus-core&&&&&&1&&&&&&&&&1&&&&&&&&&1&&&&&&&&&&&&1&&&&&&&&&&&11m
NAME&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&READY&&&&&STATUS&&&&RESTARTS&&&AGE
po/grafana-core-9c7f66868-wm68j&&&&&&&&&&1/1&&&&&&&Running&&&0&&&&&&&&&&26m
po/kube-state-metrics-694fdcf55f-bqcp8&&&1/1&&&&&&&Running&&&0&&&&&&&&&&1m
po/kube-state-metrics-694fdcf55f-nnqqd&&&1/1&&&&&&&Running&&&0&&&&&&&&&&1m
po/node-directory-size-metrics-n9wx7&&&&&2/2&&&&&&&Running&&&0&&&&&&&&&&1m
po/node-directory-size-metrics-ppscw&&&&&2/2&&&&&&&Running&&&0&&&&&&&&&&1m
po/prometheus-core-6dcnc7j&&&&&&1/1&&&&&&&Running&&&0&&&&&&&&&&11m
po/prometheus-node-exporter-kchmb&&&&&&&&1/1&&&&&&&Running&&&0&&&&&&&&&&1m
po/prometheus-node-exporter-lks5m&&&&&&&&1/1&&&&&&&Running&&&0&&&&&&&&&&1m
NAME&&&&&&&&&&&&&&&&&&&&&&&&&&&TYPE&&&&&&&&CLUSTER-IP&&&&&&EXTERNAL-IP&&&PORT(S)&&&&&&&&&&AGE
svc/grafana&&&&&&&&&&&&&&&&&&&&NodePort&&&&10.254.231.25&&&&none&&&&&&&&&/TCP&&&26m
svc/kube-state-metrics&&&&&&&&&ClusterIP&&&10.254.156.51&&&&none&&&&&&&&&8080/TCP&&&&&&&&&1m
svc/prometheus&&&&&&&&&&&&&&&&&NodePort&&&&10.254.239.90&&&&none&&&&&&&&&/TCP&&&10m
svc/prometheus-node-exporter&&&ClusterIP&&&None&&&&&&&&&&&&&none&&&&&&&&&9100/TCP&&&&&&&&&1m
Prometheus/prometheus#
Prometheus/prometheus#&kubectl&get&pod&-o&wide&-n&monitoring&
NAME&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&READY&&&&&STATUS&&&&RESTARTS&&&AGE&&&&&&&IP&&&&&&&&&&&&&NODE
prometheus-node-exporter-kchmb&&&&&&&&1/1&&&&&&&Running&&&0&&&&&&&&&&4m&&&&&&&&10.3.1.16&&&&&&10.3.1.16
prometheus-node-exporter-lks5m&&&&&&&&1/1&&&&&&&Running&&&0&&&&&&&&&&4m&&&&&&&&10.3.1.17&&&&&&10.3.1.17
#这两个是exporter,用的是daemonset&分别在这两个node上运行了。这样就可以采集到所有数据了。
& 如上部署完成,以下是用自已的话简单描述下:
&node-exporter/exporter-daemonset.yaml&文件:
&&&&&&&DaemonSet=prometheus-node-exporter&&&
&&&&&&&&&&containers:&name:&prometheus-node-exporter&&&&image:&prom/node-exporter:v0.14.0
&&&&&&&&&&containerPort:&9100&&&hostPort:&9100&&hostNetwork:&true&&&&#它用的是主机的9100端口
Prometheus/prometheus/node-exporter#&kubectl&get&&daemonset,pod&-n&monitoring&
NAME&&&&&&&&&&&&&&&&&&&&&&&&&&&&&DESIRED&&&CURRENT&&&READY&&&&&UP-TO-DATE&&&AVAILABLE&&&NODE&SELECTOR&&&AGE
ds/node-directory-size-metrics&&&2&&&&&&&&&2&&&&&&&&&2&&&&&&&&&2&&&&&&&&&&&&2&&&&&&&&&&&&none&&&&&&&&&&&16h
ds/prometheus-node-exporter&&&&&&2&&&&&&&&&2&&&&&&&&&2&&&&&&&&&2&&&&&&&&&&&&2&&&&&&&&&&&&none&&&&&&&&&&&16h
&&&&&&&&&&&因为它是daemonset,所以相应的也会运行着两个Pod:&prometheus-node-exporter
&&&&&&Service=prometheus-node-exporter&&&clusterIP:&None&&&port:&9100&&type:&ClusterIP&&&#它没有clusterIP
&&&&&&&&&&&&&&&&&&
#&kubectl&get&&service&-n&monitoring&
NAME&&&&&&&&&&&&&&&&&&&&&&&TYPE&&&&&&&&CLUSTER-IP&&&&&&EXTERNAL-IP&&&PORT(S)&&&&&&&&&&AGE
prometheus-node-exporter&&&ClusterIP&&&None&&&&&&&&&&&&&none&&&&&&&&&9100/TCP&&&&&&&&&16h
kube-state-metrics/deployment.yaml&文件:
&&&&&&Deployment=kube-state-metrics&replicas:&2&&&containers--&name:&kube-state-metrics&&image:&gcr.io/google_containers/kube-state-metrics:v0.5.0&
&&&&&&&&&&&&&&&&&containerPort:&8080
&&&&&&Service&&&&&name:&kube-state-metrics&&&port:&8080&&#没有映射
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&#kubectl&get&deployment,pod,svc&-n&monitoring&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
NAME&&&&&&&&&&&&&&&&&&&&&&&&DESIRED&&&CURRENT&&&UP-TO-DATE&&&AVAILABLE&&&AGE
deploy/kube-state-metrics&&&2&&&&&&&&&2&&&&&&&&&2&&&&&&&&&&&&2&&&&&&&&&&&16h
NAME&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&READY&&&&&STATUS&&&&RESTARTS&&&AGE
po/kube-state-metrics-694fdcf55f-2mmd5&&&1/1&&&&&&&Running&&&0&&&&&&&&&&11h
po/kube-state-metrics-694fdcf55f-bqcp8&&&1/1&&&&&&&Running&&&0&&&&&&&&&&16h
NAME&&&&&&&&&&&&&&&&&&&&&&&&&&&TYPE&&&&&&&&CLUSTER-IP&&&&&&EXTERNAL-IP&&&PORT(S)&&&&&&&&&&AGE
svc/kube-state-metrics&&&&&&&&&ClusterIP&&&10.254.156.51&&&&none&&&&&&&&&8080/TCP&&&&&&&&&16h
node-directory-size-metrics/daemonset.yaml&文件:
&&&&&&&&#因为是daemonset,所以未定义replicas数量,直接运行在每个node之上,但是它没有创建service
&&&&&&DaemonSet&:&name:&node-directory-size-metrics&&
&&&&&&&&&&&&&&&&&&containers--&name:&read-du&&image:&giantswarm/tiny-tools&&&mountPath:&/mnt/var&&&mountPath:&/tmp
&&&&&&&&&&&&&&&&&&containers--&&name:&caddy&&&&image:&dockermuenster/caddy:0.9.3&containerPort:&9102
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&mountPath:&/var/www&&&hostPath&/var
&&&&&&&&&&&&&&&&&&&&&&&&&&&&
kubectl&get&daemonset,pod,svc&-n&monitoring&
NAME&&&&&&&&&&&&&&&&&&&&&&&&&&&&&DESIRED&&&CURRENT&&&READY&&&&&UP-TO-DATE&&&AVAILABLE&&&NODE&SELECTOR&&&AGE
ds/node-directory-size-metrics&&&2&&&&&&&&&2&&&&&&&&&2&&&&&&&&&2&&&&&&&&&&&&2&&&&&&&&&&&&none&&&&&&&&&&&16h
NAME&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&READY&&&&&STATUS&&&&RESTARTS&&&AGE
po/node-directory-size-metrics-n9wx7&&&&&2/2&&&&&&&Running&&&0&&&&&&&&&&16h
po/node-directory-size-metrics-ppscw&&&&&2/2&&&&&&&Running&&&0&&&&&&&&&&16h
NAME&&&&&&&&&&&&&&&&&&&&&&&&&&&TYPE&&&&&&&&CLUSTER-IP&&&&&&EXTERNAL-IP&&&PORT(S)&&&&&&&&&&AGE
&&&&&&&&&&&&&&&&&&&&&没有node-directory-size-metrics的service
&到此 prometheus算是部署完成了,最后来看下它暴露的端口:
Prometheus/prometheus#&kubectl&get&svc&-o&wide&-n&monitoring&
NAME&&&&&&&&&&&&&&&&&&&&&&&TYPE&&&&&&&&CLUSTER-IP&&&&&&EXTERNAL-IP&&&PORT(S)&&&&&&&&&&AGE&&&&&&&SELECTOR
grafana&&&&&&&&&&&&&&&&&&&&NodePort&&&&10.254.231.25&&&&none&&&&&&&&&/TCP&&&31m&&&&&&&app=grafana,component=core
kube-state-metrics&&&&&&&&&ClusterIP&&&10.254.156.51&&&&none&&&&&&&&&8080/TCP&&&&&&&&&6m&&&&&&&&app=kube-state-metrics
prometheus&&&&&&&&&&&&&&&&&NodePort&&&&10.254.239.90&&&&none&&&&&&&&&/TCP&&&16m&&&&&&&app=prometheus,component=core
prometheus-node-exporter&&&ClusterIP&&&None&&&&&&&&&&&&&none&&&&&&&&&9100/TCP&&&&&&&&&6m&&&&&&&&app=prometheus,component=node-exporter
Prometheus/prometheus#
7,访问、使用prometheus
& & 如上可以看到grafana的端口号是30161,NodeIP:30161 就可以打开grafana,默认admin/admin
& & 登录后,添加数据源:
& &添加Prometheus的数据源:
& & &&将Prometheus的作为数据源的相关参数如下图所示:
& 添加完后,导入模板文件:
& & &&& & & &&
& & & &部署完成。标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文地址:https://www.cnblogs.com/cheyunhua/p/9019927.html
&&国之画&&&& &&&&chrome插件
版权所有 京ICP备号-2
迷上了代码!当前位置浏览文章
一、前言前面三篇文章详情了如何从零开始搭建一个基本的Kubernetes集群,本文将详情一下如何搭建K8S的Dashboard。简单的说,K8S Dashboard是官方的一个基于WEB的使用户界面,专门使用来管理K8S集群,并可展现集群的状态。K8S集群安装好后默认没有包含Dashboard,我们需要额外创立它。本人觉得Dashboard设计的还不错,界面友好,功可以也比较强大。假如你厌倦了命令行的操作,全程用Dashboard也是可行的。Dashboard的搭建过程中,会遇到少量坑。现在开始,咱们一步一步踩来,走你!二、RABC简介还是那句话,官方文档是最重要的参考资料:https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/该文档中,创立kubernetes-dashboard的命令为:kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml当然,直接这样创立的dashboard会有很多问题,参见:Please note, this works only if the apiserver is set up to allow authentication with username password. This is not currently the case with some setup tools (e.g., kubeadm). Refer to the authentication admin documentation for information on how to configure authentication manually.由于我们用kubeadm搭建的集群会默认开启RABC(角色访问控制机制),所以我们必需要进行额外的设置。关于RABC的概念,网上资料很多,大家务必提前理解。这里简要详情一下几个重要概念:RBACK8S 1.6引进,是让使用户可以够访问 k8S API 资源的受权方式【不受权就没有资格访问K8S的资源】使用户K8S有两种使用户:User和Service Account。其中,User给人使用,Service Account给进程使用,让进程有相关权限。如Dashboard就是一个进程,我们即可以创立一个Service Account给它角色Role是一系列权限的集合,例如一个Role可包含读取和列出 Pod的权限【 ClusterRole 和 Role 相似,其权限范围是整个集群】角色绑定RoleBinding把角色映射到使用户,从而让这些使用户拥有该角色的权限【ClusterRoleBinding 和RoleBinding 相似,可让使用户拥有 ClusterRole 的权限】SecretSecret是一个包含一些敏感信息如密码,令牌,或者秘钥的对象。把这些信息保存在 Secret对象中,能在这些信息被用时加以控制,并能降低信息泄露的风险如下图,灰色是“角色”,蓝色是“使用户”,绿色是“角色绑定”,黄色是该角色拥有的权限。简言之 ,角色绑定将角色和使用户进行挂钩:image.png三、官方kubernetes-dashboard.yaml简介很有必要详情一下官方的kubernetes-dashboard.yaml,我们首先将其下载下来:wget https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml该文件分为以下几部分:Dashboard ServiceDashboard DeploymentDashboard RoleRoleBindingDashboard Service AccountDashboard Secret这里,我们简单的对各个部分的功可以进行详情:Dashboard Rolekind: RoleapiVersion: rbac.authorization.k8s.io/v1metadata:
name: kubernetes-dashboard-minimal
namespace: kube-systemrules:
# Allow Dashboard to create kubernetes-dashboard-key-holder secret.- apiGroups: [""]
resources: ["secrets"]
verbs: ["create"]
# Allow Dashboard to create kubernetes-dashboard-settings config map.- apiGroups: [""]
resources: ["configmaps"]
verbs: ["create"]
# Allow Dashboard to get, Dashboard exclusive secrets.- apiGroups: [""]
resources: ["secrets"]
resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs"]
verbs: ["get", "update", "delete"]
# Allow Dashboard to get kubernetes-dashboard-settings config map.- apiGroups: [""]
resources: ["configmaps"]
resourceNames: ["kubernetes-dashboard-settings"]
verbs: ["get", "update"]
# Allow Dashboard to get metrics from heapster.- apiGroups: [""]
resources: ["services"]
resourceNames: ["heapster"]
verbs: ["proxy"]- apiGroups: [""]
resources: ["services/proxy"]
resourceNames: ["heapster", "http:heapster:", "https:heapster:"]
verbs: ["get"]如上定义了Dashboard 的角色,其角色名称为kubernetes-dashboard-minimal,rules中清晰的列出了其拥有的多个权限。通过名称我们能猜到,这个权限级别是比较低的。ServiceAccountkind: ServiceAccountmetadata:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-system如上定义了Dashboard的使用户,其类型为ServiceAccount,名称为kubernetes-dashboard。RoleBindingkind: RoleBindingmetadata:
name: kubernetes-dashboard-minimal
namespace: kube-systemroleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: kubernetes-dashboard-minimalsubjects:- kind: ServiceAc name: kubernetes-dashboard
namespace: kube-system如上定义了Dashboard的角色绑定,其名称为kubernetes-dashboard-minimal,roleRef中为被绑定的角色,也叫kubernetes-dashboard-minimal,subjects中为绑定的使用户:kubernetes-dashboard。Dashboard Secretkind: Secretmetadata:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-certs
namespace: kube-systemtype: OpaqueDashboard Deploymentkind: DeploymentapiVersion: apps/v1beta2metadata:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-systemspec:
replicas: 1
revisionHistoryLimit: 10
matchLabels:
k8s-app: kubernetes-dashboard
k8s-app: kubernetes-dashboard
containers:
- name: kubernetes-dashboard
image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.8.3
- containerPort: 8443
protocol: TCP
- --auto-generate-certificates
# Uncomment the following line to manually specify Kubernetes API server Host
# If not specified, Dashboard will attempt to auto discover the API server connect
# to it. Uncomment only if the default does not work.
# - --apiserver-host=http://my-address:port
volumeMounts:
- name: kubernetes-dashboard-certs
mountPath: /certs
# Create on-disk volume to store logs
- mountPath: /tmp
name: tmp-volume
livenessProbe:
scheme: HTTPS
port: 8443
initialDelaySeconds: 30
timeoutSeconds: 30
- name: kubernetes-dashboard-certs
secretName: kubernetes-dashboard-certs
- name: tmp-volume
emptyDir: {}
serviceAccountName: kubernetes-dashboard
# Comment the following tolerations if Dashboard must not be deployed on
tolerations:
- key: node-role.kubernetes.io/
effect: NoSchedule如上能看到,Dashboard的Deployment指定了其用的ServiceAccount是kubernetes-dashboard。并且还将Secret kubernetes-dashboard-certs通过volumes挂在到pod内部的/certs路径。为何要挂载Secret ?起因是创立Secret 时会自动生成token。请注意参数--auto-generate-certificates,其表示Dashboard会自动生成证书。四、安装Dashboard1.导入镜像假如直接用官方的kubernetes-dashboard.yaml创立Dashboard,你会踩到很多坑,首先是镜像拉取会超时失败。截止目前,Dashboard的最新版本是1.8.3,我已经将镜像k8s.gcr.io#kubernetes-dashboard-amd64.tar导出,提供给大家:链接:https://pan.baidu.com/s/11AheivJxFzc4X6Q5_qCw8A 密码:2zov在所有节点上(由于你不知道K8S会将Dashboard的pod调度到哪个节点),用如下命令导入镜像:docker load & docker load & k8s.gcr.io#kubernetes-dashboard-amd64.tar导入成功后,执行docker images能看到Dashboard的版本是1.8.3:image.png2.创立Dashboard导入镜像后,用之前下载的yaml文件就可创立Dashboard:kubectl create -f kubernetes-dashboard.yaml3.开启代理商根据官方文档,我们必需在Master上执行kubecll proxy,而后用如下地址访问Dashboard:http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy但限制就是必需在Master上访问,这显然是个坑,我们的目标是在我们真实的物理机上去访问Master的Dashboard。所以,在主节点上,我们执行kubectl proxy --address=192.168.56.101 --disable-filter=true开启代理商。其中:address表示外界能用192.168.56.101来访问Dashboard,我们也能用0.0.0.0disable-filter=true表示禁使用请求过滤功可以,否则我们的请求会被拒绝,并提醒 Forbidden (403) Unauthorized。我们也能指定端口,具体请查看kubectl proxy --help如下图所示,proxy默认对Master的8001端口进行监听:image.png这样,我们即可以用如下地址访问登录界面:http://192.168.56.101:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/#!/loginimage.png配置DashboardDashboard的配置是难点,尤其是涉及到安全权限相关,相当复杂,坑也比较多。进入Dashboard的登录界面后,认证方式有Kubeconfig和令牌两种方式(实际上还有账号密码的方式,默认不开启不显示)。看到Kubeconfig和令牌,预计头都大了。能否有简便的方法,让我们可以直接访问Dashboard?当然有,选择跳过,会出现如下页面:image.png如上图,很遗憾,我们看到了很多权限错误提醒,主要是system:serviceaccount:kube-system:kubernetes-dashboard的权限不足引起的。我们回想本文第三小节对kubernetes-dashboard.yaml的详情,现在就了解了为什么其角色的名称为kubernetes-dashboard-minimal。一句话,这个Role的权限不够!因而,我们能更改RoleBinding修改为ClusterRoleBinding,并且修改roleRef中的kind和name,用cluster-admin这个非常牛逼的CusterRole(超级使用户权限,其拥有访问kube-apiserver的所有权限)。如下:apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:
name: kubernetes-dashboard-minimal
namespace: kube-systemroleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-adminsubjects:- kind: ServiceAc name: kubernetes-dashboard
namespace: kube-system修改后,重新创立kubernetes-dashboard.yaml,Dashboard即可以拥有访问整个K8S 集群API的权限。我们重新访问Dashboard。如下图所示,一切正常,请在界面上尽情的乱点吧:image.pngKubeconfig和令牌登录登录【待续】如何用Kubeconfig和令牌登录呢?

我要回帖

更多关于 制作k8s的redis集群镜像 的文章

 

随机推荐